Cagen
Number of Case Generator for TypeScript.
Symbol | Class |
---|---|
A x B x ... x Z | CartesianProduct |
n! | Factorial |
nPr | Permutation |
n∏r | RepeatedPermutation |
nHr | RepeatedCombination |
nCr | Combination |
Usage
Installation
npm install --save cagen
Common Features
namespace cagen.base
{
export interface IForwardGenerator
{
// FREQUENCE ACCESSORS
size(): number;
begin(): Iterator;
end(): Iterator;
// ES2015, THEN FOR-OF ITERATION IS ALSO POSSIBLE
[Symbol.iterator]: IterableIterator<number[]>;
}
export interface Iterator
{
readonly value: number[];
next(): Iterator;
equals(obj: Iterator): boolean;
}
}
import { CartesianProduct } from "cagen";
function main(): void
{
let generator = new CartesianProduct(5, 4); // 5C4
console.log("n(5C4) =", generator.size());
for (let it = generator.begin(); !it.equals(generator.end()); it = it.next())
{
let aCase: number[] = it.value;
console.log(" -", aCase);
}
}
main();
for (let aCase of generator)
console.log(" -", aCase);
Random Accessor
namespace cagen.base
{
export abstract class ArrayGenerator
implements IForwardGenerator<Iterator>
{
at(index: number): Array<number>;
}
}
Most of Case Generator classes, except combination classes, provide a random accessor at()
. By that method at()
, you can access to a specific case through not full iteration, but the special index number.
- Permutation
- Factorial
- RepeatedPermutation
- CartesianProduct
CombinationRepeatedCombination
import { Permutation } from "cagen";
function main(): void
{
let generator = new Permutation(7, 3);
console.log( generator.at(13) );
console.log( generator.at(31) );
console.log( generator.at(9999) ); // throw an std.OutOfRange error.
}
main();