UNPKG

ultra-mega-enumerator

Version:

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

194 lines 5.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BitSet = void 0; class BitSet { constructor(n) { if (n < 0) { throw new Error("Invalid BitSet size."); } this.n = n; this.bits = new Set(); } getTrueBits() { return new Set(this.bits); } compareTo(other) { if (this.n < other.n) return -1; if (this.n > other.n) return 1; for (let i = 0; i < this.n; i++) { const thisBit = this.get(i); const otherBit = other.get(i); if (thisBit < otherBit) return -1; if (thisBit > otherBit) return 1; } return 0; } size() { return this.n; } getBitSetAsBooleanArray() { return Array.from({ length: this.n }, (_, i) => this.get(i) ? true : false); } get(bitIndex) { if (bitIndex >= this.n) { throw new Error("Index out of bounds"); } return this.bits.has(bitIndex); } set(bitIndex, value = true) { if (bitIndex >= this.n) { throw new Error("Index out of bounds"); } if (value) { this.bits.add(bitIndex); } else { this.bits.delete(bitIndex); } } clear(bitIndex) { this.bits.delete(bitIndex); } flip(bitIndex) { if (this.bits.has(bitIndex)) { this.bits.delete(bitIndex); } else { this.bits.add(bitIndex); } } nextSetBit(fromIndex) { for (let i = fromIndex; i < this.n; i++) { if (this.get(i)) { return i; } } return -1; } nextClearBit(fromIndex) { for (let i = fromIndex; i < this.n; i++) { if (!this.get(i)) { return i; } } return -1; } previousSetBit(fromIndex) { for (let i = fromIndex; i >= 0; i--) { if (this.get(i)) { return i; } } return -1; } previousClearBit(fromIndex) { for (let i = fromIndex; i >= 0; i--) { if (!this.get(i)) { return i; } } return -1; } toBitString() { return Array.from({ length: this.n }, (_, i) => this.get(this.n - (i + 1)) ? "1" : "0").join(""); } toString() { return this.toBitString(); } cardinality() { return this.bits.size; } and(bitSet) { this.bits = new Set([...this.bits].filter(bit => bitSet.get(bit))); } or(bitSet) { this.bits = new Set([...this.bits, ...Array.from(bitSet.bits)]); } xor(bitSet) { const union = new Set([...this.bits, ...Array.from(bitSet.bits)]); const intersection = new Set([...this.bits].filter(bit => bitSet.get(bit))); this.bits = new Set([...union].filter(bit => !intersection.has(bit))); } equals(obj) { return obj instanceof BitSet && this.n === obj.n && [...this.bits].every(bit => obj.get(bit)); } copy() { const clone = new BitSet(this.n); this.bits.forEach(bit => clone.set(bit)); return clone; } getBitSetAsNumberArray() { return Array.from({ length: this.n }, (_, i) => this.get(i) ? 1 : 0); } rotate(t) { let k = t; while (k < 0) k += this.n; while (k >= this.n) k -= this.n; const rotated = new BitSet(this.n); this.bits.forEach(bit => rotated.set((bit + k + this.n) % this.n)); return rotated; } union(other) { const result = new BitSet(this.n); other.bits.forEach(bit => result.set(bit)); this.bits.forEach(bit => result.set(bit)); return result; } intersection(other) { const result = new BitSet(this.n); this.bits.forEach(bit => { if (other.get(bit)) { result.set(bit); } }); return result; } minus(other) { const result = new BitSet(this.n); this.bits.forEach(bit => { if (!other.get(bit)) { result.set(bit); } }); return result; } static bitSetFromNumberArray(bitArray) { const bs = new BitSet(bitArray.length); bitArray.forEach((bit, i) => bs.set(i, bit === 1)); return bs; } static fromBitString(bitString) { const na = []; for (let i = 0; i < bitString.length; i++) { na.push(bitString.charAt(i) === "1" ? 1 : 0); } return BitSet.bitSetFromNumberArray(na); } intersects(bitSet) { return Array.from(this.bits).some(bit => bitSet.get(bit)); } isEmpty() { return this.bits.size === 0; } resize(newSize) { if (newSize < 1) { throw new Error("Invalid BitSet size"); } const newBits = new Set(); this.bits.forEach(bit => { if (bit < newSize) { newBits.add(bit); } }); this.n = newSize; this.bits = newBits; } } exports.BitSet = BitSet; //# sourceMappingURL=BitSet.js.map