ultra-mega-enumerator
Version:
Ultra Mega Enumerator is a lightweight library designed to enumerate various combinatorial objects.
51 lines • 1.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetPartitionEnumeration = void 0;
// SetPartitionEnumeration.ts
const AbstractEnumeration_1 = require("./AbstractEnumeration");
class SetPartitionEnumeration extends AbstractEnumeration_1.AbstractEnumeration {
constructor(n) {
super();
this.n = n;
this.k = new Array(n).fill(0);
this.m = new Array(n).fill(0);
this.hasMore = true;
this.initializeFirstPartition();
}
initializeFirstPartition() {
// No need for explicit initialization, as we already filled arrays with 0s.
}
nextPartition() {
for (let i = this.n - 1; i > 0; i--) {
if (this.k[i] <= this.m[i - 1]) {
this.k[i]++;
this.m[i] = Math.max(this.m[i], this.k[i]);
for (let j = i + 1; j < this.n; j++) {
this.k[j] = 0;
this.m[j] = this.m[i];
}
return true;
}
}
return false;
}
hasMoreElements() {
return this.hasMore;
}
nextElement() {
if (!this.hasMore) {
throw new Error("No such element");
}
const currentPartition = [...this.k];
if (this.n === 0) {
this.hasMore = false;
return [];
}
else {
this.hasMore = this.nextPartition();
}
return currentPartition;
}
}
exports.SetPartitionEnumeration = SetPartitionEnumeration;
//# sourceMappingURL=SetPartitionEnumeration.js.map