/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */ /** */
export class BitArray {
    _ref;
    bytesPerElement;
    static _bitCountPerByte = 8;
    bitCountPerArrayElement;
    constructor(_ref, bytesPerElement) {
        this._ref = _ref;
        this.bytesPerElement = bytesPerElement;
        this.bitCountPerArrayElement = bytesPerElement * 8;
    }
    get bitCount() {
        return this._ref.buffer.length * this.bitCountPerArrayElement;
    }
    get byteCount() {
        return this._ref.buffer.length * this.bytesPerElement;
    }
    get(index) {
        const bitExistsAtArrayIndex = Math.floor(index / this.bitCountPerArrayElement);
        if (bitExistsAtArrayIndex >= this._ref.buffer.length) {
            return false;
        }
        const indexOfBitInsideArrayElement = index - bitExistsAtArrayIndex * this.bitCountPerArrayElement;
        const arrayElementContainingBit = this._ref.buffer[bitExistsAtArrayIndex];
        return arrayElementContainingBit === (arrayElementContainingBit | (1 << indexOfBitInsideArrayElement));
    }
    toBinaryStrings() {
        const binary = [];
        const len = this._ref.buffer.length;
        for (let i = 0; i < len; ++i) {
            // Handles both sparse and dense arrays. Notice type coersion with >>> to force unsigned integer.
            binary.push(((this._ref.buffer[i] ?? 0) >>> 0).toString(2).padStart(this.bitCountPerArrayElement, '0'));
        }
        return binary;
    }
}
export class DenseBitArray extends BitArray {
    _lazyResizable;
    constructor(_lazyResizable) {
        // Pass _lazyResizable (byRef) so BitArray will reference _lazyResizable.buffer. Means that
        // when this.setMinLenght() resizes _lazyResizable.buffer then BitArray will still reference the new array
        super(_lazyResizable, _lazyResizable.buffer.BYTES_PER_ELEMENT);
        this._lazyResizable = _lazyResizable;
    }
    setMinLength(bitMinLength) {
        const arrayMinLength = Math.floor(bitMinLength / this.bitCountPerArrayElement);
        this._lazyResizable.setMinLength(arrayMinLength + 1);
    }
    set(index, truthy) {
        const arrayIndex = Math.floor(index / this.bitCountPerArrayElement);
        this._lazyResizable.setMinLength(arrayIndex + 1);
        const bitIndex = index - arrayIndex * this.bitCountPerArrayElement;
        if (!truthy) {
            this._ref.buffer[arrayIndex] = this._ref.buffer[arrayIndex] & ~(1 << bitIndex);
        }
        else {
            this._ref.buffer[arrayIndex] = this._ref.buffer[arrayIndex] | (1 << bitIndex);
        }
        return this;
    }
    clear() {
        this._lazyResizable.reset();
    }
}
export class SparseBitArray extends BitArray {
    constructor() {
        // [0] creates 32bit array in V8 engine
        super({ buffer: [0] }, Int32Array.BYTES_PER_ELEMENT);
    }
    set(index, truthy) {
        const arrayIndex = Math.floor(index / this.bitCountPerArrayElement);
        const bitIndex = index - arrayIndex * this.bitCountPerArrayElement;
        if (!truthy) {
            this._ref.buffer[arrayIndex] = this._ref.buffer[arrayIndex] & ~(1 << bitIndex);
        }
        else {
            this._ref.buffer[arrayIndex] = this._ref.buffer[arrayIndex] | (1 << bitIndex);
        }
        return this;
    }
    clear() {
        this._ref.buffer = [0];
    }
}
//# sourceMappingURL=bit-array.js.map