Options
All
  • Public
  • Public/Protected
  • All
Menu

The namespace for array-specific algorithms.

Index

Type aliases

MutableArrayLike

MutableArrayLike<T>: { length: number }

An array-like object which supports item assignment.

Type parameters

  • T

Type declaration

  • [index: number]: T
  • length: number

Functions

fill

  • fill<T>(array: MutableArrayLike<T>, value: T, start?: number, stop?: number): void
  • Fill an array with a static value.

    Type parameters

    • T

    Parameters

    • array: MutableArrayLike<T>

      The mutable array-like object to fill.

    • value: T

      The static value to use to fill the array.

    • Default value start: number = 0

      The index of the first element in the range to be filled, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be filled, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

      Notes

      If stop < start the fill will wrap at the end of the array.

      Complexity

      Linear.

      Undefined Behavior

      A start or stop which is non-integral.

      Example

      import { ArrayExt } from '@phosphor/algorithm';
      
      let data = ['one', 'two', 'three', 'four'];
      ArrayExt.fill(data, 'r');        // ['r', 'r', 'r', 'r']
      ArrayExt.fill(data, 'g', 1);     // ['r', 'g', 'g', 'g']
      ArrayExt.fill(data, 'b', 2, 3);  // ['r', 'g', 'b', 'b']
      ArrayExt.fill(data, 'z', 3, 1);  // ['z', 'z', 'b', 'z']

    Returns void

findFirstIndex

  • findFirstIndex<T>(array: ArrayLike<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): number
  • Find the index of the first value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>

      The array-like object to search.

    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the first matching value, or -1 if no matching value is found.

    Notes

    If stop < start the search will wrap at the end of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [1, 2, 3, 4, 3, 2, 1];
    ArrayExt.findFirstIndex(data, isEven);       // 1
    ArrayExt.findFirstIndex(data, isEven, 4);    // 5
    ArrayExt.findFirstIndex(data, isEven, 6);    // -1
    ArrayExt.findFirstIndex(data, isEven, 6, 5); // 1

findFirstValue

  • findFirstValue<T>(array: ArrayLike<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): T | undefined
  • Find the first value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>

      The array-like object to search.

    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns T | undefined

    The first matching value, or undefined if no matching value is found.

    Notes

    If stop < start the search will wrap at the end of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [1, 2, 3, 4, 3, 2, 1];
    ArrayExt.findFirstValue(data, isEven);       // 2
    ArrayExt.findFirstValue(data, isEven, 2);    // 4
    ArrayExt.findFirstValue(data, isEven, 6);    // undefined
    ArrayExt.findFirstValue(data, isEven, 6, 5); // 2

findLastIndex

  • findLastIndex<T>(array: ArrayLike<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): number
  • Find the index of the last value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>
    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = -1

      The index of the first element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = 0

      The index of the last element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the last matching value, or -1 if no matching value is found.

    Notes

    If start < stop the search will wrap at the front of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [1, 2, 3, 4, 3, 2, 1];
    ArrayExt.findLastIndex(data, isEven);        // 5
    ArrayExt.findLastIndex(data, isEven, 4);     // 3
    ArrayExt.findLastIndex(data, isEven, 0);     // -1
    ArrayExt.findLastIndex(data, isEven, 0, 1);  // 5

findLastValue

  • findLastValue<T>(array: ArrayLike<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): T | undefined
  • Find the last value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>
    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = -1

      The index of the first element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = 0

      The index of the last element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    Returns T | undefined

    The last matching value, or undefined if no matching value is found.

    Notes

    If start < stop the search will wrap at the front of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [1, 2, 3, 4, 3, 2, 1];
    ArrayExt.findLastValue(data, isEven);        // 2
    ArrayExt.findLastValue(data, isEven, 4);     // 4
    ArrayExt.findLastValue(data, isEven, 0);     // undefined
    ArrayExt.findLastValue(data, isEven, 0, 1);  // 2

firstIndexOf

  • firstIndexOf<T>(array: ArrayLike<T>, value: T, start?: number, stop?: number): number
  • Find the index of the first occurrence of a value in an array.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>

      The array-like object to search.

    • value: T

      The value to locate in the array. Values are compared using strict === equality.

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the first occurrence of the value, or -1 if the value is not found.

    Notes

    If stop < start the search will wrap at the end of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = ['one', 'two', 'three', 'four', 'one'];
    ArrayExt.firstIndexOf(data, 'red');        // -1
    ArrayExt.firstIndexOf(data, 'one');        // 0
    ArrayExt.firstIndexOf(data, 'one', 1);     // 4
    ArrayExt.firstIndexOf(data, 'two', 2);     // -1
    ArrayExt.firstIndexOf(data, 'two', 2, 1);  // 1

insert

  • insert<T>(array: Array<T>, index: number, value: T): void
  • Insert a value into an array at a specific index.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • index: number

      The index at which to insert the value. Negative values are taken as an offset from the end of the array.

    • value: T

      The value to set at the specified index.

      Complexity

      Linear.

      Undefined Behavior

      An index which is non-integral.

      Example

      import { ArrayExt } from '@phosphor/algorithm';
      
      let data = [0, 1, 2];
      ArrayExt.insert(data, 0, -1);  // [-1, 0, 1, 2]
      ArrayExt.insert(data, 2, 12);  // [-1, 0, 12, 1, 2]
      ArrayExt.insert(data, -1, 7);  // [-1, 0, 12, 1, 7, 2]
      ArrayExt.insert(data, 6, 19);  // [-1, 0, 12, 1, 7, 2, 19]

    Returns void

lastIndexOf

  • lastIndexOf<T>(array: ArrayLike<T>, value: T, start?: number, stop?: number): number
  • Find the index of the last occurrence of a value in an array.

    Type parameters

    • T

    Parameters

    • array: ArrayLike<T>

      The array-like object to search.

    • value: T

      The value to locate in the array. Values are compared using strict === equality.

    • Default value start: number = -1

      The index of the first element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = 0

      The index of the last element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the last occurrence of the value, or -1 if the value is not found.

    Notes

    If start < stop the search will wrap at the front of the array.

    Complexity

    Linear.

    Undefined Behavior

    A start or stop which is non-integral.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = ['one', 'two', 'three', 'four', 'one'];
    ArrayExt.lastIndexOf(data, 'red');        // -1
    ArrayExt.lastIndexOf(data, 'one');        // 4
    ArrayExt.lastIndexOf(data, 'one', 1);     // 0
    ArrayExt.lastIndexOf(data, 'two', 0);     // -1
    ArrayExt.lastIndexOf(data, 'two', 0, 1);  // 1

lowerBound

  • lowerBound<T, U>(array: ArrayLike<T>, value: U, fn: (element: T, value: U) => number, start?: number, stop?: number): number
  • Find the index of the first element which compares >= to a value.

    Type parameters

    • T

    • U

    Parameters

    • array: ArrayLike<T>

      The sorted array-like object to search.

    • value: U

      The value to locate in the array.

    • fn: (element: T, value: U) => number

      The 3-way comparison function to apply to the values. It should return < 0 if an element is less than a value, 0 if an element is equal to a value, or > 0 if an element is greater than a value.

        • (element: T, value: U): number
        • Parameters

          • element: T
          • value: U

          Returns number

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the first element which compares >= to the value, or length if there is no such element. If the computed index for stop is less than start, then the computed index for start is returned.

    Notes

    The array must already be sorted in ascending order according to the comparison function.

    Complexity

    Logarithmic.

    Undefined Behavior

    Searching a range which is not sorted in ascending order.

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function numberCmp(a: number, b: number): number {
      return a - b;
    }
    
    let data = [0, 3, 4, 7, 7, 9];
    ArrayExt.lowerBound(data, 0, numberCmp);   // 0
    ArrayExt.lowerBound(data, 6, numberCmp);   // 3
    ArrayExt.lowerBound(data, 7, numberCmp);   // 3
    ArrayExt.lowerBound(data, -1, numberCmp);  // 0
    ArrayExt.lowerBound(data, 10, numberCmp);  // 6

move

  • Move an element in an array from one index to another.

    Type parameters

    • T

    Parameters

    • array: MutableArrayLike<T>

      The mutable array-like object of interest.

    • fromIndex: number

      The index of the element to move. Negative values are taken as an offset from the end of the array.

    • toIndex: number

      The target index of the element. Negative values are taken as an offset from the end of the array.

      Complexity

      Linear.

      Undefined Behavior

      A fromIndex or toIndex which is non-integral.

      Example

      import { ArrayExt } from from '@phosphor/algorithm';
      
      let data = [0, 1, 2, 3, 4];
      ArrayExt.move(data, 1, 2);  // [0, 2, 1, 3, 4]
      ArrayExt.move(data, 4, 2);  // [0, 2, 4, 1, 3]

    Returns void

removeAllOf

  • removeAllOf<T>(array: Array<T>, value: T, start?: number, stop?: number): number
  • Remove all occurrences of a value from an array.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • value: T

      The value to remove from the array. Values are compared using strict === equality.

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The number of elements removed from the array.

    Notes

    If stop < start the search will conceptually wrap at the end of the array, however the array will be traversed front-to-back.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = [14, 12, 23, 39, 14, 12, 19, 14];
    ArrayExt.removeAllOf(data, 12);        // 2
    ArrayExt.removeAllOf(data, 17);        // 0
    ArrayExt.removeAllOf(data, 14, 1, 4);  // 1

removeAllWhere

  • removeAllWhere<T>(array: Array<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): number
  • Remove all occurrences of values which match a predicate.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The number of elements removed from the array.

    Notes

    If stop < start the search will conceptually wrap at the end of the array, however the array will be traversed front-to-back.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    function isNegative(value: number): boolean {
      return value < 0;
    }
    
    let data = [0, 12, -13, -9, 23, 39, 14, -15, 12, 75];
    ArrayExt.removeAllWhere(data, isEven);            // 4
    ArrayExt.removeAllWhere(data, isNegative, 0, 3);  // 2

removeAt

  • removeAt<T>(array: Array<T>, index: number): T | undefined
  • Remove and return a value at a specific index in an array.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • index: number

      The index of the value to remove. Negative values are taken as an offset from the end of the array.

    Returns T | undefined

    The value at the specified index, or undefined if the index is out of range.

    Complexity

    Linear.

    Undefined Behavior

    An index which is non-integral.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = [0, 12, 23, 39, 14, 12, 75];
    ArrayExt.removeAt(data, 2);   // 23
    ArrayExt.removeAt(data, -2);  // 12
    ArrayExt.removeAt(data, 10);  // undefined;

removeFirstOf

  • removeFirstOf<T>(array: Array<T>, value: T, start?: number, stop?: number): number
  • Remove the first occurrence of a value from an array.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • value: T

      The value to remove from the array. Values are compared using strict === equality.

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the removed value, or -1 if the value is not contained in the array.

    Notes

    If stop < start the search will wrap at the end of the array.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = [0, 12, 23, 39, 14, 12, 75];
    ArrayExt.removeFirstOf(data, 12);        // 1
    ArrayExt.removeFirstOf(data, 17);        // -1
    ArrayExt.removeFirstOf(data, 39, 3);     // -1
    ArrayExt.removeFirstOf(data, 39, 3, 2);  // 2

removeFirstWhere

  • removeFirstWhere<T>(array: Array<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): { index: number; value: T | undefined }
  • Remove the first occurrence of a value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns { index: number; value: T | undefined }

    The removed { index, value }, which will be -1 and undefined if the value is not contained in the array.

    Notes

    If stop < start the search will wrap at the end of the array.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [0, 12, 23, 39, 14, 12, 75];
    ArrayExt.removeFirstWhere(data, isEven);     // { index: 0, value: 0 }
    ArrayExt.removeFirstWhere(data, isEven, 2);  // { index: 3, value: 14 }
    ArrayExt.removeFirstWhere(data, isEven, 4);  // { index: -1, value: undefined }
    • index: number
    • value: T | undefined

removeLastOf

  • removeLastOf<T>(array: Array<T>, value: T, start?: number, stop?: number): number
  • Remove the last occurrence of a value from an array.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • value: T

      The value to remove from the array. Values are compared using strict === equality.

    • Default value start: number = -1

      The index of the first element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = 0

      The index of the last element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the removed value, or -1 if the value is not contained in the array.

    Notes

    If start < stop the search will wrap at the end of the array.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let data = [0, 12, 23, 39, 14, 12, 75];
    ArrayExt.removeLastOf(data, 12);        // 5
    ArrayExt.removeLastOf(data, 17);        // -1
    ArrayExt.removeLastOf(data, 39, 2);     // -1
    ArrayExt.removeLastOf(data, 39, 2, 3);  // 3

removeLastWhere

  • removeLastWhere<T>(array: Array<T>, fn: (value: T, index: number) => boolean, start?: number, stop?: number): { index: number; value: T | undefined }
  • Remove the last occurrence of a value which matches a predicate.

    Type parameters

    • T

    Parameters

    • array: Array<T>

      The array of interest.

    • fn: (value: T, index: number) => boolean

      The predicate function to apply to the values.

        • (value: T, index: number): boolean
        • Parameters

          • value: T
          • index: number

          Returns boolean

    • Default value start: number = -1

      The index of the first element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = 0

      The index of the last element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    Returns { index: number; value: T | undefined }

    The removed { index, value }, which will be -1 and undefined if the value is not contained in the array.

    Notes

    If start < stop the search will wrap at the end of the array.

    Complexity

    Linear.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function isEven(value: number): boolean {
      return value % 2 === 0;
    }
    
    let data = [0, 12, 23, 39, 14, 12, 75];
    ArrayExt.removeLastWhere(data, isEven);        // { index: 5, value: 12 }
    ArrayExt.removeLastWhere(data, isEven, 2);     // { index: 1, value: 12 }
    ArrayExt.removeLastWhere(data, isEven, 2, 1);  // { index: -1, value: undefined }
    • index: number
    • value: T | undefined

reverse

  • Reverse an array in-place.

    Type parameters

    • T

    Parameters

    • array: MutableArrayLike<T>

      The mutable array-like object of interest.

    • Default value start: number = 0

      The index of the first element in the range to be reversed, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be reversed, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

      Complexity

      Linear.

      Undefined Behavior

      A start or stop index which is non-integral.

      Example

      import { ArrayExt } from '@phosphor/algorithm';
      
      let data = [0, 1, 2, 3, 4];
      ArrayExt.reverse(data, 1, 3);  // [0, 3, 2, 1, 4]
      ArrayExt.reverse(data, 3);     // [0, 3, 2, 4, 1]
      ArrayExt.reverse(data);        // [1, 4, 2, 3, 0]

    Returns void

rotate

  • rotate<T>(array: MutableArrayLike<T>, delta: number, start?: number, stop?: number): void
  • Rotate the elements of an array in-place.

    Type parameters

    • T

    Parameters

    • array: MutableArrayLike<T>

      The mutable array-like object of interest.

    • delta: number

      The amount of rotation to apply to the elements. A positive value will rotate the elements to the left. A negative value will rotate the elements to the right.

    • Default value start: number = 0

      The index of the first element in the range to be rotated, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be rotated, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

      Complexity

      Linear.

      Undefined Behavior

      A delta, start, or stop which is non-integral.

      Example

      import { ArrayExt } from '@phosphor/algorithm';
      
      let data = [0, 1, 2, 3, 4];
      ArrayExt.rotate(data, 2);        // [2, 3, 4, 0, 1]
      ArrayExt.rotate(data, -2);       // [0, 1, 2, 3, 4]
      ArrayExt.rotate(data, 10);       // [0, 1, 2, 3, 4]
      ArrayExt.rotate(data, 9);        // [4, 0, 1, 2, 3]
      ArrayExt.rotate(data, 2, 1, 3);  // [4, 2, 0, 1, 3]

    Returns void

shallowEqual

  • shallowEqual<T>(a: ArrayLike<T>, b: ArrayLike<T>, fn?: (a: T, b: T) => boolean): boolean
  • Test whether two arrays are shallowly equal.

    Type parameters

    • T

    Parameters

    • a: ArrayLike<T>

      The first array-like object to compare.

    • b: ArrayLike<T>

      The second array-like object to compare.

    • Optional fn: (a: T, b: T) => boolean

      The comparison function to apply to the elements. It should return true if the elements are "equal". The default compares elements using strict === equality.

        • (a: T, b: T): boolean
        • Parameters

          • a: T
          • b: T

          Returns boolean

    Returns boolean

    Whether the two arrays are shallowly equal.

    Complexity

    Linear.

    Undefined Behavior

    Modifying the length of the arrays while comparing.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    let d1 = [0, 3, 4, 7, 7, 9];
    let d2 = [0, 3, 4, 7, 7, 9];
    let d3 = [42];
    ArrayExt.shallowEqual(d1, d2);  // true
    ArrayExt.shallowEqual(d2, d3);  // false

upperBound

  • upperBound<T, U>(array: ArrayLike<T>, value: U, fn: (element: T, value: U) => number, start?: number, stop?: number): number
  • Find the index of the first element which compares > than a value.

    Type parameters

    • T

    • U

    Parameters

    • array: ArrayLike<T>

      The sorted array-like object to search.

    • value: U

      The value to locate in the array.

    • fn: (element: T, value: U) => number

      The 3-way comparison function to apply to the values. It should return < 0 if an element is less than a value, 0 if an element is equal to a value, or > 0 if an element is greater than a value.

        • (element: T, value: U): number
        • Parameters

          • element: T
          • value: U

          Returns number

    • Default value start: number = 0

      The index of the first element in the range to be searched, inclusive. The default value is 0. Negative values are taken as an offset from the end of the array.

    • Default value stop: number = -1

      The index of the last element in the range to be searched, inclusive. The default value is -1. Negative values are taken as an offset from the end of the array.

    Returns number

    The index of the first element which compares > than the value, or length if there is no such element. If the computed index for stop is less than start, then the computed index for start is returned.

    Notes

    The array must already be sorted in ascending order according to the comparison function.

    Complexity

    Logarithmic.

    Undefined Behavior

    Searching a range which is not sorted in ascending order.

    A start or stop which is non-integral.

    Modifying the length of the array while searching.

    Example

    import { ArrayExt } from '@phosphor/algorithm';
    
    function numberCmp(a: number, b: number): number {
      return a - b;
    }
    
    let data = [0, 3, 4, 7, 7, 9];
    ArrayExt.upperBound(data, 0, numberCmp);   // 1
    ArrayExt.upperBound(data, 6, numberCmp);   // 3
    ArrayExt.upperBound(data, 7, numberCmp);   // 5
    ArrayExt.upperBound(data, -1, numberCmp);  // 0
    ArrayExt.upperBound(data, 10, numberCmp);  // 6