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>, duplicatePolicy: boolean | DuplicatePolicyfalse, 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
_clear(): void Protected Inherited ReadonlySortedArray<T> Clears the contents of the sorted array.
_extractArray(): T[] Protected Inherited 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 Inherited ReadonlySortedArray<T> Attempts to insert a new value into the array at a position determined by the ordering.
_remove(value: T): number Protected Inherited ReadonlySortedArray<T> Removes the first occurrence of a value comparing equal to the specified value from the array.
[iterator](): Iterator<T, any, any> Inherited ReadonlySortedArray<T> Returns an iterator over the contents of the array in sorted order, suitable for use in for-of loops.
contains(value: T): boolean Inherited ReadonlySortedArray<T> Returns true if this array contains at least one value comparing equal to the specified value.
findEqual(value: T): undefined | T Inherited ReadonlySortedArray<T> Looks up an element comparing equal to the specified value using binary search.
findEquivalent(criterion: (element: T) => number): undefined | T Inherited ReadonlySortedArray<T> Find an element that compares as equivalent based on some criterion.
forEach(func: (value: T) => void): void Inherited ReadonlySortedArray<T> Apply a function to each element in the array, in sorted order.
get(index: number): undefined | T Inherited ReadonlySortedArray<T> Looks up an element by its index in the array.
indexOf(value: T): number Inherited ReadonlySortedArray<T> Looks up the index of an element comparing equal to the specified value using binary search.
indexOfEquivalent(criterion: (element: T) => number): number Inherited ReadonlySortedArray<T> Find the index of an element that compares as equivalent based on some criterion.
lowerBound(value: T): { equal: boolean, index: number } Protected Inherited 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 Inherited T[] ReadonlySortedArray<T>  
_clone Protected Readonly Inherited CloneFunction<T> ReadonlySortedArray<T>  
_compare Protected Readonly Inherited OrderedComparator<T> ReadonlySortedArray<T>  
_duplicatePolicy Protected Readonly Inherited DuplicatePolicy ReadonlySortedArray<T>  
isEmpty Accessor Inherited ReadOnly boolean ReadonlySortedArray<T> Returns true if the array contains no elements.
length Accessor Inherited ReadOnly number ReadonlySortedArray<T> The number of elements in the array

Defined in

Last Updated: 23 November, 2024