@lp-labs/roaring-ranges
v0.1.0
Published
Bounded uint32 interval-set algebra using sorted disjoint ranges. Despite the package name, NOT a Roaring bitmap or its binary format.
Readme
@lp-labs/roaring-ranges
Bounded uint32 interval-set algebra using sorted disjoint ranges. Despite the package name, NOT a Roaring bitmap or its binary format.
Install
npm install @lp-labs/roaring-rangesVersion 0.1.0. MIT. Zero runtime, peer, or sibling dependencies. Node >=18; browser-compatible ES2022 runtime without browser-global reads. ESM and CommonJS exports have separate matching declarations.
Quick start
import { RoaringRanges } from '@lp-labs/roaring-ranges';
const allocated = new RoaringRanges([
[0, 10],
[20, 30],
]);
const unavailable = new RoaringRanges([[5, 25]]);
const free = allocated.difference(unavailable);
if (free.cardinality !== 10 || !free.has(29)) throw new Error('ranges');
console.log(free.toRanges()); // [[0, 5], [25, 30]]Contract, customization, errors and ownership
Uint32Range is a readonly [start,end) pair of integer endpoints: 0 <= start < end <= 2^32. The exclusive endpoint 2^32 is allowed, but membership values must be uint32 0..2^32-1. Empty/reversed intervals, fractions, NaN and out-of-domain values throw RangeError; malformed pair shapes throw TypeError. Full-domain cardinality 2^32 is exactly representable. Intervals never expand into individual members.
maxRanges defaults 100,000 and is an integer 0..1,000,000. It bounds RAW input pair count, even when they would merge, and final output range count of operations. Constructor copies, sorts by start and merges overlap/adjacency to canonical disjoint nonadjacent pairs. Empty input is allowed, including maxRanges zero.
union/intersection/difference return new sets using the LEFT operand's maxRanges budget; a result requiring more ranges throws RangeError and leaves both operands unchanged. Budget checks occur during monotonic normalized output construction; difference can require more ranges than either operand. Operations require another RoaringRanges. has uses binary search; rangeCount counts canonical pairs, cardinality sums represented integers. toRanges returns owned mutable pairs; mutating input/output cannot change stored ranges.
Constructor O(n log n), set algebra merge scans O(n+m) plus result construction/sort O(k log k) in the current implementation; has O(log n), cardinality O(n), snapshot O(n), storage O(n). No binary serialization, Roaring containers, portable Roaring compatibility, rank/select, or per-value bitmaps. The honest range representation is suited to contiguous runs, not sparse random bitsets.
Complete public declaration contract
Runtime exports and all exported types are listed below. Internal storage members are omitted. Optional settings are constructor/function options, not globals.
/** Half-open integer range; end may equal 2^32. This is NOT a Roaring bitmap format. */
export type Uint32Range = readonly [start: number, end: number];
export declare class RoaringRanges {
constructor(ranges?: readonly Uint32Range[], maxRanges?: number);
get rangeCount(): number;
get cardinality(): number;
has(value: number): boolean;
toRanges(): [number, number][];
union(other: RoaringRanges): RoaringRanges;
intersection(other: RoaringRanges): RoaringRanges;
difference(other: RoaringRanges): RoaringRanges;
}Development
npm ci --offline
npm run build
npm run typecheck
npm test
npm run format:checkTypeScript ^5.9.3 and Prettier ^3.6.2 are development-only. Offline installation requires their locked packages already in the local npm cache. Build emits dist/esm and dist/cjs with separate declarations; tests exercise runtime behavior and bare-package ESM/CommonJS type consumers with negative checks. No publish step is needed to build or test.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
