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