packageDocumentation module import { OrderedComparator } from "./Compare" ; import { CloneFunction , lowerBound , shallowClone } from "./SortedArray" ; class DictionaryIterator < K , V > implements Iterator < DictionaryEntry < K , V > > { private _keys : K [ ] ; private _values : V [ ] ; private _curIndex = - 1 ; public constructor ( keys , values ) { this . _keys = keys ; this . _values = values ; } public next ( ) { if ( ++ this . _curIndex >= this . _keys . length ) { return { done : true } as any as IteratorResult < DictionaryEntry < K , V > > ; } else { return { value : { key : this . _keys [ this . _curIndex ] , value : this . _values [ this . _curIndex ] } , done : false } ; } } } public export interface DictionaryEntry < K , V > { /** The key used for lookup in the Dictionary. */ key : K ; /** The value associated with the key in the Dictionary. */ value : V ; } public export class Dictionary < K , V > implements Iterable < DictionaryEntry < K , V > > { protected _keys : K [ ] = [ ] ; protected readonly _compareKeys : OrderedComparator < K > ; protected readonly _cloneKey : CloneFunction < K > ; protected _values : V [ ] = [ ] ; protected readonly _cloneValue : CloneFunction < V > ; param compareKeys param cloneKey param cloneValue public constructor ( compareKeys , cloneKey = shallowClone , cloneValue = shallowClone ) { this . _compareKeys = compareKeys ; this . _cloneKey = cloneKey ; this . _cloneValue = cloneValue ; } /** The number of entries in the dictionary. */ public get size ( ) : number { return this . _keys . length ; } /** Returns an iterator over the key-value pairs in the Dictionary suitable for use in `for-of` loops. Entries are returned in sorted order by key. */ public [ Symbol . iterator ] ( ) { return new DictionaryIterator < K , V > ( this . _keys , this . _values ) ; } /** Provides iteration over the keys in this Dictionary, in sorted order. */ public keys ( ) { function * iterator ( dict ) { for ( const entry of dict ) yield entry . key ; } return { [ Symbol . iterator ] : ( ) => iterator ( this ) } ; } /** Provides iteration over the values in this Dictionary, in sorted order by the corresponding keys. */ public values ( ) { function * iterator ( dict ) { for ( const entry of dict ) yield entry . value ; } return { [ Symbol . iterator ] : ( ) => iterator ( this ) } ; } /** Removes all entries from this dictionary */ public clear ( ) { this . _keys = [ ] ; this . _values = [ ] ; } param key returns public get ( key ) { const bound = this . lowerBound ( key ) ; return bound . equal ? this . _values [ bound . index ] : undefined ; } param key returns public has ( key ) { return this . lowerBound ( key ) . equal ; } param key returns public delete ( key ) { const bound = this . lowerBound ( key ) ; if ( bound . equal ) { this . _values . splice ( bound . index , 1 ) ; this . _keys . splice ( bound . index , 1 ) ; return true ; } else { return false ; } } param key param value returns public insert ( key , value ) { const result = this . findOrInsert ( key , value ) ; return result . inserted ; } param key param value returns public findOrInsert ( key , value ) { const bound = this . lowerBound ( key ) ; if ( bound . equal ) return { value : this . _values [ bound . index ] , inserted : false } ; value = this . _cloneValue ( value ) ; this . _keys . splice ( bound . index , 0 , this . _cloneKey ( key ) ) ; this . _values . splice ( bound . index , 0 , this . _cloneValue ( value ) ) ; return { value , inserted : true } ; } /**
     * Sets the value associated with the specified key in the dictionary.
     * If no such key already exists, this is equivalent to insert(key, value); otherwise, the existing value associated with the key is replaced.
     * In either case, the value will be cloned using the function supplied to the dictionary's constructor.
     */ public set ( key , value ) { value = this . _cloneValue ( value ) ; const bound = this . lowerBound ( key ) ; if ( bound . equal ) { this . _values [ bound . index ] = value ; } else { this . _keys . splice ( bound . index , 0 , this . _cloneKey ( key ) ) ; this . _values . splice ( bound . index , 0 , value ) ; } } returns public extractPairs ( ) { const pairs = [ ] ; for ( let i = 0 ; i < this . size ; i ++ ) pairs . push ( { key : this . _keys [ i ] , value : this . _values [ i ] } ) ; this . clear ( ) ; return pairs ; } returns public extractArrays ( ) { const result = { keys : this . _keys , values : this . _values } ; this . clear ( ) ; return result ; } param func public forEach ( func ) { for ( let i = 0 ; i < this . size ; i ++ ) func ( this . _keys [ i ] , this . _values [ i ] ) ; } param key returns protected lowerBound ( key ) { return lowerBound ( key , this . _keys , this . _compareKeys ) ; } }