thoemo/calc (1.2.3)

Published 2026-10-25 14:26:34 +00:00 by elnebuloso

Installation

{
	"repositories": [{
			"type": "composer",
			"url": ""
		}
	]
}
composer config repositories.thoemo composer 
composer require thoemo/calc:1.2.3

About this package

Calculations Library

thoemo/calc

Small combinatorics helpers: generate all k-element combinations of an array, and convert between a set of numbers and its position in the ordered list of all such sets (lottery style, e.g. 6 out of 49).

Requirements

  • PHP >= 8.2, no extensions or other packages required

Installation

The package is served by the Forgejo Composer registry. Add the repository to your composer.json:

"repositories": {
    "packages.link.thoemo.net/thoemo": {
        "type": "composer",
        "url": "https://packages.link.thoemo.net/api/packages/thoemo/composer"
    }
}

Then require the package:

composer require thoemo/calc

The PHP namespace is elnebuloso\Calc.

Overview

Class Description
Combinations generate(array $elements, int $length): Generator yields every combination of $length elements.
NumbersCombinations Maps a sorted set of numbers from a range to its 1-based combination number and back.

Both classes only have static methods.

Usage

Generate combinations

Combinations::generate() yields each combination as an array, in the order of the input elements. The input must be a list (keys 0..n-1).

use elnebuloso\Calc\Combinations;

foreach (Combinations::generate(["a", "b", "c", "d"], 2) as $combination) {
    echo implode("", $combination), " ";
}
// ab ac ad bc bd cd

echo iterator_count(Combinations::generate([1, 2, 3, 4, 5, 6, 7], 5)); // 21

Number of a combination and back

Combinations of $length numbers out of $rangeMin..$rangeMax are numbered from 1 in ascending order: for 6 out of 49, [1, 2, 3, 4, 5, 6] is 1, [1, 2, 3, 4, 5, 7] is 2 and [44, 45, 46, 47, 48, 49] is 13983816.

use elnebuloso\Calc\NumbersCombinations;

NumbersCombinations::getCombination([2, 6, 12, 41, 47, 49], 1, 49); // 2217543
NumbersCombinations::getNumbers(2217543, 1, 49, 6);                // [2, 6, 12, 41, 47, 49]

Pass the numbers to getCombination() in ascending order. A number outside the range throws an InvalidArgumentException (Number not in range: 50).

Development

Tests and tools run through redo, the Docker-based CI wrapper; build.yml holds the project's redo properties (default PHP version 8.2).

redo composer:install phpunit                     # PHPUnit with the PHP version from build.yml
redo composer:install phpunit -Dphp_version=8.4   # PHPUnit with another PHP version
redo phplint -Dphp_version=8.4

The coverage report is written to build/reports/phpunit/all/coverage/html.

Releases

Every push to main runs the GitLab pipeline (phplint, prettier, composer:validate:require and PHPUnit on PHP 8.2 to 8.5), raises the patch version in VERSION and uploads that version to the Forgejo registry. The released archive contains only README.md, composer.json and src/.

Dependencies

Dependencies

ID Version
php >=8.2
Details
Composer
2026-10-25 14:26:34 +00:00
4
2026-10-25 15:08:24 +00:00
proprietary
3.4 KiB
Assets (1)
Versions (3) View all
1.3.1 2026-10-25
1.2.3 2026-10-25
1.2.2 2026-10-25