@rimbu/sorted
Version:
Immutable SortedMap and SortedSet implementations for TypeScript
61 lines • 2.05 kB
JavaScript
import { RMapBase } from '@rimbu/collection-types/map-custom';
import { Comp } from '@rimbu/common';
import { SortedMapEmpty, SortedMapNode, SortedMapLeaf, SortedMapInner, SortedMapBuilder, } from '@rimbu/sorted/map-custom';
export class SortedMapContext extends RMapBase.ContextBase {
constructor(blockSizeBits, comp) {
super();
this.blockSizeBits = blockSizeBits;
this.comp = comp;
this.typeTag = 'SortedMap';
this.builder = () => {
return new SortedMapBuilder(this);
};
this.maxEntries = 1 << blockSizeBits;
this.minEntries = this.maxEntries >>> 1;
this._empty = Object.freeze(new SortedMapEmpty(this));
}
isNonEmptyInstance(source) {
return source instanceof SortedMapNode;
}
createBuilder(source) {
return new SortedMapBuilder(this, source);
}
isValidKey(key) {
return this.comp.isComparable(key);
}
findIndex(key, 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(key, midEntry[0]);
if (comp < 0)
end = mid - 1;
else if (comp > 0)
start = mid + 1;
else
return mid;
}
return -(start + 1);
}
leaf(entries) {
return new SortedMapLeaf(this, entries);
}
inner(entries, children, size) {
return new SortedMapInner(this, entries, children, size);
}
isSortedMapEmpty(obj) {
return obj instanceof SortedMapEmpty;
}
isSortedMapLeaf(obj) {
return obj instanceof SortedMapLeaf;
}
isSortedMapInner(obj) {
return obj instanceof SortedMapInner;
}
}
export function createSortedMapContext(options) {
return Object.freeze(new SortedMapContext(options?.blockSizeBits ?? 5, options?.comp ?? Comp.defaultComp()));
}
//# sourceMappingURL=context.mjs.map