//================================================================ /** * @packageDocumentation * @module std */ //================================================================ import { UniqueTreeSet } from "../internal/container/associative/UniqueTreeSet"; import { ITreeContainer } from "../internal/container/associative/ITreeContainer"; import { IForwardIterator } from "../iterator/IForwardIterator"; import { SetElementList } from "../internal/container/associative/SetElementList"; import { UniqueSetTree } from "../internal/tree/UniqueSetTree"; import { Comparator } from "../internal/functional/Comparator"; import { Temporary } from "../internal/functional/Temporary"; /** * Unique-key Set based on Tree. * * @author Jeongho Nam - https://github.com/samchon */ export class TreeSet extends UniqueTreeSet< Key, TreeSet, TreeSet.Iterator, TreeSet.ReverseIterator > { private tree_!: UniqueSetTree>; /* --------------------------------------------------------- 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: Key[], comp?: Comparator); /** * Copy Constructor. * * @param obj Object to copy. */ public constructor(container: TreeSet); /** * 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[]) { super((thisArg) => new SetElementList(thisArg) as Temporary); ITreeContainer.construct< Key, Key, TreeSet, TreeSet.Iterator, TreeSet.ReverseIterator, Key >( this, TreeSet, (comp) => { this.tree_ = new UniqueSetTree(this as TreeSet, comp); }, ...args, ); } /** * @inheritDoc */ public clear(): void { super.clear(); this.tree_.clear(); } /** * @inheritDoc */ public swap(obj: TreeSet): void { // SWAP CONTENTS [this.data_, obj.data_] = [obj.data_, this.data_]; SetElementList._Swap_associative( this.data_ as Temporary, obj.data_ as Temporary, ); // SWAP RB-TREE UniqueSetTree._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): TreeSet.Iterator { return this.tree_.lower_bound(key); } /** * @inheritDoc */ public upper_bound(key: Key): TreeSet.Iterator { return this.tree_.upper_bound(key); } /* --------------------------------------------------------- POST-PROCESS --------------------------------------------------------- */ protected _Handle_insert( first: TreeSet.Iterator, last: TreeSet.Iterator, ): void { for (; !first.equals(last); first = first.next()) this.tree_.insert(first); } protected _Handle_erase( first: TreeSet.Iterator, last: TreeSet.Iterator, ): void { for (; !first.equals(last); first = first.next()) this.tree_.erase(first); } } /** * */ export namespace TreeSet { // HEAD /** * Iterator of {@link TreeSet} */ export type Iterator = SetElementList.Iterator< Key, true, TreeSet >; /** * Reverse iterator of {@link TreeSet} */ export type ReverseIterator = SetElementList.ReverseIterator< Key, true, TreeSet >; // BODY export const Iterator = SetElementList.Iterator; export const ReverseIterator = SetElementList.ReverseIterator; }