UNPKG

ultra-mega-enumerator

Version:

Ultra Mega Enumerator is a lightweight library designed to enumerate various combinatorial objects.

120 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Composition = void 0; const Combination_1 = require("./Combination"); class Composition extends Combination_1.Combination { constructor(n) { super(n - 1); } getSum() { return this.size() + 1; } // Creating a Composition from a boolean array static compositionFromBooleanArray(comp) { const instance = new Composition(comp.length + 1); for (let i = 0; i < instance.n; i++) { if (comp[i]) { instance.set(i, true); } } return instance; } static compositionFromCombination(comb) { const nsb = comb.nextSetBit(0); if (nsb === -1) { return new Composition(comb.size()); } else { const t = comb.rotate(-nsb); const l = []; for (let i = 1; i < comb.size(); i++) { l.push(t.get(i)); } return Composition.compositionFromBooleanArray(l); } } static compositionFromBitSet(x) { const instance = new Composition(x.size() + 1); for (let i = 0; i < x.size(); i++) { if (x.get(i)) { instance.set(i, true); } } return instance; } getCompositionAsArray() { const result = []; let count = 1; for (let i = 0; i < this.n; i++) { if (this.get(i)) { result.push(count); count = 1; } else { count++; } } result.push(count); // Push the last segment return result; } getCompositionAsCombination() { const o = new Combination_1.Combination(this.n + 1); o.set(0); for (let i = 1; i < this.n + 1; i++) { o.set(i, this.get(i - 1)); } return o; } degrade() { const c = this.cardinality(); if (c === 0) { throw new Error("Composition cannot be degraded because it has a cardinality of zero."); } const o = new Composition(this.getSum() - 1); if (c === 1) { return o; } let i = this.nextSetBit(0); // It can't be that i = -1 for (let j = 0; j < o.n; j++) { o.set(j, this.get((j + i + 1) % o.n)); } return o; } partitionByEquality() { const s = this.getCompositionAsArray(); const groups = new Array(s.length).fill(0); let k = 0; for (let j = s.length - 1; j >= 0; j--) { if (s[0] === s[j]) { k--; } else { break; } } k += s.length; k = k % s.length; let previousValue = s[k]; let currentGroup = 0; for (let i = k + 1; i < s.length + k; i++) { const v = s[i % s.length]; if (v !== previousValue) { currentGroup++; } groups[i % groups.length] = currentGroup; previousValue = v; } return groups; } toString() { return "{" + this.getCompositionAsArray().join(",") + "}"; } static compositionRefinements(co) { const c = Combination_1.Combination.combinationRefinements(co); return c.map((o) => { const x = new Composition(co.getSum()); x.or(o); return x; }); } } exports.Composition = Composition; //# sourceMappingURL=Composition.js.map