@webdeveric/ts-data-structures
    Preparing search index...

    Class BitArray

    Fixed-size bit array backed by an unsigned typed array, used as dense storage for structures like Bloom filters where individual bits need to be set/tested without the overhead of a boolean array (8x smaller).

    Implements

    • Iterable<boolean>
    Index

    Constructors

    • input is either the number of addressable bits (rounded up internally to a whole number of storage items) or an existing typed array to use as backing storage directly - the array is not copied, so external mutation of it, or reuse of the same array across multiple BitArrays, is shared state.

      Choosing Uint8Array/Uint16Array/Uint32Array storage is a memory-density trade-off only, not a CPU one - JS bitwise operators coerce all operands to Int32 regardless of the typed array's element width. Total bytes used is the same either way (1 bit always costs 1/8 byte); what changes is how many storage items that's split across. Uint32Array (the default for a numeric size) uses the fewest elements, which means less per-element overhead. Uint8Array gives the finest granularity, which matters when handing the storage off to byte-oriented interop - serialization, Buffer/network transfer, etc. Uint16Array is a middle ground, useful mainly to match an existing 16-bit-oriented format.

      Parameters

      • input:
            | number
            | Uint16Array<ArrayBufferLike>
            | Uint32Array<ArrayBufferLike>
            | Uint8Array<ArrayBufferLike>

      Returns BitArray

      new BitArray(1000);              // 1000 bits, rounds up to 1024 (32 items)
      new BitArray(new Uint8Array(4)); // bring your own storage, 32 bits

    Accessors

    • get size(): number

      Returns the number of addressable bits in the array.

      Returns number

    • get storageSize(): number

      Returns the number of bits allocated in the underlying storage.

      This is not the same as size, which is the number of addressable bits.

      Returns number

    • get storage(): | Uint16Array<ArrayBufferLike>
      | Uint32Array<ArrayBufferLike>
      | Uint8Array<ArrayBufferLike>

      Returns the underlying typed array backing this BitArray, without copying it - useful for byte-oriented interop (serialization, Buffer/network transfer, etc.) that needs direct access to the bytes.

      Mutating the returned array mutates this BitArray directly.

      Returns
          | Uint16Array<ArrayBufferLike>
          | Uint32Array<ArrayBufferLike>
          | Uint8Array<ArrayBufferLike>

    • get "[toStringTag]"(): string

      Returns a string tag for Object.prototype.toString.call().

      Returns string

    Methods

    • Sets the bit at bitIndex to 1.

      Parameters

      • bitIndex: number

      Returns void

    • Clears the bit at bitIndex back to 0.

      Parameters

      • bitIndex: number

      Returns void

    • Toggles the bit at bitIndex.

      Parameters

      • bitIndex: number

      Returns void

    • Test if the bit at bitIndex is set (1 not 0).

      Parameters

      • bitIndex: number

      Returns boolean

    • Create an independent copy: a new BitArray with its own storage (of the same concrete type) and the same size. Unlike passing a typed array into the constructor, this does not alias the original storage.

      Returns BitArray

      const original = new BitArray(64);
      const copy = original.clone();
      copy.set(0); // does not affect `original`
    • Clears all bits back to 0, reusing the existing allocation.

      Returns void

    • Population count: total number of set bits across the whole array.

      Returns number

    • Bitwise AND with another BitArray.

      Parameters

      Returns this

      const a = new BitArray(64);
      a.set(1);
      a.set(2);

      const b = new BitArray(64);
      b.set(2);
      b.set(3);

      a.and(b); // `a` now has only bit 2 set

      if the two BitArrays have different backing storage types.

      if the two BitArrays are not the same size.

    • Bitwise OR with another BitArray.

      Parameters

      Returns this

      const a = new BitArray(64);
      a.set(1);
      a.set(2);

      const b = new BitArray(64);
      b.set(2);
      b.set(3);

      a.or(b); // `a` now has bits 1, 2, and 3 set

      if the two BitArrays have different backing storage types.

      if the two BitArrays are not the same size.

    • Bitwise XOR (exclusive OR) with another BitArray.

      Parameters

      Returns this

      const a = new BitArray(64);
      a.set(1);
      a.set(2);

      const b = new BitArray(64);
      b.set(2);
      b.set(3);

      a.xor(b); // `a` now has bits 1 and 3 set, bit 2 cleared

      if the two BitArrays have different backing storage types.

      if the two BitArrays are not the same size.

    • Iterates each bit in index order, yielding true for set bits and false for unset bits.

      Returns Generator<boolean>