123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /**
- * 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;
- }
- export function binarySearch(array, key, comparator) {
- let low = 0, high = array.length - 1;
- while (low <= high) {
- const mid = ((low + high) / 2) | 0;
- const comp = comparator(array[mid], key);
- 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');
- }
- let pivotValue = data[Math.floor(data.length * Math.random())];
- let lower = [];
- let higher = [];
- let pivots = [];
- for (let 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);
- }
- /**
- * @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 = lastIndex(arr, predicate);
- if (idx === -1) {
- return undefined;
- }
- return arr[idx];
- }
- export function lastIndex(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 flatten(arr) {
- return [].concat(...arr);
- }
- 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 asArray(x) {
- return Array.isArray(x) ? x : [x];
- }
- /**
- * 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 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;
- }
- /**
- * 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() {
- 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;
- }
- }
|