/** * Returns the last element of an array. * @param array The array. * @param n Which element from the end (default is zero). */ export function tail(array, n = 0) { return array[array.length - (1 + n)]; } export function tail2(arr) { if (arr.length === 0) { throw new Error('Invalid tail call'); } return [arr.slice(0, arr.length - 1), arr[arr.length - 1]]; } export function equals(one, other, itemEquals = (a, b) => a === b) { if (one === other) { return true; } if (!one || !other) { return false; } if (one.length !== other.length) { return false; } for (let i = 0, len = one.length; i < len; i++) { if (!itemEquals(one[i], other[i])) { return false; } } return true; } /** * 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 function removeFastWithoutKeepingOrder(array, index) { const last = array.length - 1; if (index < last) { array[index] = array[last]; } array.pop(); } /** * 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 function binarySearch(array, key, comparator) { return binarySearch2(array.length, i => comparator(array[i], key)); } /** * 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 function binarySearch2(length, compareToKey) { let low = 0, high = length - 1; while (low <= high) { const mid = ((low + high) / 2) | 0; const comp = compareToKey(mid); if (comp < 0) { low = mid + 1; } else if (comp > 0) { high = mid - 1; } else { return mid; } } return -(low + 1); } /** * Takes a sorted array and a function p. The array is sorted in such a way that all elements where p(x) is false * are located before all elements where p(x) is true. * @returns the least x for which p(x) is true or array.length if no element fullfills the given function. */ export function findFirstInSorted(array, p) { let low = 0, high = array.length; if (high === 0) { return 0; // no children } while (low < high) { const mid = Math.floor((low + high) / 2); if (p(array[mid])) { high = mid; } else { low = mid + 1; } } return low; } export function quickSelect(nth, data, compare) { nth = nth | 0; if (nth >= data.length) { throw new TypeError('invalid index'); } const pivotValue = data[Math.floor(data.length * Math.random())]; const lower = []; const higher = []; const pivots = []; for (const value of data) { const val = compare(value, pivotValue); if (val < 0) { lower.push(value); } else if (val > 0) { higher.push(value); } else { pivots.push(value); } } if (nth < lower.length) { return quickSelect(nth, lower, compare); } else if (nth < lower.length + pivots.length) { return pivots[0]; } else { return quickSelect(nth - (lower.length + pivots.length), higher, compare); } } export function groupBy(data, compare) { const result = []; let currentGroup = undefined; for (const element of data.slice(0).sort(compare)) { if (!currentGroup || compare(currentGroup[0], element) !== 0) { currentGroup = [element]; result.push(currentGroup); } else { currentGroup.push(element); } } return result; } /** * @returns New array with all falsy values removed. The original array IS NOT modified. */ export function coalesce(array) { return array.filter(e => !!e); } /** * Remove all falsy values from `array`. The original array IS modified. */ export function coalesceInPlace(array) { let to = 0; for (let i = 0; i < array.length; i++) { if (!!array[i]) { array[to] = array[i]; to += 1; } } array.length = to; } /** * @returns false if the provided object is an array and not empty. */ export function isFalsyOrEmpty(obj) { return !Array.isArray(obj) || obj.length === 0; } export function isNonEmptyArray(obj) { return Array.isArray(obj) && obj.length > 0; } /** * 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 function distinct(array, keyFn = value => value) { const seen = new Set(); return array.filter(element => { const key = keyFn(element); if (seen.has(key)) { return false; } seen.add(key); return true; }); } export function findLast(arr, predicate) { const idx = findLastIndex(arr, predicate); if (idx === -1) { return undefined; } return arr[idx]; } export function findLastIndex(array, fn) { for (let i = array.length - 1; i >= 0; i--) { const element = array[i]; if (fn(element)) { return i; } } return -1; } export function firstOrDefault(array, notFoundValue) { return array.length > 0 ? array[0] : notFoundValue; } export function range(arg, to) { let from = typeof to === 'number' ? arg : 0; if (typeof to === 'number') { from = arg; } else { from = 0; to = arg; } const result = []; if (from <= to) { for (let i = from; i < to; i++) { result.push(i); } } else { for (let i = from; i > to; i--) { result.push(i); } } return result; } /** * Insert `insertArr` inside `target` at `insertIndex`. * Please don't touch unless you understand https://jsperf.com/inserting-an-array-within-an-array */ export function arrayInsert(target, insertIndex, insertArr) { const before = target.slice(0, insertIndex); const after = target.slice(insertIndex); return before.concat(insertArr, after); } /** * Pushes an element to the start of the array, if found. */ export function pushToStart(arr, value) { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); arr.unshift(value); } } /** * Pushes an element to the end of the array, if found. */ export function pushToEnd(arr, value) { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); arr.push(value); } } export function pushMany(arr, items) { for (const item of items) { arr.push(item); } } export function asArray(x) { return Array.isArray(x) ? x : [x]; } /** * Returns the first mapped value of the array which is not undefined. */ export function mapFind(array, mapFn) { for (const value of array) { const mapped = mapFn(value); if (mapped !== undefined) { return mapped; } } return undefined; } /** * 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 function insertInto(array, start, newItems) { const startIdx = getActualStartIndex(array, start); const originalLength = array.length; const newItemsLength = newItems.length; array.length = originalLength + newItemsLength; // Move the items after the start index, start from the end so that we don't overwrite any value. for (let i = originalLength - 1; i >= startIdx; i--) { array[i + newItemsLength] = array[i]; } for (let i = 0; i < newItemsLength; i++) { array[i + startIdx] = newItems[i]; } } /** * 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 function splice(array, start, deleteCount, newItems) { const index = getActualStartIndex(array, start); const result = array.splice(index, deleteCount); insertInto(array, index, newItems); return result; } /** * Determine the actual start index (same logic as the native splice() or slice()) * If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided. * If negative, it will begin that many elements from the end of the array. (In this case, the origin -1, meaning -n is the index of the nth last element, and is therefore equivalent to the index of array.length - n.) If array.length + start is less than 0, it will begin from index 0. * @param array The target array. * @param start The operation index. */ function getActualStartIndex(array, start) { return start < 0 ? Math.max(start + array.length, 0) : Math.min(start, array.length); } export var CompareResult; (function (CompareResult) { function isLessThan(result) { return result < 0; } CompareResult.isLessThan = isLessThan; function isLessThanOrEqual(result) { return result <= 0; } CompareResult.isLessThanOrEqual = isLessThanOrEqual; function isGreaterThan(result) { return result > 0; } CompareResult.isGreaterThan = isGreaterThan; function isNeitherLessOrGreaterThan(result) { return result === 0; } CompareResult.isNeitherLessOrGreaterThan = isNeitherLessOrGreaterThan; CompareResult.greaterThan = 1; CompareResult.lessThan = -1; CompareResult.neitherLessOrGreaterThan = 0; })(CompareResult || (CompareResult = {})); export function compareBy(selector, comparator) { return (a, b) => comparator(selector(a), selector(b)); } export function tieBreakComparators(...comparators) { return (item1, item2) => { for (const comparator of comparators) { const result = comparator(item1, item2); if (!CompareResult.isNeitherLessOrGreaterThan(result)) { return result; } } return CompareResult.neitherLessOrGreaterThan; }; } /** * The natural order on numbers. */ export const numberComparator = (a, b) => a - b; export const booleanComparator = (a, b) => numberComparator(a ? 1 : 0, b ? 1 : 0); export function reverseOrder(comparator) { return (a, b) => -comparator(a, b); } /** * Returns the first item that is equal to or greater than every other item. */ export function findMaxBy(items, comparator) { if (items.length === 0) { return undefined; } let max = items[0]; for (let i = 1; i < items.length; i++) { const item = items[i]; if (comparator(item, max) > 0) { max = item; } } return max; } /** * Returns the last item that is equal to or greater than every other item. */ export function findLastMaxBy(items, comparator) { if (items.length === 0) { return undefined; } let max = items[0]; for (let i = 1; i < items.length; i++) { const item = items[i]; if (comparator(item, max) >= 0) { max = item; } } return max; } /** * Returns the first item that is equal to or less than every other item. */ export function findMinBy(items, comparator) { return findMaxBy(items, (a, b) => -comparator(a, b)); } export function findMaxIdxBy(items, comparator) { if (items.length === 0) { return -1; } let maxIdx = 0; for (let i = 1; i < items.length; i++) { const item = items[i]; if (comparator(item, items[maxIdx]) > 0) { maxIdx = i; } } return maxIdx; } export class ArrayQueue { /** * Constructs a queue that is backed by the given array. Runtime is O(1). */ constructor(items) { this.items = items; this.firstIdx = 0; this.lastIdx = this.items.length - 1; } get length() { return this.lastIdx - this.firstIdx + 1; } /** * 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) { // P(k) := k <= this.lastIdx && predicate(this.items[k]) // Find s := min { k | k >= this.firstIdx && !P(k) } and return this.data[this.firstIdx...s) let startIdx = this.firstIdx; while (startIdx < this.items.length && predicate(this.items[startIdx])) { startIdx++; } const result = startIdx === this.firstIdx ? null : this.items.slice(this.firstIdx, startIdx); this.firstIdx = startIdx; return result; } /** * 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) { // P(k) := this.firstIdx >= k && predicate(this.items[k]) // Find s := max { k | k <= this.lastIdx && !P(k) } and return this.data(s...this.lastIdx] let endIdx = this.lastIdx; while (endIdx >= 0 && predicate(this.items[endIdx])) { endIdx--; } const result = endIdx === this.lastIdx ? null : this.items.slice(endIdx + 1, this.lastIdx + 1); this.lastIdx = endIdx; return result; } peek() { if (this.length === 0) { return undefined; } return this.items[this.firstIdx]; } dequeue() { const result = this.items[this.firstIdx]; this.firstIdx++; return result; } takeCount(count) { const result = this.items.slice(this.firstIdx, this.firstIdx + count); this.firstIdx += count; return result; } } /** * This class is faster than an iterator and array for lazy computed data. */ export class CallbackIterable { constructor( /** * Calls the callback for every item. * Stops when the callback returns false. */ iterate) { this.iterate = iterate; } toArray() { const result = []; this.iterate(item => { result.push(item); return true; }); return result; } filter(predicate) { return new CallbackIterable(cb => this.iterate(item => predicate(item) ? cb(item) : true)); } map(mapFn) { return new CallbackIterable(cb => this.iterate(item => cb(mapFn(item)))); } findLast(predicate) { let result; this.iterate(item => { if (predicate(item)) { result = item; } return true; }); return result; } findLastMaxBy(comparator) { let result; let first = true; this.iterate(item => { if (first || CompareResult.isGreaterThan(comparator(item, result))) { first = false; result = item; } return true; }); return result; } } CallbackIterable.empty = new CallbackIterable(_callback => { });