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