/** * Returns the last entry and the initial N-1 entries of the array, as a tuple of [rest, last]. * * The array must have at least one element. * * @param arr The input array * @returns A tuple of [rest, last] where rest is all but the last element and last is the last element * @throws Error if the array is empty */ export declare function tail(arr: T[]): [T[], T]; export declare function equals(one: ReadonlyArray | undefined, other: ReadonlyArray | undefined, itemEquals?: (a: T, b: T) => boolean): boolean; /** * Remove the element at `index` by replacing it with the last element. This is faster than `splice` * but changes the order of the array */ export declare function removeFastWithoutKeepingOrder(array: T[], index: number): void; /** * Performs a binary search algorithm over a sorted array. * * @param array The array being searched. * @param key The value we search for. * @param comparator A function that takes two array elements and returns zero * if they are equal, a negative number if the first element precedes the * second one in the sorting order, or a positive number if the second element * precedes the first one. * @return See {@link binarySearch2} */ export declare function binarySearch(array: ReadonlyArray, key: T, comparator: (op1: T, op2: T) => number): number; /** * Performs a binary search algorithm over a sorted collection. Useful for cases * when we need to perform a binary search over something that isn't actually an * array, and converting data to an array would defeat the use of binary search * in the first place. * * @param length The collection length. * @param compareToKey A function that takes an index of an element in the * collection and returns zero if the value at this index is equal to the * search key, a negative number if the value precedes the search key in the * sorting order, or a positive number if the search key precedes the value. * @return A non-negative index of an element, if found. If not found, the * result is -(n+1) (or ~n, using bitwise notation), where n is the index * where the key should be inserted to maintain the sorting order. */ export declare function binarySearch2(length: number, compareToKey: (index: number) => number): number; type Compare = (a: T, b: T) => number; export declare function quickSelect(nth: number, data: T[], compare: Compare): T; export declare function groupBy(data: ReadonlyArray, compare: (a: T, b: T) => number): T[][]; /** * Splits the given items into a list of (non-empty) groups. * `shouldBeGrouped` is used to decide if two consecutive items should be in the same group. * The order of the items is preserved. */ export declare function groupAdjacentBy(items: Iterable, shouldBeGrouped: (item1: T, item2: T) => boolean): Iterable; export declare function forEachAdjacent(arr: T[], f: (item1: T | undefined, item2: T | undefined) => void): void; export declare function forEachWithNeighbors(arr: T[], f: (before: T | undefined, element: T, after: T | undefined) => void): void; /** * @returns New array with all falsy values removed. The original array IS NOT modified. */ export declare function coalesce(array: ReadonlyArray): T[]; /** * Remove all falsy values from `array`. The original array IS modified. */ export declare function coalesceInPlace(array: Array): asserts array is Array; /** * @returns false if the provided object is an array and not empty. */ export declare function isFalsyOrEmpty(obj: any): boolean; /** * @returns True if the provided object is an array and has at least one element. */ export declare function isNonEmptyArray(obj: T[] | undefined | null): obj is T[]; export declare function isNonEmptyArray(obj: readonly T[] | undefined | null): obj is readonly T[]; /** * Removes duplicates from the given array. The optional keyFn allows to specify * how elements are checked for equality by returning an alternate value for each. */ export declare function distinct(array: ReadonlyArray, keyFn?: (value: T) => unknown): T[]; export declare function range(to: number): number[]; export declare function range(from: number, to: number): number[]; /** * Insert `insertArr` inside `target` at `insertIndex`. * Please don't touch unless you understand https://jsperf.com/inserting-an-array-within-an-array */ export declare function arrayInsert(target: T[], insertIndex: number, insertArr: T[]): T[]; /** * Uses Fisher-Yates shuffle to shuffle the given array */ export declare function shuffle(array: T[], _seed?: number): void; /** * Pushes an element to the start of the array, if found. */ export declare function pushToStart(arr: T[], value: T): void; /** * Pushes an element to the end of the array, if found. */ export declare function pushToEnd(arr: T[], value: T): void; export declare function pushMany(arr: T[], items: ReadonlyArray): void; export declare function asArray(x: T | T[]): T[]; export declare function asArray(x: T | readonly T[]): readonly T[]; /** * Insert the new items in the array. * @param array The original array. * @param start The zero-based location in the array from which to start inserting elements. * @param newItems The items to be inserted */ export declare function insertInto(array: T[], start: number, newItems: T[]): void; /** * Removes elements from an array and inserts new elements in their place, returning the deleted elements. Alternative to the native Array.splice method, it * can only support limited number of items due to the maximum call stack size limit. * @param array The original array. * @param start The zero-based location in the array from which to start removing elements. * @param deleteCount The number of elements to remove. * @returns An array containing the elements that were deleted. */ export declare function splice(array: T[], start: number, deleteCount: number, newItems: T[]): T[]; /** * When comparing two values, * a negative number indicates that the first value is less than the second, * a positive number indicates that the first value is greater than the second, * and zero indicates that neither is the case. */ export type CompareResult = number; export declare namespace CompareResult { function isLessThan(result: CompareResult): boolean; function isLessThanOrEqual(result: CompareResult): boolean; function isGreaterThan(result: CompareResult): boolean; function isNeitherLessOrGreaterThan(result: CompareResult): boolean; const greaterThan = 1; const lessThan = -1; const neitherLessOrGreaterThan = 0; } /** * A comparator `c` defines a total order `<=` on `T` as following: * `c(a, b) <= 0` iff `a` <= `b`. * We also have `c(a, b) == 0` iff `c(b, a) == 0`. */ export type Comparator = (a: T, b: T) => CompareResult; export declare function compareBy(selector: (item: TItem) => TCompareBy, comparator: Comparator): Comparator; export declare function tieBreakComparators(...comparators: Comparator[]): Comparator; /** * The natural order on numbers. */ export declare const numberComparator: Comparator; export declare const booleanComparator: Comparator; export declare function reverseOrder(comparator: Comparator): Comparator; export declare class ArrayQueue { private readonly items; private firstIdx; private lastIdx; /** * Constructs a queue that is backed by the given array. Runtime is O(1). */ constructor(items: readonly T[]); get length(): number; /** * Consumes elements from the beginning of the queue as long as the predicate returns true. * If no elements were consumed, `null` is returned. Has a runtime of O(result.length). */ takeWhile(predicate: (value: T) => boolean): T[] | null; /** * Consumes elements from the end of the queue as long as the predicate returns true. * If no elements were consumed, `null` is returned. * The result has the same order as the underlying array! */ takeFromEndWhile(predicate: (value: T) => boolean): T[] | null; peek(): T | undefined; peekLast(): T | undefined; dequeue(): T | undefined; removeLast(): T | undefined; takeCount(count: number): T[]; } /** * This class is faster than an iterator and array for lazy computed data. */ export declare class CallbackIterable { /** * Calls the callback for every item. * Stops when the callback returns false. */ readonly iterate: (callback: (item: T) => boolean) => void; static readonly empty: CallbackIterable; constructor( /** * Calls the callback for every item. * Stops when the callback returns false. */ iterate: (callback: (item: T) => boolean) => void); forEach(handler: (item: T) => void): void; toArray(): T[]; filter(predicate: (item: T) => boolean): CallbackIterable; map(mapFn: (item: T) => TResult): CallbackIterable; some(predicate: (item: T) => boolean): boolean; findFirst(predicate: (item: T) => boolean): T | undefined; findLast(predicate: (item: T) => boolean): T | undefined; findLastMaxBy(comparator: Comparator): T | undefined; } /** * Represents a re-arrangement of items in an array. */ export declare class Permutation { private readonly _indexMap; constructor(_indexMap: readonly number[]); /** * Returns a permutation that sorts the given array according to the given compare function. */ static createSortPermutation(arr: readonly T[], compareFn: (a: T, b: T) => number): Permutation; /** * Returns a new array with the elements of the given array re-arranged according to this permutation. */ apply(arr: readonly T[]): T[]; /** * Returns a new permutation that undoes the re-arrangement of this permutation. */ inverse(): Permutation; } export {};