@@ -33,16 +33,19 @@ type index = number;
33
33
/**
34
34
* Types that BTree supports by default
35
35
*/
36
- export type DefaultComparable = number | string | Date | boolean | null | undefined | ( number | string ) [ ] | { valueOf : ( ) => number | string | Date | boolean | null | undefined | ( number | string ) [ ] } ;
36
+ export type DefaultComparable = number | string | Date | boolean | null | undefined | ( number | string ) [ ] |
37
+ { valueOf : ( ) => number | string | Date | boolean | null | undefined | ( number | string ) [ ] } ;
37
38
38
39
/**
39
40
* Compares DefaultComparables to form a strict partial ordering.
40
41
*
41
42
* Handles +/-0 and NaN like Map: NaN is equal to NaN, and -0 is equal to +0.
42
43
*
43
- * Arrays are compared using '<' and '>', which may cause unexpected equality: for example [1] will be considered equal to ['1'].
44
+ * Arrays are compared using '<' and '>', which may cause unexpected equality:
45
+ * for example [1] will be considered equal to ['1'].
44
46
*
45
- * Two objects with equal valueOf compare the same, but compare unequal to primitives that have the same value.
47
+ * Two objects with equal valueOf compare the same, but compare unequal to
48
+ * primitives that have the same value.
46
49
*/
47
50
export function defaultComparator ( a : DefaultComparable , b : DefaultComparable ) : number {
48
51
// Special case finite numbers first for performance.
@@ -89,22 +92,24 @@ export function defaultComparator(a: DefaultComparable, b: DefaultComparable): n
89
92
return Number . isNaN ( b ) ? 0 : - 1 ;
90
93
else if ( Number . isNaN ( b ) )
91
94
return 1 ;
92
- return 0 ; // unreachable?
95
+ // This could be two objects (e.g. [7] and ['7']) that aren't ordered
96
+ return Array . isArray ( a ) ? 0 : Number . NaN ;
93
97
} ;
94
98
95
99
/**
96
100
* Compares items using the < and > operators. This function is probably slightly
97
101
* faster than the defaultComparator for Dates and strings, but has not been benchmarked.
98
102
* Unlike defaultComparator, this comparator doesn't support mixed types correctly,
99
- * i.e. use it with `BTree<string>` or `BTree<Date >` but not `BTree<string|Date >`.
103
+ * i.e. use it with `BTree<string>` or `BTree<number >` but not `BTree<string|number >`.
100
104
*
101
- * Note: null compares as less than any number or Date, but in general null is
102
- * incomparable with strings, and undefined is not comparable with other types
103
- * using the > and < operators
105
+ * Note: null is treated like 0 when compared with numbers or Date, but in general
106
+ * null is not ordered with respect to strings (neither greater nor less), and
107
+ * undefined is not ordered with other types.
104
108
*/
105
109
export function simpleComparator ( a : string , b :string ) : number ;
106
110
export function simpleComparator ( a : number | null , b :number | null ) : number ;
107
111
export function simpleComparator ( a : Date | null , b :Date | null ) : number ;
112
+ export function simpleComparator ( a : ( number | string ) [ ] , b :( number | string ) [ ] ) : number ;
108
113
export function simpleComparator ( a : any , b : any ) : number {
109
114
return a > b ? 1 : a < b ? - 1 : 0 ;
110
115
} ;
@@ -848,8 +853,12 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
848
853
849
854
/** Ensures mutations are allowed, reversing the effect of freeze(). */
850
855
unfreeze ( ) {
856
+ // @ts -ignore "The operand of a 'delete' operator must be optional."
857
+ // (wrong: delete does not affect the prototype.)
851
858
delete this . clear ;
859
+ // @ts -ignore
852
860
delete this . set ;
861
+ // @ts -ignore
853
862
delete this . editRange ;
854
863
}
855
864
@@ -907,7 +916,6 @@ class BNode<K,V> {
907
916
// If key not found, returns i^failXor where i is the insertion index.
908
917
// Callers that don't care whether there was a match will set failXor=0.
909
918
indexOf ( key : K , failXor : number , cmp : ( a :K , b :K ) => number ) : index {
910
- // TODO: benchmark multiple search strategies
911
919
const keys = this . keys ;
912
920
var lo = 0 , hi = keys . length , mid = hi >> 1 ;
913
921
while ( lo < hi ) {
0 commit comments