//================================================================ /** * @packageDocumentation * @module std.internal */ //================================================================ import { HashBuckets } from "./HashBuckets"; import { IHashMap } from "../../base/container/IHashMap"; import { Comparator } from "../functional/Comparator"; import { Hasher } from "../functional/Hasher"; /** * Hash buckets for map containers. * * @author Jeongho Nam - https://github.com/samchon */ export class MapHashBuckets< Key, T, Unique extends boolean, Source extends IHashMap, > extends HashBuckets> { private source_: IHashMap; private readonly key_eq_: Comparator; /* --------------------------------------------------------- CONSTRUCTORS --------------------------------------------------------- */ /** * Initializer Constructor * * @param source Source map container * @param hasher Hash function * @param pred Equality function */ public constructor( source: Source, hasher: Hasher, pred: Comparator, ) { super(fetcher, hasher); this.source_ = source; this.key_eq_ = pred; } /** * @internal */ public static _Swap_source< Key, T, Unique extends boolean, Source extends IHashMap, >( x: MapHashBuckets, y: MapHashBuckets, ): void { [x.source_, y.source_] = [y.source_, x.source_]; } /* --------------------------------------------------------- FINDERS --------------------------------------------------------- */ public key_eq(): Comparator { return this.key_eq_; } public find(key: Key): IHashMap.Iterator { const index: number = this.hash_function()(key) % this.length(); const bucket: IHashMap.Iterator[] = this.at(index); for (const it of bucket) if (this.key_eq_(it.first, key)) return it; return this.source_.end(); } } function fetcher< Key, T, Unique extends boolean, Source extends IHashMap, >(elem: IHashMap.Iterator): Key { return elem.first; }