//================================================================ /** * @packageDocumentation * @module std.internal */ //================================================================ import { XTree } from "./XTree"; import { ITreeSet } from "../../base/container/ITreeSet"; import { SetElementList } from "../container/associative/SetElementList"; import { XTreeNode } from "./XTreeNode"; import { Pair } from "../../utility/Pair"; import { Comparator } from "../functional/Comparator"; export abstract class SetTree< Key, Unique extends boolean, Source extends ITreeSet< Key, Unique, Source, SetElementList.Iterator, SetElementList.ReverseIterator >, > extends XTree> { private source_: Source; private key_comp_: Comparator; private key_eq_: Comparator; /* --------------------------------------------------------- CONSTRUCTOR --------------------------------------------------------- */ public constructor( set: Source, comp: Comparator, it_comp: Comparator>, ) { super(it_comp); this.source_ = set; this.key_comp_ = comp; this.key_eq_ = (x, y) => !comp(x, y) && !comp(y, x); } /** * @internal */ public static _Swap_source< Key, Unique extends boolean, Source extends ITreeSet< Key, Unique, Source, SetElementList.Iterator, SetElementList.ReverseIterator >, >(x: SetTree, y: SetTree): void { [x.source_, y.source_] = [y.source_, x.source_]; } /* --------------------------------------------------------- FINDERS --------------------------------------------------------- */ public get_by_key( val: Key, ): XTreeNode> | null { const ret = this.nearest_by_key(val); if (ret === null || !this.key_eq_(val, ret.value.value)) return null; else return ret; } public abstract nearest_by_key( val: Key, ): XTreeNode> | null; public lower_bound(val: Key): SetElementList.Iterator { const node: XTreeNode< SetElementList.Iterator > | null = this.nearest_by_key(val); if (node === null) return this.source_.end(); else if (this.key_comp_(node.value.value, val)) // it < key return node.value.next(); else return node.value; } public abstract upper_bound( val: Key, ): SetElementList.Iterator; public equal_range( val: Key, ): Pair< SetElementList.Iterator, SetElementList.Iterator > { return new Pair(this.lower_bound(val), this.upper_bound(val)); } /* --------------------------------------------------------- ACCESSORS --------------------------------------------------------- */ public source(): Source { return this.source_; } public key_comp(): Comparator { return this.key_comp_; } public key_eq(): Comparator { return this.key_eq_; } public value_comp(): Comparator { return this.key_comp_; } }