Skip to content

Commit 1edc8c6

Browse files
committed
Use ReadonlyArray in more places
1 parent a179a4f commit 1edc8c6

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

src/vs/base/common/arrays.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function equals<T>(one: ReadonlyArray<T> | undefined, other: ReadonlyArra
4646
return true;
4747
}
4848

49-
export function binarySearch<T>(array: T[], key: T, comparator: (op1: T, op2: T) => number): number {
49+
export function binarySearch<T>(array: ReadonlyArray<T>, key: T, comparator: (op1: T, op2: T) => number): number {
5050
let low = 0,
5151
high = array.length - 1;
5252

@@ -69,7 +69,7 @@ export function binarySearch<T>(array: T[], key: T, comparator: (op1: T, op2: T)
6969
* are located before all elements where p(x) is true.
7070
* @returns the least x for which p(x) is true or array.length if no element fullfills the given function.
7171
*/
72-
export function findFirstInSorted<T>(array: T[], p: (x: T) => boolean): number {
72+
export function findFirstInSorted<T>(array: ReadonlyArray<T>, p: (x: T) => boolean): number {
7373
let low = 0, high = array.length;
7474
if (high === 0) {
7575
return 0; // no children
@@ -135,7 +135,7 @@ function _sort<T>(a: T[], compare: Compare<T>, lo: number, hi: number, aux: T[])
135135
}
136136

137137

138-
export function groupBy<T>(data: T[], compare: (a: T, b: T) => number): T[][] {
138+
export function groupBy<T>(data: ReadonlyArray<T>, compare: (a: T, b: T) => number): T[][] {
139139
const result: T[][] = [];
140140
let currentGroup: T[] | undefined = undefined;
141141
for (const element of mergeSort(data.slice(0), compare)) {
@@ -156,7 +156,7 @@ interface IMutableSplice<T> extends ISplice<T> {
156156
/**
157157
* Diffs two *sorted* arrays and computes the splices which apply the diff.
158158
*/
159-
export function sortedDiff<T>(before: T[], after: T[], compare: (a: T, b: T) => number): ISplice<T>[] {
159+
export function sortedDiff<T>(before: ReadonlyArray<T>, after: ReadonlyArray<T>, compare: (a: T, b: T) => number): ISplice<T>[] {
160160
const result: IMutableSplice<T>[] = [];
161161

162162
function pushSplice(start: number, deleteCount: number, toInsert: T[]): void {
@@ -215,7 +215,7 @@ export function sortedDiff<T>(before: T[], after: T[], compare: (a: T, b: T) =>
215215
* @param after
216216
* @param compare
217217
*/
218-
export function delta<T>(before: T[], after: T[], compare: (a: T, b: T) => number): { removed: T[], added: T[] } {
218+
export function delta<T>(before: ReadonlyArray<T>, after: ReadonlyArray<T>, compare: (a: T, b: T) => number): { removed: T[], added: T[] } {
219219
const splices = sortedDiff(before, after, compare);
220220
const removed: T[] = [];
221221
const added: T[] = [];
@@ -238,7 +238,7 @@ export function delta<T>(before: T[], after: T[], compare: (a: T, b: T) => numbe
238238
* @param n The number of elements to return.
239239
* @return The first n elemnts from array when sorted with compare.
240240
*/
241-
export function top<T>(array: T[], compare: (a: T, b: T) => number, n: number): T[] {
241+
export function top<T>(array: ReadonlyArray<T>, compare: (a: T, b: T) => number, n: number): T[] {
242242
if (n === 0) {
243243
return [];
244244
}
@@ -284,7 +284,7 @@ export function topAsync<T>(array: T[], compare: (a: T, b: T) => number, n: numb
284284
});
285285
}
286286

287-
function topStep<T>(array: T[], compare: (a: T, b: T) => number, result: T[], i: number, m: number): void {
287+
function topStep<T>(array: ReadonlyArray<T>, compare: (a: T, b: T) => number, result: T[], i: number, m: number): void {
288288
for (const n = result.length; i < m; i++) {
289289
const element = array[i];
290290
if (compare(element, result[n - 1]) < 0) {
@@ -348,7 +348,7 @@ export function isNonEmptyArray<T>(obj: ReadonlyArray<T> | undefined | null): ob
348348
* Removes duplicates from the given array. The optional keyFn allows to specify
349349
* how elements are checked for equalness by returning a unique string for each.
350350
*/
351-
export function distinct<T>(array: T[], keyFn?: (t: T) => string): T[] {
351+
export function distinct<T>(array: ReadonlyArray<T>, keyFn?: (t: T) => string): T[] {
352352
if (!keyFn) {
353353
return array.filter((element, position) => {
354354
return array.indexOf(element) === position;
@@ -383,7 +383,7 @@ export function uniqueFilter<T>(keyFn: (t: T) => string): (t: T) => boolean {
383383
};
384384
}
385385

386-
export function firstIndex<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean): number {
386+
export function firstIndex<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): number {
387387
for (let i = 0; i < array.length; i++) {
388388
const element = array[i];
389389

@@ -395,14 +395,14 @@ export function firstIndex<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => bo
395395
return -1;
396396
}
397397

398-
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T): T;
399-
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean): T | null;
400-
export function first<T>(array: T[] | ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T | null = null): T | null {
398+
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T): T;
399+
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): T | null;
400+
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T | null = null): T | null {
401401
const index = firstIndex(array, fn);
402402
return index < 0 ? notFoundValue : array[index];
403403
}
404404

405-
export function commonPrefixLength<T>(one: T[], other: T[], equals: (a: T, b: T) => boolean = (a, b) => a === b): number {
405+
export function commonPrefixLength<T>(one: ReadonlyArray<T>, other: ReadonlyArray<T>, equals: (a: T, b: T) => boolean = (a, b) => a === b): number {
406406
let result = 0;
407407

408408
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<T>(num: number, valueFn: () => T, arr: T[] = []): T[] {
451451
return arr;
452452
}
453453

454-
export function index<T>(array: T[], indexer: (t: T) => string): { [key: string]: T; };
455-
export function index<T, R>(array: T[], indexer: (t: T) => string, merger?: (t: T, r: R) => R): { [key: string]: R; };
456-
export function index<T, R>(array: T[], indexer: (t: T) => string, merger: (t: T, r: R) => R = t => t as any): { [key: string]: R; } {
454+
export function index<T>(array: ReadonlyArray<T>, indexer: (t: T) => string): { [key: string]: T; };
455+
export function index<T, R>(array: ReadonlyArray<T>, indexer: (t: T) => string, merger?: (t: T, r: R) => R): { [key: string]: R; };
456+
export function index<T, R>(array: ReadonlyArray<T>, indexer: (t: T) => string, merger: (t: T, r: R) => R = t => t as any): { [key: string]: R; } {
457457
return array.reduce((r, t) => {
458458
const key = indexer(t);
459459
r[key] = merger(t, r[key]);

0 commit comments

Comments
 (0)