@@ -1061,10 +1061,10 @@ namespace ts {
1061
1061
/**
1062
1062
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
1063
1063
*/
1064
- export function stableSort < T > ( array : ReadonlyArray < T > , comparer : Comparer < T > ) {
1064
+ export function stableSort < T > ( array : ReadonlyArray < T > , comparer : Comparer < T > ) : SortedReadonlyArray < T > {
1065
1065
const indices = array . map ( ( _ , i ) => i ) ;
1066
1066
stableSortIndices ( array , indices , comparer ) ;
1067
- return indices . map ( i => array [ i ] ) ;
1067
+ return indices . map ( i => array [ i ] ) as SortedArray < T > as SortedReadonlyArray < T > ;
1068
1068
}
1069
1069
1070
1070
export function rangeEquals < T > ( array1 : ReadonlyArray < T > , array2 : ReadonlyArray < T > , pos : number , end : number ) {
@@ -1156,13 +1156,26 @@ namespace ts {
1156
1156
* @param offset An offset into `array` at which to start the search.
1157
1157
*/
1158
1158
export function binarySearch < T , U > ( array : ReadonlyArray < T > , value : T , keySelector : ( v : T ) => U , keyComparer : Comparer < U > , offset ?: number ) : number {
1159
- if ( ! array || array . length === 0 ) {
1159
+ return binarySearchKey ( array , keySelector ( value ) , keySelector , keyComparer , offset ) ;
1160
+ }
1161
+
1162
+ /**
1163
+ * Performs a binary search, finding the index at which an object with `key` occurs in `array`.
1164
+ * If no such index is found, returns the 2's-complement of first index at which
1165
+ * `array[index]` exceeds `key`.
1166
+ * @param array A sorted array whose first element must be no larger than number
1167
+ * @param key The key to be searched for in the array.
1168
+ * @param keySelector A callback used to select the search key from each element of `array`.
1169
+ * @param keyComparer A callback used to compare two keys in a sorted array.
1170
+ * @param offset An offset into `array` at which to start the search.
1171
+ */
1172
+ export function binarySearchKey < T , U > ( array : ReadonlyArray < T > , key : U , keySelector : ( v : T ) => U , keyComparer : Comparer < U > , offset ?: number ) : number {
1173
+ if ( ! some ( array ) ) {
1160
1174
return - 1 ;
1161
1175
}
1162
1176
1163
1177
let low = offset || 0 ;
1164
1178
let high = array . length - 1 ;
1165
- const key = keySelector ( value ) ;
1166
1179
while ( low <= high ) {
1167
1180
const middle = low + ( ( high - low ) >> 1 ) ;
1168
1181
const midKey = keySelector ( array [ middle ] ) ;
0 commit comments