SortedArray<T> Class

Maintains an array of some type T in sorted order. The ordering is specified by a function supplied by the user. By default, only unique elements are permitted; attempting to insert a new element that compares as equal to an element already in the array will not modify the contents of the array.

This allows a SortedArray to behave like a Set where T is an object and equality is determined by some criterion other than object identity.

Because the array is always sorted, querying for the presence of an element is performed using binary search, which is more efficient than a linear search for reasonably large arrays.

The user can also specify how the SortedArray takes ownership of inserted values, e.g., by cloning them.

The comparison function must meet the following criteria, given 'lhs' and 'rhs' of type T:

  • If lhs is equal to rhs, returns 0
  • If lhs is less than rhs, returns a negative value
  • If lhs is greater than rhs, returns a positive value
  • If compare(lhs, rhs) returns 0, then compare(rhs, lhs) must also return 0
  • If compare(lhs, rhs) returns a negative value, then compare(rhs, lhs) must return a positive value, and vice versa.

Modifying an element in a way that affects the comparison function will produce unpredictable results, the most likely of which is that the array will cease to be sorted.

Extends

Extended by

Methods

Name Description
constructor<T>(compare: OrderedComparator<T, T>, duplicatePolicy: boolean | DuplicatePolicy = false, clone: CloneFunction<T> = shallowClone): SortedArray<T> Construct a new SortedArray.  
clear(): void Clears the contents of the sorted array.  
extractArray(): T[] Extracts the sorted array as a T[] and empties the contents of this SortedArray.  
insert(value: T, onInsert?: (value: T) => any): number Attempts to insert a new value into the array at a position determined by the ordering.  
remove(value: T): number Removes the first occurrence of a value comparing equal to the specified value from the array.  
slice(start?: number, end?: number): SortedArray<T> The equivalent of Array.slice.  

Inherited methods

Name Inherited from Description
[iterator](): Iterator<T, any, undefined> ReadonlySortedArray<T> Returns an iterator over the contents of the array in sorted order, suitable for use in for-of loops.
_clear(): void Protected ReadonlySortedArray<T> Clears the contents of the sorted array.
_extractArray(): T[] Protected ReadonlySortedArray<T> Extracts the sorted array as a T[] and empties the contents of this ReadonlySortedArray.
_insert(value: T, onInsert?: (value: T) => any): number Protected ReadonlySortedArray<T> Attempts to insert a new value into the array at a position determined by the ordering.
_remove(value: T): number Protected ReadonlySortedArray<T> Removes the first occurrence of a value comparing equal to the specified value from the array.
contains(value: T): boolean ReadonlySortedArray<T> Returns true if this array contains at least one value comparing equal to the specified value.
findEqual(value: T): undefined | T ReadonlySortedArray<T> Looks up an element comparing equal to the specified value using binary search.
findEquivalent(criterion: (element: T) => number): undefined | T ReadonlySortedArray<T> Find an element that compares as equivalent based on some criterion.
forEach(func: (value: T) => void): void ReadonlySortedArray<T> Apply a function to each element in the array, in sorted order.
get(index: number): undefined | T ReadonlySortedArray<T> Looks up an element by its index in the array.
indexOf(value: T): number ReadonlySortedArray<T> Looks up the index of an element comparing equal to the specified value using binary search.
indexOfEquivalent(criterion: (element: T) => number): number ReadonlySortedArray<T> Find the index of an element that compares as equivalent based on some criterion.
lowerBound(value: T): { equal: boolean, index: number } Protected ReadonlySortedArray<T> Computes the position at which the specified value should be inserted to maintain sorted order.

Inherited properties

Name Type Inherited from Description
_array Protected T[] ReadonlySortedArray<T>  
_clone ProtectedReadonly CloneFunction<T> ReadonlySortedArray<T>  
_compare ProtectedReadonly OrderedComparator<T, T> ReadonlySortedArray<T>  
_duplicatePolicy ProtectedReadonly DuplicatePolicy ReadonlySortedArray<T>  
isEmpty Accessor ReadOnly boolean ReadonlySortedArray<T> Returns true if the array contains no elements.
length Accessor ReadOnly number ReadonlySortedArray<T> The number of elements in the array

Defined in

Last Updated: 20 June, 2023