Efficient TypeScript bitmap indexing library for set operations and bitmasking.
Go to file
Eugene f840742724 fix(bitmap): correct growth at byte boundaries, add explicit bounds checks, and adjust bitwise ops
- grow now accounts for bit index + 1 when computing required byte length to avoid off-by-one at byte boundaries
- contains/remove use explicit bounds checks (idx >= this.bits.length) instead of relying on truthy checks
- and clears tail bytes not covered by the shorter operand
- or/xor expand the result to encompass a longer operand so all bits are preserved
- update behavior to be more predictable and safer; tests pass
2025-12-01 21:58:51 +03:00
src fix(bitmap): correct growth at byte boundaries, add explicit bounds checks, and adjust bitwise ops 2025-12-01 21:58:51 +03:00
.gitignore init commit 2024-07-04 22:34:19 +03:00
README.md markdown update 2025-12-01 17:46:25 +03:00
jest.config.js Fixed bug with 0-size bitmap (.grow(x)) 2025-04-02 09:08:46 +03:00
package.json 0.0.8 2025-12-01 19:26:31 +03:00
tsconfig.json init commit 2024-07-04 22:34:19 +03:00
vite.config.ts init commit 2024-07-04 22:34:19 +03:00
yarn.lock Update vite version 2025-04-02 09:09:47 +03:00

README.md

Bitmap-Index Library

NPM version Downloads

This TypeScript library provides a bitmap implementation (Bitmap) that allows efficient manipulation of a bitmap data structure, useful for various indexing and set operations.

Features

  • Set Operations: set, remove, contains, and clear operations on the bitmap.
  • Logical Operations: and, andNot, or, and xor operations between bitmaps.
  • Iteration and Filtering: range iteration, generator-based iteration via [Symbol.iterator], and filter based on user-defined conditions.
  • Count and Extremes: Methods to count set bits (count), find the minimum and maximum set bit (min, max), as well as the minimum and maximum unset bit (minZero, maxZero).
  • Cloning: Supports cloning of the bitmap instance.
  • Iterable Support: Can be used directly in for...of loops and spread syntax ([...bitmap]) for convenient iteration over set bits.

Installation

You can install the library via npm:

npm install bitmap-index

Usage Example

import { Bitmap } from 'bitmap-index';

// Create a new Bitmap with a size of 32 bits
const bitmap = new Bitmap(32);

// Set bits
bitmap.set(2);
bitmap.set(5);
bitmap.set(10);

// Check if a bit is set
console.log(bitmap.contains(5)); // true
console.log(bitmap.contains(3)); // false

// Perform logical operations with another bitmap
const otherBitmap = new Bitmap(32);
otherBitmap.set(5);
otherBitmap.set(10);

const andResult = bitmap.and(otherBitmap); // Bitmap with bits [5, 10]
const orResult = bitmap.or(otherBitmap);   // Bitmap with bits [2, 5, 10]
const xorResult = bitmap.xor(otherBitmap); // Bitmap with bits [2]

// Iterate over set bits using range
bitmap.range((x) => {
  console.log(`Bit ${x} is set`);
  return true; // Continue iterating
});

// Iterate using for...of (generator)
for (const bit of bitmap) {
  console.log(`Iterated bit: ${bit}`);
}

// Spread operator
const bitsArray = [...bitmap]; // [2, 5, 10]

// Filter based on a condition
bitmap.filter((x) => x % 2 === 0); // Remove odd-numbered bits

// Count the number of set bits
console.log(bitmap.count()); // 1

// Find the minimum and maximum set bits
console.log(bitmap.min()); // 2
console.log(bitmap.max()); // 10

// Clear all bits
bitmap.clear();

// Clone the bitmap
const clonedBitmap = bitmap.clone();

API Documentation

  • Bitmap Class

    • Constructor: new Bitmap(size: number = 32)
    • Set Operations: set(x: number), remove(x: number), contains(x: number), clear()
    • Logical Operations: and(bitmap: Bitmap), andNot(bitmap: Bitmap), or(bitmap: Bitmap), xor(bitmap: Bitmap)
    • Iteration and Filtering: range(fn: (x: number) => boolean), filter(fn: (x: number) => boolean), [Symbol.iterator](): Iterator<number>
    • Count and Extremes: count(), min(), max(), minZero(), maxZero()
    • Cloning: clone(): Bitmap

Note

This library is designed for bitmap indexing operations and is not related to image processing. For more information on bitmap indexing, please refer to Bitmap Index Wiki.


Feel free to contribute, report issues, or suggest improvements on GitHub.