diff --git a/src/vs/base/common/arrays.ts b/src/vs/base/common/arrays.ts index d4e3bb7bf7c..8083d73602f 100644 --- a/src/vs/base/common/arrays.ts +++ b/src/vs/base/common/arrays.ts @@ -46,7 +46,7 @@ export function equals(one: ReadonlyArray | undefined, other: ReadonlyArra return true; } -export function binarySearch(array: T[], key: T, comparator: (op1: T, op2: T) => number): number { +export function binarySearch(array: ReadonlyArray, key: T, comparator: (op1: T, op2: T) => number): number { let low = 0, high = array.length - 1; @@ -69,7 +69,7 @@ export function binarySearch(array: T[], key: T, comparator: (op1: T, op2: T) * 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: T[], p: (x: T) => boolean): number { +export function findFirstInSorted(array: ReadonlyArray, p: (x: T) => boolean): number { let low = 0, high = array.length; if (high === 0) { return 0; // no children @@ -135,7 +135,7 @@ function _sort(a: T[], compare: Compare, lo: number, hi: number, aux: T[]) } -export function groupBy(data: T[], compare: (a: T, b: T) => number): T[][] { +export function groupBy(data: ReadonlyArray, compare: (a: T, b: T) => number): T[][] { const result: T[][] = []; let currentGroup: T[] | undefined = undefined; for (const element of mergeSort(data.slice(0), compare)) { @@ -156,7 +156,7 @@ interface IMutableSplice extends ISplice { /** * Diffs two *sorted* arrays and computes the splices which apply the diff. */ -export function sortedDiff(before: T[], after: T[], compare: (a: T, b: T) => number): ISplice[] { +export function sortedDiff(before: ReadonlyArray, after: ReadonlyArray, compare: (a: T, b: T) => number): ISplice[] { const result: IMutableSplice[] = []; function pushSplice(start: number, deleteCount: number, toInsert: T[]): void { @@ -215,7 +215,7 @@ export function sortedDiff(before: T[], after: T[], compare: (a: T, b: T) => * @param after * @param compare */ -export function delta(before: T[], after: T[], compare: (a: T, b: T) => number): { removed: T[], added: T[] } { +export function delta(before: ReadonlyArray, after: ReadonlyArray, compare: (a: T, b: T) => number): { removed: T[], added: T[] } { const splices = sortedDiff(before, after, compare); const removed: T[] = []; const added: T[] = []; @@ -238,7 +238,7 @@ export function delta(before: T[], after: T[], compare: (a: T, b: T) => numbe * @param n The number of elements to return. * @return The first n elemnts from array when sorted with compare. */ -export function top(array: T[], compare: (a: T, b: T) => number, n: number): T[] { +export function top(array: ReadonlyArray, compare: (a: T, b: T) => number, n: number): T[] { if (n === 0) { return []; } @@ -284,7 +284,7 @@ export function topAsync(array: T[], compare: (a: T, b: T) => number, n: numb }); } -function topStep(array: T[], compare: (a: T, b: T) => number, result: T[], i: number, m: number): void { +function topStep(array: ReadonlyArray, compare: (a: T, b: T) => number, result: T[], i: number, m: number): void { for (const n = result.length; i < m; i++) { const element = array[i]; if (compare(element, result[n - 1]) < 0) { @@ -348,7 +348,7 @@ export function isNonEmptyArray(obj: ReadonlyArray | undefined | null): ob * Removes duplicates from the given array. The optional keyFn allows to specify * how elements are checked for equalness by returning a unique string for each. */ -export function distinct(array: T[], keyFn?: (t: T) => string): T[] { +export function distinct(array: ReadonlyArray, keyFn?: (t: T) => string): T[] { if (!keyFn) { return array.filter((element, position) => { return array.indexOf(element) === position; @@ -383,7 +383,7 @@ export function uniqueFilter(keyFn: (t: T) => string): (t: T) => boolean { }; } -export function firstIndex(array: T[] | ReadonlyArray, fn: (item: T) => boolean): number { +export function firstIndex(array: ReadonlyArray, fn: (item: T) => boolean): number { for (let i = 0; i < array.length; i++) { const element = array[i]; @@ -395,14 +395,14 @@ export function firstIndex(array: T[] | ReadonlyArray, fn: (item: T) => bo return -1; } -export function first(array: T[] | ReadonlyArray, fn: (item: T) => boolean, notFoundValue: T): T; -export function first(array: T[] | ReadonlyArray, fn: (item: T) => boolean): T | null; -export function first(array: T[] | ReadonlyArray, fn: (item: T) => boolean, notFoundValue: T | null = null): T | null { +export function first(array: ReadonlyArray, fn: (item: T) => boolean, notFoundValue: T): T; +export function first(array: ReadonlyArray, fn: (item: T) => boolean): T | null; +export function first(array: ReadonlyArray, fn: (item: T) => boolean, notFoundValue: T | null = null): T | null { const index = firstIndex(array, fn); return index < 0 ? notFoundValue : array[index]; } -export function commonPrefixLength(one: T[], other: T[], equals: (a: T, b: T) => boolean = (a, b) => a === b): number { +export function commonPrefixLength(one: ReadonlyArray, other: ReadonlyArray, equals: (a: T, b: T) => boolean = (a, b) => a === b): number { let result = 0; for (let i = 0, len = Math.min(one.length, other.length); i < len && equals(one[i], other[i]); i++) { @@ -451,9 +451,9 @@ export function fill(num: number, valueFn: () => T, arr: T[] = []): T[] { return arr; } -export function index(array: T[], indexer: (t: T) => string): { [key: string]: T; }; -export function index(array: T[], indexer: (t: T) => string, merger?: (t: T, r: R) => R): { [key: string]: R; }; -export function index(array: T[], indexer: (t: T) => string, merger: (t: T, r: R) => R = t => t as any): { [key: string]: R; } { +export function index(array: ReadonlyArray, indexer: (t: T) => string): { [key: string]: T; }; +export function index(array: ReadonlyArray, indexer: (t: T) => string, merger?: (t: T, r: R) => R): { [key: string]: R; }; +export function index(array: ReadonlyArray, indexer: (t: T) => string, merger: (t: T, r: R) => R = t => t as any): { [key: string]: R; } { return array.reduce((r, t) => { const key = indexer(t); r[key] = merger(t, r[key]);