UNPKG

@thi.ng/bitfield

Version:

1D / 2D bit field implementations

114 lines 3.6 kB
import type { Fn2, IClear, ICopy, ILength } from "@thi.ng/api"; /** * 1D bit field, backed by a Uint8Array. Hence size is always rounded * up to a multiple of 8. */ export declare class BitField implements IClear, ICopy<BitField>, ILength { /** Backing byte array */ data: Uint8Array; /** Field size in bits (always a multiple of 8) */ n: number; constructor(bits: number | string | ArrayLike<boolean | number>, buf?: Uint8Array); get length(): number; /** * Yields iterator of the field's individual bits. */ [Symbol.iterator](): Generator<1 | 0, void, unknown>; /** * Yields iterator of positions/indices of all set bits only. */ positions(): Generator<number, void, unknown>; clear(): void; copy(): BitField; /** * Resizes bitfield to new size given (rounded up to multiples of * 8). * * @param n - new size */ resize(n: number): this; /** * Returns a non-zero value if bit `n` is enabled. No bounds * checking. * * @param n - bit number */ at(n: number): number; /** * Enables or disables bit `n`. Returns a non-zero value if the bit * was previously enabled. No bounds checking. * * @param n - bit number * @param v - new bit value */ setAt(n: number, v?: boolean | number): number; /** * Sets bits from `start` index with given `values`. No bounds * checking. * * @param start - * @param vals - */ setRange(start: number, vals: string | ArrayLike<boolean | number>): this; /** * Set bits from `start` until `end` index to given `value`. If `end` is * omitted, defaults to end of entire field. * * @param value * @param start * @param end */ fill(value: boolean | number, start?: number, end?: number): this; /** * Inverts bit `n`. Returns a non-zero value if the bit was * previously enabled. No bounds checking. * * @param n - bit number */ toggleAt(n: number): number; /** * Returns number of set bits (1's) in the bitfield. */ popCount(): number; /** * Same as {@link BitField.popCount}, but as normalized ratio/percentage. */ density(): number; /** * Computes the Jaccard similarity with given `field`. Returns a value in * `[0,1]` interval: 1.0 if `a` and `b` are equal, or 0.0 if none of the * components match. * * @remarks * If `field` is not a `BitField` instance, one will be created for it. The * resulting sizes of both fields MUST be equal. * * Reference: https://en.wikipedia.org/wiki/Jaccard_index * * @param field */ similarity(field: BitField | string | ArrayLike<boolean | number>): number; and(field: BitField): this; or(field: BitField): this; xor(field: BitField): this; not(): this; /** * Returns position of first 0-bit, starting at given `from` search * position. Returns -1 if unsuccessful. * * @param from */ firstZero(from?: number): number; /** * Returns position of first 1-bit, starting at given `from` search * position. Returns -1 if unsuccessful. * * @param from */ firstOne(from?: number): number; toString(): string; protected binOp(field: BitField, op: Fn2<number, number, number>): this; protected ensureSize(field: BitField): void; } export declare const defBitField: (bits: number | string | ArrayLike<boolean | number>) => BitField; //# sourceMappingURL=bitfield.d.ts.map