Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/pipeline/alternativeAction/util.ts
13394 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
const DEBUG = true;
7
8
export function log(...args: any[]) {
9
if (DEBUG) {
10
console.log(...args);
11
}
12
}
13
14
export function binarySearch<T>(
15
array: readonly T[],
16
compare: (element: T) => number
17
): number {
18
let left = 0;
19
let right = array.length - 1;
20
let lastLess = -1;
21
22
while (left <= right) {
23
const mid = Math.floor((left + right) / 2);
24
const cmp = compare(array[mid]);
25
26
if (cmp === 0) {
27
return mid;
28
} else if (cmp < 0) {
29
lastLess = mid;
30
left = mid + 1;
31
} else {
32
right = mid - 1;
33
}
34
}
35
36
return lastLess;
37
}
38
39
//#region Either
40
41
export type Either<L, R> = Left<L> | Right<R>;
42
43
export namespace Either {
44
export function left<L>(value: L): Left<L> {
45
return new Left(value);
46
}
47
48
export function right<R>(value: R): Right<R> {
49
return new Right(value);
50
}
51
}
52
53
/**
54
* To instantiate a Left, use `Either.left(value)`.
55
* To instantiate a Right, use `Either.right(value)`.
56
*/
57
class Left<L> {
58
constructor(readonly value: L) { }
59
60
isLeft(): this is Left<L> {
61
return true;
62
}
63
64
isRight(): this is Right<never> {
65
return false;
66
}
67
}
68
69
/**
70
* To instantiate a Left, use `Either.left(value)`.
71
* To instantiate a Right, use `Either.right(value)`.
72
*/
73
class Right<R> {
74
constructor(readonly value: R) { }
75
76
isLeft(): this is Left<never> {
77
return false;
78
}
79
80
isRight(): this is Right<R> {
81
return true;
82
}
83
}
84
85
//#endregion
86
87