@rimbu/hashed
Version:
Immutable HashMap and HashSet implementations for TypeScript
61 lines • 2.27 kB
JavaScript
import { RSetBase } from '@rimbu/collection-types/set-custom';
import { Eq } from '@rimbu/common';
import { List } from '@rimbu/list';
import { Hasher } from '../../common/index.mjs';
import { HashSetNonEmptyBase, HashSetEmpty, HashSetBlock, HashSetCollision, HashSetBlockBuilder, } from '@rimbu/hashed/set-custom';
export class HashSetContext extends RSetBase.ContextBase {
constructor(hasher, eq, blockSizeBits, listContext) {
super();
this.hasher = hasher;
this.eq = eq;
this.blockSizeBits = blockSizeBits;
this.listContext = listContext;
this.typeTag = 'HashSet';
this.builder = () => {
return new HashSetBlockBuilder(this);
};
this.blockCapacity = 1 << blockSizeBits;
this.blockMask = this.blockCapacity - 1;
this.maxDepth = Math.ceil(32 / blockSizeBits);
this._empty = Object.freeze(new HashSetEmpty(this));
this._emptyBlock = Object.freeze(new HashSetBlock(this, null, null, 0, 0));
}
isNonEmptyInstance(source) {
return source instanceof HashSetNonEmptyBase;
}
hash(value) {
return this.hasher.hash(value);
}
getKeyIndex(level, hash) {
const shift = this.blockSizeBits * level;
return (hash >>> shift) & this.blockMask;
}
emptyBlock() {
return this._emptyBlock;
}
isValidValue(value) {
return this.hasher.isValid(value);
}
createBuilder(source) {
return new HashSetBlockBuilder(this, source);
}
block(entries, entrySets, size, level) {
return new HashSetBlock(this, entries, entrySets, size, level);
}
collision(entries) {
return new HashSetCollision(this, entries);
}
isHashSetBlock(obj) {
return obj instanceof HashSetBlock;
}
isHashSetCollision(obj) {
return obj instanceof HashSetCollision;
}
isHashSetBlockBuilder(obj) {
return obj instanceof HashSetBlockBuilder;
}
}
export function createHashSetContext(options) {
return Object.freeze(new HashSetContext(options?.hasher ?? Hasher.defaultHasher(), options?.eq ?? Eq.defaultEq(), options?.blockSizeBits ?? 5, options?.listContext ?? List.defaultContext()));
}
//# sourceMappingURL=context.mjs.map