//================================================================ /** * @packageDocumentation * @module std */ //================================================================ import { UniqueTreeMap } from "../internal/container/associative/UniqueTreeMap"; import { ITreeContainer } from "../internal/container/associative/ITreeContainer"; import { MapElementList } from "../internal/container/associative/MapElementList"; import { UniqueMapTree } from "../internal/tree/UniqueMapTree"; import { Comparator } from "../internal/functional/Comparator"; import { IForwardIterator } from "../iterator/IForwardIterator"; import { IPair } from "../utility/IPair"; import { Entry } from "../utility/Entry"; import { Temporary } from "../internal/functional/Temporary"; /** * Unique-key Map based on Tree. * * @author Jeongho Nam - https://github.com/samchon */ export class TreeMap extends UniqueTreeMap< Key, T, TreeMap, TreeMap.Iterator, TreeMap.ReverseIterator > { private tree_!: UniqueMapTree>; /* --------------------------------------------------------- CONSTURCTORS --------------------------------------------------------- */ /** * Default Constructor. * * @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Note that, because *equality* is predicated by `!comp(x, y) && !comp(y, x)`, the function must not cover the *equality* like `<=` or `>=`. It must exclude the *equality* like `<` or `>`. Default is {@link less}. */ public constructor(comp?: Comparator); /** * Initializer Constructor. * * @param items Items to assign. * @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Note that, because *equality* is predicated by `!comp(x, y) && !comp(y, x)`, the function must not cover the *equality* like `<=` or `>=`. It must exclude the *equality* like `<` or `>`. Default is {@link less}. */ public constructor(items: IPair[], comp?: Comparator); /** * Copy Constructor. * * @param obj Object to copy. */ public constructor(obj: TreeMap); /** * Range Constructor. * * @param first Input iterator of the first position. * @param last Input iterator of the last position. * @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Note that, because *equality* is predicated by `!comp(x, y) && !comp(y, x)`, the function must not cover the *equality* like `<=` or `>=`. It must exclude the *equality* like `<` or `>`. Default is {@link less}. */ public constructor( first: Readonly>>, last: Readonly>>, comp?: Comparator, ); public constructor(...args: any[]) { // INITIALIZATION super((thisArg) => new MapElementList(thisArg) as Temporary); // OVERLOADINGS ITreeContainer.construct< Key, Entry, TreeMap, TreeMap.Iterator, TreeMap.ReverseIterator, IPair >( this, TreeMap, (comp) => { this.tree_ = new UniqueMapTree(this as TreeMap, comp); }, ...args, ); } /** * @inheritDoc */ public clear(): void { super.clear(); this.tree_.clear(); } /** * @inheritDoc */ public swap(obj: TreeMap): void { // SWAP CONTENTS [this.data_, obj.data_] = [obj.data_, this.data_]; MapElementList._Swap_associative( this.data_ as Temporary, obj.data_ as Temporary, ); // SWAP RB-TREE UniqueMapTree._Swap_source(this.tree_, obj.tree_); [this.tree_, obj.tree_] = [obj.tree_, this.tree_]; } /* --------------------------------------------------------- ACCESSORS --------------------------------------------------------- */ /** * @inheritDoc */ public key_comp(): Comparator { return this.tree_.key_comp(); } /** * @inheritDoc */ public lower_bound(key: Key): TreeMap.Iterator { return this.tree_.lower_bound(key); } /** * @inheritDoc */ public upper_bound(key: Key): TreeMap.Iterator { return this.tree_.upper_bound(key); } /* --------------------------------------------------------- POST-PROCESS --------------------------------------------------------- */ protected _Handle_insert( first: TreeMap.Iterator, last: TreeMap.Iterator, ): void { for (; !first.equals(last); first = first.next()) this.tree_.insert(first); } protected _Handle_erase( first: TreeMap.Iterator, last: TreeMap.Iterator, ): void { for (; !first.equals(last); first = first.next()) this.tree_.erase(first); } } /** * */ export namespace TreeMap { // HEAD /** * Iterator of {@link TreeMap} */ export type Iterator = MapElementList.Iterator< Key, T, true, TreeMap >; /** * Reverse iterator of {@link TreeMap} */ export type ReverseIterator = MapElementList.ReverseIterator< Key, T, true, TreeMap >; // BODY export const Iterator = MapElementList.Iterator; export const ReverseIterator = MapElementList.ReverseIterator; }