packageDocumentation module import { OrderedComparator } from "./Compare" ; import { CloneFunction , lowerBound , shallowClone } from "./SortedArray" ; public export class IndexedValue < T > { public readonly value : T ; public readonly index : number ; public constructor ( value , index ) { this . value = value ; this . index = index ; } } public export class IndexMap < T > { protected _array : Array < IndexedValue < T > > = [ ] ; protected readonly _compareValues : OrderedComparator < T > ; protected readonly _clone : CloneFunction < T > ; protected readonly _maximumSize : number ; param compare param maximumSize param clone public constructor ( compare , maximumSize = Number . MAX_SAFE_INTEGER , clone = shallowClone ) { this . _compareValues = compare ; this . _clone = clone ; this . _maximumSize = maximumSize ; } /** The number of elements in the map. */ public get length ( ) : number { return this . _array . length ; } /** Returns true if the maximum number of elements have been inserted. */ public get isFull ( ) : boolean { return this . length >= this . _maximumSize ; } /** Returns true if the map contains no elements. */ public get isEmpty ( ) : boolean { return 0 === this . length ; } /** Removes all elements from the map. */ public clear ( ) { this . _array = [ ] ; } param value param onInsert returns public insert ( value , onInsert ? ) { const bound = this . lowerBound ( value ) ; if ( bound . equal ) return this . _array [ bound . index ] . index ; else if ( this . isFull ) return - 1 ; const entry = new IndexedValue < T > ( this . _clone ( value ) , this . _array . length ) ; if ( undefined !== onInsert ) onInsert ( entry . value ) ; this . _array . splice ( bound . index , 0 , entry ) ; return entry . index ; } param value returns public indexOf ( value ) { const bound = this . lowerBound ( value ) ; return bound . equal ? this . _array [ bound . index ] . index : - 1 ; } protected lowerBound ( value ) { return lowerBound ( value , this . _array , ( lhs , rhs ) => this . _compareValues ( lhs , rhs . value ) ) ; } /** Return an array of the elements in this map in which the array index of each element corresponds to the index assigned to it by the map. */ public toArray ( ) { const array = [ ] ; for ( const entry of this . _array ) array [ entry . index ] = entry . value ; return array ; } }