UNPKG

typedfastbitset

Version:

Speed-optimized BitSet implementation for modern browsers and JavaScript engines, using typed arrays

196 lines (195 loc) 6.83 kB
/** * TypedFastBitSet.js : a fast bit set implementation in JavaScript. * (c) the authors * Licensed under the Apache License, Version 2.0. * * Speed-optimized BitSet implementation for modern browsers and JavaScript engines. * * A BitSet is an ideal data structure to implement a Set when values being stored are * reasonably small integers. It can be orders of magnitude faster than a generic set implementation. * The FastBitSet implementation optimizes for speed, leveraging commonly available features * like typed arrays. * * Simple usage : * const b = new TypedFastBitSet();// initially empty * // will throw exception if typed arrays are not supported * b.add(1);// add the value "1" * b.has(1); // check that the value is present! (will return true) * b.add(2); * console.log(""+b);// should display {1,2} * b.add(10); * b.array(); // would return [1,2,10] * * let c = new FastBitSet([1,2,3,10]); // create bitset initialized with values 1,2,3,10 * c.difference(b); // from c, remove elements that are in b (modifies c) * c.difference2(b); // from c, remove elements that are in b (modifies b) * c.change(b); // c will contain elements that are in b or in c, but not both * const su = c.union_size(b);// compute the size of the union (bitsets are unchanged) * c.union(b); // c will contain all elements that are in c and b * const s1 = c.intersection_size(b);// compute the size of the intersection (bitsets are unchanged) * c.intersection(b); // c will only contain elements that are in both c and b * c = b.clone(); // create a (deep) copy of b and assign it to c. * c.equals(b); // check whether c and b are equal * * See README.md file for a more complete description. * * You can install the library under node with the command line * npm install typedfastbitset */ import { BitSet } from "./utils"; /** * you can provide an iterable * an exception is thrown if typed arrays are not supported */ export declare class TypedFastBitSet implements BitSet { words: Uint32Array; constructor(iterable?: Iterable<number> | null, words?: Uint32Array); /** * @returns a new TypedFastBitset given a Uint32Array of words */ static fromWords(words: Uint32Array): TypedFastBitSet; /** * Add the value (Set the bit at index to true) */ add(index: number): void; /** * If the value was not in the set, add it, otherwise remove it (flip bit at index) */ flip(index: number): void; /** * Remove all values, reset memory usage */ clear(): void; /** * Set the bit at index to false */ remove(index: number): void; /** * Set bits from start (inclusive) to end (exclusive) */ addRange(start: number, end: number): void; /** * Remove bits from start (inclusive) to end (exclusive) */ removeRange(start: number, end: number): void; /** * @returns true if no bit is set */ isEmpty(): boolean; /** * Is the value contained in the set? Is the bit at index true or false? */ has(index: number): boolean; /** * Is any value of the (exclusive) range contained in the set? */ hasAnyInRange(start: number, end: number): boolean; /** * Tries to add the value (Set the bit at index to true) * * @returns 1 if the value was added, 0 if the value was already present */ checkedAdd(index: number): 0 | 1; /** * Reduce the memory usage to a minimum */ trim(): void; /** * Resize the bitset so that we can write a value at index */ resize(index: number): void; /** * @returns How many values stored in the set? How many set bits? */ size(): number; /** * @returns an array with the set bit locations (values) */ array(): number[]; forEach(fnc: (id: number) => void): void; /** * Iterator of set bit locations (values) */ [Symbol.iterator](): IterableIterator<number>; /** * @returns a copy of this bitmap */ clone(): TypedFastBitSet; /** * Check if this bitset intersects with another one, * no bitmap is modified */ intersects(otherbitmap: BitSet): boolean; /** * Computes the intersection between this bitset and another one, * the current bitmap is modified (and returned by the function) */ intersection(otherbitmap: BitSet): this; /** * Computes the size of the intersection between this bitset and another one */ intersection_size(otherbitmap: BitSet): number; /** * Computes the intersection between this bitset and another one, * a new bitmap is generated */ new_intersection(otherbitmap: BitSet): TypedFastBitSet; /** * Computes the intersection between this bitset and another one, * the current bitmap is modified */ equals(otherbitmap: BitSet): boolean; /** * Computes the difference between this bitset and another one, * the current bitset is modified (and returned by the function) */ difference(otherbitmap: BitSet): this; /** * Computes the difference between this bitset and another one, * the other bitset is modified (and returned by the function) * * (for this set A and other set B, this computes B = A - B and returns B) */ difference2(otherbitmap: BitSet): BitSet; /** * Computes the difference between this bitset and another one, * a new bitmap is generated */ new_difference(otherbitmap: BitSet): TypedFastBitSet; /** * Computes the size of the difference between this bitset and another one */ difference_size(otherbitmap: BitSet): number; /** * Computes the changed elements (XOR) between this bitset and another one, * the current bitset is modified (and returned by the function) */ change(otherbitmap: BitSet): this; /** * Computes the change between this bitset and another one, * a new bitmap is generated */ new_change(otherbitmap: BitSet): TypedFastBitSet; /** * Computes the number of changed elements between this bitset and another one */ change_size(otherbitmap: BitSet): number; /** * @returns a string representation */ toString(): string; /** * Computes the union between this bitset and another one, * the current bitset is modified (and returned by the function) */ union(otherbitmap: BitSet): this; /** * Computes the union between this bitset and another one, * a new bitmap is generated */ new_union(otherbitmap: BitSet): TypedFastBitSet; /** * Computes the size union between this bitset and another one */ union_size(otherbitmap: BitSet): number; }