@rimbu/hashed
Version:
Immutable HashMap and HashSet implementations for TypeScript
58 lines • 2.18 kB
JavaScript
import { RMapBase } from '@rimbu/collection-types/map-custom';
import { Eq } from '@rimbu/common';
import { List } from '@rimbu/list';
import { Hasher } from '../../common/index.mjs';
import { HashMapNonEmptyBase, HashMapEmpty, HashMapBlock, HashMapCollision, HashMapBlockBuilder, } from '@rimbu/hashed/map-custom';
export class HashMapContext extends RMapBase.ContextBase {
constructor(hasher, eq, blockSizeBits, listContext) {
super();
this.hasher = hasher;
this.eq = eq;
this.blockSizeBits = blockSizeBits;
this.listContext = listContext;
this.typeTag = 'HashMap';
this.builder = () => {
return new HashMapBlockBuilder(this);
};
this.blockCapacity = 1 << blockSizeBits;
this.blockMask = this.blockCapacity - 1;
this.maxDepth = Math.ceil(32 / blockSizeBits);
this._empty = Object.freeze(new HashMapEmpty(this));
this._emptyBlock = Object.freeze(new HashMapBlock(this, null, null, 0, 0));
}
hash(value) {
return this.hasher.hash(value);
}
getKeyIndex(level, hash) {
const shift = this.blockSizeBits * level;
return (hash >>> shift) & this.blockMask;
}
isNonEmptyInstance(source) {
return source instanceof HashMapNonEmptyBase;
}
createBuilder(source) {
return new HashMapBlockBuilder(this, source);
}
isValidKey(key) {
return this.hasher.isValid(key);
}
emptyBlock() {
return this._emptyBlock;
}
block(entries, entrySets, size, level) {
return new HashMapBlock(this, entries, entrySets, size, level);
}
collision(entries) {
return new HashMapCollision(this, entries);
}
isHashMapBlock(obj) {
return obj instanceof HashMapBlock;
}
isHashMapBlockBuilder(obj) {
return obj instanceof HashMapBlockBuilder;
}
}
export function createHashMapContext(options) {
return Object.freeze(new HashMapContext(options?.hasher ?? Hasher.defaultHasher(), options?.eq ?? Eq.defaultEq(), options?.blockSizeBits ?? 5, options?.listContext ?? List.defaultContext()));
}
//# sourceMappingURL=context.mjs.map