@@ -1061,10 +1061,10 @@ namespace ts {
10611061 /**
10621062 * Stable sort of an array. Elements equal to each other maintain their relative position in the array.
10631063 */
1064- export function stableSort < T > ( array : ReadonlyArray < T > , comparer : Comparer < T > ) {
1064+ export function stableSort < T > ( array : ReadonlyArray < T > , comparer : Comparer < T > ) : SortedReadonlyArray < T > {
10651065 const indices = array . map ( ( _ , i ) => i ) ;
10661066 stableSortIndices ( array , indices , comparer ) ;
1067- return indices . map ( i => array [ i ] ) ;
1067+ return indices . map ( i => array [ i ] ) as SortedArray < T > as SortedReadonlyArray < T > ;
10681068 }
10691069
10701070 export function rangeEquals < T > ( array1 : ReadonlyArray < T > , array2 : ReadonlyArray < T > , pos : number , end : number ) {
@@ -1156,13 +1156,26 @@ namespace ts {
11561156 * @param offset An offset into `array` at which to start the search.
11571157 */
11581158 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 ) ) {
11601174 return - 1 ;
11611175 }
11621176
11631177 let low = offset || 0 ;
11641178 let high = array . length - 1 ;
1165- const key = keySelector ( value ) ;
11661179 while ( low <= high ) {
11671180 const middle = low + ( ( high - low ) >> 1 ) ;
11681181 const midKey = keySelector ( array [ middle ] ) ;
0 commit comments