UNPKG

@rimbu/hashed

Version:

Immutable HashMap and HashSet implementations for TypeScript

65 lines 2.05 kB
import { TraverseState } from '@rimbu/common'; import { List } from '@rimbu/list'; /** * Base class for mutable builders that construct hash blocks for hashed collections. * @typeparam E - the entry type stored in the block */ export class BlockBuilderBase { get isEmpty() { return this.size === 0; } forEach(f, options = {}) { const { state = TraverseState() } = options; if (this.isEmpty || state.halted) return; if (undefined !== this.source) return this.source.forEach(f, { state }); const { halt } = state; if (undefined !== this._entries) { for (const key in this._entries) { f(this._entries[key], state.nextIndex(), halt); if (state.halted) break; } } if (undefined !== this._entrySets) { for (const key in this._entrySets) { this._entrySets[key].forEach(f, { state }); if (state.halted) break; } } } } /** * Base class for mutable builders that handle hash collisions in hashed collections. * @typeparam E - the entry type contained in the collision bucket */ export class CollisionBuilderBase { get size() { if (undefined !== this.source) return this.source.size; return this.entries.length; } get entries() { if (undefined === this._entries) { if (undefined !== this.source) { this._entries = this.source.entries.toBuilder(); } else { this._entries = List.builder(); } } return this._entries; } forEach(f, options = {}) { const { state = TraverseState() } = options; if (state.halted) return; if (undefined !== this.source) { return this.source.forEach(f, { state }); } this.entries.forEach(f, { state }); } } //# sourceMappingURL=hashed-custom.mjs.map