ultra-mega-enumerator
Version:
Ultra Mega Enumerator is a lightweight library designed to enumerate various combinatorial objects.
61 lines • 1.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MixedRadixEnumeration = void 0;
const AbstractEnumeration_1 = require("./AbstractEnumeration");
/**
* Enumerates all values of the mixed radix base given as a parameter.
*
* @link http://en.wikipedia.org/wiki/Mixed_radix
*/
class MixedRadixEnumeration extends AbstractEnumeration_1.AbstractEnumeration {
constructor(base) {
super();
this.base = [];
this.current = null;
this.currentIndex = -1;
this.total = -1;
this.base = [...base].map((b => Math.round(b)));
for (let i = 0; i < base.length; i++) {
if (base[i] < 1) {
throw new Error("MixedRadixEnumeration - non-positive base");
}
}
this.total = 1;
for (const b of base) {
this.total *= b;
}
this.total = Math.round(this.total);
this.current = null;
}
hasMoreElements() {
return this.currentIndex < this.total - 1;
}
nextElement() {
if (!this.hasMoreElements()) {
throw new Error("No such element");
}
let result = [];
if (this.currentIndex == -1) {
for (let i = 0; i < this.base.length; i++) {
result.push(0);
}
}
else {
result = [...this.current];
for (let i = 0; i < this.base.length; i++) {
if (result[i] < (this.base[i] - 1)) {
result[i]++;
break;
}
else {
result[i] = 0;
}
}
}
this.current = [...result];
this.currentIndex++;
return result;
}
}
exports.MixedRadixEnumeration = MixedRadixEnumeration;
//# sourceMappingURL=MixedRadixEnumeration.js.map