//================================================================ /** * @packageDocumentation * @module std.internal */ //================================================================ import { ListContainer } from "../linear/ListContainer"; import { ListIterator } from "../../iterator/ListIterator"; import { ReverseIterator as _ReverseIterator } from "../../iterator/ReverseIterator"; import { MapContainer } from "../../../base/container/MapContainer"; import { IPair } from "../../../utility/IPair"; import { Entry } from "../../../utility/Entry"; /** * Doubly Linked List storing map elements. * * @template Key Key type * @template T Mapped type * @template Unique Whether duplicated key is blocked or not * @template Source Source type * * @author Jeongho Nam - https://github.com/samchon */ export class MapElementList< Key, T, Unique extends boolean, Source extends MapContainer< Key, T, Unique, Source, MapElementList.Iterator, MapElementList.ReverseIterator >, > extends ListContainer< Entry, Source, MapElementList.Iterator, MapElementList.ReverseIterator > { private associative_: Source; /* --------------------------------------------------------- CONSTRUCTORS --------------------------------------------------------- */ public constructor(associative: Source) { super(); this.associative_ = associative; } protected _Create_iterator( prev: MapElementList.Iterator, next: MapElementList.Iterator, val: Entry, ): MapElementList.Iterator { return MapElementList.Iterator.create(this, prev, next, val); } /** * @internal */ public static _Swap_associative< Key, T, Unique extends boolean, Source extends MapContainer< Key, T, Unique, Source, MapElementList.Iterator, MapElementList.ReverseIterator >, >( x: MapElementList, y: MapElementList, ): void { [x.associative_, y.associative_] = [y.associative_, x.associative_]; } /* --------------------------------------------------------- ACCESSORS --------------------------------------------------------- */ public associative(): Source { return this.associative_; } } /** * */ export namespace MapElementList { /** * Iterator of map container storing elements in a list. * * @template Key Key type * @template T Mapped type * @template Unique Whether duplicated key is blocked or not * @template Source Source container type * * @author Jeongho Nam - https://github.com/samchon */ export class Iterator< Key, T, Unique extends boolean, Source extends MapContainer< Key, T, Unique, Source, Iterator, ReverseIterator >, > extends ListIterator< Entry, Source, Iterator, ReverseIterator, IPair > implements MapContainer.Iterator< Key, T, Unique, Source, Iterator, ReverseIterator > { private list_: MapElementList; /* --------------------------------------------------------- CONSTRUCTORS --------------------------------------------------------- */ private constructor( list: MapElementList, prev: Iterator, next: Iterator, val: Entry, ) { super(prev, next, val); this.list_ = list; } /** * @internal */ public static create< Key, T, Unique extends boolean, Source extends MapContainer< Key, T, Unique, Source, Iterator, ReverseIterator >, >( list: MapElementList, prev: Iterator, next: Iterator, val: Entry, ) { return new Iterator(list, prev, next, val); } /** * @inheritDoc */ public reverse(): ReverseIterator { return new ReverseIterator(this); } /* --------------------------------------------------------- ACCESSORS --------------------------------------------------------- */ /** * @inheritDoc */ public source(): Source { return this.list_.associative(); } /** * @inheritDoc */ public get first(): Key { return this.value.first; } /** * @inheritDoc */ public get second(): T { return this.value.second; } /** * @inheritDoc */ public set second(val: T) { this.value.second = val; } } /** * Reverse iterator of map container storing elements a list. * * @template Key Key type * @template T Mapped type * @template Unique Whether duplicated key is blocked or not * @template Source Source container type * * @author Jeongho Nam - https://github.com/samchon */ export class ReverseIterator< Key, T, Unique extends boolean, Source extends MapContainer< Key, T, Unique, Source, Iterator, ReverseIterator >, > extends _ReverseIterator< Entry, Source, Iterator, ReverseIterator, IPair > implements MapContainer.ReverseIterator< Key, T, Unique, Source, Iterator, ReverseIterator > { /* --------------------------------------------------------- CONSTRUCTORS --------------------------------------------------------- */ protected _Create_neighbor( base: Iterator, ): ReverseIterator { return new ReverseIterator(base); } /* --------------------------------------------------------- ACCESSORS --------------------------------------------------------- */ /** * Get the first, key element. * * @return The first element. */ public get first(): Key { return this.base_.first; } /** * Get the second, stored element. * * @return The second element. */ public get second(): T { return this.base_.second; } /** * Set the second, stored element. * * @param val The value to set. */ public set second(val: T) { this.base_.second = val; } } }