UNPKG

@rimbu/sorted

Version:

Immutable SortedMap and SortedSet implementations for TypeScript

64 lines 2.14 kB
import { RSetBase } from '@rimbu/collection-types/set-custom'; import { Comp } from '@rimbu/common'; import { SortedSetBuilder, SortedSetEmpty, SortedSetInner, SortedSetLeaf, SortedSetNode, } from '@rimbu/sorted/set-custom'; export class SortedSetContext extends RSetBase.ContextBase { constructor(blockSizeBits, comp) { super(); this.blockSizeBits = blockSizeBits; this.comp = comp; this.typeTag = 'SortedSet'; this.builder = () => { return new SortedSetBuilder(this); }; this.maxEntries = 1 << this.blockSizeBits; this.minEntries = this.maxEntries >>> 1; this._empty = Object.freeze(new SortedSetEmpty(this)); } isNonEmptyInstance(source) { return source instanceof SortedSetNode; } isValidValue(value) { return this.comp.isComparable(value); } createBuilder(source) { return new SortedSetBuilder(this, source); } findIndex(value, entries) { let start = 0; let end = entries.length - 1; while (start <= end) { const mid = (start + end) >>> 1; const midEntry = entries[mid]; const comp = this.comp.compare(value, midEntry); if (comp < 0) end = mid - 1; else if (comp > 0) start = mid + 1; else return mid; } return -(start + 1); } leaf(entries) { return new SortedSetLeaf(this, entries); } inner(entries, children, size) { return new SortedSetInner(this, entries, children, size); } isSortedSetEmpty(obj) { return obj instanceof SortedSetEmpty; } isSortedSetLeaf(obj) { return obj instanceof SortedSetLeaf; } isSortedSetInner(obj) { return obj instanceof SortedSetInner; } isSortedSetNode(obj) { return obj instanceof SortedSetNode; } } export function createSortedSetContext(options) { return Object.freeze(new SortedSetContext(options?.blockSizeBits ?? 5, options?.comp ?? Comp.defaultComp())); } //# sourceMappingURL=context.mjs.map