@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
192 lines • 6.49 kB
TypeScript
/**
* A dynamically resizable bitset (bit array) implementation.
* The bitset automatically grows and shrinks as bits are set and cleared.
*
* @example
* const bits = new BitSet();
* bits.set(10);
*
* bits.get(11) // false
* bits.get(10) // true
* bits.get(9) // false
*
* @author Alex Goldring
* @copyright Company Named Limited (c) 2025
*/
export class BitSet {
/**
*
* @param {number} x
* @returns {BitSet}
*/
static fixedSize(x: number): BitSet;
/**
* @constructor
* @param {number} [initial_capacity] optional, can supply initial capacity in bits to avoid resizing.
*/
constructor(initial_capacity?: number);
/**
* Number of bits currently in use (highest set bit + 1).
* @private
* @type {number}
*/
private __length;
/**
* Current capacity in bits, this is at least equal to length
* Always a multiple of 32.
* @private
* @type {number}
*/
private __capacity;
/**
* Uint32-backed storage
* @type {Uint32Array}
* @private
*/
private __data_uint32;
/**
* The fraction of capacity at which to trigger a shrink operation.
* @type {number}
*/
__shrinkFactor: number;
/**
* Prevents the BitSet from shrinking automatically.
* Useful for fixed-size bitsets.
* @returns {void}
*/
preventShrink(): void;
/**
*
* @param {number} x The new shrink factor (must be between 0 and 1, inclusive of 0, exclusive of 1).
*/
setShrinkFactor(x: number): void;
/**
* Sets the capacity of the bitset, ensuring enough space.
* Does NOT change set bits.
* @param {number} bit_count The new capacity, in bits.
* @throws {Error} If the requested capacity is smaller than the current {@link size}.
* @returns {void}
*/
setCapacity(bit_count: number): void;
/**
* Returns the number of bits currently "used" by the BitSet.
* This is equivalent to the index of the highest set bit plus one.
* @returns {number} The size of the BitSet (in bits).
*/
size(): number;
/**
* Returns the current capacity (allocated bits) of the BitSet.
* This is always a multiple of 32.
* @returns {number} The capacity of the BitSet (in bits).
*/
capacity(): number;
/**
* Resizes the internal Uint32Array to the specified capacity.
* @param {number} bitCapacity New capacity in bits.
* @private
*/
private __resize;
/**
* Updates the internal length of the bitset after clearing bits.
* @private
*/
private __updateLength;
/**
* Sets the internal length of the BitSet and manages capacity.
* Grows or shrinks if necessary
* @param {number} new_length The new length of the BitSet.
* @private
*/
private __setLength;
/**
* Returns the index of the nearest bit that is set to true that occurs on or before the specified starting index
* @param {number} from_index The index to start searching from (inclusive).
* @returns {number} Index of previous set bit, or -1 if no set bit found
*/
previousSetBit(from_index: number): number;
/**
* Returns the index of the first bit that is set to false that occurs on or after the specified starting index.
* @param {number} from_index The index to start searching from (inclusive).
* @returns {number} index of the next set bit, or -1 if no bits are set beyond supplied index
*/
nextSetBit(from_index: number): number;
/**
* Returns the index of the first bit that is set to false that occurs on or after the specified starting index.
* @param {number} from_index The index to start searching from (inclusive).
* @returns {number} Index of the next clear bit (bit set to false), or the current length of BitSet if all the bits are set.
*/
nextClearBit(from_index: number): number;
/**
* Sets the bit at the specified index to the specified value.
* @param {number} bit_index The index of the bit to set.
* @param {boolean} value The value to set the bit to (true for 1, false for 0).
* @returns {void}
*/
set(bit_index: number, value: boolean): void;
/**
* Sets the bit specified by the index to false (0).
* @param {number} bit_index The index of the bit to clear.
*/
clear(bit_index: number): void;
/**
* Set all bits in a given range
* @param {number} start_index first bit to be set (inclusive)
* @param {number} end_index last bit to be set (inclusive)
*/
setRange(start_index: number, end_index: number): void;
/**
* Clears bit values in a given (inclusive) range
* @param {number} start_index first bit to be cleared (inclusive)
* @param {number} end_index clear up to here, excluding this position
*/
clearRange(start_index: number, end_index: number): void;
/**
* Returns the value of the bit with the specified index.
* @param {int} bit_index The index of the bit to get.
* @returns {boolean} The value of the bit (true for 1, false for 0). Returns `false` if `bitIndex` is out of bounds.
*/
get(bit_index: int): boolean;
/**
* Set a bit at the specified index and return its previous value
* @param {number} index
* @returns {boolean}
*/
getAndSet(index: number): boolean;
/**
* Clear a bit at the specified index and return its previous value
* @param {number} index
* @returns {boolean}
*/
getAndClear(index: number): boolean;
/**
*
* @param {number} distance
* @param {number} start_index
* @param {number} end_index
*/
shift_right(distance: number, start_index: number, end_index: number): void;
/**
*
* @param {number} distance
* @param {number} start_index
* @param {number} end_index
*/
shift_left(distance: number, start_index: number, end_index: number): void;
/**
*
* @param {number} distance
* @param {number} start_index
* @param {number} end_index
*/
shift(distance: number, start_index: number, end_index: number): void;
/**
* Sets all the bits in this BitSet to false.
*/
reset(): void;
/**
* Copy contents of another bit set into this one
* @param {BitSet} other
*/
copy(other: BitSet): void;
}
//# sourceMappingURL=BitSet.d.ts.map