From 0e22703b47fc1ac828961b4f84873752ed50f355 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Tue, 16 Jan 2018 16:28:07 +0800 Subject: [PATCH 1/5] clean up mapm API --- jscomp/others/.depend | 4 +- jscomp/others/bs_MapM.ml | 112 +++++--- jscomp/others/bs_MapM.mli | 76 +++--- jscomp/others/bs_SetM.ml | 315 +++++++++++------------ jscomp/others/bs_SetM.mli | 87 +++---- jscomp/others/bs_internalAVLtree.ml | 38 ++- jscomp/others/bs_internalAVLtree.mli | 14 +- lib/js/bs_MapM.js | 107 +++++--- lib/js/bs_SetM.js | 371 +++++++++++++-------------- lib/js/bs_internalAVLtree.js | 74 +++++- 10 files changed, 668 insertions(+), 530 deletions(-) diff --git a/jscomp/others/.depend b/jscomp/others/.depend index 9eed9eb613..fa6f8469d1 100644 --- a/jscomp/others/.depend +++ b/jscomp/others/.depend @@ -61,8 +61,8 @@ bs_Set.cmj : bs_internalAVLset.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj \ bs_Set.cmi bs_SetM.cmj : bs_internalAVLset.cmj bs_Sort.cmj bs_Cmp.cmj bs_BagM.cmj \ bs_Array.cmj bs_SetM.cmi -bs_MapM.cmj : bs_internalAVLtree.cmj bs_Cmp.cmj bs_BagM.cmj bs_Array.cmj \ - bs_MapM.cmi +bs_MapM.cmj : bs_internalAVLtree.cmj bs_Sort.cmj bs_Cmp.cmj bs_BagM.cmj \ + bs_Array.cmj bs_MapM.cmi bs_internalSetInt.cmj : bs_internalAVLset.cmj bs_SortInt.cmj bs_Array.cmj bs_internalSetString.cmj : bs_internalAVLset.cmj bs_SortString.cmj \ bs_Array.cmj diff --git a/jscomp/others/bs_MapM.ml b/jscomp/others/bs_MapM.ml index 34d3fec12b..a3ef3a711e 100644 --- a/jscomp/others/bs_MapM.ml +++ b/jscomp/others/bs_MapM.ml @@ -27,11 +27,12 @@ module N = Bs_internalAVLtree module B = Bs_BagM module A = Bs_Array +module S = Bs_Sort type ('k, 'v, 'id) t = (('k,'id) Bs_Cmp.t, ('k,'v) N.t0 ) B.bag -let rec removeMutateAux ~cmp nt x = +let rec removeMutateAux nt x ~cmp = let k = N.key nt in let c = (Bs_Cmp.getCmp cmp) x k [@bs] in if c = 0 then @@ -68,21 +69,49 @@ let removeOnly (type elt) (type id) (d : (elt,_,id) t) k = let newRoot = removeMutateAux ~cmp:M.cmp oldRoot2 k in if newRoot != oldRoot then B.dataSet d newRoot + let remove d v = removeOnly d v; d + +let rec removeArrayMutateAux t xs i len ~cmp = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeMutateAux t ele ~cmp in + match N.toOpt u with + | None -> N.empty0 + | Some t -> removeArrayMutateAux t xs (i+1) len ~cmp + else N.return t + +let removeArrayOnly (type elt) (type id) (d : (elt,_,id) t) xs = + let oldRoot = B.data d in + match N.toOpt oldRoot with + | None -> () + | Some nt -> + let len = A.length xs in + let dict = B.dict d in + let module M = (val dict) in + let newRoot = removeArrayMutateAux nt xs 0 len ~cmp:M.cmp in + if newRoot != oldRoot then + B.dataSet d newRoot + +let removeArray d xs = + removeArrayOnly d xs; + d + let empty dict = - B.bag ~dict ~data:N.empty0 + B.bag ~dict ~data:N.empty0 let isEmpty d = N.isEmpty0 (B.data d) let singleton dict x v= B.bag ~data:(N.singleton0 x v) ~dict +let minKeyOpt m = N.minKeyOpt0 (B.data m) +let minKeyNull m = N.minKeyNull0 (B.data m) let minKVOpt m = N.minKVOpt0 (B.data m) let minKVNull m = N.minKVNull0 (B.data m) let maxKVOpt m = N.maxKVOpt0 (B.data m) let maxKVNull m = N.maxKVNull0 (B.data m) - let iter d f = N.iter0 (B.data d) f let fold d acc cb = @@ -91,7 +120,6 @@ let forAll d p = N.forAll0 (B.data d) p let exists d p = N.exists0 (B.data d) p - let length d = N.length0 (B.data d) let toList d = @@ -101,68 +129,68 @@ let toArray d = let ofSortedArrayUnsafe ~dict xs : _ t = B.bag ~data:(N.ofSortedArrayUnsafe0 xs) ~dict let checkInvariant d = - N.checkInvariant (B.data d) - -let addOnly (type elt) (type id) (m : (elt,_,id) t) e v = - let dict, oldRoot = B.(dict m, data m) in - let module M = (val dict) in - let newRoot = N.addMutate ~cmp:M.cmp oldRoot e v in - if newRoot != oldRoot then - B.dataSet m newRoot - -let add m e v = - addOnly m e v; - m - -let ofArray (type k) (type id) (dict : (k,id) Bs_Cmp.t) data = - let module M = (val dict ) in - B.bag - ~dict - ~data:(N.ofArray0 ~cmp:M.cmp data) - -let cmp (type k) (type id) - (m1 : (k,'v,id) t) (m2 : (k,'v,id) t) - cmp - = + N.checkInvariant (B.data d) +let cmp (type k) (type id) (m1 : (k,'v,id) t) (m2 : (k,'v,id) t) cmp = let dict, m1_data, m2_data = B.(dict m1, data m1, data m2) in - let module X = (val dict) in - N.cmp0 ~kcmp:X.cmp ~vcmp:cmp m1_data m2_data - -let eq (type k) (type id) - (m1 : (k,'v,id) t) (m2 : (k,'v,id) t) cmp = + let module X = (val dict) in + N.cmp0 ~kcmp:X.cmp ~vcmp:cmp m1_data m2_data +let eq (type k) (type id) (m1 : (k,'v,id) t) (m2 : (k,'v,id) t) cmp = let dict, m1_data, m2_data = B.(dict m1, data m1, data m2) in - let module X = (val dict) in - N.eq0 ~kcmp:X.cmp ~vcmp:cmp m1_data m2_data - + let module M = (val dict) in + N.eq0 ~kcmp:M.cmp ~vcmp:cmp m1_data m2_data let map m f = let dict, map = B.(dict m, data m) in B.bag ~dict ~data:(N.map0 map f) - let mapi map f = let dict,map = B.(dict map, data map) in B.bag ~dict ~data:(N.mapi0 map f) - let findOpt (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findOpt0 ~cmp:X.cmp map x - let findNull (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findNull0 ~cmp:X.cmp map x - let findWithDefault (type k) (type id) (map : (k,_,id) t) x def = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findWithDefault0 ~cmp:X.cmp map x def - let findExn (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findExn0 ~cmp:X.cmp map x - let mem (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in - N.mem0 ~cmp:X.cmp map x \ No newline at end of file + N.mem0 ~cmp:X.cmp map x +let ofArray (type k) (type id) (dict : (k,id) Bs_Cmp.t) data = + let module M = (val dict ) in + B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) +let updateOnly (type elt) (type id) (m : (elt,_,id) t) e v = + let dict, oldRoot = B.(dict m, data m) in + let module M = (val dict) in + let newRoot = N.updateMutate ~cmp:M.cmp oldRoot e v in + if newRoot != oldRoot then + B.dataSet m newRoot +let update m e v = + updateOnly m e v; + m +let updateArrayMutate t xs ~cmp = + let v = ref t in + for i = 0 to A.length xs - 1 do + let key,value = A.unsafe_get xs i in + v := N.updateMutate !v key value ~cmp + done; + !v +let updateArrayOnly (type elt) (type id) (d : (elt,_,id) t ) xs = + let dict = B.dict d in + let oldRoot = B.data d in + let module M = (val dict) in + let newRoot = updateArrayMutate oldRoot xs ~cmp:M.cmp in + if newRoot != oldRoot then + B.dataSet d newRoot +let updateArray d xs = + updateArrayOnly d xs ; + d + diff --git a/jscomp/others/bs_MapM.mli b/jscomp/others/bs_MapM.mli index 3e5b55fc2d..8efdbcf914 100644 --- a/jscomp/others/bs_MapM.mli +++ b/jscomp/others/bs_MapM.mli @@ -26,26 +26,28 @@ type ('k,'v,'id) t val empty: ('k, 'id) Bs_Cmp.t -> ('k, 'a, 'id) t - val ofArray: ('k,'id) Bs_Cmp.t -> ('k * 'a) array -> - ('k,'a,'id) t - + ('k,'a,'id) t val isEmpty: ('k, 'a, 'id) t -> bool -val mem: - ('k, 'a, 'id) t -> 'k -> bool +val singleton: + ('k,'id) Bs_Cmp.t -> + 'k -> 'a -> ('k, 'a, 'id) t +val mem: ('k, _, _) t -> 'k -> bool + +val updateOnly: ('k, 'a, 'id) t -> 'k -> 'a -> unit +val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t +(** [update m x y ] do the in-place modification, returnning [m] for chaining. *) -val add: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t -(** [add m x y ] do the in-place modification, - returnning [m] for chaining. *) -val singleton: ('k,'id) Bs_Cmp.t -> - 'k -> 'a -> ('k, 'a, 'id) t +val removeOnly: ('k, 'a, 'id) t -> 'k -> unit val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t (** [remove m x] do the in-place modification, returnning [m] for chaining. *) +val removeArrayOnly: ('k, 'a, 'id) t -> 'k array -> unit +val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t (* val merge: *) @@ -60,12 +62,10 @@ val cmp: ('k, 'a, 'id) t -> ('a -> 'a -> int [@bs]) -> int - - val eq: ('k, 'a, 'id) t -> ('k, 'a, 'id) t -> ('a -> 'a -> bool [@bs]) -> bool -(** [eq m1 m2 cmp] tests whether the maps [m1] and [m2] are +(** [eq m1 m2 eqf] tests whether the maps [m1] and [m2] are equal, that is, contain equal keys and associate them with - equal data. [cmp] is the equality predicate used to compare + equal data. [eqf] is the equality predicate used to compare the data associated with the keys. *) val iter: ('k, 'a, 'id) t -> ('k -> 'a -> unit [@bs]) -> unit @@ -89,19 +89,6 @@ val exists: ('k, 'a, 'id) t -> ('k -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. *) - -(* val filter: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t *) -(** [filter p m] returns the map with all the bindings in [m] - that satisfy predicate [p]. -*) - -(* val partition: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * ('k, 'a, 'id) t *) -(** [partition p m] returns a pair of maps [(m1, m2)], where - [m1] contains all the bindings of [s] that satisfy the - predicate [p], and [m2] is the map with all the bindings of - [s] that do not satisfy [p]. -*) - val length: ('k, 'a, 'id) t -> int @@ -113,17 +100,6 @@ val minKVOpt: ('k, 'a, _) t -> ('k * 'a) option val minKVNull: ('k, 'a, _) t -> ('k * 'a) Js.null val maxKVOpt: ('k, 'a, _) t -> ('k * 'a) option val maxKVNull:('k, 'a, _) t -> ('k * 'a) Js.null - -(* val split: 'k -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * 'a option * ('k, 'a, 'id) t *) -(** [split x m] returns a triple [(l, data, r)], where - [l] is the map with all the bindings of [m] whose 'k - is strictly less than [x]; - [r] is the map with all the bindings of [m] whose 'k - is strictly greater than [x]; - [data] is [None] if [m] contains no binding for [x], - or [Some v] if [m] binds [v] to [x]. -*) - val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null val findWithDefault: @@ -139,3 +115,27 @@ val map: ('k, 'a, 'id) t -> ('a -> 'b [@bs]) -> ('k ,'b,'id ) t val mapi: ('k, 'a, 'id) t -> ('k -> 'a -> 'b [@bs]) -> ('k, 'b, 'id) t + +(* val filter: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t *) +(** [filter p m] returns the map with all the bindings in [m] + that satisfy predicate [p]. +*) + +(* val partition: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * ('k, 'a, 'id) t *) +(** [partition p m] returns a pair of maps [(m1, m2)], where + [m1] contains all the bindings of [s] that satisfy the + predicate [p], and [m2] is the map with all the bindings of + [s] that do not satisfy [p]. +*) + + +(* val split: 'k -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * 'a option * ('k, 'a, 'id) t *) +(** [split x m] returns a triple [(l, data, r)], where + [l] is the map with all the bindings of [m] whose 'k + is strictly less than [x]; + [r] is the map with all the bindings of [m] whose 'k + is strictly greater than [x]; + [data] is [None] if [m] contains no binding for [x], + or [Some v] if [m] binds [v] to [x]. +*) + diff --git a/jscomp/others/bs_SetM.ml b/jscomp/others/bs_SetM.ml index e316c0ffbe..3f8d1eeb1e 100644 --- a/jscomp/others/bs_SetM.ml +++ b/jscomp/others/bs_SetM.ml @@ -2,13 +2,14 @@ module N = Bs_internalAVLset module B = Bs_BagM module A = Bs_Array +module S = Bs_Sort type ('k,'id) t0 = 'k N.t0 type ('elt,'id) t = (('elt,'id) Bs_Cmp.t , ('elt,'id) t0) B.bag -let rec removeMutateAux ~cmp nt x = +let rec removeMutateAux nt x ~cmp = let k = N.key nt in let c = (Bs_Cmp.getCmp cmp) x k [@bs] in if c = 0 then @@ -36,33 +37,19 @@ let rec removeMutateAux ~cmp nt x = N.return (N.balMutate nt) end -let addArrayMutate (t : _ t0) xs ~cmp = - let v = ref t in - for i = 0 to A.length xs - 1 do - v := N.addMutate !v (A.unsafe_get xs i) ~cmp - done; - !v - - -let rec addMutateCheckAux (t : _ t0) x added ~cmp = - match N.toOpt t with - | None -> - added := true; - N.singleton0 x - | Some nt -> - let k = N.key nt in - let c = (Bs_Cmp.getCmp cmp) x k [@bs] in - if c = 0 then t - else - let l, r = N.(left nt, right nt) in - (if c < 0 then - let ll = addMutateCheckAux ~cmp l x added in - N.leftSet nt ll - else - N.rightSet nt (addMutateCheckAux ~cmp r x added ); - ); - N.return (N.balMutate nt) +let removeOnly (type elt) (type id) (d : (elt,id) t) v = + let dict, oldRoot = B.(dict d, data d) in + let module M = (val dict) in + match N.toOpt oldRoot with + | None -> () + | Some oldRoot2 -> + let newRoot = removeMutateAux ~cmp:M.cmp oldRoot2 v in + if newRoot != oldRoot then + B.dataSet d newRoot +let remove d v = + removeOnly d v; + d let rec removeArrayMutateAux t xs i len ~cmp = if i < len then @@ -73,13 +60,22 @@ let rec removeArrayMutateAux t xs i len ~cmp = | Some t -> removeArrayMutateAux t xs (i+1) len ~cmp else N.return t -let removeArrayMutate (t : _ t0) xs ~cmp = - match N.toOpt t with - | None -> t +let removeArrayOnly (type elt) (type id) (d : (elt,id) t) xs = + let oldRoot = B.data d in + match N.toOpt oldRoot with + | None -> () | Some nt -> let len = A.length xs in - removeArrayMutateAux nt xs 0 len ~cmp + let dict = B.dict d in + let module M = (val dict) in + let newRoot = removeArrayMutateAux nt xs 0 len ~cmp:M.cmp in + if newRoot != oldRoot then + B.dataSet d newRoot +let removeArray d xs = + removeArrayOnly d xs; + d + let rec removeMutateCheckAux nt x removed ~cmp= let k = N.key nt in let c = (Bs_Cmp.getCmp cmp) x k [@bs] in @@ -108,9 +104,77 @@ let rec removeMutateCheckAux nt x removed ~cmp= N.rightSet nt (removeMutateCheckAux ~cmp r x removed); N.return (N.balMutate nt) end -module S = Bs_Sort + +let removeCheck (type elt) (type id) (d : (elt,id) t) v = + let dict, oldRoot = B.(dict d, data d) in + let module M = (val dict) in + match N.toOpt oldRoot with + | None -> false + | Some oldRoot2 -> + let removed = ref false in + let newRoot = removeMutateCheckAux ~cmp:M.cmp oldRoot2 v removed in + if newRoot != oldRoot then + B.dataSet d newRoot ; + !removed + + + +let rec addMutateCheckAux (t : _ t0) x added ~cmp = + match N.toOpt t with + | None -> + added := true; + N.singleton0 x + | Some nt -> + let k = N.key nt in + let c = (Bs_Cmp.getCmp cmp) x k [@bs] in + if c = 0 then t + else + let l, r = N.(left nt, right nt) in + (if c < 0 then + let ll = addMutateCheckAux ~cmp l x added in + N.leftSet nt ll + else + N.rightSet nt (addMutateCheckAux ~cmp r x added ); + ); + N.return (N.balMutate nt) + + + +let split (type elt) (type id) (d : (elt,id) t) key = + let dict, s = B.dict d, B.data d in + let module M = (val dict ) in + let arr = N.toArray0 s in + let i = S.binSearch arr key (Bs_Cmp.getCmp M.cmp) in + let len = A.length arr in + if i < 0 then + let next = - i -1 in + (B.bag + ~data:(N.ofSortedArrayAux arr 0 next) + ~dict + , + B.bag + ~data:(N.ofSortedArrayAux arr next (len - next)) + ~dict + ), false + else + (B.bag + ~data:(N.ofSortedArrayAux arr 0 i) + ~dict, + B.bag + ~data:(N.ofSortedArrayAux arr (i+1) (len - i - 1)) + ~dict + ), true + +let filter d p = + let data, dict = B.(data d, dict d) in + B.bag ~data:(N.filterCopy data p ) ~dict +let partition d p = + let data, dict = B.(data d, dict d) in + let a , b = N.partitionCopy data p in + B.bag ~data:a ~dict, B.bag ~data:b ~dict + let empty dict = B.bag ~dict ~data:N.empty0 let isEmpty d = @@ -133,39 +197,6 @@ let forAll d p = N.forAll0 (B.data d) p let exists d p = N.exists0 (B.data d) p - -let split (type elt) (type id) (d : (elt,id) t) key = - let dict, s = B.dict d, B.data d in - let module M = (val dict ) in - let arr = N.toArray0 s in - let i = S.binSearch arr key (Bs_Cmp.getCmp M.cmp) in - let len = A.length arr in - if i < 0 then - let next = - i -1 in - (B.bag - ~data:(N.ofSortedArrayAux arr 0 next) - ~dict - , - B.bag - ~data:(N.ofSortedArrayAux arr next (len - next)) - ~dict - ), false - else - (B.bag - ~data:(N.ofSortedArrayAux arr 0 i) - ~dict, - B.bag - ~data:(N.ofSortedArrayAux arr (i+1) (len - i - 1)) - ~dict - ), true - -let filter d p = - let data, dict = B.(data d, dict d) in - B.bag ~data:(N.filterCopy data p ) ~dict -let partition d p = - let data, dict = B.(data d, dict d) in - let a , b = N.partitionCopy data p in - B.bag ~data:a ~dict, B.bag ~data:b ~dict let length d = N.length0 (B.data d) let toList d = @@ -176,19 +207,38 @@ let ofSortedArrayUnsafe ~dict xs : _ t = B.bag ~data:(N.ofSortedArrayUnsafe0 xs) ~dict let checkInvariant d = N.checkInvariant (B.data d) - - +let cmp (type elt) (type id) (d0 : (elt,id) t) d1 = + let dict = B.dict d0 in + let module M = (val dict) in + N.cmp0 ~cmp:M.cmp (B.data d0) (B.data d1) +let eq (type elt) (type id) (d0 : (elt,id) t) d1 = + let dict = B.dict d0 in + let module M = (val dict) in + N.eq0 ~cmp:M.cmp (B.data d0) (B.data d1) +let findOpt (type elt) (type id) (d : (elt,id) t) x = + let dict = B.dict d in + let module M = (val dict) in + N.findOpt0 ~cmp:M.cmp (B.data d) x +let findNull (type elt) (type id) (d : (elt,id) t) x = + let dict = B.dict d in + let module M = (val dict) in + N.findNull0 ~cmp:M.cmp (B.data d) x +let mem (type elt) (type id) (d : (elt,id) t) x = + let dict = B.dict d in + let module M = (val dict) in + N.mem0 ~cmp:M.cmp (B.data d) x +let ofArray (type elt) (type id) (dict : (elt,id) Bs_Cmp.t) data = + let module M = (val dict) in + B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) let addOnly (type elt) (type id) (m : (elt,id) t) e = let dict, oldRoot = B.(dict m, data m) in let module M = (val dict) in let newRoot = N.addMutate ~cmp:M.cmp oldRoot e in if newRoot != oldRoot then B.dataSet m newRoot - let add m e = addOnly m e; m - let addCheck (type elt) (type id) (m : (elt,id) t) e = let dict, oldRoot = B.(dict m, data m) in let module M = (val dict) in @@ -196,9 +246,13 @@ let addCheck (type elt) (type id) (m : (elt,id) t) e = let newRoot = addMutateCheckAux ~cmp:M.cmp oldRoot e added in if newRoot != oldRoot then B.dataSet m newRoot; - !added - - + !added +let addArrayMutate (t : _ t0) xs ~cmp = + let v = ref t in + for i = 0 to A.length xs - 1 do + v := N.addMutate !v (A.unsafe_get xs i) ~cmp + done; + !v let addArrayOnly (type elt) (type id) (d : (elt,id) t ) xs = let dict = B.dict d in let oldRoot = B.data d in @@ -206,72 +260,14 @@ let addArrayOnly (type elt) (type id) (d : (elt,id) t ) xs = let newRoot = addArrayMutate oldRoot xs ~cmp:M.cmp in if newRoot != oldRoot then B.dataSet d newRoot - let addArray d xs = addArrayOnly d xs ; d -let removeArrayOnly (type elt) (type id) (d : (elt,id) t) xs = - let dict, oldRoot = B.dict d, B.data d in - let module M = (val dict) in - let newRoot = removeArrayMutate oldRoot xs ~cmp:M.cmp in - if newRoot != oldRoot then - B.dataSet d newRoot - -let removeArray d xs = - removeArrayOnly d xs; - d -let removeOnly (type elt) (type id) (d : (elt,id) t) v = - let dict, oldRoot = B.(dict d, data d) in - let module M = (val dict) in - match N.toOpt oldRoot with - | None -> () - | Some oldRoot2 -> - let newRoot = removeMutateAux ~cmp:M.cmp oldRoot2 v in - if newRoot != oldRoot then - B.dataSet d newRoot - -let removeCheck (type elt) (type id) (d : (elt,id) t) v = - let dict, oldRoot = B.(dict d, data d) in - let module M = (val dict) in - match N.toOpt oldRoot with - | None -> false - | Some oldRoot2 -> - let removed = ref false in - let newRoot = removeMutateCheckAux ~cmp:M.cmp oldRoot2 v removed in - if newRoot != oldRoot then - B.dataSet d newRoot ; - !removed - -let remove d v = - removeOnly d v; - d - -let cmp (type elt) (type id) (d0 : (elt,id) t) d1 = - let dict = B.dict d0 in - let module M = (val dict) in - N.cmp0 ~cmp:M.cmp (B.data d0) (B.data d1) -let eq (type elt) (type id) (d0 : (elt,id) t) d1 = - let dict = B.dict d0 in - let module M = (val dict) in - N.eq0 ~cmp:M.cmp (B.data d0) (B.data d1) -let findOpt (type elt) (type id) (d : (elt,id) t) x = - let dict = B.dict d in - let module M = (val dict) in - N.findOpt0 ~cmp:M.cmp (B.data d) x - -let findNull (type elt) (type id) (d : (elt,id) t) x = - let dict = B.dict d in - let module M = (val dict) in - N.findNull0 ~cmp:M.cmp (B.data d) x - -let ofArray (type elt) (type id) (dict : (elt,id) Bs_Cmp.t) data = - let module M = (val dict) in - B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) let subset (type elt) (type id) (a : (elt,id) t) b = @@ -287,34 +283,34 @@ let inter (type elt) (type id) (a : (elt,id) t) b : _ t = | _, None -> empty dict | Some dataa0, Some datab0 -> let sizea, sizeb = - N.lengthNode dataa0, N.lengthNode datab0 in + N.lengthNode dataa0, N.lengthNode datab0 in let totalSize = sizea + sizeb in let tmp = A.makeUninitializedUnsafe totalSize in ignore @@ N.fillArray dataa0 0 tmp ; ignore @@ N.fillArray datab0 sizea tmp; let p = Bs_Cmp.getCmp M.cmp in if (p (A.unsafe_get tmp (sizea - 1)) - (A.unsafe_get tmp sizea) [@bs] < 0) - || - (p - (A.unsafe_get tmp (totalSize - 1)) - (A.unsafe_get tmp 0) [@bs] < 0 - ) - then empty dict + (A.unsafe_get tmp sizea) [@bs] < 0) + || + (p + (A.unsafe_get tmp (totalSize - 1)) + (A.unsafe_get tmp 0) [@bs] < 0 + ) + then empty dict else - let tmp2 = A.makeUninitializedUnsafe (min sizea sizeb) in - let k = S.inter tmp 0 sizea tmp sizea sizeb tmp2 0 p in - B.bag ~data:(N.ofSortedArrayAux tmp2 0 k) - ~dict + let tmp2 = A.makeUninitializedUnsafe (min sizea sizeb) in + let k = S.inter tmp 0 sizea tmp sizea sizeb tmp2 0 p in + B.bag ~data:(N.ofSortedArrayAux tmp2 0 k) + ~dict let diff (type elt) (type id) (a : (elt,id) t) b : _ t = let dict, dataa, datab = B.dict a, B.data a, B.data b in let module M = (val dict) in match N.toOpt dataa, N.toOpt datab with | None, _ -> empty dict | _, None -> - B.bag ~data:(N.copy dataa) ~dict + B.bag ~data:(N.copy dataa) ~dict | Some dataa0, Some datab0 - -> + -> let sizea, sizeb = N.lengthNode dataa0, N.lengthNode datab0 in let totalSize = sizea + sizeb in let tmp = A.makeUninitializedUnsafe totalSize in @@ -322,17 +318,17 @@ let diff (type elt) (type id) (a : (elt,id) t) b : _ t = ignore @@ N.fillArray datab0 sizea tmp; let p = Bs_Cmp.getCmp M.cmp in if (p (A.unsafe_get tmp (sizea - 1)) - (A.unsafe_get tmp sizea) [@bs] < 0) - || - (p - (A.unsafe_get tmp (totalSize - 1)) - (A.unsafe_get tmp 0) [@bs] < 0 - ) - then B.bag ~data:(N.copy dataa) ~dict + (A.unsafe_get tmp sizea) [@bs] < 0) + || + (p + (A.unsafe_get tmp (totalSize - 1)) + (A.unsafe_get tmp 0) [@bs] < 0 + ) + then B.bag ~data:(N.copy dataa) ~dict else - let tmp2 = A.makeUninitializedUnsafe sizea in - let k = S.diff tmp 0 sizea tmp sizea sizeb tmp2 0 p in - B.bag ~data:(N.ofSortedArrayAux tmp2 0 k) ~dict + let tmp2 = A.makeUninitializedUnsafe sizea in + let k = S.diff tmp 0 sizea tmp sizea sizeb tmp2 0 p in + B.bag ~data:(N.ofSortedArrayAux tmp2 0 k) ~dict let union (type elt) (type id) (a : (elt,id) t) b = let dict, dataa, datab = B.dict a, B.data a, B.data b in @@ -349,16 +345,11 @@ let union (type elt) (type id) (a : (elt,id) t) b = ignore @@ N.fillArray datab0 sizea tmp ; let p = (Bs_Cmp.getCmp M.cmp) in if p - (A.unsafe_get tmp (sizea - 1)) - (A.unsafe_get tmp sizea) [@bs] < 0 then + (A.unsafe_get tmp (sizea - 1)) + (A.unsafe_get tmp sizea) [@bs] < 0 then B.bag ~data:(N.ofSortedArrayAux tmp 0 totalSize) ~dict else let tmp2 = A.makeUninitializedUnsafe totalSize in let k = S.union tmp 0 sizea tmp sizea sizeb tmp2 0 p in B.bag ~data:(N.ofSortedArrayAux tmp2 0 k) ~dict - - -let mem (type elt) (type id) (d : (elt,id) t) x = - let dict = B.dict d in - let module M = (val dict) in - N.mem0 ~cmp:M.cmp (B.data d) x + diff --git a/jscomp/others/bs_SetM.mli b/jscomp/others/bs_SetM.mli index c8acea1c6d..53fac752b5 100644 --- a/jscomp/others/bs_SetM.mli +++ b/jscomp/others/bs_SetM.mli @@ -27,81 +27,80 @@ type ('k,'id) t val empty : ('elt, 'id) Bs_Cmp.t -> ('elt, 'id) t - -val ofArray: ('k, 'id) Bs_Cmp.t -> 'k array -> ('k, 'id) t - +val ofArray: + ('k, 'id) Bs_Cmp.t -> + 'k array -> + ('k, 'id) t val isEmpty : _ t -> bool - +val singleton : + ('elt,'id) Bs_Cmp.t -> + 'elt -> ('elt, 'id) t val mem: ('elt, _) t -> 'elt -> bool val addOnly: ('elt, 'id) t -> 'elt -> unit - val add: ('elt, 'id) t -> 'elt -> ('elt, 'id) t - val addCheck: ('elt, 'id) t -> 'elt -> bool - val addArrayOnly: ('elt, 'id) t -> 'elt array -> unit - val addArray: ('elt, 'id) t -> 'elt array -> ('elt, 'id) t -val removeArrayOnly: - ('elt, 'id) t -> 'elt array -> unit - -val removeArray: - ('elt, 'id) t -> 'elt array -> ('elt, 'id) t - -val singleton : - ('elt,'id) Bs_Cmp.t -> - 'elt -> ('elt, 'id) t -(** [singleton x] returns the one-element set containing only [x]. *) - val removeOnly: ('elt, 'id) t -> 'elt -> unit - val remove: ('elt, 'id) t -> 'elt -> ('elt, 'id) t - val removeCheck: ('elt, 'id) t -> 'elt -> bool -(* [b = removeCheck s e] [b] is true means one element removed *) - - + (* [b = removeCheck s e] [b] is true means one element removed *) +val removeArrayOnly: + ('elt, 'id) t -> 'elt array -> unit +val removeArray: + ('elt, 'id) t -> 'elt array -> ('elt, 'id) t + val union: ('elt, 'id) t -> ('elt, 'id) t -> ('elt, 'id) t - val inter: ('elt, 'id) t -> ('elt, 'id) t -> ('elt, 'id) t - val diff: ('elt, 'id) t -> ('elt, 'id) t -> ('elt, 'id) t - val subset: ('elt, 'id) t -> ('elt, 'id) t -> bool val cmp: ('elt, 'id) t -> ('elt, 'id) t -> int -(** Total ordering between sets. Can be used as the ordering function - for doing sets of sets. *) val eq: ('elt, 'id) t -> ('elt, 'id) t -> bool val iter: ('elt, 'id) t -> ('elt -> unit [@bs]) -> unit -(** [iter s f] applies [f] in turn to all elements of [s]. +(** [iter m f] applies [f] in turn to all elements of [m]. In increasing order *) val fold: ('elt, 'id) t -> 'a -> ('a -> 'elt -> 'a [@bs]) -> 'a (** In increasing order. *) val forAll: ('elt, 'id) t -> ('elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set satisfy the predicate [p]. Order unspecified *) - val exists: ('elt, 'id) t -> ('elt -> bool [@bs]) -> bool (** [exists p s] checks if at least one element of the set satisfies the predicate [p]. *) + +val length: ('elt, 'id) t -> int +val toList: ('elt, 'id) t -> 'elt list +(** In increasing order*) +val toArray: ('elt, 'id) t -> 'elt array + +val minOpt: ('elt, 'id) t -> 'elt option +val minNull: ('elt, 'id) t -> 'elt Js.null +val maxOpt: ('elt, 'id) t -> 'elt option +val maxNull: ('elt, 'id) t -> 'elt Js.null + +val findOpt: + ('elt, 'id) t -> 'elt -> 'elt option +val findNull: + ('elt, 'id) t -> 'elt -> 'elt Js.null + val filter: ('elt, 'id) t -> ('elt -> bool [@bs]) -> ('elt, 'id) t (** [filter p s] returns the set of all elements in [s] that satisfy predicate [p]. *) @@ -111,18 +110,10 @@ val partition: ('elt, 'id) t -> ('elt -> bool [@bs]) -> ('elt, 'id) t * ('elt, predicate [p], and [s2] is the set of all the elements of [s] that do not satisfy [p]. *) -val length: ('elt, 'id) t -> int - -val toList: ('elt, 'id) t -> 'elt list -(** In increasing order*) -val toArray: ('elt, 'id) t -> 'elt array + -val minOpt: ('elt, 'id) t -> 'elt option -val minNull: ('elt, 'id) t -> 'elt Js.null -val maxOpt: ('elt, 'id) t -> 'elt option -val maxNull: ('elt, 'id) t -> 'elt Js.null val split: ('elt, 'id) t -> 'elt -> (('elt, 'id) t * ('elt, 'id) t) * bool (** [split s x] returns a triple [((l, r), present)], where @@ -139,22 +130,6 @@ val ofSortedArrayUnsafe: dict:('elt, 'id) Bs_Cmp.t -> 'elt array -> ('elt,'id) t -val findOpt: - ('elt, 'id) t -> 'elt -> 'elt option -val findNull: - ('elt, 'id) t -> 'elt -> 'elt Js.null -(** [findOpt s ele] - return the element in the collection - which is semantically equal to it - *) - -(* No need - could be made use of by - [Js.assertNonNull (findNull s x)] - *) -(* val findAssert: - ('elt, 'id) t -> 'elt -> 'elt *) - (* [add0] was not exposed for various reasons: 1. such api is dangerious diff --git a/jscomp/others/bs_internalAVLtree.ml b/jscomp/others/bs_internalAVLtree.ml index 0ac1e1d83c..15d451fe65 100644 --- a/jscomp/others/bs_internalAVLtree.ml +++ b/jscomp/others/bs_internalAVLtree.ml @@ -89,6 +89,36 @@ let bal l x d r = return @@ node ~left:l ~key:x ~value:d ~right:r ~h:(if hl >= hr then hl + 1 else hr + 1) +let rec minKey0Aux n = + match toOpt (left n) with + | None -> key n + | Some n -> minKey0Aux n + +let minKeyOpt0 n = + match toOpt n with + | None -> None + | Some n -> Some (minKey0Aux n) + +let minKeyNull0 n = + match toOpt n with + | None -> Js.null + | Some n -> return (minKey0Aux n) + +let rec maxKey0Aux n = + match toOpt (right n) with + | None -> key n + | Some n -> maxKey0Aux n + +let maxKeyOpt0 n = + match toOpt n with + | None -> None + | Some n -> Some (maxKey0Aux n) + +let maxKeyNull0 n = + match toOpt n with + | None -> Js.null + | Some n -> return (maxKey0Aux n) + let rec minKV0Aux n = match toOpt (left n) with | None -> key n , value n @@ -639,7 +669,7 @@ let balMutate nt = nt end -let rec addMutate ~cmp (t : _ t0) x data = +let rec updateMutate (t : _ t0) x data ~cmp = match toOpt t with | None -> singleton0 x data | Some nt -> @@ -653,10 +683,10 @@ let rec addMutate ~cmp (t : _ t0) x data = else let l, r = (left nt, right nt) in (if c < 0 then - let ll = addMutate ~cmp l x data in + let ll = updateMutate ~cmp l x data in leftSet nt ll else - rightSet nt (addMutate ~cmp r x data); + rightSet nt (updateMutate ~cmp r x data); ); return (balMutate nt) @@ -680,7 +710,7 @@ let ofArray0 ~cmp (xs : _ array) = ) in for i = !next to len - 1 do let k, v = (A.unsafe_get xs i) in - result := addMutate ~cmp !result k v + result := updateMutate ~cmp !result k v done ; !result diff --git a/jscomp/others/bs_internalAVLtree.mli b/jscomp/others/bs_internalAVLtree.mli index d84627fa0c..6c51c51bee 100644 --- a/jscomp/others/bs_internalAVLtree.mli +++ b/jscomp/others/bs_internalAVLtree.mli @@ -46,8 +46,16 @@ val bal : val singleton0 : 'a -> 'b -> ('a,'b) t0 val updateKV : ('k, 'v) node -> 'k -> 'v -> ('k,'v) t0 -val minKVOpt0 : ('a,'b) t0 -> ('a * 'b) option + +val minKeyOpt0 : ('a, 'b) t0 -> 'a option +val minKeyNull0: ('a, 'b) t0 -> 'a Js.null + +val maxKeyOpt0 : ('a, 'b) t0 -> 'a option +val maxKeyNull0 : ('a, 'b) t0 -> 'a Js.null + +val minKVOpt0 : ('a, 'b) t0 -> ('a * 'b) option val minKVNull0 : ('a,'b) t0 -> ('a * 'b) Js.null + val maxKVOpt0 : ('a,'b) t0 -> ('a * 'b) option val maxKVNull0 : ('a,'b) t0 -> ('a * 'b) Js.null @@ -150,9 +158,9 @@ val mem0: val ofArray0 : cmp:('a,'id) Bs_Cmp.cmp -> ('a * 'b) array -> ('a, 'b) t0 -val addMutate : - cmp:('a,'id) Bs_Cmp.cmp -> +val updateMutate : ('a, 'b) t0 -> 'a -> 'b -> + cmp:('a,'id) Bs_Cmp.cmp -> ('a, 'b) t0 val balMutate : diff --git a/lib/js/bs_MapM.js b/lib/js/bs_MapM.js index 7e23c7ea2e..38f5d6c857 100644 --- a/lib/js/bs_MapM.js +++ b/lib/js/bs_MapM.js @@ -2,14 +2,14 @@ var Bs_internalAVLtree = require("./bs_internalAVLtree.js"); -function removeMutateAux(cmp, nt, x) { +function removeMutateAux(nt, x, cmp) { var k = nt.key; var c = cmp(x, k); if (c) { if (c < 0) { var match = nt.left; if (match !== null) { - nt.left = removeMutateAux(cmp, match, x); + nt.left = removeMutateAux(match, x, cmp); return Bs_internalAVLtree.balMutate(nt); } else { return nt; @@ -17,7 +17,7 @@ function removeMutateAux(cmp, nt, x) { } else { var match$1 = nt.right; if (match$1 !== null) { - nt.right = removeMutateAux(cmp, match$1, x); + nt.right = removeMutateAux(match$1, x, cmp); return Bs_internalAVLtree.balMutate(nt); } else { return nt; @@ -45,7 +45,7 @@ function removeOnly(d, k) { var dict = d.dict; var oldRoot = d.data; if (oldRoot !== null) { - var newRoot = removeMutateAux(dict[/* cmp */0], oldRoot, k); + var newRoot = removeMutateAux(oldRoot, k, dict[/* cmp */0]); if (newRoot !== oldRoot) { d.data = newRoot; return /* () */0; @@ -62,6 +62,49 @@ function remove(d, v) { return d; } +function removeArrayMutateAux(_t, xs, _i, len, cmp) { + while(true) { + var i = _i; + var t = _t; + if (i < len) { + var ele = xs[i]; + var u = removeMutateAux(t, ele, cmp); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + + } else { + return Bs_internalAVLtree.empty0; + } + } else { + return t; + } + }; +} + +function removeArrayOnly(d, xs) { + var oldRoot = d.data; + if (oldRoot !== null) { + var len = xs.length; + var dict = d.dict; + var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len, dict[/* cmp */0]); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } + } else { + return /* () */0; + } +} + +function removeArray(d, xs) { + removeArrayOnly(d, xs); + return d; +} + function empty(dict) { return { dict: dict, @@ -124,30 +167,6 @@ function toArray(d) { return Bs_internalAVLtree.toArray0(d.data); } -function addOnly(m, e, v) { - var dict = m.dict; - var oldRoot = m.data; - var newRoot = Bs_internalAVLtree.addMutate(dict[/* cmp */0], oldRoot, e, v); - if (newRoot !== oldRoot) { - m.data = newRoot; - return /* () */0; - } else { - return 0; - } -} - -function add(m, e, v) { - addOnly(m, e, v); - return m; -} - -function ofArray(dict, data) { - return { - dict: dict, - data: Bs_internalAVLtree.ofArray0(dict[/* cmp */0], data) - }; -} - function cmp(m1, m2, cmp$1) { var dict = m1.dict; var m1_data = m1.data; @@ -210,13 +229,41 @@ function mem(map, x) { return Bs_internalAVLtree.mem0(map$1, x, dict[/* cmp */0]); } +function ofArray(dict, data) { + return { + dict: dict, + data: Bs_internalAVLtree.ofArray0(dict[/* cmp */0], data) + }; +} + +function updateOnly(m, e, v) { + var dict = m.dict; + var oldRoot = m.data; + var newRoot = Bs_internalAVLtree.updateMutate(oldRoot, e, v, dict[/* cmp */0]); + if (newRoot !== oldRoot) { + m.data = newRoot; + return /* () */0; + } else { + return 0; + } +} + +function update(m, e, v) { + updateOnly(m, e, v); + return m; +} + exports.empty = empty; exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.add = add; exports.singleton = singleton; +exports.mem = mem; +exports.updateOnly = updateOnly; +exports.update = update; +exports.removeOnly = removeOnly; exports.remove = remove; +exports.removeArrayOnly = removeArrayOnly; +exports.removeArray = removeArray; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; diff --git a/lib/js/bs_SetM.js b/lib/js/bs_SetM.js index 0a916c1f34..adae6a387a 100644 --- a/lib/js/bs_SetM.js +++ b/lib/js/bs_SetM.js @@ -3,14 +3,14 @@ var Bs_Sort = require("./bs_Sort.js"); var Bs_internalAVLset = require("./bs_internalAVLset.js"); -function removeMutateAux(cmp, nt, x) { +function removeMutateAux(nt, x, cmp) { var k = nt.key; var c = cmp(x, k); if (c) { if (c < 0) { var match = nt.left; if (match !== null) { - nt.left = removeMutateAux(cmp, match, x); + nt.left = removeMutateAux(match, x, cmp); return Bs_internalAVLset.balMutate(nt); } else { return nt; @@ -18,7 +18,7 @@ function removeMutateAux(cmp, nt, x) { } else { var match$1 = nt.right; if (match$1 !== null) { - nt.right = removeMutateAux(cmp, match$1, x); + nt.right = removeMutateAux(match$1, x, cmp); return Bs_internalAVLset.balMutate(nt); } else { return nt; @@ -42,68 +42,70 @@ function removeMutateAux(cmp, nt, x) { } } -function addArrayMutate(t, xs, cmp) { - var v = t; - for(var i = 0 ,i_finish = xs.length - 1 | 0; i <= i_finish; ++i){ - v = Bs_internalAVLset.addMutate(cmp, v, xs[i]); +function removeOnly(d, v) { + var dict = d.dict; + var oldRoot = d.data; + if (oldRoot !== null) { + var newRoot = removeMutateAux(oldRoot, v, dict[/* cmp */0]); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } + } else { + return /* () */0; } - return v; } -function addMutateCheckAux(t, x, added, cmp) { - if (t !== null) { - var k = t.key; - var c = cmp(x, k); - if (c) { - var l = t.left; - var r = t.right; - if (c < 0) { - var ll = addMutateCheckAux(l, x, added, cmp); - t.left = ll; +function remove(d, v) { + removeOnly(d, v); + return d; +} + +function removeArrayMutateAux(_t, xs, _i, len, cmp) { + while(true) { + var i = _i; + var t = _t; + if (i < len) { + var ele = xs[i]; + var u = removeMutateAux(t, ele, cmp); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + } else { - t.right = addMutateCheckAux(r, x, added, cmp); + return Bs_internalAVLset.empty0; } - return Bs_internalAVLset.balMutate(t); } else { return t; } - } else { - added[0] = /* true */1; - return Bs_internalAVLset.singleton0(x); - } + }; } -function removeArrayMutate(t, xs, cmp) { - if (t !== null) { +function removeArrayOnly(d, xs) { + var oldRoot = d.data; + if (oldRoot !== null) { var len = xs.length; - var _t = t; - var xs$1 = xs; - var _i = 0; - var len$1 = len; - var cmp$1 = cmp; - while(true) { - var i = _i; - var t$1 = _t; - if (i < len$1) { - var ele = xs$1[i]; - var u = removeMutateAux(cmp$1, t$1, ele); - if (u !== null) { - _i = i + 1 | 0; - _t = u; - continue ; - - } else { - return Bs_internalAVLset.empty0; - } - } else { - return t$1; - } - }; + var dict = d.dict; + var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len, dict[/* cmp */0]); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } } else { - return t; + return /* () */0; } } +function removeArray(d, xs) { + removeArrayOnly(d, xs); + return d; +} + function removeMutateCheckAux(nt, x, removed, cmp) { var k = nt.key; var c = cmp(x, k); @@ -144,54 +146,42 @@ function removeMutateCheckAux(nt, x, removed, cmp) { } } -function empty(dict) { - return { - dict: dict, - data: Bs_internalAVLset.empty0 - }; -} - -function isEmpty(d) { - return Bs_internalAVLset.isEmpty0(d.data); -} - -function singleton(dict, x) { - return { - dict: dict, - data: Bs_internalAVLset.singleton0(x) - }; -} - -function minOpt(d) { - return Bs_internalAVLset.minOpt0(d.data); -} - -function minNull(d) { - return Bs_internalAVLset.minNull0(d.data); -} - -function maxOpt(d) { - return Bs_internalAVLset.maxOpt0(d.data); -} - -function maxNull(d) { - return Bs_internalAVLset.maxNull0(d.data); -} - -function iter(d, f) { - return Bs_internalAVLset.iter0(d.data, f); -} - -function fold(d, acc, cb) { - return Bs_internalAVLset.fold0(d.data, acc, cb); -} - -function forAll(d, p) { - return Bs_internalAVLset.forAll0(d.data, p); +function removeCheck(d, v) { + var dict = d.dict; + var oldRoot = d.data; + if (oldRoot !== null) { + var removed = [/* false */0]; + var newRoot = removeMutateCheckAux(oldRoot, v, removed, dict[/* cmp */0]); + if (newRoot !== oldRoot) { + d.data = newRoot; + } + return removed[0]; + } else { + return /* false */0; + } } -function exists(d, p) { - return Bs_internalAVLset.exists0(d.data, p); +function addMutateCheckAux(t, x, added, cmp) { + if (t !== null) { + var k = t.key; + var c = cmp(x, k); + if (c) { + var l = t.left; + var r = t.right; + if (c < 0) { + var ll = addMutateCheckAux(l, x, added, cmp); + t.left = ll; + } else { + t.right = addMutateCheckAux(r, x, added, cmp); + } + return Bs_internalAVLset.balMutate(t); + } else { + return t; + } + } else { + added[0] = /* true */1; + return Bs_internalAVLset.singleton0(x); + } } function split(d, key) { @@ -257,6 +247,56 @@ function partition(d, p) { ]; } +function empty(dict) { + return { + dict: dict, + data: Bs_internalAVLset.empty0 + }; +} + +function isEmpty(d) { + return Bs_internalAVLset.isEmpty0(d.data); +} + +function singleton(dict, x) { + return { + dict: dict, + data: Bs_internalAVLset.singleton0(x) + }; +} + +function minOpt(d) { + return Bs_internalAVLset.minOpt0(d.data); +} + +function minNull(d) { + return Bs_internalAVLset.minNull0(d.data); +} + +function maxOpt(d) { + return Bs_internalAVLset.maxOpt0(d.data); +} + +function maxNull(d) { + return Bs_internalAVLset.maxNull0(d.data); +} + +function iter(d, f) { + return Bs_internalAVLset.iter0(d.data, f); +} + +function fold(d, acc, cb) { + return Bs_internalAVLset.fold0(d.data, acc, cb); +} + +function forAll(d, p) { + return Bs_internalAVLset.forAll0(d.data, p); +} + +function exists(d, p) { + return Bs_internalAVLset.exists0(d.data, p); +} + function length(d) { return Bs_internalAVLset.length0(d.data); } @@ -276,6 +316,38 @@ function ofSortedArrayUnsafe(dict, xs) { }; } +function cmp(d0, d1) { + var dict = d0.dict; + return Bs_internalAVLset.cmp0(d0.data, d1.data, dict[/* cmp */0]); +} + +function eq(d0, d1) { + var dict = d0.dict; + return Bs_internalAVLset.eq0(dict[/* cmp */0], d0.data, d1.data); +} + +function findOpt(d, x) { + var dict = d.dict; + return Bs_internalAVLset.findOpt0(dict[/* cmp */0], d.data, x); +} + +function findNull(d, x) { + var dict = d.dict; + return Bs_internalAVLset.findNull0(dict[/* cmp */0], d.data, x); +} + +function mem(d, x) { + var dict = d.dict; + return Bs_internalAVLset.mem0(dict[/* cmp */0], d.data, x); +} + +function ofArray(dict, data) { + return { + dict: dict, + data: Bs_internalAVLset.ofArray0(dict[/* cmp */0], data) + }; +} + function addOnly(m, e) { var dict = m.dict; var oldRoot = m.data; @@ -304,6 +376,14 @@ function addCheck(m, e) { return added[0]; } +function addArrayMutate(t, xs, cmp) { + var v = t; + for(var i = 0 ,i_finish = xs.length - 1 | 0; i <= i_finish; ++i){ + v = Bs_internalAVLset.addMutate(cmp, v, xs[i]); + } + return v; +} + function addArrayOnly(d, xs) { var dict = d.dict; var oldRoot = d.data; @@ -321,86 +401,6 @@ function addArray(d, xs) { return d; } -function removeArrayOnly(d, xs) { - var dict = d.dict; - var oldRoot = d.data; - var newRoot = removeArrayMutate(oldRoot, xs, dict[/* cmp */0]); - if (newRoot !== oldRoot) { - d.data = newRoot; - return /* () */0; - } else { - return 0; - } -} - -function removeArray(d, xs) { - removeArrayOnly(d, xs); - return d; -} - -function removeOnly(d, v) { - var dict = d.dict; - var oldRoot = d.data; - if (oldRoot !== null) { - var newRoot = removeMutateAux(dict[/* cmp */0], oldRoot, v); - if (newRoot !== oldRoot) { - d.data = newRoot; - return /* () */0; - } else { - return 0; - } - } else { - return /* () */0; - } -} - -function removeCheck(d, v) { - var dict = d.dict; - var oldRoot = d.data; - if (oldRoot !== null) { - var removed = [/* false */0]; - var newRoot = removeMutateCheckAux(oldRoot, v, removed, dict[/* cmp */0]); - if (newRoot !== oldRoot) { - d.data = newRoot; - } - return removed[0]; - } else { - return /* false */0; - } -} - -function remove(d, v) { - removeOnly(d, v); - return d; -} - -function cmp(d0, d1) { - var dict = d0.dict; - return Bs_internalAVLset.cmp0(d0.data, d1.data, dict[/* cmp */0]); -} - -function eq(d0, d1) { - var dict = d0.dict; - return Bs_internalAVLset.eq0(dict[/* cmp */0], d0.data, d1.data); -} - -function findOpt(d, x) { - var dict = d.dict; - return Bs_internalAVLset.findOpt0(dict[/* cmp */0], d.data, x); -} - -function findNull(d, x) { - var dict = d.dict; - return Bs_internalAVLset.findNull0(dict[/* cmp */0], d.data, x); -} - -function ofArray(dict, data) { - return { - dict: dict, - data: Bs_internalAVLset.ofArray0(dict[/* cmp */0], data) - }; -} - function subset(a, b) { var dict = a.dict; return Bs_internalAVLset.subset0(dict[/* cmp */0], a.data, b.data); @@ -526,26 +526,21 @@ function union(a, b) { } } -function mem(d, x) { - var dict = d.dict; - return Bs_internalAVLset.mem0(dict[/* cmp */0], d.data, x); -} - exports.empty = empty; exports.ofArray = ofArray; exports.isEmpty = isEmpty; +exports.singleton = singleton; exports.mem = mem; exports.addOnly = addOnly; exports.add = add; exports.addCheck = addCheck; exports.addArrayOnly = addArrayOnly; exports.addArray = addArray; -exports.removeArrayOnly = removeArrayOnly; -exports.removeArray = removeArray; -exports.singleton = singleton; exports.removeOnly = removeOnly; exports.remove = remove; exports.removeCheck = removeCheck; +exports.removeArrayOnly = removeArrayOnly; +exports.removeArray = removeArray; exports.union = union; exports.inter = inter; exports.diff = diff; @@ -556,8 +551,6 @@ exports.iter = iter; exports.fold = fold; exports.forAll = forAll; exports.exists = exists; -exports.filter = filter; -exports.partition = partition; exports.length = length; exports.toList = toList; exports.toArray = toArray; @@ -565,8 +558,10 @@ exports.minOpt = minOpt; exports.minNull = minNull; exports.maxOpt = maxOpt; exports.maxNull = maxNull; -exports.split = split; -exports.ofSortedArrayUnsafe = ofSortedArrayUnsafe; exports.findOpt = findOpt; exports.findNull = findNull; +exports.filter = filter; +exports.partition = partition; +exports.split = split; +exports.ofSortedArrayUnsafe = ofSortedArrayUnsafe; /* No side effect */ diff --git a/lib/js/bs_internalAVLtree.js b/lib/js/bs_internalAVLtree.js index d6d3e471fd..072029218b 100644 --- a/lib/js/bs_internalAVLtree.js +++ b/lib/js/bs_internalAVLtree.js @@ -112,6 +112,66 @@ function bal(l, x, d, r) { } } +function minKey0Aux(_n) { + while(true) { + var n = _n; + var match = n.left; + if (match !== null) { + _n = match; + continue ; + + } else { + return n.key; + } + }; +} + +function minKeyOpt0(n) { + if (n !== null) { + return /* Some */[minKey0Aux(n)]; + } else { + return /* None */0; + } +} + +function minKeyNull0(n) { + if (n !== null) { + return minKey0Aux(n); + } else { + return null; + } +} + +function maxKey0Aux(_n) { + while(true) { + var n = _n; + var match = n.right; + if (match !== null) { + _n = match; + continue ; + + } else { + return n.key; + } + }; +} + +function maxKeyOpt0(n) { + if (n !== null) { + return /* Some */[maxKey0Aux(n)]; + } else { + return /* None */0; + } +} + +function maxKeyNull0(n) { + if (n !== null) { + return maxKey0Aux(n); + } else { + return null; + } +} + function minKV0Aux(_n) { while(true) { var n = _n; @@ -978,7 +1038,7 @@ function balMutate(nt) { } } -function addMutate(cmp, t, x, data) { +function updateMutate(t, x, data, cmp) { if (t !== null) { var k = t.key; var c = cmp(x, k); @@ -986,10 +1046,10 @@ function addMutate(cmp, t, x, data) { var l = t.left; var r = t.right; if (c < 0) { - var ll = addMutate(cmp, l, x, data); + var ll = updateMutate(l, x, data, cmp); t.left = ll; } else { - t.right = addMutate(cmp, r, x, data); + t.right = updateMutate(r, x, data, cmp); } return balMutate(t); } else { @@ -1017,7 +1077,7 @@ function ofArray0(cmp, xs) { } for(var i = next ,i_finish = len - 1 | 0; i <= i_finish; ++i){ var match = xs[i]; - result = addMutate(cmp, result, match[0], match[1]); + result = updateMutate(result, match[0], match[1], cmp); } return result; } else { @@ -1042,6 +1102,10 @@ exports.create = create; exports.bal = bal; exports.singleton0 = singleton0; exports.updateKV = updateKV; +exports.minKeyOpt0 = minKeyOpt0; +exports.minKeyNull0 = minKeyNull0; +exports.maxKeyOpt0 = maxKeyOpt0; +exports.maxKeyNull0 = maxKeyNull0; exports.minKVOpt0 = minKVOpt0; exports.minKVNull0 = minKVNull0; exports.maxKVOpt0 = maxKVOpt0; @@ -1081,7 +1145,7 @@ exports.findWithDefault0 = findWithDefault0; exports.findExn0 = findExn0; exports.mem0 = mem0; exports.ofArray0 = ofArray0; -exports.addMutate = addMutate; +exports.updateMutate = updateMutate; exports.balMutate = balMutate; exports.removeMinAuxWithRootMutate = removeMinAuxWithRootMutate; /* No side effect */ From 215f9f61fdfc20cb480e3e52ce04573336ffeece Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Wed, 17 Jan 2018 09:57:17 +0800 Subject: [PATCH 2/5] make API more consistent --- jscomp/others/bs_Map.ml | 20 +++--- jscomp/others/bs_Map.mli | 116 ++++++++++++++------------------ jscomp/others/bs_MapInt.ml | 18 +++-- jscomp/others/bs_MapInt.mli | 97 +++++++++++++------------- jscomp/others/bs_MapIntM.ml | 18 +++-- jscomp/others/bs_MapIntM.mli | 108 +++++++++++------------------ jscomp/others/bs_MapM.ml | 21 ++++-- jscomp/others/bs_MapM.mli | 92 ++++++++++--------------- jscomp/others/bs_MapString.ml | 18 +++-- jscomp/others/bs_MapString.mli | 97 +++++++++++++------------- jscomp/others/bs_MapStringM.ml | 18 +++-- jscomp/others/bs_MapStringM.mli | 108 +++++++++++------------------ jscomp/others/map.cppo.ml | 18 +++-- jscomp/others/map.cppo.mli | 97 +++++++++++++------------- jscomp/others/mapm.cppo.ml | 18 +++-- jscomp/others/mapm.cppo.mli | 108 +++++++++++------------------ jscomp/test/bs_map_test.js | 2 +- jscomp/test/bs_map_test.ml | 2 +- jscomp/test/bs_poly_map_test.js | 8 +-- jscomp/test/bs_poly_map_test.ml | 2 +- lib/js/bs_Map.js | 60 +++++++++++------ lib/js/bs_MapInt.js | 75 +++++++++++++-------- lib/js/bs_MapIntM.js | 61 +++++++++++++---- lib/js/bs_MapM.js | 62 ++++++++++++----- lib/js/bs_MapString.js | 75 +++++++++++++-------- lib/js/bs_MapStringM.js | 61 +++++++++++++---- 26 files changed, 737 insertions(+), 643 deletions(-) diff --git a/jscomp/others/bs_Map.ml b/jscomp/others/bs_Map.ml index 3f6ca46550..0becb73d6e 100644 --- a/jscomp/others/bs_Map.ml +++ b/jscomp/others/bs_Map.ml @@ -172,7 +172,7 @@ let rec merge0 s1 s2 f ~cmp = -let ofArray (type k) (type id) (dict : (k,id) Bs_Cmp.t) data = +let ofArray (type k) (type id) data ~(dict : (k,id) Bs_Cmp.t) = let module M = (val dict ) in B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) @@ -213,13 +213,13 @@ let merge (type k) (type id) (s1 : (k,_,id) t) B.bag ~data:(merge0 ~cmp:X.cmp s1_data s2_data f) ~dict -let empty dict = +let empty ~dict = B.bag ~dict ~data:N.empty0 let isEmpty map = N.isEmpty0 (B.data map) -let singleton dict k v = +let singleton k v ~dict = B.bag ~dict ~data:(N.singleton0 k v) let cmp (type k) (type id) (m1 : (k,'v,id) t) (m2 : (k,'v,id) t) cmp @@ -270,10 +270,14 @@ let keysToArray m = let valuesToArray m = N.valuesToArray0 (B.data m) -let minKVOpt m = N.minKVOpt0 (B.data m) -let minKVNull m = N.minKVNull0 (B.data m) -let maxKVOpt m = N.maxKVOpt0 (B.data m) -let maxKVNull m = N.maxKVNull0 (B.data m) +let minKeyOpt m = N.minKeyOpt0 (B.data m) +let minKeyNull m = N.minKeyNull0 (B.data m) +let maxKeyOpt m = N.maxKeyOpt0 (B.data m) +let maxKeyNull m = N.maxKeyNull0 (B.data m) +let minKeyValueOpt m = N.minKVOpt0 (B.data m) +let minKeyValueNull m = N.minKVNull0 (B.data m) +let maxKeyValueOpt m = N.maxKVOpt0 (B.data m) +let maxKeyValueNull m = N.maxKVNull0 (B.data m) let findOpt (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in @@ -331,4 +335,4 @@ let mapi0 = N.mapi0 let map0 = N.map0 let filter0 = N.filterShared0 -let partition0 = N.partitionShared0 \ No newline at end of file +let partition0 = N.partitionShared0 diff --git a/jscomp/others/bs_Map.mli b/jscomp/others/bs_Map.mli index d96c01a837..36c7570377 100644 --- a/jscomp/others/bs_Map.mli +++ b/jscomp/others/bs_Map.mli @@ -47,57 +47,15 @@ type ('k,'v,'id) t = (* should not export [Bs_Cmp.compare]. should only export [Bs_Cmp.t] or [Bs_Cmp.cmp] instead *) - - - -val empty: ('k, 'id) Bs_Cmp.t -> ('k, 'a, 'id) t - -val ofArray: - ('k,'id) Bs_Cmp.t -> - ('k * 'a) array -> - ('k,'a,'id) t - +val empty: dict:('k, 'id) Bs_Cmp.t -> ('k, 'a, 'id) t val isEmpty: _ t -> bool -val mem: - ('k, 'a, 'id) t -> 'k -> bool - -val update: - ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t -(** [update m x y ] returns a map containing the same bindings as - [m], with a new binding of [x] to [y]. If [x] was already bound - in [m], its previous binding disappears. *) -val updateArray: - ('k, 'a, 'id) t -> ('k * 'a) array -> ('k, 'a, 'id) t -val updateWithOpt: - ('k, 'a, 'id) t -> - 'k -> - ('k option -> 'a option [@bs]) -> - ('k, 'a, 'id) t - -val singleton: - ('k,'id) Bs_Cmp.t -> - 'k -> 'a -> ('k, 'a, 'id) t - -val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t -(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *) - -val merge: - ('k, 'a, 'id ) t -> - ('k, 'b,'id) t -> - ('k -> 'a option -> 'b option -> 'c option [@bs]) -> - ('k, 'c,'id) t -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. -*) - +val singleton: 'k -> 'a -> dict:('k,'id) Bs_Cmp.t -> ('k, 'a, 'id) t +val mem: ('k, 'a, 'id) t -> 'k -> bool val cmp: ('k, 'v, 'id) t -> ('k, 'v, 'id) t -> ('v -> 'v -> int [@bs]) -> int - - val eq: ('k, 'a, 'id) t -> ('k, 'a, 'id) t -> @@ -127,6 +85,55 @@ val exists: ('k, 'a, 'id) t -> ('k -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. Order unspecified *) +val length: ('k, 'a, 'id) t -> int +val toList: ('k, 'a, 'id) t -> ('k * 'a) list +(** In increasing order*) +val toArray: ('k, 'a, 'id) t -> ('k * 'a) array +val ofArray: ('k * 'a) array -> dict:('k,'id) Bs_Cmp.t -> ('k,'a,'id) t +val keysToArray: ('k, 'a, 'id) t -> 'k array +val valuesToArray: ('k, 'a, 'id) t -> 'a array +val minKeyOpt: ('k, _, _) t -> 'k option +val minKeyNull: ('k, _, _) t -> 'k Js.null +val maxKeyOpt: ('k, _, _) t -> 'k option +val maxKeyNull: ('k, _, _) t -> 'k Js.null +val minKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option +val minKeyValueNull: ('k, 'a, _) t -> ('k * 'a) Js.null +val maxKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option +val maxKeyValueNull:('k, 'a, _) t -> ('k * 'a) Js.null +val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option +val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null +val findWithDefault: + ('k, 'a, 'id) t -> 'k -> 'a -> 'a +val findExn: ('k, 'a, 'id) t -> 'k -> 'a + +(****************************************************************************) + +val update: + ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t +(** [update m x y ] returns a map containing the same bindings as + [m], with a new binding of [x] to [y]. If [x] was already bound + in [m], its previous binding disappears. *) +val updateArray: + ('k, 'a, 'id) t -> ('k * 'a) array -> ('k, 'a, 'id) t +val updateWithOpt: + ('k, 'a, 'id) t -> + 'k -> + ('k option -> 'a option [@bs]) -> + ('k, 'a, 'id) t + +val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t +(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *) + +val merge: + ('k, 'a, 'id ) t -> + ('k, 'b,'id) t -> + ('k -> 'a option -> 'b option -> 'c option [@bs]) -> + ('k, 'c,'id) t +(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] + and of [m2]. The presence of each such binding, and the corresponding + value, is determined with the function [f]. +*) + val filter: ('k, 'a, 'id) t -> ('k -> 'a -> bool [@bs]) -> @@ -144,20 +151,6 @@ val partition: [s] that do not satisfy [p]. *) -val length: ('k, 'a, 'id) t -> int - - -val toList: ('k, 'a, 'id) t -> ('k * 'a) list -(** In increasing order*) -val toArray : ('k, 'a, 'id) t -> ('k * 'a) array -val keysToArray : ('k, 'a, 'id) t -> 'k array -val valuesToArray : ('k, 'a, 'id) t -> 'a array - -val minKVOpt: ('k, 'a, _) t -> ('k * 'a) option -val minKVNull: ('k, 'a, _) t -> ('k * 'a) Js.null -val maxKVOpt: ('k, 'a, _) t -> ('k * 'a) option -val maxKVNull:('k, 'a, _) t -> ('k * 'a) Js.null - val split: ('k, 'a, 'id) t -> 'k -> (('k, 'a, 'id) t * ('k, 'a, 'id) t )* 'a option @@ -170,11 +163,6 @@ val split: or [Some v] if [m] binds [v] to [x]. *) -val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option -val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null -val findWithDefault: - ('k, 'a, 'id) t -> 'k -> 'a -> 'a -val findExn: ('k, 'a, 'id) t -> 'k -> 'a val map: ('k, 'a, 'id) t -> ('a -> 'b [@bs]) -> ('k ,'b,'id ) t (** [map m f] returns a map with same domain as [m], where the associated value [a] of all bindings of [m] has been diff --git a/jscomp/others/bs_MapInt.ml b/jscomp/others/bs_MapInt.ml index 7a9a18a47b..d49954612c 100644 --- a/jscomp/others/bs_MapInt.ml +++ b/jscomp/others/bs_MapInt.ml @@ -12,10 +12,15 @@ type 'a t = (key, 'a) N.t0 let empty = N.empty0 let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let minKVOpt = N.minKVOpt0 -let minKVNull = N.minKVNull0 -let maxKVOpt = N.maxKVOpt0 -let maxKVNull = N.maxKVNull0 + +let minKeyOpt = N.minKeyOpt0 +let minKeyNull = N.minKeyNull0 +let maxKeyOpt = N.maxKeyOpt0 +let maxKeyNull = N.maxKeyNull0 +let minKeyValueOpt = N.minKVOpt0 +let minKeyValueNull = N.minKVNull0 +let maxKeyValueOpt = N.maxKVOpt0 +let maxKeyValueNull = N.maxKVNull0 let iter = N.iter0 let map = N.map0 let mapi = N.mapi0 @@ -26,6 +31,9 @@ let filter = N.filterShared0 let partition = N.partitionShared0 let length = N.length0 let toList = N.toList0 +let toArray = N.toArray0 +let keysToArray = N.keysToArray0 +let valuesToArray = N.valuesToArray0 let checkInvariant = N.checkInvariant let rec update t (newK : key) (newD : _) = @@ -92,4 +100,4 @@ let findWithDefault = I.findWithDefault let findExn = I.findExn let split = I.split let merge = I.merge -let ofArray = I.ofArray \ No newline at end of file +let ofArray = I.ofArray diff --git a/jscomp/others/bs_MapInt.mli b/jscomp/others/bs_MapInt.mli index c278ba945f..5e3d58d289 100644 --- a/jscomp/others/bs_MapInt.mli +++ b/jscomp/others/bs_MapInt.mli @@ -5,38 +5,9 @@ type 'a t (** The type of maps from type [key] to type ['a]. *) val empty: 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val update: 'a t -> key -> 'a -> 'a t -(** [add m x y] returns a map containing the same bindings as - [m], plus a binding of [x] to [y]. If [x] was already bound - in [m], its previous binding disappears. *) -val updateWithOpt: - 'a t -> - key -> - (key option -> 'a option [@bs]) -> - 'a t - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) - -val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool @@ -65,6 +36,52 @@ val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. *) +val length: 'a t -> int +val toList: 'a t -> (key * 'a) list +(** In increasing order with respect *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) + +val update: 'a t -> key -> 'a -> 'a t +(** [add m x y] returns a map containing the same bindings as + [m], plus a binding of [x] to [y]. If [x] was already bound + in [m], its previous binding disappears. *) +val updateWithOpt: + 'a t -> + key -> + (key option -> 'a option [@bs]) -> + 'a t + + +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + + +val merge: + 'a t -> 'b t -> + (key -> 'a option -> 'b option -> 'c option [@bs]) -> + 'c t +(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] + and of [m2]. The presence of each such binding, and the corresponding + value, is determined with the function [f]. + *) val filter: 'a t -> @@ -84,20 +101,6 @@ val partition: [s] that do not satisfy [p]. *) -val length: 'a t -> int - - -val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null @@ -112,10 +115,6 @@ val split: key -> 'a t -> 'a t * 'a option * 'a t or [Some v] if [m] binds [v] to [x]. *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the @@ -127,4 +126,4 @@ val map: 'a t -> ('a -> 'b [@bs]) -> 'b t val mapi: 'a t -> (key -> 'a -> 'b [@bs]) -> 'b t -val checkInvariant : _ t -> bool \ No newline at end of file +val checkInvariant : _ t -> bool diff --git a/jscomp/others/bs_MapIntM.ml b/jscomp/others/bs_MapIntM.ml index d93aaae60a..90ab3f7ae2 100644 --- a/jscomp/others/bs_MapIntM.ml +++ b/jscomp/others/bs_MapIntM.ml @@ -17,10 +17,15 @@ type 'a t = { let empty () = t ~data:N.empty0 let isEmpty m = N.isEmpty0 (data m) let singleton k v = t ~data:(N.singleton0 k v) -let minKVOpt m = N.minKVOpt0 (data m) -let minKVNull m = N.minKVNull0 (data m) -let maxKVOpt m = N.maxKVOpt0 (data m) -let maxKVNull m = N.maxKVNull0 (data m) + +let minKeyNull m = N.minKeyNull0 (data m) +let minKeyOpt m = N.minKeyOpt0 (data m) +let maxKeyNull m = N.maxKeyNull0 (data m) +let maxKeyOpt m = N.maxKeyOpt0 (data m) +let minKeyValueOpt m = N.minKVOpt0 (data m) +let minKeyValueNull m = N.minKVNull0 (data m) +let maxKeyValueOpt m = N.maxKVOpt0 (data m) +let maxKeyValueNull m = N.maxKVNull0 (data m) let addOnly (m : _ t) k v = let old_data = data m in @@ -40,6 +45,9 @@ let exists d f = N.exists0 (data d) f let length d = N.length0 (data d) let toList d = N.toList0 (data d) +let toArray d = N.toArray0 (data d) +let keysToArray d = N.keysToArray0 (data d) +let valuesToArray d = N.valuesToArray0 (data d) let checkInvariant d = N.checkInvariant (data d) let mem d v = I.mem (data d) v @@ -105,4 +113,4 @@ let findOpt d x = I.findOpt (data d) x let findNull d x = I.findNull (data d) x let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x \ No newline at end of file +let findExn d x = I.findExn (data d) x diff --git a/jscomp/others/bs_MapIntM.mli b/jscomp/others/bs_MapIntM.mli index ab5c9afeb3..96345598af 100644 --- a/jscomp/others/bs_MapIntM.mli +++ b/jscomp/others/bs_MapIntM.mli @@ -5,46 +5,22 @@ type 'a t val empty: unit -> 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val addOnly : 'a t -> key -> 'a -> unit -val add: 'a t -> key -> 'a -> 'a t -(** [add m x y] do the in-place modification, return - [m] for chaining. If [x] was already bound - in [m], its previous binding disappears. *) - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) - -(* val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t *) -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int - +(** [cmp m1 m2 cmp] + First compare by size, if size is the same, + compare by key, value pair +*) val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool -(** [equal m1 m2 cmp] tests whether the maps [m1] and [m2] are - equal, that is, contain equal keys and associate them with - equal data. [cmp] is the equality predicate used to compare - the data associated with the keys. *) - +(** [eq m1 m2 cmp] *) + val iter: 'a t -> (key -> 'a -> unit [@bs]) -> unit (** [iter m f] applies [f] to all bindings in map [m]. [f] receives the key as first argument, and the associated value - as second argument. The bindings are passed to [f] in increasing - order with respect to the ordering over the type of the keys. *) + as second argument. + The application order of [f] is in increasing order. *) val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b (** [fold m a f] computes [(f kN dN ... (f k1 d1 a)...)], @@ -54,58 +30,52 @@ val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b val forAll: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [forAll m p] checks if all the bindings of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) -(* val filter: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t *) -(** [filter m p] returns the map with all the bindings in [m] - that satisfy predicate [p]. -*) -(* val partition: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t * 'a t *) -(** [partition p m] returns a pair of maps [(m1, m2)], where - [m1] contains all the bindings of [s] that satisfy the - predicate [p], and [m2] is the map with all the bindings of - [s] that do not satisfy [p]. - *) val length: 'a t -> int - - val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null - +(** In increasing order *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) +(*TODO: add functional [merge, partition, filter, split]*) + +val addOnly : 'a t -> key -> 'a -> unit +val add: 'a t -> key -> 'a -> 'a t +(** [add m x y] do the in-place modification, return + [m] for chaining. If [x] was already bound + in [m], its previous binding disappears. *) -(* val split: key -> 'a t -> 'a t * 'a option * 'a t *) -(** [split x m] returns a triple [(l, data, r)], where - [l] is the map with all the bindings of [m] whose key - is strictly less than [x]; - [r] is the map with all the bindings of [m] whose key - is strictly greater than [x]; - [data] is [None] if [m] contains no binding for [x], - or [Some v] if [m] binds [v] to [x]. - *) +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn : 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the diff --git a/jscomp/others/bs_MapM.ml b/jscomp/others/bs_MapM.ml index a3ef3a711e..be16562a4a 100644 --- a/jscomp/others/bs_MapM.ml +++ b/jscomp/others/bs_MapM.ml @@ -98,8 +98,8 @@ let removeArrayOnly (type elt) (type id) (d : (elt,_,id) t) xs = let removeArray d xs = removeArrayOnly d xs; d - -let empty dict = + +let empty ~dict = B.bag ~dict ~data:N.empty0 let isEmpty d = N.isEmpty0 (B.data d) @@ -108,10 +108,13 @@ let singleton dict x v= let minKeyOpt m = N.minKeyOpt0 (B.data m) let minKeyNull m = N.minKeyNull0 (B.data m) -let minKVOpt m = N.minKVOpt0 (B.data m) -let minKVNull m = N.minKVNull0 (B.data m) -let maxKVOpt m = N.maxKVOpt0 (B.data m) -let maxKVNull m = N.maxKVNull0 (B.data m) +let maxKeyOpt m = N.maxKeyOpt0 (B.data m) +let maxKeyNull m = N.maxKeyNull0 (B.data m) +let minKeyValueOpt m = N.minKVOpt0 (B.data m) +let minKeyValueNull m = N.minKVNull0 (B.data m) +let maxKeyValueOpt m = N.maxKVOpt0 (B.data m) +let maxKeyValueNull m = N.maxKVNull0 (B.data m) + let iter d f = N.iter0 (B.data d) f let fold d acc cb = @@ -126,6 +129,10 @@ let toList d = N.toList0 (B.data d) let toArray d = N.toArray0 (B.data d) +let keysToArray d = + N.keysToArray0 (B.data d) +let valuesToArray d = + N.valuesToArray0 (B.data d) let ofSortedArrayUnsafe ~dict xs : _ t = B.bag ~data:(N.ofSortedArrayUnsafe0 xs) ~dict let checkInvariant d = @@ -164,7 +171,7 @@ let mem (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.mem0 ~cmp:X.cmp map x -let ofArray (type k) (type id) (dict : (k,id) Bs_Cmp.t) data = +let ofArray (type k) (type id) data ~(dict : (k,id) Bs_Cmp.t)= let module M = (val dict ) in B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) let updateOnly (type elt) (type id) (m : (elt,_,id) t) e v = diff --git a/jscomp/others/bs_MapM.mli b/jscomp/others/bs_MapM.mli index 8efdbcf914..939de6d0c7 100644 --- a/jscomp/others/bs_MapM.mli +++ b/jscomp/others/bs_MapM.mli @@ -25,43 +25,21 @@ type ('k,'v,'id) t -val empty: ('k, 'id) Bs_Cmp.t -> ('k, 'a, 'id) t -val ofArray: - ('k,'id) Bs_Cmp.t -> - ('k * 'a) array -> - ('k,'a,'id) t -val isEmpty: ('k, 'a, 'id) t -> bool +val empty: dict:('k, 'id) Bs_Cmp.t -> ('k, 'a, 'id) t +val isEmpty: _ t -> bool val singleton: ('k,'id) Bs_Cmp.t -> 'k -> 'a -> ('k, 'a, 'id) t val mem: ('k, _, _) t -> 'k -> bool - -val updateOnly: ('k, 'a, 'id) t -> 'k -> 'a -> unit -val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t -(** [update m x y ] do the in-place modification, returnning [m] for chaining. *) - - - -val removeOnly: ('k, 'a, 'id) t -> 'k -> unit -val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t -(** [remove m x] do the in-place modification, - returnning [m] for chaining. *) -val removeArrayOnly: ('k, 'a, 'id) t -> 'k array -> unit -val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t - - -(* val merge: *) -(* ('k, 'a, 'id ) t -> ('k, 'b,'id) t -> ('k -> 'a option -> 'b option -> 'c option [@bs]) -> ('k, 'c,'id) t *) -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. -*) - val cmp: ('k, 'a, 'id) t -> ('k, 'a, 'id) t -> ('a -> 'a -> int [@bs]) -> int +(** [cmp m1 m2 cmp] + First compare by size, if size is the same, + compare by key, value pair +*) val eq: ('k, 'a, 'id) t -> ('k, 'a, 'id) t -> ('a -> 'a -> bool [@bs]) -> bool (** [eq m1 m2 eqf] tests whether the maps [m1] and [m2] are equal, that is, contain equal keys and associate them with @@ -94,17 +72,40 @@ val length: ('k, 'a, 'id) t -> int val toList: ('k, 'a, 'id) t -> ('k * 'a) list (** In increasing order*) -val toArray : ('k, 'a, 'id) t -> ('k * 'a) array - -val minKVOpt: ('k, 'a, _) t -> ('k * 'a) option -val minKVNull: ('k, 'a, _) t -> ('k * 'a) Js.null -val maxKVOpt: ('k, 'a, _) t -> ('k * 'a) option -val maxKVNull:('k, 'a, _) t -> ('k * 'a) Js.null +val toArray: ('k, 'a, 'id) t -> ('k * 'a) array +val ofArray: ('k * 'a) array -> dict:('k,'id) Bs_Cmp.t -> ('k,'a,'id) t +val keysToArray: ('k, _, _) t -> 'k array +val valuesToArray: (_, 'a, _) t -> 'a array +val minKeyOpt: ('k, _, _) t -> 'k option +val minKeyNull: ('k, _, _) t -> 'k Js.null +val maxKeyOpt: ('k, _, _) t -> 'k option +val maxKeyNull: ('k, _, _) t -> 'k Js.null +val minKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option +val minKeyValueNull: ('k, 'a, _) t -> ('k * 'a) Js.null +val maxKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option +val maxKeyValueNull:('k, 'a, _) t -> ('k * 'a) Js.null val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null val findWithDefault: ('k, 'a, 'id) t -> 'k -> 'a -> 'a val findExn: ('k, 'a, 'id) t -> 'k -> 'a + +(****************************************************************************) + +(*TODO: add functional [merge, partition, filter, split]*) + +val updateOnly: ('k, 'a, 'id) t -> 'k -> 'a -> unit +val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t +(** [update m x y ] do the in-place modification, returnning [m] for chaining. *) + +val removeOnly: ('k, 'a, 'id) t -> 'k -> unit +val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t +(** [remove m x] do the in-place modification, + returnning [m] for chaining. *) +val removeArrayOnly: ('k, 'a, 'id) t -> 'k array -> unit +val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t + + val map: ('k, 'a, 'id) t -> ('a -> 'b [@bs]) -> ('k ,'b,'id ) t (** [map m f] returns a map with same domain as [m], where the associated value [a] of all bindings of [m] has been @@ -116,26 +117,3 @@ val mapi: ('k, 'a, 'id) t -> ('k -> 'a -> 'b [@bs]) -> ('k, 'b, 'id) t -(* val filter: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t *) -(** [filter p m] returns the map with all the bindings in [m] - that satisfy predicate [p]. -*) - -(* val partition: ('k -> 'a -> bool [@bs]) -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * ('k, 'a, 'id) t *) -(** [partition p m] returns a pair of maps [(m1, m2)], where - [m1] contains all the bindings of [s] that satisfy the - predicate [p], and [m2] is the map with all the bindings of - [s] that do not satisfy [p]. -*) - - -(* val split: 'k -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * 'a option * ('k, 'a, 'id) t *) -(** [split x m] returns a triple [(l, data, r)], where - [l] is the map with all the bindings of [m] whose 'k - is strictly less than [x]; - [r] is the map with all the bindings of [m] whose 'k - is strictly greater than [x]; - [data] is [None] if [m] contains no binding for [x], - or [Some v] if [m] binds [v] to [x]. -*) - diff --git a/jscomp/others/bs_MapString.ml b/jscomp/others/bs_MapString.ml index c1941da38e..024377a04a 100644 --- a/jscomp/others/bs_MapString.ml +++ b/jscomp/others/bs_MapString.ml @@ -12,10 +12,15 @@ type 'a t = (key, 'a) N.t0 let empty = N.empty0 let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let minKVOpt = N.minKVOpt0 -let minKVNull = N.minKVNull0 -let maxKVOpt = N.maxKVOpt0 -let maxKVNull = N.maxKVNull0 + +let minKeyOpt = N.minKeyOpt0 +let minKeyNull = N.minKeyNull0 +let maxKeyOpt = N.maxKeyOpt0 +let maxKeyNull = N.maxKeyNull0 +let minKeyValueOpt = N.minKVOpt0 +let minKeyValueNull = N.minKVNull0 +let maxKeyValueOpt = N.maxKVOpt0 +let maxKeyValueNull = N.maxKVNull0 let iter = N.iter0 let map = N.map0 let mapi = N.mapi0 @@ -26,6 +31,9 @@ let filter = N.filterShared0 let partition = N.partitionShared0 let length = N.length0 let toList = N.toList0 +let toArray = N.toArray0 +let keysToArray = N.keysToArray0 +let valuesToArray = N.valuesToArray0 let checkInvariant = N.checkInvariant let rec update t (newK : key) (newD : _) = @@ -92,4 +100,4 @@ let findWithDefault = I.findWithDefault let findExn = I.findExn let split = I.split let merge = I.merge -let ofArray = I.ofArray \ No newline at end of file +let ofArray = I.ofArray diff --git a/jscomp/others/bs_MapString.mli b/jscomp/others/bs_MapString.mli index 362872f0e2..f98487a199 100644 --- a/jscomp/others/bs_MapString.mli +++ b/jscomp/others/bs_MapString.mli @@ -5,38 +5,9 @@ type 'a t (** The type of maps from type [key] to type ['a]. *) val empty: 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val update: 'a t -> key -> 'a -> 'a t -(** [add m x y] returns a map containing the same bindings as - [m], plus a binding of [x] to [y]. If [x] was already bound - in [m], its previous binding disappears. *) -val updateWithOpt: - 'a t -> - key -> - (key option -> 'a option [@bs]) -> - 'a t - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) - -val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool @@ -65,6 +36,52 @@ val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. *) +val length: 'a t -> int +val toList: 'a t -> (key * 'a) list +(** In increasing order with respect *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) + +val update: 'a t -> key -> 'a -> 'a t +(** [add m x y] returns a map containing the same bindings as + [m], plus a binding of [x] to [y]. If [x] was already bound + in [m], its previous binding disappears. *) +val updateWithOpt: + 'a t -> + key -> + (key option -> 'a option [@bs]) -> + 'a t + + +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + + +val merge: + 'a t -> 'b t -> + (key -> 'a option -> 'b option -> 'c option [@bs]) -> + 'c t +(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] + and of [m2]. The presence of each such binding, and the corresponding + value, is determined with the function [f]. + *) val filter: 'a t -> @@ -84,20 +101,6 @@ val partition: [s] that do not satisfy [p]. *) -val length: 'a t -> int - - -val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null @@ -112,10 +115,6 @@ val split: key -> 'a t -> 'a t * 'a option * 'a t or [Some v] if [m] binds [v] to [x]. *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the @@ -127,4 +126,4 @@ val map: 'a t -> ('a -> 'b [@bs]) -> 'b t val mapi: 'a t -> (key -> 'a -> 'b [@bs]) -> 'b t -val checkInvariant : _ t -> bool \ No newline at end of file +val checkInvariant : _ t -> bool diff --git a/jscomp/others/bs_MapStringM.ml b/jscomp/others/bs_MapStringM.ml index 22f236d9a3..a40f517116 100644 --- a/jscomp/others/bs_MapStringM.ml +++ b/jscomp/others/bs_MapStringM.ml @@ -17,10 +17,15 @@ type 'a t = { let empty () = t ~data:N.empty0 let isEmpty m = N.isEmpty0 (data m) let singleton k v = t ~data:(N.singleton0 k v) -let minKVOpt m = N.minKVOpt0 (data m) -let minKVNull m = N.minKVNull0 (data m) -let maxKVOpt m = N.maxKVOpt0 (data m) -let maxKVNull m = N.maxKVNull0 (data m) + +let minKeyNull m = N.minKeyNull0 (data m) +let minKeyOpt m = N.minKeyOpt0 (data m) +let maxKeyNull m = N.maxKeyNull0 (data m) +let maxKeyOpt m = N.maxKeyOpt0 (data m) +let minKeyValueOpt m = N.minKVOpt0 (data m) +let minKeyValueNull m = N.minKVNull0 (data m) +let maxKeyValueOpt m = N.maxKVOpt0 (data m) +let maxKeyValueNull m = N.maxKVNull0 (data m) let addOnly (m : _ t) k v = let old_data = data m in @@ -40,6 +45,9 @@ let exists d f = N.exists0 (data d) f let length d = N.length0 (data d) let toList d = N.toList0 (data d) +let toArray d = N.toArray0 (data d) +let keysToArray d = N.keysToArray0 (data d) +let valuesToArray d = N.valuesToArray0 (data d) let checkInvariant d = N.checkInvariant (data d) let mem d v = I.mem (data d) v @@ -105,4 +113,4 @@ let findOpt d x = I.findOpt (data d) x let findNull d x = I.findNull (data d) x let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x \ No newline at end of file +let findExn d x = I.findExn (data d) x diff --git a/jscomp/others/bs_MapStringM.mli b/jscomp/others/bs_MapStringM.mli index 20f018dc31..cfcb6b6ac3 100644 --- a/jscomp/others/bs_MapStringM.mli +++ b/jscomp/others/bs_MapStringM.mli @@ -5,46 +5,22 @@ type 'a t val empty: unit -> 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val addOnly : 'a t -> key -> 'a -> unit -val add: 'a t -> key -> 'a -> 'a t -(** [add m x y] do the in-place modification, return - [m] for chaining. If [x] was already bound - in [m], its previous binding disappears. *) - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) - -(* val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t *) -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int - +(** [cmp m1 m2 cmp] + First compare by size, if size is the same, + compare by key, value pair +*) val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool -(** [equal m1 m2 cmp] tests whether the maps [m1] and [m2] are - equal, that is, contain equal keys and associate them with - equal data. [cmp] is the equality predicate used to compare - the data associated with the keys. *) - +(** [eq m1 m2 cmp] *) + val iter: 'a t -> (key -> 'a -> unit [@bs]) -> unit (** [iter m f] applies [f] to all bindings in map [m]. [f] receives the key as first argument, and the associated value - as second argument. The bindings are passed to [f] in increasing - order with respect to the ordering over the type of the keys. *) + as second argument. + The application order of [f] is in increasing order. *) val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b (** [fold m a f] computes [(f kN dN ... (f k1 d1 a)...)], @@ -54,58 +30,52 @@ val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b val forAll: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [forAll m p] checks if all the bindings of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) -(* val filter: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t *) -(** [filter m p] returns the map with all the bindings in [m] - that satisfy predicate [p]. -*) -(* val partition: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t * 'a t *) -(** [partition p m] returns a pair of maps [(m1, m2)], where - [m1] contains all the bindings of [s] that satisfy the - predicate [p], and [m2] is the map with all the bindings of - [s] that do not satisfy [p]. - *) val length: 'a t -> int - - val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null - +(** In increasing order *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) +(*TODO: add functional [merge, partition, filter, split]*) + +val addOnly : 'a t -> key -> 'a -> unit +val add: 'a t -> key -> 'a -> 'a t +(** [add m x y] do the in-place modification, return + [m] for chaining. If [x] was already bound + in [m], its previous binding disappears. *) -(* val split: key -> 'a t -> 'a t * 'a option * 'a t *) -(** [split x m] returns a triple [(l, data, r)], where - [l] is the map with all the bindings of [m] whose key - is strictly less than [x]; - [r] is the map with all the bindings of [m] whose key - is strictly greater than [x]; - [data] is [None] if [m] contains no binding for [x], - or [Some v] if [m] binds [v] to [x]. - *) +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn : 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the diff --git a/jscomp/others/map.cppo.ml b/jscomp/others/map.cppo.ml index 3c410cff6a..258d11bf80 100644 --- a/jscomp/others/map.cppo.ml +++ b/jscomp/others/map.cppo.ml @@ -17,10 +17,15 @@ type 'a t = (key, 'a) N.t0 let empty = N.empty0 let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let minKVOpt = N.minKVOpt0 -let minKVNull = N.minKVNull0 -let maxKVOpt = N.maxKVOpt0 -let maxKVNull = N.maxKVNull0 + +let minKeyOpt = N.minKeyOpt0 +let minKeyNull = N.minKeyNull0 +let maxKeyOpt = N.maxKeyOpt0 +let maxKeyNull = N.maxKeyNull0 +let minKeyValueOpt = N.minKVOpt0 +let minKeyValueNull = N.minKVNull0 +let maxKeyValueOpt = N.maxKVOpt0 +let maxKeyValueNull = N.maxKVNull0 let iter = N.iter0 let map = N.map0 let mapi = N.mapi0 @@ -31,6 +36,9 @@ let filter = N.filterShared0 let partition = N.partitionShared0 let length = N.length0 let toList = N.toList0 +let toArray = N.toArray0 +let keysToArray = N.keysToArray0 +let valuesToArray = N.valuesToArray0 let checkInvariant = N.checkInvariant let rec update t (newK : key) (newD : _) = @@ -97,4 +105,4 @@ let findWithDefault = I.findWithDefault let findExn = I.findExn let split = I.split let merge = I.merge -let ofArray = I.ofArray \ No newline at end of file +let ofArray = I.ofArray diff --git a/jscomp/others/map.cppo.mli b/jscomp/others/map.cppo.mli index de1c0f7793..0300e73f27 100644 --- a/jscomp/others/map.cppo.mli +++ b/jscomp/others/map.cppo.mli @@ -9,38 +9,9 @@ type 'a t (** The type of maps from type [key] to type ['a]. *) val empty: 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val update: 'a t -> key -> 'a -> 'a t -(** [add m x y] returns a map containing the same bindings as - [m], plus a binding of [x] to [y]. If [x] was already bound - in [m], its previous binding disappears. *) -val updateWithOpt: - 'a t -> - key -> - (key option -> 'a option [@bs]) -> - 'a t - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) - -val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool @@ -69,6 +40,52 @@ val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. *) +val length: 'a t -> int +val toList: 'a t -> (key * 'a) list +(** In increasing order with respect *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) + +val update: 'a t -> key -> 'a -> 'a t +(** [add m x y] returns a map containing the same bindings as + [m], plus a binding of [x] to [y]. If [x] was already bound + in [m], its previous binding disappears. *) +val updateWithOpt: + 'a t -> + key -> + (key option -> 'a option [@bs]) -> + 'a t + + +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + + +val merge: + 'a t -> 'b t -> + (key -> 'a option -> 'b option -> 'c option [@bs]) -> + 'c t +(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] + and of [m2]. The presence of each such binding, and the corresponding + value, is determined with the function [f]. + *) val filter: 'a t -> @@ -88,20 +105,6 @@ val partition: [s] that do not satisfy [p]. *) -val length: 'a t -> int - - -val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null @@ -116,10 +119,6 @@ val split: key -> 'a t -> 'a t * 'a option * 'a t or [Some v] if [m] binds [v] to [x]. *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the @@ -131,4 +130,4 @@ val map: 'a t -> ('a -> 'b [@bs]) -> 'b t val mapi: 'a t -> (key -> 'a -> 'b [@bs]) -> 'b t -val checkInvariant : _ t -> bool \ No newline at end of file +val checkInvariant : _ t -> bool diff --git a/jscomp/others/mapm.cppo.ml b/jscomp/others/mapm.cppo.ml index 68c717f9c5..f0dd635d37 100644 --- a/jscomp/others/mapm.cppo.ml +++ b/jscomp/others/mapm.cppo.ml @@ -23,10 +23,15 @@ type 'a t = { let empty () = t ~data:N.empty0 let isEmpty m = N.isEmpty0 (data m) let singleton k v = t ~data:(N.singleton0 k v) -let minKVOpt m = N.minKVOpt0 (data m) -let minKVNull m = N.minKVNull0 (data m) -let maxKVOpt m = N.maxKVOpt0 (data m) -let maxKVNull m = N.maxKVNull0 (data m) + +let minKeyNull m = N.minKeyNull0 (data m) +let minKeyOpt m = N.minKeyOpt0 (data m) +let maxKeyNull m = N.maxKeyNull0 (data m) +let maxKeyOpt m = N.maxKeyOpt0 (data m) +let minKeyValueOpt m = N.minKVOpt0 (data m) +let minKeyValueNull m = N.minKVNull0 (data m) +let maxKeyValueOpt m = N.maxKVOpt0 (data m) +let maxKeyValueNull m = N.maxKVNull0 (data m) let addOnly (m : _ t) k v = let old_data = data m in @@ -46,6 +51,9 @@ let exists d f = N.exists0 (data d) f let length d = N.length0 (data d) let toList d = N.toList0 (data d) +let toArray d = N.toArray0 (data d) +let keysToArray d = N.keysToArray0 (data d) +let valuesToArray d = N.valuesToArray0 (data d) let checkInvariant d = N.checkInvariant (data d) let mem d v = I.mem (data d) v @@ -111,4 +119,4 @@ let findOpt d x = I.findOpt (data d) x let findNull d x = I.findNull (data d) x let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x \ No newline at end of file +let findExn d x = I.findExn (data d) x diff --git a/jscomp/others/mapm.cppo.mli b/jscomp/others/mapm.cppo.mli index 530ebd0b1f..a462d51a38 100644 --- a/jscomp/others/mapm.cppo.mli +++ b/jscomp/others/mapm.cppo.mli @@ -9,46 +9,22 @@ type 'a t val empty: unit -> 'a t - -val ofArray: (key * 'a) array -> 'a t - val isEmpty: 'a t -> bool - -val mem: 'a t -> key -> bool - -val addOnly : 'a t -> key -> 'a -> unit -val add: 'a t -> key -> 'a -> 'a t -(** [add m x y] do the in-place modification, return - [m] for chaining. If [x] was already bound - in [m], its previous binding disappears. *) - val singleton: key -> 'a -> 'a t - -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) - -(* val merge: - 'a t -> 'b t -> - (key -> 'a option -> 'b option -> 'c option [@bs]) -> - 'c t *) -(** [merge m1 m2 f] computes a map whose keys is a subset of keys of [m1] - and of [m2]. The presence of each such binding, and the corresponding - value, is determined with the function [f]. - *) - +val mem: 'a t -> key -> bool val cmp: 'a t -> 'a t -> ('a -> 'a -> int [@bs]) -> int - +(** [cmp m1 m2 cmp] + First compare by size, if size is the same, + compare by key, value pair +*) val eq: 'a t -> 'a t -> ('a -> 'a -> bool [@bs]) -> bool -(** [equal m1 m2 cmp] tests whether the maps [m1] and [m2] are - equal, that is, contain equal keys and associate them with - equal data. [cmp] is the equality predicate used to compare - the data associated with the keys. *) - +(** [eq m1 m2 cmp] *) + val iter: 'a t -> (key -> 'a -> unit [@bs]) -> unit (** [iter m f] applies [f] to all bindings in map [m]. [f] receives the key as first argument, and the associated value - as second argument. The bindings are passed to [f] in increasing - order with respect to the ordering over the type of the keys. *) + as second argument. + The application order of [f] is in increasing order. *) val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b (** [fold m a f] computes [(f kN dN ... (f k1 d1 a)...)], @@ -58,58 +34,52 @@ val fold: 'a t -> 'b -> ('b -> key -> 'a -> 'b [@bs]) -> 'b val forAll: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [forAll m p] checks if all the bindings of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) val exists: 'a t -> (key -> 'a -> bool [@bs]) -> bool (** [exists m p] checks if at least one binding of the map satisfy the predicate [p]. + The application order of [p] is unspecified. *) -(* val filter: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t *) -(** [filter m p] returns the map with all the bindings in [m] - that satisfy predicate [p]. -*) -(* val partition: (key -> 'a -> bool [@bs]) -> 'a t -> 'a t * 'a t *) -(** [partition p m] returns a pair of maps [(m1, m2)], where - [m1] contains all the bindings of [s] that satisfy the - predicate [p], and [m2] is the map with all the bindings of - [s] that do not satisfy [p]. - *) val length: 'a t -> int - - val toList: 'a t -> (key * 'a) list -(** Return the list of all bindings of the given map. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Map.Make}. - *) - -val minKVOpt: 'a t -> (key * 'a) option -val minKVNull: 'a t -> (key * 'a) Js.null -val maxKVOpt: 'a t -> (key * 'a) option -val maxKVNull: 'a t -> (key * 'a) Js.null - +(** In increasing order *) +val toArray: 'a t -> (key * 'a) array +val ofArray: (key * 'a) array -> 'a t +val keysToArray: 'a t -> key array +val valuesToArray: 'a t -> 'a array +val minKeyOpt: _ t -> key option +val minKeyNull: _ t -> key Js.null +val maxKeyOpt: _ t -> key option +val maxKeyNull: _ t -> key Js.null +val minKeyValueOpt: 'a t -> (key * 'a) option +val minKeyValueNull: 'a t -> (key * 'a) Js.null +val maxKeyValueOpt: 'a t -> (key * 'a) option +val maxKeyValueNull: 'a t -> (key * 'a) Js.null +val findOpt: 'a t -> key -> 'a option +val findNull: 'a t -> key -> 'a Js.null +val findWithDefault: 'a t -> key -> 'a -> 'a +val findExn: 'a t -> key -> 'a + +(****************************************************************************) +(*TODO: add functional [merge, partition, filter, split]*) + +val addOnly : 'a t -> key -> 'a -> unit +val add: 'a t -> key -> 'a -> 'a t +(** [add m x y] do the in-place modification, return + [m] for chaining. If [x] was already bound + in [m], its previous binding disappears. *) -(* val split: key -> 'a t -> 'a t * 'a option * 'a t *) -(** [split x m] returns a triple [(l, data, r)], where - [l] is the map with all the bindings of [m] whose key - is strictly less than [x]; - [r] is the map with all the bindings of [m] whose key - is strictly greater than [x]; - [data] is [None] if [m] contains no binding for [x], - or [Some v] if [m] binds [v] to [x]. - *) +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn : 'a t -> key -> 'a val map: 'a t -> ('a -> 'b [@bs]) -> 'b t (** [map m f] returns a map with same domain as [m], where the diff --git a/jscomp/test/bs_map_test.js b/jscomp/test/bs_map_test.js index 1ece4acede..6258a7399b 100644 --- a/jscomp/test/bs_map_test.js +++ b/jscomp/test/bs_map_test.js @@ -89,7 +89,7 @@ for(var i$1 = 0; i$1 <= 100000; ++i$1){ console.log(data$1); function f(param) { - return Bs_Map.ofArray(Icmp, param); + return Bs_Map.ofArray(param, Icmp); } function $eq$tilde(a, b) { diff --git a/jscomp/test/bs_map_test.ml b/jscomp/test/bs_map_test.ml index c02bc54a80..5c469c7a7d 100644 --- a/jscomp/test/bs_map_test.ml +++ b/jscomp/test/bs_map_test.ml @@ -74,7 +74,7 @@ let () = done ; Js.log !data -let f = M.ofArray (module Icmp) +let f = M.ofArray ~dict:(module Icmp) let (=~) a b = M.eq a b let () = diff --git a/jscomp/test/bs_poly_map_test.js b/jscomp/test/bs_poly_map_test.js index 4a5ce30cfe..e52a954e5a 100644 --- a/jscomp/test/bs_poly_map_test.js +++ b/jscomp/test/bs_poly_map_test.js @@ -23,7 +23,7 @@ function b(loc, v) { var Icmp = /* module */[/* cmp */Caml_primitive.caml_int_compare]; function f(x) { - return Bs_Map.ofArray(Icmp, x); + return Bs_Map.ofArray(x, Icmp); } function ff(x) { @@ -79,11 +79,11 @@ function randomRange(i, j) { var x = randomRange(0, 100); -var u0 = Bs_Map.ofArray(Icmp, x); +var u0 = Bs_Map.ofArray(x, Icmp); var x$1 = randomRange(30, 120); -var u1 = Bs_Map.ofArray(Icmp, x$1); +var u1 = Bs_Map.ofArray(x$1, Icmp); var x$2 = Array_data_util.range(30, 100); @@ -103,7 +103,7 @@ b("File \"bs_poly_map_test.ml\", line 50, characters 4-11", Bs_Set.eq(mergeDiff( var x$6 = randomRange(0, 10); -var a0 = Bs_Map.ofArray(Icmp, x$6); +var a0 = Bs_Map.ofArray(x$6, Icmp); var a1 = Bs_Map.update(a0, 3, 33); diff --git a/jscomp/test/bs_poly_map_test.ml b/jscomp/test/bs_poly_map_test.ml index e2e6112465..c53949e563 100644 --- a/jscomp/test/bs_poly_map_test.ml +++ b/jscomp/test/bs_poly_map_test.ml @@ -14,7 +14,7 @@ module N = Bs.Set module A = Bs_Array module I = Array_data_util -let f x = M.ofArray (module Icmp) x +let f x = M.ofArray ~dict:(module Icmp) x let ff x = N.ofArray (module Icmp) x let mergeInter s1 s2 = diff --git a/lib/js/bs_Map.js b/lib/js/bs_Map.js index 104fd5a09f..3f30fc070a 100644 --- a/lib/js/bs_Map.js +++ b/lib/js/bs_Map.js @@ -205,7 +205,7 @@ function merge0(s1, s2, f, cmp) { } } -function ofArray(dict, data) { +function ofArray(data, dict) { return { dict: dict, data: Bs_internalAVLtree.ofArray0(dict[/* cmp */0], data) @@ -295,7 +295,7 @@ function isEmpty(map) { return Bs_internalAVLtree.isEmpty0(map.data); } -function singleton(dict, k, v) { +function singleton(k, v, dict) { return { dict: dict, data: Bs_internalAVLtree.singleton0(k, v) @@ -395,19 +395,35 @@ function valuesToArray(m) { return Bs_internalAVLtree.valuesToArray0(m.data); } -function minKVOpt(m) { +function minKeyOpt(m) { + return Bs_internalAVLtree.minKeyOpt0(m.data); +} + +function minKeyNull(m) { + return Bs_internalAVLtree.minKeyNull0(m.data); +} + +function maxKeyOpt(m) { + return Bs_internalAVLtree.maxKeyOpt0(m.data); +} + +function maxKeyNull(m) { + return Bs_internalAVLtree.maxKeyNull0(m.data); +} + +function minKeyValueOpt(m) { return Bs_internalAVLtree.minKVOpt0(m.data); } -function minKVNull(m) { +function minKeyValueNull(m) { return Bs_internalAVLtree.minKVNull0(m.data); } -function maxKVOpt(m) { +function maxKeyValueOpt(m) { return Bs_internalAVLtree.maxKVOpt0(m.data); } -function maxKVNull(m) { +function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } @@ -488,37 +504,41 @@ var map0 = Bs_internalAVLtree.map0; var mapi0 = Bs_internalAVLtree.mapi0; exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.update = update; -exports.updateArray = updateArray; -exports.updateWithOpt = updateWithOpt; exports.singleton = singleton; -exports.remove = remove; -exports.merge = merge; +exports.mem = mem; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; exports.fold = fold; exports.forAll = forAll; exports.exists = exists; -exports.filter = filter; -exports.partition = partition; exports.length = length; exports.toList = toList; exports.toArray = toArray; +exports.ofArray = ofArray; exports.keysToArray = keysToArray; exports.valuesToArray = valuesToArray; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; -exports.split = split; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.update = update; +exports.updateArray = updateArray; +exports.updateWithOpt = updateWithOpt; +exports.remove = remove; +exports.merge = merge; +exports.filter = filter; +exports.partition = partition; +exports.split = split; exports.map = map; exports.mapi = mapi; exports.empty0 = empty0; diff --git a/lib/js/bs_MapInt.js b/lib/js/bs_MapInt.js index cdf1096083..b3476eb5ef 100644 --- a/lib/js/bs_MapInt.js +++ b/lib/js/bs_MapInt.js @@ -79,15 +79,11 @@ function remove(n, x) { var empty = Bs_internalAVLtree.empty0; -var ofArray = Bs_internalMapInt.ofArray; - var isEmpty = Bs_internalAVLtree.isEmpty0; -var mem = Bs_internalMapInt.mem; - var singleton = Bs_internalAVLtree.singleton0; -var merge = Bs_internalMapInt.merge; +var mem = Bs_internalMapInt.mem; var cmp = Bs_internalMapInt.cmp; @@ -101,23 +97,33 @@ var forAll = Bs_internalAVLtree.forAll0; var exists = Bs_internalAVLtree.exists0; -var filter = Bs_internalAVLtree.filterShared0; - -var partition = Bs_internalAVLtree.partitionShared0; - var length = Bs_internalAVLtree.length0; var toList = Bs_internalAVLtree.toList0; -var minKVOpt = Bs_internalAVLtree.minKVOpt0; +var toArray = Bs_internalAVLtree.toArray0; -var minKVNull = Bs_internalAVLtree.minKVNull0; +var ofArray = Bs_internalMapInt.ofArray; -var maxKVOpt = Bs_internalAVLtree.maxKVOpt0; +var keysToArray = Bs_internalAVLtree.keysToArray0; -var maxKVNull = Bs_internalAVLtree.maxKVNull0; +var valuesToArray = Bs_internalAVLtree.valuesToArray0; -var split = Bs_internalMapInt.split; +var minKeyOpt = Bs_internalAVLtree.minKeyOpt0; + +var minKeyNull = Bs_internalAVLtree.minKeyNull0; + +var maxKeyOpt = Bs_internalAVLtree.maxKeyOpt0; + +var maxKeyNull = Bs_internalAVLtree.maxKeyNull0; + +var minKeyValueOpt = Bs_internalAVLtree.minKVOpt0; + +var minKeyValueNull = Bs_internalAVLtree.minKVNull0; + +var maxKeyValueOpt = Bs_internalAVLtree.maxKVOpt0; + +var maxKeyValueNull = Bs_internalAVLtree.maxKVNull0; var findOpt = Bs_internalMapInt.findOpt; @@ -127,6 +133,14 @@ var findWithDefault = Bs_internalMapInt.findWithDefault; var findExn = Bs_internalMapInt.findExn; +var merge = Bs_internalMapInt.merge; + +var filter = Bs_internalAVLtree.filterShared0; + +var partition = Bs_internalAVLtree.partitionShared0; + +var split = Bs_internalMapInt.split; + var map = Bs_internalAVLtree.map0; var mapi = Bs_internalAVLtree.mapi0; @@ -134,33 +148,40 @@ var mapi = Bs_internalAVLtree.mapi0; var checkInvariant = Bs_internalAVLtree.checkInvariant; exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.update = update; -exports.updateWithOpt = updateWithOpt; exports.singleton = singleton; -exports.remove = remove; -exports.merge = merge; +exports.mem = mem; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; exports.fold = fold; exports.forAll = forAll; exports.exists = exists; -exports.filter = filter; -exports.partition = partition; exports.length = length; exports.toList = toList; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; -exports.split = split; +exports.toArray = toArray; +exports.ofArray = ofArray; +exports.keysToArray = keysToArray; +exports.valuesToArray = valuesToArray; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.update = update; +exports.updateWithOpt = updateWithOpt; +exports.remove = remove; +exports.merge = merge; +exports.filter = filter; +exports.partition = partition; +exports.split = split; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; diff --git a/lib/js/bs_MapIntM.js b/lib/js/bs_MapIntM.js index c8541698a9..4cbc09289d 100644 --- a/lib/js/bs_MapIntM.js +++ b/lib/js/bs_MapIntM.js @@ -19,19 +19,35 @@ function singleton(k, v) { }; } -function minKVOpt(m) { +function minKeyNull(m) { + return Bs_internalAVLtree.minKeyNull0(m.data); +} + +function minKeyOpt(m) { + return Bs_internalAVLtree.minKeyOpt0(m.data); +} + +function maxKeyNull(m) { + return Bs_internalAVLtree.maxKeyNull0(m.data); +} + +function maxKeyOpt(m) { + return Bs_internalAVLtree.maxKeyOpt0(m.data); +} + +function minKeyValueOpt(m) { return Bs_internalAVLtree.minKVOpt0(m.data); } -function minKVNull(m) { +function minKeyValueNull(m) { return Bs_internalAVLtree.minKVNull0(m.data); } -function maxKVOpt(m) { +function maxKeyValueOpt(m) { return Bs_internalAVLtree.maxKVOpt0(m.data); } -function maxKVNull(m) { +function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } @@ -87,6 +103,18 @@ function toList(d) { return Bs_internalAVLtree.toList0(d.data); } +function toArray(d) { + return Bs_internalAVLtree.toArray0(d.data); +} + +function keysToArray(d) { + return Bs_internalAVLtree.keysToArray0(d.data); +} + +function valuesToArray(d) { + return Bs_internalAVLtree.valuesToArray0(d.data); +} + function checkInvariant(d) { return Bs_internalAVLtree.checkInvariant(d.data); } @@ -194,13 +222,9 @@ function findExn(d, x) { } exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.addOnly = addOnly; -exports.add = add; exports.singleton = singleton; -exports.remove = remove; +exports.mem = mem; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; @@ -209,14 +233,25 @@ exports.forAll = forAll; exports.exists = exists; exports.length = length; exports.toList = toList; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; +exports.toArray = toArray; +exports.ofArray = ofArray; +exports.keysToArray = keysToArray; +exports.valuesToArray = valuesToArray; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.addOnly = addOnly; +exports.add = add; +exports.remove = remove; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; diff --git a/lib/js/bs_MapM.js b/lib/js/bs_MapM.js index 38f5d6c857..6bdd5bfc40 100644 --- a/lib/js/bs_MapM.js +++ b/lib/js/bs_MapM.js @@ -123,19 +123,35 @@ function singleton(dict, x, v) { }; } -function minKVOpt(m) { +function minKeyOpt(m) { + return Bs_internalAVLtree.minKeyOpt0(m.data); +} + +function minKeyNull(m) { + return Bs_internalAVLtree.minKeyNull0(m.data); +} + +function maxKeyOpt(m) { + return Bs_internalAVLtree.maxKeyOpt0(m.data); +} + +function maxKeyNull(m) { + return Bs_internalAVLtree.maxKeyNull0(m.data); +} + +function minKeyValueOpt(m) { return Bs_internalAVLtree.minKVOpt0(m.data); } -function minKVNull(m) { +function minKeyValueNull(m) { return Bs_internalAVLtree.minKVNull0(m.data); } -function maxKVOpt(m) { +function maxKeyValueOpt(m) { return Bs_internalAVLtree.maxKVOpt0(m.data); } -function maxKVNull(m) { +function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } @@ -167,6 +183,14 @@ function toArray(d) { return Bs_internalAVLtree.toArray0(d.data); } +function keysToArray(d) { + return Bs_internalAVLtree.keysToArray0(d.data); +} + +function valuesToArray(d) { + return Bs_internalAVLtree.valuesToArray0(d.data); +} + function cmp(m1, m2, cmp$1) { var dict = m1.dict; var m1_data = m1.data; @@ -229,7 +253,7 @@ function mem(map, x) { return Bs_internalAVLtree.mem0(map$1, x, dict[/* cmp */0]); } -function ofArray(dict, data) { +function ofArray(data, dict) { return { dict: dict, data: Bs_internalAVLtree.ofArray0(dict[/* cmp */0], data) @@ -254,16 +278,9 @@ function update(m, e, v) { } exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; exports.singleton = singleton; exports.mem = mem; -exports.updateOnly = updateOnly; -exports.update = update; -exports.removeOnly = removeOnly; -exports.remove = remove; -exports.removeArrayOnly = removeArrayOnly; -exports.removeArray = removeArray; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; @@ -273,14 +290,27 @@ exports.exists = exists; exports.length = length; exports.toList = toList; exports.toArray = toArray; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; +exports.ofArray = ofArray; +exports.keysToArray = keysToArray; +exports.valuesToArray = valuesToArray; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.updateOnly = updateOnly; +exports.update = update; +exports.removeOnly = removeOnly; +exports.remove = remove; +exports.removeArrayOnly = removeArrayOnly; +exports.removeArray = removeArray; exports.map = map; exports.mapi = mapi; /* No side effect */ diff --git a/lib/js/bs_MapString.js b/lib/js/bs_MapString.js index 9ab90bfe53..01cafbd4c9 100644 --- a/lib/js/bs_MapString.js +++ b/lib/js/bs_MapString.js @@ -79,15 +79,11 @@ function remove(n, x) { var empty = Bs_internalAVLtree.empty0; -var ofArray = Bs_internalMapString.ofArray; - var isEmpty = Bs_internalAVLtree.isEmpty0; -var mem = Bs_internalMapString.mem; - var singleton = Bs_internalAVLtree.singleton0; -var merge = Bs_internalMapString.merge; +var mem = Bs_internalMapString.mem; var cmp = Bs_internalMapString.cmp; @@ -101,23 +97,33 @@ var forAll = Bs_internalAVLtree.forAll0; var exists = Bs_internalAVLtree.exists0; -var filter = Bs_internalAVLtree.filterShared0; - -var partition = Bs_internalAVLtree.partitionShared0; - var length = Bs_internalAVLtree.length0; var toList = Bs_internalAVLtree.toList0; -var minKVOpt = Bs_internalAVLtree.minKVOpt0; +var toArray = Bs_internalAVLtree.toArray0; -var minKVNull = Bs_internalAVLtree.minKVNull0; +var ofArray = Bs_internalMapString.ofArray; -var maxKVOpt = Bs_internalAVLtree.maxKVOpt0; +var keysToArray = Bs_internalAVLtree.keysToArray0; -var maxKVNull = Bs_internalAVLtree.maxKVNull0; +var valuesToArray = Bs_internalAVLtree.valuesToArray0; -var split = Bs_internalMapString.split; +var minKeyOpt = Bs_internalAVLtree.minKeyOpt0; + +var minKeyNull = Bs_internalAVLtree.minKeyNull0; + +var maxKeyOpt = Bs_internalAVLtree.maxKeyOpt0; + +var maxKeyNull = Bs_internalAVLtree.maxKeyNull0; + +var minKeyValueOpt = Bs_internalAVLtree.minKVOpt0; + +var minKeyValueNull = Bs_internalAVLtree.minKVNull0; + +var maxKeyValueOpt = Bs_internalAVLtree.maxKVOpt0; + +var maxKeyValueNull = Bs_internalAVLtree.maxKVNull0; var findOpt = Bs_internalMapString.findOpt; @@ -127,6 +133,14 @@ var findWithDefault = Bs_internalMapString.findWithDefault; var findExn = Bs_internalMapString.findExn; +var merge = Bs_internalMapString.merge; + +var filter = Bs_internalAVLtree.filterShared0; + +var partition = Bs_internalAVLtree.partitionShared0; + +var split = Bs_internalMapString.split; + var map = Bs_internalAVLtree.map0; var mapi = Bs_internalAVLtree.mapi0; @@ -134,33 +148,40 @@ var mapi = Bs_internalAVLtree.mapi0; var checkInvariant = Bs_internalAVLtree.checkInvariant; exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.update = update; -exports.updateWithOpt = updateWithOpt; exports.singleton = singleton; -exports.remove = remove; -exports.merge = merge; +exports.mem = mem; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; exports.fold = fold; exports.forAll = forAll; exports.exists = exists; -exports.filter = filter; -exports.partition = partition; exports.length = length; exports.toList = toList; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; -exports.split = split; +exports.toArray = toArray; +exports.ofArray = ofArray; +exports.keysToArray = keysToArray; +exports.valuesToArray = valuesToArray; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.update = update; +exports.updateWithOpt = updateWithOpt; +exports.remove = remove; +exports.merge = merge; +exports.filter = filter; +exports.partition = partition; +exports.split = split; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; diff --git a/lib/js/bs_MapStringM.js b/lib/js/bs_MapStringM.js index 117f6f54d6..c468ff4a5c 100644 --- a/lib/js/bs_MapStringM.js +++ b/lib/js/bs_MapStringM.js @@ -19,19 +19,35 @@ function singleton(k, v) { }; } -function minKVOpt(m) { +function minKeyNull(m) { + return Bs_internalAVLtree.minKeyNull0(m.data); +} + +function minKeyOpt(m) { + return Bs_internalAVLtree.minKeyOpt0(m.data); +} + +function maxKeyNull(m) { + return Bs_internalAVLtree.maxKeyNull0(m.data); +} + +function maxKeyOpt(m) { + return Bs_internalAVLtree.maxKeyOpt0(m.data); +} + +function minKeyValueOpt(m) { return Bs_internalAVLtree.minKVOpt0(m.data); } -function minKVNull(m) { +function minKeyValueNull(m) { return Bs_internalAVLtree.minKVNull0(m.data); } -function maxKVOpt(m) { +function maxKeyValueOpt(m) { return Bs_internalAVLtree.maxKVOpt0(m.data); } -function maxKVNull(m) { +function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } @@ -87,6 +103,18 @@ function toList(d) { return Bs_internalAVLtree.toList0(d.data); } +function toArray(d) { + return Bs_internalAVLtree.toArray0(d.data); +} + +function keysToArray(d) { + return Bs_internalAVLtree.keysToArray0(d.data); +} + +function valuesToArray(d) { + return Bs_internalAVLtree.valuesToArray0(d.data); +} + function checkInvariant(d) { return Bs_internalAVLtree.checkInvariant(d.data); } @@ -194,13 +222,9 @@ function findExn(d, x) { } exports.empty = empty; -exports.ofArray = ofArray; exports.isEmpty = isEmpty; -exports.mem = mem; -exports.addOnly = addOnly; -exports.add = add; exports.singleton = singleton; -exports.remove = remove; +exports.mem = mem; exports.cmp = cmp; exports.eq = eq; exports.iter = iter; @@ -209,14 +233,25 @@ exports.forAll = forAll; exports.exists = exists; exports.length = length; exports.toList = toList; -exports.minKVOpt = minKVOpt; -exports.minKVNull = minKVNull; -exports.maxKVOpt = maxKVOpt; -exports.maxKVNull = maxKVNull; +exports.toArray = toArray; +exports.ofArray = ofArray; +exports.keysToArray = keysToArray; +exports.valuesToArray = valuesToArray; +exports.minKeyOpt = minKeyOpt; +exports.minKeyNull = minKeyNull; +exports.maxKeyOpt = maxKeyOpt; +exports.maxKeyNull = maxKeyNull; +exports.minKeyValueOpt = minKeyValueOpt; +exports.minKeyValueNull = minKeyValueNull; +exports.maxKeyValueOpt = maxKeyValueOpt; +exports.maxKeyValueNull = maxKeyValueNull; exports.findOpt = findOpt; exports.findNull = findNull; exports.findWithDefault = findWithDefault; exports.findExn = findExn; +exports.addOnly = addOnly; +exports.add = add; +exports.remove = remove; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; From 4de5a64328b9d9a1a13e944db5a8cb7afec65045 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Wed, 17 Jan 2018 10:20:32 +0800 Subject: [PATCH 3/5] more consistent --- jscomp/others/bs_Map.ml | 8 ++++---- jscomp/others/bs_Map.mli | 13 +++++++------ jscomp/others/bs_MapInt.ml | 8 ++++---- jscomp/others/bs_MapInt.mli | 15 ++++++++------- jscomp/others/bs_MapIntM.ml | 8 ++++---- jscomp/others/bs_MapIntM.mli | 15 ++++++++------- jscomp/others/bs_MapM.ml | 8 ++++---- jscomp/others/bs_MapM.mli | 15 ++++++++------- jscomp/others/bs_MapString.ml | 8 ++++---- jscomp/others/bs_MapString.mli | 15 ++++++++------- jscomp/others/bs_MapStringM.ml | 8 ++++---- jscomp/others/bs_MapStringM.mli | 15 ++++++++------- jscomp/others/map.cppo.ml | 8 ++++---- jscomp/others/map.cppo.mli | 15 ++++++++------- jscomp/others/mapm.cppo.ml | 8 ++++---- jscomp/others/mapm.cppo.mli | 15 ++++++++------- jscomp/stdlib/camlinternalOO.ml | 12 ++++++------ jscomp/test/bs_MapInt_test.js | 2 +- jscomp/test/bs_MapInt_test.ml | 2 +- jscomp/test/bs_map_test.js | 4 ++-- jscomp/test/bs_map_test.ml | 4 ++-- jscomp/test/bs_poly_map_test.js | 10 +++++----- jscomp/test/bs_poly_map_test.ml | 10 +++++----- lib/js/bs_Map.js | 18 +++++++++--------- lib/js/bs_MapInt.js | 18 +++++++++--------- lib/js/bs_MapIntM.js | 18 +++++++++--------- lib/js/bs_MapM.js | 18 +++++++++--------- lib/js/bs_MapString.js | 18 +++++++++--------- lib/js/bs_MapStringM.js | 18 +++++++++--------- lib/js/camlinternalOO.js | 16 ++++++++-------- 30 files changed, 179 insertions(+), 171 deletions(-) diff --git a/jscomp/others/bs_Map.ml b/jscomp/others/bs_Map.ml index 0becb73d6e..6f2c072a9f 100644 --- a/jscomp/others/bs_Map.ml +++ b/jscomp/others/bs_Map.ml @@ -279,22 +279,22 @@ let minKeyValueNull m = N.minKVNull0 (B.data m) let maxKeyValueOpt m = N.maxKVOpt0 (B.data m) let maxKeyValueNull m = N.maxKVNull0 (B.data m) -let findOpt (type k) (type id) (map : (k,_,id) t) x = +let getOpt (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findOpt0 ~cmp:X.cmp map x -let findNull (type k) (type id) (map : (k,_,id) t) x = +let getNull (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findNull0 ~cmp:X.cmp map x -let findWithDefault (type k) (type id) (map : (k,_,id) t) x def = +let getWithDefault (type k) (type id) (map : (k,_,id) t) x def = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findWithDefault0 ~cmp:X.cmp map x def -let findExn (type k) (type id) (map : (k,_,id) t) x = +let getExn (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findExn0 ~cmp:X.cmp map x diff --git a/jscomp/others/bs_Map.mli b/jscomp/others/bs_Map.mli index 36c7570377..6a7c5c7cfc 100644 --- a/jscomp/others/bs_Map.mli +++ b/jscomp/others/bs_Map.mli @@ -100,14 +100,17 @@ val minKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option val minKeyValueNull: ('k, 'a, _) t -> ('k * 'a) Js.null val maxKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option val maxKeyValueNull:('k, 'a, _) t -> ('k * 'a) Js.null -val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option -val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null -val findWithDefault: +val getOpt: ('k, 'a, 'id) t -> 'k -> 'a option +val getNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null +val getWithDefault: ('k, 'a, 'id) t -> 'k -> 'a -> 'a -val findExn: ('k, 'a, 'id) t -> 'k -> 'a +val getExn: ('k, 'a, 'id) t -> 'k -> 'a (****************************************************************************) +val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t +(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *) + val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t (** [update m x y ] returns a map containing the same bindings as @@ -121,8 +124,6 @@ val updateWithOpt: ('k option -> 'a option [@bs]) -> ('k, 'a, 'id) t -val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t -(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *) val merge: ('k, 'a, 'id ) t -> diff --git a/jscomp/others/bs_MapInt.ml b/jscomp/others/bs_MapInt.ml index d49954612c..b10f265a59 100644 --- a/jscomp/others/bs_MapInt.ml +++ b/jscomp/others/bs_MapInt.ml @@ -94,10 +94,10 @@ let rec remove n (x : key) = let mem = I.mem let cmp = I.cmp let eq = I.eq -let findOpt = I.findOpt -let findNull = I.findNull -let findWithDefault = I.findWithDefault -let findExn = I.findExn +let getOpt = I.findOpt +let getNull = I.findNull +let getWithDefault = I.findWithDefault +let getExn = I.findExn let split = I.split let merge = I.merge let ofArray = I.ofArray diff --git a/jscomp/others/bs_MapInt.mli b/jscomp/others/bs_MapInt.mli index 5e3d58d289..922daf8557 100644 --- a/jscomp/others/bs_MapInt.mli +++ b/jscomp/others/bs_MapInt.mli @@ -51,13 +51,17 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as [m], plus a binding of [x] to [y]. If [x] was already bound @@ -69,9 +73,6 @@ val updateWithOpt: 'a t -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) val merge: diff --git a/jscomp/others/bs_MapIntM.ml b/jscomp/others/bs_MapIntM.ml index 90ab3f7ae2..e394843119 100644 --- a/jscomp/others/bs_MapIntM.ml +++ b/jscomp/others/bs_MapIntM.ml @@ -109,8 +109,8 @@ let cmp d0 d1 = I.cmp (data d0) (data d1) let eq d0 d1 = I.eq (data d0) (data d1) -let findOpt d x = +let getOpt d x = I.findOpt (data d) x -let findNull d x = I.findNull (data d) x -let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x +let getNull d x = I.findNull (data d) x +let getWithDefault d x def = I.findWithDefault (data d) x def +let getExn d x = I.findExn (data d) x diff --git a/jscomp/others/bs_MapIntM.mli b/jscomp/others/bs_MapIntM.mli index 96345598af..61a71489a8 100644 --- a/jscomp/others/bs_MapIntM.mli +++ b/jscomp/others/bs_MapIntM.mli @@ -57,15 +57,18 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) (*TODO: add functional [merge, partition, filter, split]*) - + +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) + val addOnly : 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return @@ -73,8 +76,6 @@ val add: 'a t -> key -> 'a -> 'a t in [m], its previous binding disappears. *) -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) val map: 'a t -> ('a -> 'b [@bs]) -> 'b t diff --git a/jscomp/others/bs_MapM.ml b/jscomp/others/bs_MapM.ml index be16562a4a..384adb6314 100644 --- a/jscomp/others/bs_MapM.ml +++ b/jscomp/others/bs_MapM.ml @@ -151,19 +151,19 @@ let map m f = let mapi map f = let dict,map = B.(dict map, data map) in B.bag ~dict ~data:(N.mapi0 map f) -let findOpt (type k) (type id) (map : (k,_,id) t) x = +let getOpt (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findOpt0 ~cmp:X.cmp map x -let findNull (type k) (type id) (map : (k,_,id) t) x = +let getNull (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findNull0 ~cmp:X.cmp map x -let findWithDefault (type k) (type id) (map : (k,_,id) t) x def = +let getWithDefault (type k) (type id) (map : (k,_,id) t) x def = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findWithDefault0 ~cmp:X.cmp map x def -let findExn (type k) (type id) (map : (k,_,id) t) x = +let getExn (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findExn0 ~cmp:X.cmp map x diff --git a/jscomp/others/bs_MapM.mli b/jscomp/others/bs_MapM.mli index 939de6d0c7..dcde0f5468 100644 --- a/jscomp/others/bs_MapM.mli +++ b/jscomp/others/bs_MapM.mli @@ -84,14 +84,18 @@ val minKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option val minKeyValueNull: ('k, 'a, _) t -> ('k * 'a) Js.null val maxKeyValueOpt: ('k, 'a, _) t -> ('k * 'a) option val maxKeyValueNull:('k, 'a, _) t -> ('k * 'a) Js.null -val findOpt: ('k, 'a, 'id) t -> 'k -> 'a option -val findNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null -val findWithDefault: +val getOpt: ('k, 'a, 'id) t -> 'k -> 'a option +val getNull: ('k, 'a, 'id) t -> 'k -> 'a Js.null +val getWithDefault: ('k, 'a, 'id) t -> 'k -> 'a -> 'a -val findExn: ('k, 'a, 'id) t -> 'k -> 'a +val getExn: ('k, 'a, 'id) t -> 'k -> 'a (****************************************************************************) +val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t +(** [remove m x] do the in-place modification, + returnning [m] for chaining. *) + (*TODO: add functional [merge, partition, filter, split]*) val updateOnly: ('k, 'a, 'id) t -> 'k -> 'a -> unit @@ -99,9 +103,6 @@ val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t (** [update m x y ] do the in-place modification, returnning [m] for chaining. *) val removeOnly: ('k, 'a, 'id) t -> 'k -> unit -val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t -(** [remove m x] do the in-place modification, - returnning [m] for chaining. *) val removeArrayOnly: ('k, 'a, 'id) t -> 'k array -> unit val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t diff --git a/jscomp/others/bs_MapString.ml b/jscomp/others/bs_MapString.ml index 024377a04a..1d4d95d096 100644 --- a/jscomp/others/bs_MapString.ml +++ b/jscomp/others/bs_MapString.ml @@ -94,10 +94,10 @@ let rec remove n (x : key) = let mem = I.mem let cmp = I.cmp let eq = I.eq -let findOpt = I.findOpt -let findNull = I.findNull -let findWithDefault = I.findWithDefault -let findExn = I.findExn +let getOpt = I.findOpt +let getNull = I.findNull +let getWithDefault = I.findWithDefault +let getExn = I.findExn let split = I.split let merge = I.merge let ofArray = I.ofArray diff --git a/jscomp/others/bs_MapString.mli b/jscomp/others/bs_MapString.mli index f98487a199..e161756a93 100644 --- a/jscomp/others/bs_MapString.mli +++ b/jscomp/others/bs_MapString.mli @@ -51,13 +51,17 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as [m], plus a binding of [x] to [y]. If [x] was already bound @@ -69,9 +73,6 @@ val updateWithOpt: 'a t -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) val merge: diff --git a/jscomp/others/bs_MapStringM.ml b/jscomp/others/bs_MapStringM.ml index a40f517116..bf91d7128b 100644 --- a/jscomp/others/bs_MapStringM.ml +++ b/jscomp/others/bs_MapStringM.ml @@ -109,8 +109,8 @@ let cmp d0 d1 = I.cmp (data d0) (data d1) let eq d0 d1 = I.eq (data d0) (data d1) -let findOpt d x = +let getOpt d x = I.findOpt (data d) x -let findNull d x = I.findNull (data d) x -let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x +let getNull d x = I.findNull (data d) x +let getWithDefault d x def = I.findWithDefault (data d) x def +let getExn d x = I.findExn (data d) x diff --git a/jscomp/others/bs_MapStringM.mli b/jscomp/others/bs_MapStringM.mli index cfcb6b6ac3..e9b8ee04b5 100644 --- a/jscomp/others/bs_MapStringM.mli +++ b/jscomp/others/bs_MapStringM.mli @@ -57,15 +57,18 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) (*TODO: add functional [merge, partition, filter, split]*) - + +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) + val addOnly : 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return @@ -73,8 +76,6 @@ val add: 'a t -> key -> 'a -> 'a t in [m], its previous binding disappears. *) -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) val map: 'a t -> ('a -> 'b [@bs]) -> 'b t diff --git a/jscomp/others/map.cppo.ml b/jscomp/others/map.cppo.ml index 258d11bf80..41046ae488 100644 --- a/jscomp/others/map.cppo.ml +++ b/jscomp/others/map.cppo.ml @@ -99,10 +99,10 @@ let rec remove n (x : key) = let mem = I.mem let cmp = I.cmp let eq = I.eq -let findOpt = I.findOpt -let findNull = I.findNull -let findWithDefault = I.findWithDefault -let findExn = I.findExn +let getOpt = I.findOpt +let getNull = I.findNull +let getWithDefault = I.findWithDefault +let getExn = I.findExn let split = I.split let merge = I.merge let ofArray = I.ofArray diff --git a/jscomp/others/map.cppo.mli b/jscomp/others/map.cppo.mli index 0300e73f27..4e954dfc90 100644 --- a/jscomp/others/map.cppo.mli +++ b/jscomp/others/map.cppo.mli @@ -55,13 +55,17 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) +val remove: 'a t -> key -> 'a t +(** [remove m x] returns a map containing the same bindings as + [m], except for [x] which is unbound in the returned map. *) + val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as [m], plus a binding of [x] to [y]. If [x] was already bound @@ -73,9 +77,6 @@ val updateWithOpt: 'a t -val remove: 'a t -> key -> 'a t -(** [remove m x] returns a map containing the same bindings as - [m], except for [x] which is unbound in the returned map. *) val merge: diff --git a/jscomp/others/mapm.cppo.ml b/jscomp/others/mapm.cppo.ml index f0dd635d37..38394de0b3 100644 --- a/jscomp/others/mapm.cppo.ml +++ b/jscomp/others/mapm.cppo.ml @@ -115,8 +115,8 @@ let cmp d0 d1 = I.cmp (data d0) (data d1) let eq d0 d1 = I.eq (data d0) (data d1) -let findOpt d x = +let getOpt d x = I.findOpt (data d) x -let findNull d x = I.findNull (data d) x -let findWithDefault d x def = I.findWithDefault (data d) x def -let findExn d x = I.findExn (data d) x +let getNull d x = I.findNull (data d) x +let getWithDefault d x def = I.findWithDefault (data d) x def +let getExn d x = I.findExn (data d) x diff --git a/jscomp/others/mapm.cppo.mli b/jscomp/others/mapm.cppo.mli index a462d51a38..d67a41b25f 100644 --- a/jscomp/others/mapm.cppo.mli +++ b/jscomp/others/mapm.cppo.mli @@ -61,15 +61,18 @@ val minKeyValueOpt: 'a t -> (key * 'a) option val minKeyValueNull: 'a t -> (key * 'a) Js.null val maxKeyValueOpt: 'a t -> (key * 'a) option val maxKeyValueNull: 'a t -> (key * 'a) Js.null -val findOpt: 'a t -> key -> 'a option -val findNull: 'a t -> key -> 'a Js.null -val findWithDefault: 'a t -> key -> 'a -> 'a -val findExn: 'a t -> key -> 'a +val getOpt: 'a t -> key -> 'a option +val getNull: 'a t -> key -> 'a Js.null +val getWithDefault: 'a t -> key -> 'a -> 'a +val getExn: 'a t -> key -> 'a (****************************************************************************) (*TODO: add functional [merge, partition, filter, split]*) - + +val remove: 'a t -> key -> 'a t +(** [remove m x] do the in-place modification, return [m] for chaining *) + val addOnly : 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return @@ -77,8 +80,6 @@ val add: 'a t -> key -> 'a -> 'a t in [m], its previous binding disappears. *) -val remove: 'a t -> key -> 'a t -(** [remove m x] do the in-place modification, return [m] for chaining *) val map: 'a t -> ('a -> 'b [@bs]) -> 'b t diff --git a/jscomp/stdlib/camlinternalOO.ml b/jscomp/stdlib/camlinternalOO.ml index 2c44867ab5..65bdbb3653 100644 --- a/jscomp/stdlib/camlinternalOO.ml +++ b/jscomp/stdlib/camlinternalOO.ml @@ -185,7 +185,7 @@ let new_method table = let get_method_label table name = #if BS then - match Js.nullToOption (Meths.findNull table.methods_by_name name) + match Js.nullToOption (Meths.getNull table.methods_by_name name) with | Some x -> x | None -> @@ -210,7 +210,7 @@ let set_method table label element = incr method_count; if #if BS then - Labs.findExn table.methods_by_label label + Labs.getExn table.methods_by_label label #else Labs.find label table.methods_by_label #end @@ -254,7 +254,7 @@ let narrow table vars virt_meths concr_meths = by_name := Meths.update !by_name met label; by_label := Labs.update !by_label label - (Labs.findWithDefault table.methods_by_label label true) + (Labs.getWithDefault table.methods_by_label label true) ) concr_meths concr_meth_labs; List.iter2 (fun met label -> @@ -293,7 +293,7 @@ let widen table = table.vars <- List.fold_left #if BS then - (fun s v -> Vars.update s v (Vars.findExn table.vars v)) + (fun s v -> Vars.update s v (Vars.getExn table.vars v)) #else (fun s v -> Vars.add v (Vars.find v table.vars) s) #end @@ -314,7 +314,7 @@ let new_slot table = let new_variable table name = #if BS then - match Js.nullToOption (Vars.findNull table.vars name : int Js.null) with + match Js.nullToOption (Vars.getNull table.vars name : int Js.null) with | Some x -> x | None -> let index = new_slot table in @@ -345,7 +345,7 @@ let new_methods_variables table meths vals = let get_variable table name = #if BS then - Vars.findExn table.vars name + Vars.getExn table.vars name #else try Vars.find name table.vars with Not_found -> assert false #end diff --git a/jscomp/test/bs_MapInt_test.js b/jscomp/test/bs_MapInt_test.js index e7302cc7bb..91e2a495cb 100644 --- a/jscomp/test/bs_MapInt_test.js +++ b/jscomp/test/bs_MapInt_test.js @@ -16,7 +16,7 @@ function test() { m = Bs_MapInt.update(m, i, i); } for(var i$1 = 0; i$1 <= 999999; ++i$1){ - should(+(Bs_MapInt.findOpt(m, i$1) !== /* None */0)); + should(+(Bs_MapInt.getOpt(m, i$1) !== /* None */0)); } for(var i$2 = 0; i$2 <= 999999; ++i$2){ m = Bs_MapInt.remove(m, i$2); diff --git a/jscomp/test/bs_MapInt_test.ml b/jscomp/test/bs_MapInt_test.ml index e86f01330c..49e1beedd8 100644 --- a/jscomp/test/bs_MapInt_test.ml +++ b/jscomp/test/bs_MapInt_test.ml @@ -7,7 +7,7 @@ let test () = m := Bs.MapInt.update !m i i done; for i = 0 to count do - should (Bs.MapInt.findOpt !m i <> None) + should (Bs.MapInt.getOpt !m i <> None) done; for i = 0 to count do m := Bs.MapInt.remove !m i ; diff --git a/jscomp/test/bs_map_test.js b/jscomp/test/bs_map_test.js index 6258a7399b..9128b9c117 100644 --- a/jscomp/test/bs_map_test.js +++ b/jscomp/test/bs_map_test.js @@ -133,9 +133,9 @@ b("File \"bs_map_test.ml\", line 88, characters 4-11", Bs_List.forAll2(Bs_intern } }))); -eq("File \"bs_map_test.ml\", line 93, characters 5-12", Bs_Map.findOpt(u0, 39), /* Some */[39]); +eq("File \"bs_map_test.ml\", line 93, characters 5-12", Bs_Map.getOpt(u0, 39), /* Some */[39]); -eq("File \"bs_map_test.ml\", line 94, characters 5-12", Bs_Map.findOpt(u1, 39), /* Some */[120]); +eq("File \"bs_map_test.ml\", line 94, characters 5-12", Bs_Map.getOpt(u1, 39), /* Some */[120]); var u = f(Bs_Array.shuffle(Bs_Array.init(10000, (function (x) { return /* tuple */[ diff --git a/jscomp/test/bs_map_test.ml b/jscomp/test/bs_map_test.ml index 5c469c7a7d..292d7fe8b4 100644 --- a/jscomp/test/bs_map_test.ml +++ b/jscomp/test/bs_map_test.ml @@ -90,8 +90,8 @@ let () = (M.toList u0) (A.toList (A.map (I.range 0 39) (fun [@bs] x -> (x,x)))) (fun[@bs] (x0,x1) (y0,y1) -> x0 = y0 && x1 = y1)); - eq __LOC__ (M.findOpt u0 39) (Some 39); - eq __LOC__ (M.findOpt u1 39) (Some 120) + eq __LOC__ (M.getOpt u0 39) (Some 39); + eq __LOC__ (M.getOpt u1 39) (Some 120) let () = diff --git a/jscomp/test/bs_poly_map_test.js b/jscomp/test/bs_poly_map_test.js index e52a954e5a..c2979f26d2 100644 --- a/jscomp/test/bs_poly_map_test.js +++ b/jscomp/test/bs_poly_map_test.js @@ -135,15 +135,15 @@ b("File \"bs_poly_map_test.ml\", line 70, characters 4-11", Bs_Map.mem(a0, 3)); b("File \"bs_poly_map_test.ml\", line 71, characters 4-11", 1 - Bs_Map.mem(a5, 3)); -b("File \"bs_poly_map_test.ml\", line 72, characters 4-11", +(3 === Bs_Map.findNull(a0, 3))); +b("File \"bs_poly_map_test.ml\", line 72, characters 4-11", +(3 === Bs_Map.getNull(a0, 3))); -b("File \"bs_poly_map_test.ml\", line 73, characters 4-11", +(33 === Bs_Map.findNull(a1, 3))); +b("File \"bs_poly_map_test.ml\", line 73, characters 4-11", +(33 === Bs_Map.getNull(a1, 3))); -b("File \"bs_poly_map_test.ml\", line 74, characters 4-11", +(Bs_Map.findNull(a2, 3) === null)); +b("File \"bs_poly_map_test.ml\", line 74, characters 4-11", +(Bs_Map.getNull(a2, 3) === null)); -b("File \"bs_poly_map_test.ml\", line 76, characters 4-11", +(11 === Bs_Map.findNull(a3, 3))); +b("File \"bs_poly_map_test.ml\", line 76, characters 4-11", +(11 === Bs_Map.getNull(a3, 3))); -b("File \"bs_poly_map_test.ml\", line 77, characters 4-11", +(Bs_Map.findNull(a4, 3) === null)); +b("File \"bs_poly_map_test.ml\", line 77, characters 4-11", +(Bs_Map.getNull(a4, 3) === null)); Mt.from_pair_suites("bs_poly_map_test.ml", suites[0]); diff --git a/jscomp/test/bs_poly_map_test.ml b/jscomp/test/bs_poly_map_test.ml index c53949e563..5f5b47c108 100644 --- a/jscomp/test/bs_poly_map_test.ml +++ b/jscomp/test/bs_poly_map_test.ml @@ -69,10 +69,10 @@ let () = b __LOC__ (a5 == a6); b __LOC__ (M.mem a0 3); b __LOC__ (not (M.mem a5 3)); - b __LOC__ (Js.eqNull 3 (M.findNull a0 3)); - b __LOC__ (Js.eqNull 33 (M.findNull a1 3)); - b __LOC__ (Js.Null.test (M.findNull a2 3)); + b __LOC__ (Js.eqNull 3 (M.getNull a0 3)); + b __LOC__ (Js.eqNull 33 (M.getNull a1 3)); + b __LOC__ (Js.Null.test (M.getNull a2 3)); - b __LOC__ (Js.eqNull 11 (M.findNull a3 3)); - b __LOC__ (Js.Null.test (M.findNull a4 3)) + b __LOC__ (Js.eqNull 11 (M.getNull a3 3)); + b __LOC__ (Js.Null.test (M.getNull a4 3)) ;; Mt.from_pair_suites __FILE__ !suites \ No newline at end of file diff --git a/lib/js/bs_Map.js b/lib/js/bs_Map.js index 3f30fc070a..060d647ca6 100644 --- a/lib/js/bs_Map.js +++ b/lib/js/bs_Map.js @@ -427,25 +427,25 @@ function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } -function findOpt(map, x) { +function getOpt(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findOpt0(map$1, x, dict[/* cmp */0]); } -function findNull(map, x) { +function getNull(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findNull0(map$1, x, dict[/* cmp */0]); } -function findWithDefault(map, x, def) { +function getWithDefault(map, x, def) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findWithDefault0(map$1, x, def, dict[/* cmp */0]); } -function findExn(map, x) { +function getExn(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findExn0(map$1, x, dict[/* cmp */0]); @@ -527,14 +527,14 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.update = update; exports.updateArray = updateArray; exports.updateWithOpt = updateWithOpt; -exports.remove = remove; exports.merge = merge; exports.filter = filter; exports.partition = partition; diff --git a/lib/js/bs_MapInt.js b/lib/js/bs_MapInt.js index b3476eb5ef..d1f5bce7fc 100644 --- a/lib/js/bs_MapInt.js +++ b/lib/js/bs_MapInt.js @@ -125,13 +125,13 @@ var maxKeyValueOpt = Bs_internalAVLtree.maxKVOpt0; var maxKeyValueNull = Bs_internalAVLtree.maxKVNull0; -var findOpt = Bs_internalMapInt.findOpt; +var getOpt = Bs_internalMapInt.findOpt; -var findNull = Bs_internalMapInt.findNull; +var getNull = Bs_internalMapInt.findNull; -var findWithDefault = Bs_internalMapInt.findWithDefault; +var getWithDefault = Bs_internalMapInt.findWithDefault; -var findExn = Bs_internalMapInt.findExn; +var getExn = Bs_internalMapInt.findExn; var merge = Bs_internalMapInt.merge; @@ -171,13 +171,13 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.update = update; exports.updateWithOpt = updateWithOpt; -exports.remove = remove; exports.merge = merge; exports.filter = filter; exports.partition = partition; diff --git a/lib/js/bs_MapIntM.js b/lib/js/bs_MapIntM.js index 4cbc09289d..1cd4a9c4c4 100644 --- a/lib/js/bs_MapIntM.js +++ b/lib/js/bs_MapIntM.js @@ -205,19 +205,19 @@ function eq(d0, d1) { }); } -function findOpt(d, x) { +function getOpt(d, x) { return Bs_internalMapInt.findOpt(d.data, x); } -function findNull(d, x) { +function getNull(d, x) { return Bs_internalMapInt.findNull(d.data, x); } -function findWithDefault(d, x, def) { +function getWithDefault(d, x, def) { return Bs_internalMapInt.findWithDefault(d.data, x, def); } -function findExn(d, x) { +function getExn(d, x) { return Bs_internalMapInt.findExn(d.data, x); } @@ -245,13 +245,13 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.addOnly = addOnly; exports.add = add; -exports.remove = remove; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; diff --git a/lib/js/bs_MapM.js b/lib/js/bs_MapM.js index 6bdd5bfc40..bd151cdf99 100644 --- a/lib/js/bs_MapM.js +++ b/lib/js/bs_MapM.js @@ -223,25 +223,25 @@ function mapi(map, f) { }; } -function findOpt(map, x) { +function getOpt(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findOpt0(map$1, x, dict[/* cmp */0]); } -function findNull(map, x) { +function getNull(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findNull0(map$1, x, dict[/* cmp */0]); } -function findWithDefault(map, x, def) { +function getWithDefault(map, x, def) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findWithDefault0(map$1, x, def, dict[/* cmp */0]); } -function findExn(map, x) { +function getExn(map, x) { var dict = map.dict; var map$1 = map.data; return Bs_internalAVLtree.findExn0(map$1, x, dict[/* cmp */0]); @@ -301,14 +301,14 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.updateOnly = updateOnly; exports.update = update; exports.removeOnly = removeOnly; -exports.remove = remove; exports.removeArrayOnly = removeArrayOnly; exports.removeArray = removeArray; exports.map = map; diff --git a/lib/js/bs_MapString.js b/lib/js/bs_MapString.js index 01cafbd4c9..2d0b61956d 100644 --- a/lib/js/bs_MapString.js +++ b/lib/js/bs_MapString.js @@ -125,13 +125,13 @@ var maxKeyValueOpt = Bs_internalAVLtree.maxKVOpt0; var maxKeyValueNull = Bs_internalAVLtree.maxKVNull0; -var findOpt = Bs_internalMapString.findOpt; +var getOpt = Bs_internalMapString.findOpt; -var findNull = Bs_internalMapString.findNull; +var getNull = Bs_internalMapString.findNull; -var findWithDefault = Bs_internalMapString.findWithDefault; +var getWithDefault = Bs_internalMapString.findWithDefault; -var findExn = Bs_internalMapString.findExn; +var getExn = Bs_internalMapString.findExn; var merge = Bs_internalMapString.merge; @@ -171,13 +171,13 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.update = update; exports.updateWithOpt = updateWithOpt; -exports.remove = remove; exports.merge = merge; exports.filter = filter; exports.partition = partition; diff --git a/lib/js/bs_MapStringM.js b/lib/js/bs_MapStringM.js index c468ff4a5c..3d49275433 100644 --- a/lib/js/bs_MapStringM.js +++ b/lib/js/bs_MapStringM.js @@ -205,19 +205,19 @@ function eq(d0, d1) { }); } -function findOpt(d, x) { +function getOpt(d, x) { return Bs_internalMapString.findOpt(d.data, x); } -function findNull(d, x) { +function getNull(d, x) { return Bs_internalMapString.findNull(d.data, x); } -function findWithDefault(d, x, def) { +function getWithDefault(d, x, def) { return Bs_internalMapString.findWithDefault(d.data, x, def); } -function findExn(d, x) { +function getExn(d, x) { return Bs_internalMapString.findExn(d.data, x); } @@ -245,13 +245,13 @@ exports.minKeyValueOpt = minKeyValueOpt; exports.minKeyValueNull = minKeyValueNull; exports.maxKeyValueOpt = maxKeyValueOpt; exports.maxKeyValueNull = maxKeyValueNull; -exports.findOpt = findOpt; -exports.findNull = findNull; -exports.findWithDefault = findWithDefault; -exports.findExn = findExn; +exports.getOpt = getOpt; +exports.getNull = getNull; +exports.getWithDefault = getWithDefault; +exports.getExn = getExn; +exports.remove = remove; exports.addOnly = addOnly; exports.add = add; -exports.remove = remove; exports.map = map; exports.mapi = mapi; exports.checkInvariant = checkInvariant; diff --git a/lib/js/camlinternalOO.js b/lib/js/camlinternalOO.js index 88e63b2c48..52398ddbc9 100644 --- a/lib/js/camlinternalOO.js +++ b/lib/js/camlinternalOO.js @@ -106,7 +106,7 @@ function new_method(table) { } function get_method_label(table, name) { - var match = Bs_MapString.findNull(table[/* methods_by_name */2], name); + var match = Bs_MapString.getNull(table[/* methods_by_name */2], name); if (match !== null) { return match; } else { @@ -125,7 +125,7 @@ function get_method_labels(table, names) { function set_method(table, label, element) { method_count[0] = method_count[0] + 1 | 0; - if (Bs_MapInt.findExn(table[/* methods_by_label */3], label)) { + if (Bs_MapInt.getExn(table[/* methods_by_label */3], label)) { var array = table; var label$1 = label; var element$1 = element; @@ -196,7 +196,7 @@ function narrow(table, vars, virt_meths, concr_meths) { var by_label = [Bs_MapInt.empty]; List.iter2((function (met, label) { by_name[0] = Bs_MapString.update(by_name[0], met, label); - by_label[0] = Bs_MapInt.update(by_label[0], label, Bs_MapInt.findWithDefault(table[/* methods_by_label */3], label, /* true */1)); + by_label[0] = Bs_MapInt.update(by_label[0], label, Bs_MapInt.getWithDefault(table[/* methods_by_label */3], label, /* true */1)); return /* () */0; }), concr_meths$1, concr_meth_labs); List.iter2((function (met, label) { @@ -224,7 +224,7 @@ function widen(table) { var virt_meths = match[4]; table[/* previous_states */4] = List.tl(table[/* previous_states */4]); table[/* vars */6] = List.fold_left((function (s, v) { - return Bs_MapString.update(s, v, Bs_MapString.findExn(table[/* vars */6], v)); + return Bs_MapString.update(s, v, Bs_MapString.getExn(table[/* vars */6], v)); }), match[3], match[5]); table[/* methods_by_name */2] = match[0]; table[/* methods_by_label */3] = match[1]; @@ -248,7 +248,7 @@ function new_slot(table) { } function new_variable(table, name) { - var match = Bs_MapString.findNull(table[/* vars */6], name); + var match = Bs_MapString.getNull(table[/* vars */6], name); if (match !== null) { return match; } else { @@ -283,12 +283,12 @@ function new_methods_variables(table, meths, vals) { } function get_variable(table, name) { - return Bs_MapString.findExn(table[/* vars */6], name); + return Bs_MapString.getExn(table[/* vars */6], name); } function get_variables(table, names) { return $$Array.map((function (param) { - return Bs_MapString.findExn(table[/* vars */6], param); + return Bs_MapString.getExn(table[/* vars */6], param); }), names); } @@ -331,7 +331,7 @@ function inherits(cla, vals, virt_meths, concr_meths, param, top) { /* array */[init], /* :: */[ $$Array.map((function (param) { - return Bs_MapString.findExn(cla[/* vars */6], param); + return Bs_MapString.getExn(cla[/* vars */6], param); }), to_array(vals)), /* :: */[ $$Array.map((function (nm) { From a84595336ea7620314b793de7443d3568d0e850c Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Wed, 17 Jan 2018 11:43:29 +0800 Subject: [PATCH 4/5] add removeArray for all modules --- jscomp/others/bs_Array.ml | 4 +- jscomp/others/bs_Array.mli | 2 +- jscomp/others/bs_Map.ml | 102 ++++++++++++------ jscomp/others/bs_Map.mli | 13 ++- jscomp/others/bs_MapInt.ml | 39 +++++-- jscomp/others/bs_MapInt.mli | 1 + jscomp/others/bs_MapIntM.ml | 47 ++++++--- jscomp/others/bs_MapIntM.mli | 7 +- jscomp/others/bs_MapM.ml | 23 ++-- jscomp/others/bs_MapM.mli | 13 ++- jscomp/others/bs_MapString.ml | 39 +++++-- jscomp/others/bs_MapString.mli | 1 + jscomp/others/bs_MapStringM.ml | 47 ++++++--- jscomp/others/bs_MapStringM.mli | 7 +- jscomp/others/bs_SetIntM.ml | 10 +- jscomp/others/bs_SetIntM.mli | 6 +- jscomp/others/bs_SetM.ml | 18 ++-- jscomp/others/bs_SetM.mli | 8 +- jscomp/others/bs_SetStringM.ml | 10 +- jscomp/others/bs_SetStringM.mli | 6 +- jscomp/others/map.cppo.ml | 39 +++++-- jscomp/others/map.cppo.mli | 1 + jscomp/others/mapm.cppo.ml | 47 ++++++--- jscomp/others/mapm.cppo.mli | 7 +- jscomp/others/setm.cppo.ml | 10 +- jscomp/others/setm.cppo.mli | 6 +- jscomp/test/bs_array_test.js | 2 +- jscomp/test/bs_array_test.ml | 2 +- jscomp/test/bs_mutable_set_test.js | 18 ++-- jscomp/test/bs_mutable_set_test.ml | 18 ++-- jscomp/test/bs_poly_mutable_set_test.js | 28 ++--- jscomp/test/bs_poly_mutable_set_test.ml | 28 ++--- lib/js/bs_Array.js | 6 +- lib/js/bs_Map.js | 133 ++++++++++++++++++------ lib/js/bs_MapInt.js | 83 +++++++++++---- lib/js/bs_MapIntM.js | 73 ++++++++++--- lib/js/bs_MapM.js | 28 ++--- lib/js/bs_MapString.js | 83 +++++++++++---- lib/js/bs_MapStringM.js | 73 ++++++++++--- lib/js/bs_SetIntM.js | 16 +-- lib/js/bs_SetM.js | 24 ++--- lib/js/bs_SetStringM.js | 16 +-- 42 files changed, 794 insertions(+), 350 deletions(-) diff --git a/jscomp/others/bs_Array.ml b/jscomp/others/bs_Array.ml index ee04011843..447e322044 100644 --- a/jscomp/others/bs_Array.ml +++ b/jscomp/others/bs_Array.ml @@ -45,13 +45,13 @@ let swapUnsafe xs i j = unsafe_set xs j tmp -let shuffleOnly xs = +let shuffleDone xs = let len = length xs in for i = 0 to len - 1 do swapUnsafe xs i (Js_math.random_int i len) (* [i,len)*) done -let shuffle xs = shuffleOnly xs; xs +let shuffle xs = shuffleDone xs; xs let makeMatrix sx sy init = [%assert sx >=0 && sy >=0 ]; diff --git a/jscomp/others/bs_Array.mli b/jscomp/others/bs_Array.mli index 32f5a3e9a6..d29d9d0ffa 100644 --- a/jscomp/others/bs_Array.mli +++ b/jscomp/others/bs_Array.mli @@ -39,7 +39,7 @@ external makeUninitializedUnsafe : int -> 'a array = "Array" [@@bs.new] val init : int -> (int -> 'a [@bs]) -> 'a array -val shuffleOnly : 'a array -> unit +val shuffleDone : 'a array -> unit val shuffle :'a array -> 'a array (** [shuffle xs] it mutates [xs] and return diff --git a/jscomp/others/bs_Map.ml b/jscomp/others/bs_Map.ml index 6f2c072a9f..a1fc90fa7b 100644 --- a/jscomp/others/bs_Map.ml +++ b/jscomp/others/bs_Map.ml @@ -40,8 +40,8 @@ let rec updateWithOpt0 (t : _ t0) newK f ~cmp = match N.toOpt t with | None -> begin match f None [@bs] with - | None -> t - | Some newD -> N.singleton0 newK newD + | None -> t + | Some newD -> N.singleton0 newK newD end | Some n -> let k= N.key n in @@ -71,32 +71,39 @@ let rec updateWithOpt0 (t : _ t0) newK f ~cmp = when [exist] is [true], [v] could be [null], since ['a] is polymorphic *) - - -let rec remove0 t x ~cmp = - match N.toOpt t with - | None -> - t - | Some n -> - let l,v,r = N.(left n, key n, right n ) in - let c = (Bs_Cmp.getCmp cmp) x v [@bs] in - if c = 0 then - match N.toOpt l, N.toOpt r with - | None, _ -> r - | _, None -> l - | _, Some rn -> - let kr, vr = ref (N.key rn), ref (N.value rn) in - let r = N.removeMinAuxWithRef rn kr vr in - N.bal l !kr !vr r - else if c < 0 then - let ll = remove0 l x ~cmp in - if ll == l then t + + +let rec removeAux0 n x ~cmp = + let l,v,r = N.(left n, key n, right n ) in + let c = (Bs_Cmp.getCmp cmp) x v [@bs] in + if c = 0 then + match N.toOpt l, N.toOpt r with + | None, _ -> r + | _, None -> l + | _, Some rn -> + let kr, vr = ref (N.key rn), ref (N.value rn) in + let r = N.removeMinAuxWithRef rn kr vr in + N.bal l !kr !vr r + else if c < 0 then + match N.toOpt l with + | None -> N.return n (* Nothing to remove *) + | Some left -> + let ll = removeAux0 left x ~cmp in + if ll == l then (N.return n) else N.bal ll v (N.value n) r - else - let rr = remove0 ~cmp r x in - if rr == r then t + else + match N.toOpt r with + | None -> N.return n (* Nothing to remove *) + | Some right -> + let rr = removeAux0 ~cmp right x in + if rr == r then N.return n else N.bal l v (N.value n) rr +let remove0 n x ~cmp = + match N.toOpt n with + | None -> N.empty0 + | Some n -> removeAux0 n x ~cmp + let updateArray0 h arr ~cmp = let len = A.length arr in let v = ref h in @@ -170,19 +177,48 @@ let rec merge0 s1 s2 f ~cmp = let newRight = (merge0 ~cmp r1 r2 f) in N.concatOrJoin newLeft v2 newD newRight - +let rec removeArrayAux t xs i len ~cmp = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeAux0 t ele ~cmp in + match N.toOpt u with + | None -> u + | Some t -> removeArrayAux t xs (i + 1) len ~cmp + else + N.return t + +let removeArray0 t keys ~cmp = + let len = A.length keys in + match N.toOpt t with + | None -> N.empty0 + | Some t -> removeArrayAux t keys 0 len ~cmp let ofArray (type k) (type id) data ~(dict : (k,id) Bs_Cmp.t) = let module M = (val dict ) in B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) let remove (type k) (type id) (m : (k,_,id) t) x = - let dict,data = B.(dict m, data m) in - let module M = (val dict) in - let newData = remove0 ~cmp:M.cmp data x in - if newData == data then m - else B.bag ~dict ~data:newData - + let odata = B.data m in + match N.toOpt odata with + | None -> m + | Some data -> + let dict = B.dict m in + let module M = (val dict) in + let newData = removeAux0 ~cmp:M.cmp data x in + if newData == odata then m + else B.bag ~dict ~data:newData + +let removeArray (type k) (type id) (m : (k,_,id) t) xs = + let odata = B.data m in + match N.toOpt odata with + | None -> m + | Some data -> + let dict = B.dict m in + let module M = (val dict) in + let len = A.length xs in + let newData = removeArrayAux data xs 0 len ~cmp:M.cmp in + if newData == odata then m + else B.bag ~dict ~data:newData let update (type k) (type id) (map : (k,_,id) t) key data = let dict,map = B.(dict map, data map) in @@ -298,7 +334,7 @@ let getExn (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in N.findExn0 ~cmp:X.cmp map x - + let mem (type k) (type id) (map : (k,_,id) t) x = let dict,map = B.(dict map, data map) in let module X = (val dict) in diff --git a/jscomp/others/bs_Map.mli b/jscomp/others/bs_Map.mli index 6a7c5c7cfc..671eb908b3 100644 --- a/jscomp/others/bs_Map.mli +++ b/jscomp/others/bs_Map.mli @@ -110,7 +110,8 @@ val getExn: ('k, 'a, 'id) t -> 'k -> 'a val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t (** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *) - +val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t + val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t (** [update m x y ] returns a map containing the same bindings as @@ -172,7 +173,9 @@ val map: ('k, 'a, 'id) t -> ('a -> 'b [@bs]) -> ('k ,'b,'id ) t with respect to the ordering over the type of the keys. *) val mapi: ('k, 'a, 'id) t -> ('k -> 'a -> 'b [@bs]) -> ('k, 'b, 'id) t - + + +(****************************************************************************) val empty0 : ('k, 'a, 'id) t0 val ofArray0: @@ -202,6 +205,12 @@ val remove0: cmp: ('k,'id) Bs_Cmp.cmp -> ('k, 'a, 'id) t0 +val removeArray0: + ('k, 'a, 'id) t0 -> + 'k array -> + cmp: ('k,'id) Bs_Cmp.cmp -> + ('k, 'a, 'id) t0 + val merge0: ('k, 'a, 'id ) t0 -> ('k, 'b,'id) t0 -> ('k -> 'a option -> 'b option -> 'c option [@bs]) -> diff --git a/jscomp/others/bs_MapInt.ml b/jscomp/others/bs_MapInt.ml index b10f265a59..98fe1a151b 100644 --- a/jscomp/others/bs_MapInt.ml +++ b/jscomp/others/bs_MapInt.ml @@ -73,10 +73,7 @@ let rec updateWithOpt t (x : key) f = else N.bal (N.left n) k v (updateWithOpt (N.right n) x f) -let rec remove n (x : key) = - match N.toOpt n with - | None -> n - | Some n -> +let rec removeAux n (x : key) = let l,v,r = N.(left n, key n, right n) in if x = v then match N.toOpt l, N.toOpt r with @@ -87,9 +84,39 @@ let rec remove n (x : key) = let r = N.removeMinAuxWithRef rn kr vr in N.bal l !kr !vr r else if x < v then - N.(bal (remove l x ) v (value n) r) + match N.toOpt l with + | None -> N.return n + | Some left -> + let ll = removeAux left x in + if ll == l then N.return n + else N.(bal ll v (value n) r) else - N.(bal l v (value n) (remove r x )) + match N.toOpt r with + | None -> N.return n + | Some right -> + let rr = removeAux right x in + N.bal l v (N.value n) rr + +let remove n x = + match N.toOpt n with + | None -> N.empty0 + | Some n -> removeAux n x + +let rec removeArrayAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeAux t ele in + match N.toOpt u with + | None -> u + | Some t -> removeArrayAux t xs (i + 1) len + else + N.return t + +let removeArray t keys = + let len = A.length keys in + match N.toOpt t with + | None -> N.empty0 + | Some t -> removeArrayAux t keys 0 len let mem = I.mem let cmp = I.cmp diff --git a/jscomp/others/bs_MapInt.mli b/jscomp/others/bs_MapInt.mli index 922daf8557..7108c97c86 100644 --- a/jscomp/others/bs_MapInt.mli +++ b/jscomp/others/bs_MapInt.mli @@ -61,6 +61,7 @@ val getExn: 'a t -> key -> 'a val remove: 'a t -> key -> 'a t (** [remove m x] returns a map containing the same bindings as [m], except for [x] which is unbound in the returned map. *) +val removeArray: 'a t -> key array -> 'a t val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as diff --git a/jscomp/others/bs_MapIntM.ml b/jscomp/others/bs_MapIntM.ml index e394843119..3745ddf970 100644 --- a/jscomp/others/bs_MapIntM.ml +++ b/jscomp/others/bs_MapIntM.ml @@ -27,14 +27,14 @@ let minKeyValueNull m = N.minKVNull0 (data m) let maxKeyValueOpt m = N.maxKVOpt0 (data m) let maxKeyValueNull m = N.maxKVNull0 (data m) -let addOnly (m : _ t) k v = +let addDone (m : _ t) k v = let old_data = data m in let v = I.addMutate old_data k v in if v != old_data then dataSet m v let add (d : 'a t) (k : key) (v : 'a) : 'a t= - addOnly d k v; + addDone d k v; d let iter d f = N.iter0 (data d) f let map d f = t ~data:(N.map0 (data d) f) @@ -78,21 +78,42 @@ let rec removeMutateAux nt (x : key)= N.rightSet nt (removeMutateAux r x); N.return (N.balMutate nt) end -let removeMutate nt x = - match N.toOpt nt with - | None -> nt - | Some nt -> removeMutateAux nt x - -let removeOnly d v = - let old_data = data d in - let v = removeMutate old_data v in - if v != old_data then - dataSet d v + +let removeDone d v = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some root -> + let newRoot = removeMutateAux root v in + if newRoot != oldRoot then + dataSet d newRoot let remove d v = - removeOnly d v; + removeDone d v; d +let rec removeArrayMutateAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeMutateAux t ele in + match N.toOpt u with + | None -> N.empty0 + | Some t -> removeArrayMutateAux t xs (i+1) len + else N.return t + +let removeArrayDone (type elt) (type id) (d : _ t) xs = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some nt -> + let len = A.length xs in + let newRoot = removeArrayMutateAux nt xs 0 len in + if newRoot != oldRoot then + dataSet d newRoot + +let removeArray d xs = + removeArrayDone d xs; + d let cmp = I.cmp let eq = I.eq diff --git a/jscomp/others/bs_MapIntM.mli b/jscomp/others/bs_MapIntM.mli index 61a71489a8..7d589650c5 100644 --- a/jscomp/others/bs_MapIntM.mli +++ b/jscomp/others/bs_MapIntM.mli @@ -66,10 +66,13 @@ val getExn: 'a t -> key -> 'a (*TODO: add functional [merge, partition, filter, split]*) +val removeDone: 'a t -> key -> unit val remove: 'a t -> key -> 'a t (** [remove m x] do the in-place modification, return [m] for chaining *) - -val addOnly : 'a t -> key -> 'a -> unit +val removeArrayDone: 'a t -> key array -> unit +val removeArray: 'a t -> key array -> 'a t + +val addDone: 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return [m] for chaining. If [x] was already bound diff --git a/jscomp/others/bs_MapM.ml b/jscomp/others/bs_MapM.ml index 384adb6314..beac886697 100644 --- a/jscomp/others/bs_MapM.ml +++ b/jscomp/others/bs_MapM.ml @@ -60,18 +60,18 @@ let rec removeMutateAux nt x ~cmp = N.return (N.balMutate nt) end -let removeOnly (type elt) (type id) (d : (elt,_,id) t) k = - let dict, oldRoot = B.(dict d, data d) in - let module M = (val dict) in +let removeDone (type elt) (type id) (d : (elt,_,id) t) k = + let oldRoot = B.data d in match N.toOpt oldRoot with | None -> () | Some oldRoot2 -> + let module M = (val B.dict d) in let newRoot = removeMutateAux ~cmp:M.cmp oldRoot2 k in if newRoot != oldRoot then B.dataSet d newRoot let remove d v = - removeOnly d v; + removeDone d v; d let rec removeArrayMutateAux t xs i len ~cmp = @@ -83,20 +83,19 @@ let rec removeArrayMutateAux t xs i len ~cmp = | Some t -> removeArrayMutateAux t xs (i+1) len ~cmp else N.return t -let removeArrayOnly (type elt) (type id) (d : (elt,_,id) t) xs = +let removeArrayDone (type elt) (type id) (d : (elt,_,id) t) xs = let oldRoot = B.data d in match N.toOpt oldRoot with | None -> () | Some nt -> let len = A.length xs in - let dict = B.dict d in - let module M = (val dict) in + let module M = (val B.dict d) in let newRoot = removeArrayMutateAux nt xs 0 len ~cmp:M.cmp in if newRoot != oldRoot then B.dataSet d newRoot let removeArray d xs = - removeArrayOnly d xs; + removeArrayDone d xs; d let empty ~dict = @@ -174,14 +173,14 @@ let mem (type k) (type id) (map : (k,_,id) t) x = let ofArray (type k) (type id) data ~(dict : (k,id) Bs_Cmp.t)= let module M = (val dict ) in B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) -let updateOnly (type elt) (type id) (m : (elt,_,id) t) e v = +let updateDone (type elt) (type id) (m : (elt,_,id) t) e v = let dict, oldRoot = B.(dict m, data m) in let module M = (val dict) in let newRoot = N.updateMutate ~cmp:M.cmp oldRoot e v in if newRoot != oldRoot then B.dataSet m newRoot let update m e v = - updateOnly m e v; + updateDone m e v; m let updateArrayMutate t xs ~cmp = let v = ref t in @@ -190,7 +189,7 @@ let updateArrayMutate t xs ~cmp = v := N.updateMutate !v key value ~cmp done; !v -let updateArrayOnly (type elt) (type id) (d : (elt,_,id) t ) xs = +let updateArrayDone (type elt) (type id) (d : (elt,_,id) t ) xs = let dict = B.dict d in let oldRoot = B.data d in let module M = (val dict) in @@ -198,6 +197,6 @@ let updateArrayOnly (type elt) (type id) (d : (elt,_,id) t ) xs = if newRoot != oldRoot then B.dataSet d newRoot let updateArray d xs = - updateArrayOnly d xs ; + updateArrayDone d xs ; d diff --git a/jscomp/others/bs_MapM.mli b/jscomp/others/bs_MapM.mli index dcde0f5468..d5c57fa6e6 100644 --- a/jscomp/others/bs_MapM.mli +++ b/jscomp/others/bs_MapM.mli @@ -92,19 +92,22 @@ val getExn: ('k, 'a, 'id) t -> 'k -> 'a (****************************************************************************) +(*TODO: add functional [merge, partition, filter, split]*) + +val removeDone: ('k, 'a, 'id) t -> 'k -> unit val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t (** [remove m x] do the in-place modification, returnning [m] for chaining. *) +val removeArrayDone: ('k, 'a, 'id) t -> 'k array -> unit +val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t + -(*TODO: add functional [merge, partition, filter, split]*) -val updateOnly: ('k, 'a, 'id) t -> 'k -> 'a -> unit +val updateDone: ('k, 'a, 'id) t -> 'k -> 'a -> unit val update: ('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t (** [update m x y ] do the in-place modification, returnning [m] for chaining. *) -val removeOnly: ('k, 'a, 'id) t -> 'k -> unit -val removeArrayOnly: ('k, 'a, 'id) t -> 'k array -> unit -val removeArray: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t + val map: ('k, 'a, 'id) t -> ('a -> 'b [@bs]) -> ('k ,'b,'id ) t diff --git a/jscomp/others/bs_MapString.ml b/jscomp/others/bs_MapString.ml index 1d4d95d096..3c92d50fe2 100644 --- a/jscomp/others/bs_MapString.ml +++ b/jscomp/others/bs_MapString.ml @@ -73,10 +73,7 @@ let rec updateWithOpt t (x : key) f = else N.bal (N.left n) k v (updateWithOpt (N.right n) x f) -let rec remove n (x : key) = - match N.toOpt n with - | None -> n - | Some n -> +let rec removeAux n (x : key) = let l,v,r = N.(left n, key n, right n) in if x = v then match N.toOpt l, N.toOpt r with @@ -87,9 +84,39 @@ let rec remove n (x : key) = let r = N.removeMinAuxWithRef rn kr vr in N.bal l !kr !vr r else if x < v then - N.(bal (remove l x ) v (value n) r) + match N.toOpt l with + | None -> N.return n + | Some left -> + let ll = removeAux left x in + if ll == l then N.return n + else N.(bal ll v (value n) r) else - N.(bal l v (value n) (remove r x )) + match N.toOpt r with + | None -> N.return n + | Some right -> + let rr = removeAux right x in + N.bal l v (N.value n) rr + +let remove n x = + match N.toOpt n with + | None -> N.empty0 + | Some n -> removeAux n x + +let rec removeArrayAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeAux t ele in + match N.toOpt u with + | None -> u + | Some t -> removeArrayAux t xs (i + 1) len + else + N.return t + +let removeArray t keys = + let len = A.length keys in + match N.toOpt t with + | None -> N.empty0 + | Some t -> removeArrayAux t keys 0 len let mem = I.mem let cmp = I.cmp diff --git a/jscomp/others/bs_MapString.mli b/jscomp/others/bs_MapString.mli index e161756a93..ea77120258 100644 --- a/jscomp/others/bs_MapString.mli +++ b/jscomp/others/bs_MapString.mli @@ -61,6 +61,7 @@ val getExn: 'a t -> key -> 'a val remove: 'a t -> key -> 'a t (** [remove m x] returns a map containing the same bindings as [m], except for [x] which is unbound in the returned map. *) +val removeArray: 'a t -> key array -> 'a t val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as diff --git a/jscomp/others/bs_MapStringM.ml b/jscomp/others/bs_MapStringM.ml index bf91d7128b..f90af5f7b3 100644 --- a/jscomp/others/bs_MapStringM.ml +++ b/jscomp/others/bs_MapStringM.ml @@ -27,14 +27,14 @@ let minKeyValueNull m = N.minKVNull0 (data m) let maxKeyValueOpt m = N.maxKVOpt0 (data m) let maxKeyValueNull m = N.maxKVNull0 (data m) -let addOnly (m : _ t) k v = +let addDone (m : _ t) k v = let old_data = data m in let v = I.addMutate old_data k v in if v != old_data then dataSet m v let add (d : 'a t) (k : key) (v : 'a) : 'a t= - addOnly d k v; + addDone d k v; d let iter d f = N.iter0 (data d) f let map d f = t ~data:(N.map0 (data d) f) @@ -78,21 +78,42 @@ let rec removeMutateAux nt (x : key)= N.rightSet nt (removeMutateAux r x); N.return (N.balMutate nt) end -let removeMutate nt x = - match N.toOpt nt with - | None -> nt - | Some nt -> removeMutateAux nt x - -let removeOnly d v = - let old_data = data d in - let v = removeMutate old_data v in - if v != old_data then - dataSet d v + +let removeDone d v = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some root -> + let newRoot = removeMutateAux root v in + if newRoot != oldRoot then + dataSet d newRoot let remove d v = - removeOnly d v; + removeDone d v; d +let rec removeArrayMutateAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeMutateAux t ele in + match N.toOpt u with + | None -> N.empty0 + | Some t -> removeArrayMutateAux t xs (i+1) len + else N.return t + +let removeArrayDone (type elt) (type id) (d : _ t) xs = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some nt -> + let len = A.length xs in + let newRoot = removeArrayMutateAux nt xs 0 len in + if newRoot != oldRoot then + dataSet d newRoot + +let removeArray d xs = + removeArrayDone d xs; + d let cmp = I.cmp let eq = I.eq diff --git a/jscomp/others/bs_MapStringM.mli b/jscomp/others/bs_MapStringM.mli index e9b8ee04b5..031e22f709 100644 --- a/jscomp/others/bs_MapStringM.mli +++ b/jscomp/others/bs_MapStringM.mli @@ -66,10 +66,13 @@ val getExn: 'a t -> key -> 'a (*TODO: add functional [merge, partition, filter, split]*) +val removeDone: 'a t -> key -> unit val remove: 'a t -> key -> 'a t (** [remove m x] do the in-place modification, return [m] for chaining *) - -val addOnly : 'a t -> key -> 'a -> unit +val removeArrayDone: 'a t -> key array -> unit +val removeArray: 'a t -> key array -> 'a t + +val addDone: 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return [m] for chaining. If [x] was already bound diff --git a/jscomp/others/bs_SetIntM.ml b/jscomp/others/bs_SetIntM.ml index cbfc3a4e40..10e2510f8d 100644 --- a/jscomp/others/bs_SetIntM.ml +++ b/jscomp/others/bs_SetIntM.ml @@ -106,18 +106,18 @@ let ofSortedArrayUnsafe xs = let checkInvariant d = N.checkInvariant (data d) -let addOnly d k = +let addDone d k = let old_data = data d in let v = I.addMutate old_data k in if v != old_data then dataSet d v let add d k = - addOnly d k; + addDone d k; d -let addArrayOnly d arr = +let addArrayDone d arr = let old_data = data d in let v = addArrayMutate old_data arr in if v != old_data then @@ -130,14 +130,14 @@ let addArray d arr = dataSet d v ; d -let removeOnly d v = +let removeDone d v = let old_data = data d in let v = removeMutate old_data v in if v != old_data then dataSet d v let remove d v = - removeOnly d v; + removeDone d v; d diff --git a/jscomp/others/bs_SetIntM.mli b/jscomp/others/bs_SetIntM.mli index eab7c425b7..f379ec37c4 100644 --- a/jscomp/others/bs_SetIntM.mli +++ b/jscomp/others/bs_SetIntM.mli @@ -7,12 +7,12 @@ val empty: unit -> t val isEmpty: t -> bool val mem: t -> elt -> bool -val addOnly: t -> elt -> unit +val addDone: t -> elt -> unit val add: t -> elt -> t val singleton: elt -> t val remove: t -> elt -> t -val removeOnly: t -> elt -> unit +val removeDone: t -> elt -> unit val union: t -> t -> t val inter: t -> t -> t val diff: t -> t -> t @@ -63,7 +63,7 @@ val split: t -> elt -> (t * t) * bool val findOpt: t -> elt -> elt option val addArray: t -> elt array -> t -val addArrayOnly: t -> elt array -> unit +val addArrayDone: t -> elt array -> unit val checkInvariant: t -> bool diff --git a/jscomp/others/bs_SetM.ml b/jscomp/others/bs_SetM.ml index 3f8d1eeb1e..9a78bd754a 100644 --- a/jscomp/others/bs_SetM.ml +++ b/jscomp/others/bs_SetM.ml @@ -37,7 +37,7 @@ let rec removeMutateAux nt x ~cmp = N.return (N.balMutate nt) end -let removeOnly (type elt) (type id) (d : (elt,id) t) v = +let removeDone (type elt) (type id) (d : (elt,id) t) v = let dict, oldRoot = B.(dict d, data d) in let module M = (val dict) in match N.toOpt oldRoot with @@ -48,7 +48,7 @@ let removeOnly (type elt) (type id) (d : (elt,id) t) v = B.dataSet d newRoot let remove d v = - removeOnly d v; + removeDone d v; d let rec removeArrayMutateAux t xs i len ~cmp = @@ -60,7 +60,7 @@ let rec removeArrayMutateAux t xs i len ~cmp = | Some t -> removeArrayMutateAux t xs (i+1) len ~cmp else N.return t -let removeArrayOnly (type elt) (type id) (d : (elt,id) t) xs = +let removeArrayDone (type elt) (type id) (d : (elt,id) t) xs = let oldRoot = B.data d in match N.toOpt oldRoot with | None -> () @@ -73,9 +73,9 @@ let removeArrayOnly (type elt) (type id) (d : (elt,id) t) xs = B.dataSet d newRoot let removeArray d xs = - removeArrayOnly d xs; + removeArrayDone d xs; d - + let rec removeMutateCheckAux nt x removed ~cmp= let k = N.key nt in let c = (Bs_Cmp.getCmp cmp) x k [@bs] in @@ -230,14 +230,14 @@ let mem (type elt) (type id) (d : (elt,id) t) x = let ofArray (type elt) (type id) (dict : (elt,id) Bs_Cmp.t) data = let module M = (val dict) in B.bag ~dict ~data:(N.ofArray0 ~cmp:M.cmp data) -let addOnly (type elt) (type id) (m : (elt,id) t) e = +let addDone (type elt) (type id) (m : (elt,id) t) e = let dict, oldRoot = B.(dict m, data m) in let module M = (val dict) in let newRoot = N.addMutate ~cmp:M.cmp oldRoot e in if newRoot != oldRoot then B.dataSet m newRoot let add m e = - addOnly m e; + addDone m e; m let addCheck (type elt) (type id) (m : (elt,id) t) e = let dict, oldRoot = B.(dict m, data m) in @@ -253,7 +253,7 @@ let addArrayMutate (t : _ t0) xs ~cmp = v := N.addMutate !v (A.unsafe_get xs i) ~cmp done; !v -let addArrayOnly (type elt) (type id) (d : (elt,id) t ) xs = +let addArrayDone (type elt) (type id) (d : (elt,id) t ) xs = let dict = B.dict d in let oldRoot = B.data d in let module M = (val dict) in @@ -261,7 +261,7 @@ let addArrayOnly (type elt) (type id) (d : (elt,id) t ) xs = if newRoot != oldRoot then B.dataSet d newRoot let addArray d xs = - addArrayOnly d xs ; + addArrayDone d xs ; d diff --git a/jscomp/others/bs_SetM.mli b/jscomp/others/bs_SetM.mli index 53fac752b5..eef9ce0114 100644 --- a/jscomp/others/bs_SetM.mli +++ b/jscomp/others/bs_SetM.mli @@ -37,25 +37,25 @@ val singleton : 'elt -> ('elt, 'id) t val mem: ('elt, _) t -> 'elt -> bool -val addOnly: +val addDone: ('elt, 'id) t -> 'elt -> unit val add: ('elt, 'id) t -> 'elt -> ('elt, 'id) t val addCheck: ('elt, 'id) t -> 'elt -> bool -val addArrayOnly: +val addArrayDone: ('elt, 'id) t -> 'elt array -> unit val addArray: ('elt, 'id) t -> 'elt array -> ('elt, 'id) t -val removeOnly: +val removeDone: ('elt, 'id) t -> 'elt -> unit val remove: ('elt, 'id) t -> 'elt -> ('elt, 'id) t val removeCheck: ('elt, 'id) t -> 'elt -> bool (* [b = removeCheck s e] [b] is true means one element removed *) -val removeArrayOnly: +val removeArrayDone: ('elt, 'id) t -> 'elt array -> unit val removeArray: ('elt, 'id) t -> 'elt array -> ('elt, 'id) t diff --git a/jscomp/others/bs_SetStringM.ml b/jscomp/others/bs_SetStringM.ml index 0910d941f1..dc5aa02e17 100644 --- a/jscomp/others/bs_SetStringM.ml +++ b/jscomp/others/bs_SetStringM.ml @@ -106,18 +106,18 @@ let ofSortedArrayUnsafe xs = let checkInvariant d = N.checkInvariant (data d) -let addOnly d k = +let addDone d k = let old_data = data d in let v = I.addMutate old_data k in if v != old_data then dataSet d v let add d k = - addOnly d k; + addDone d k; d -let addArrayOnly d arr = +let addArrayDone d arr = let old_data = data d in let v = addArrayMutate old_data arr in if v != old_data then @@ -130,14 +130,14 @@ let addArray d arr = dataSet d v ; d -let removeOnly d v = +let removeDone d v = let old_data = data d in let v = removeMutate old_data v in if v != old_data then dataSet d v let remove d v = - removeOnly d v; + removeDone d v; d diff --git a/jscomp/others/bs_SetStringM.mli b/jscomp/others/bs_SetStringM.mli index 50cb8fe8a3..82aa004fc8 100644 --- a/jscomp/others/bs_SetStringM.mli +++ b/jscomp/others/bs_SetStringM.mli @@ -7,12 +7,12 @@ val empty: unit -> t val isEmpty: t -> bool val mem: t -> elt -> bool -val addOnly: t -> elt -> unit +val addDone: t -> elt -> unit val add: t -> elt -> t val singleton: elt -> t val remove: t -> elt -> t -val removeOnly: t -> elt -> unit +val removeDone: t -> elt -> unit val union: t -> t -> t val inter: t -> t -> t val diff: t -> t -> t @@ -63,7 +63,7 @@ val split: t -> elt -> (t * t) * bool val findOpt: t -> elt -> elt option val addArray: t -> elt array -> t -val addArrayOnly: t -> elt array -> unit +val addArrayDone: t -> elt array -> unit val checkInvariant: t -> bool diff --git a/jscomp/others/map.cppo.ml b/jscomp/others/map.cppo.ml index 41046ae488..7aa542c105 100644 --- a/jscomp/others/map.cppo.ml +++ b/jscomp/others/map.cppo.ml @@ -78,10 +78,7 @@ let rec updateWithOpt t (x : key) f = else N.bal (N.left n) k v (updateWithOpt (N.right n) x f) -let rec remove n (x : key) = - match N.toOpt n with - | None -> n - | Some n -> +let rec removeAux n (x : key) = let l,v,r = N.(left n, key n, right n) in if x = v then match N.toOpt l, N.toOpt r with @@ -92,9 +89,39 @@ let rec remove n (x : key) = let r = N.removeMinAuxWithRef rn kr vr in N.bal l !kr !vr r else if x < v then - N.(bal (remove l x ) v (value n) r) + match N.toOpt l with + | None -> N.return n + | Some left -> + let ll = removeAux left x in + if ll == l then N.return n + else N.(bal ll v (value n) r) else - N.(bal l v (value n) (remove r x )) + match N.toOpt r with + | None -> N.return n + | Some right -> + let rr = removeAux right x in + N.bal l v (N.value n) rr + +let remove n x = + match N.toOpt n with + | None -> N.empty0 + | Some n -> removeAux n x + +let rec removeArrayAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeAux t ele in + match N.toOpt u with + | None -> u + | Some t -> removeArrayAux t xs (i + 1) len + else + N.return t + +let removeArray t keys = + let len = A.length keys in + match N.toOpt t with + | None -> N.empty0 + | Some t -> removeArrayAux t keys 0 len let mem = I.mem let cmp = I.cmp diff --git a/jscomp/others/map.cppo.mli b/jscomp/others/map.cppo.mli index 4e954dfc90..e1cefd77e8 100644 --- a/jscomp/others/map.cppo.mli +++ b/jscomp/others/map.cppo.mli @@ -65,6 +65,7 @@ val getExn: 'a t -> key -> 'a val remove: 'a t -> key -> 'a t (** [remove m x] returns a map containing the same bindings as [m], except for [x] which is unbound in the returned map. *) +val removeArray: 'a t -> key array -> 'a t val update: 'a t -> key -> 'a -> 'a t (** [add m x y] returns a map containing the same bindings as diff --git a/jscomp/others/mapm.cppo.ml b/jscomp/others/mapm.cppo.ml index 38394de0b3..589080ead8 100644 --- a/jscomp/others/mapm.cppo.ml +++ b/jscomp/others/mapm.cppo.ml @@ -33,14 +33,14 @@ let minKeyValueNull m = N.minKVNull0 (data m) let maxKeyValueOpt m = N.maxKVOpt0 (data m) let maxKeyValueNull m = N.maxKVNull0 (data m) -let addOnly (m : _ t) k v = +let addDone (m : _ t) k v = let old_data = data m in let v = I.addMutate old_data k v in if v != old_data then dataSet m v let add (d : 'a t) (k : key) (v : 'a) : 'a t= - addOnly d k v; + addDone d k v; d let iter d f = N.iter0 (data d) f let map d f = t ~data:(N.map0 (data d) f) @@ -84,21 +84,42 @@ let rec removeMutateAux nt (x : key)= N.rightSet nt (removeMutateAux r x); N.return (N.balMutate nt) end -let removeMutate nt x = - match N.toOpt nt with - | None -> nt - | Some nt -> removeMutateAux nt x - -let removeOnly d v = - let old_data = data d in - let v = removeMutate old_data v in - if v != old_data then - dataSet d v + +let removeDone d v = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some root -> + let newRoot = removeMutateAux root v in + if newRoot != oldRoot then + dataSet d newRoot let remove d v = - removeOnly d v; + removeDone d v; d +let rec removeArrayMutateAux t xs i len = + if i < len then + let ele = A.unsafe_get xs i in + let u = removeMutateAux t ele in + match N.toOpt u with + | None -> N.empty0 + | Some t -> removeArrayMutateAux t xs (i+1) len + else N.return t + +let removeArrayDone (type elt) (type id) (d : _ t) xs = + let oldRoot = data d in + match N.toOpt oldRoot with + | None -> () + | Some nt -> + let len = A.length xs in + let newRoot = removeArrayMutateAux nt xs 0 len in + if newRoot != oldRoot then + dataSet d newRoot + +let removeArray d xs = + removeArrayDone d xs; + d let cmp = I.cmp let eq = I.eq diff --git a/jscomp/others/mapm.cppo.mli b/jscomp/others/mapm.cppo.mli index d67a41b25f..ffb1251803 100644 --- a/jscomp/others/mapm.cppo.mli +++ b/jscomp/others/mapm.cppo.mli @@ -70,10 +70,13 @@ val getExn: 'a t -> key -> 'a (*TODO: add functional [merge, partition, filter, split]*) +val removeDone: 'a t -> key -> unit val remove: 'a t -> key -> 'a t (** [remove m x] do the in-place modification, return [m] for chaining *) - -val addOnly : 'a t -> key -> 'a -> unit +val removeArrayDone: 'a t -> key array -> unit +val removeArray: 'a t -> key array -> 'a t + +val addDone: 'a t -> key -> 'a -> unit val add: 'a t -> key -> 'a -> 'a t (** [add m x y] do the in-place modification, return [m] for chaining. If [x] was already bound diff --git a/jscomp/others/setm.cppo.ml b/jscomp/others/setm.cppo.ml index 98d2b53631..42528edd9a 100644 --- a/jscomp/others/setm.cppo.ml +++ b/jscomp/others/setm.cppo.ml @@ -111,18 +111,18 @@ let ofSortedArrayUnsafe xs = let checkInvariant d = N.checkInvariant (data d) -let addOnly d k = +let addDone d k = let old_data = data d in let v = I.addMutate old_data k in if v != old_data then dataSet d v let add d k = - addOnly d k; + addDone d k; d -let addArrayOnly d arr = +let addArrayDone d arr = let old_data = data d in let v = addArrayMutate old_data arr in if v != old_data then @@ -135,14 +135,14 @@ let addArray d arr = dataSet d v ; d -let removeOnly d v = +let removeDone d v = let old_data = data d in let v = removeMutate old_data v in if v != old_data then dataSet d v let remove d v = - removeOnly d v; + removeDone d v; d diff --git a/jscomp/others/setm.cppo.mli b/jscomp/others/setm.cppo.mli index 8bb6925480..2e09c54d39 100644 --- a/jscomp/others/setm.cppo.mli +++ b/jscomp/others/setm.cppo.mli @@ -11,12 +11,12 @@ val empty: unit -> t val isEmpty: t -> bool val mem: t -> elt -> bool -val addOnly: t -> elt -> unit +val addDone: t -> elt -> unit val add: t -> elt -> t val singleton: elt -> t val remove: t -> elt -> t -val removeOnly: t -> elt -> unit +val removeDone: t -> elt -> unit val union: t -> t -> t val inter: t -> t -> t val diff: t -> t -> t @@ -67,7 +67,7 @@ val split: t -> elt -> (t * t) * bool val findOpt: t -> elt -> elt option val addArray: t -> elt array -> t -val addArrayOnly: t -> elt array -> unit +val addArrayDone: t -> elt array -> unit val checkInvariant: t -> bool diff --git a/jscomp/test/bs_array_test.js b/jscomp/test/bs_array_test.js index 27003466dd..6b2f89a911 100644 --- a/jscomp/test/bs_array_test.js +++ b/jscomp/test/bs_array_test.js @@ -178,7 +178,7 @@ var v = Bs_Array.init(3000, (function (i) { var u = Bs_Array.copy(v); -Bs_Array.shuffleOnly(u); +Bs_Array.shuffleDone(u); neq("File \"bs_array_test.ml\", line 63, characters 6-13", u, v); diff --git a/jscomp/test/bs_array_test.ml b/jscomp/test/bs_array_test.ml index 20207696de..b50d0b88f3 100644 --- a/jscomp/test/bs_array_test.ml +++ b/jscomp/test/bs_array_test.ml @@ -59,7 +59,7 @@ let add = fun [@bs] x y -> x + y let () = let v = Bs.Array.init 3000 (fun[@bs] i -> i) in let u = Bs.Array.copy v in - Bs.Array.shuffleOnly u ; + Bs.Array.shuffleDone u ; neq __LOC__ u v (* unlikely*); let sum x = Bs.Array.foldLeft x 0 add in eq __LOC__ ( sum u) (sum v) diff --git a/jscomp/test/bs_mutable_set_test.js b/jscomp/test/bs_mutable_set_test.js index c15dae971b..131b51cda8 100644 --- a/jscomp/test/bs_mutable_set_test.js +++ b/jscomp/test/bs_mutable_set_test.js @@ -26,7 +26,7 @@ var v = { }; for(var i = 0; i <= 100000; ++i){ - Bs_SetIntM.addOnly(v, i); + Bs_SetIntM.addDone(v, i); } b("File \"bs_mutable_set_test.ml\", line 19, characters 4-11", Bs_internalAVLset.checkInvariant(v.data)); @@ -43,7 +43,7 @@ var v$1 = { data: Bs_internalAVLset.empty0 }; -Bs_SetIntM.addArrayOnly(v$1, u); +Bs_SetIntM.addArrayDone(v$1, u); eq("File \"bs_mutable_set_test.ml\", line 29, characters 5-12", Bs_internalAVLset.length0(v$1.data), 91); @@ -60,7 +60,7 @@ eq("File \"bs_mutable_set_test.ml\", line 35, characters 5-12", Bs_internalAVLse var u$2 = Array_data_util.randomRange(50000, 80000); for(var i$1 = 0 ,i_finish = u$2.length - 1 | 0; i$1 <= i_finish; ++i$1){ - Bs_SetIntM.removeOnly(v$2, i$1); + Bs_SetIntM.removeDone(v$2, i$1); } eq("File \"bs_mutable_set_test.ml\", line 42, characters 5-12", Bs_internalAVLset.length0(v$2.data), 70000); @@ -68,7 +68,7 @@ eq("File \"bs_mutable_set_test.ml\", line 42, characters 5-12", Bs_internalAVLse var vv = Array_data_util.randomRange(0, 100000); for(var i$2 = 0 ,i_finish$1 = vv.length - 1 | 0; i$2 <= i_finish$1; ++i$2){ - Bs_SetIntM.removeOnly(v$2, Caml_array.caml_array_get(vv, i$2)); + Bs_SetIntM.removeDone(v$2, Caml_array.caml_array_get(vv, i$2)); } eq("File \"bs_mutable_set_test.ml\", line 48, characters 5-12", Bs_internalAVLset.length0(v$2.data), 0); @@ -83,13 +83,13 @@ var v$3 = { data: Bs_internalSetInt.ofArray(xs) }; -Bs_SetIntM.removeOnly(v$3, 30); +Bs_SetIntM.removeDone(v$3, 30); -Bs_SetIntM.removeOnly(v$3, 29); +Bs_SetIntM.removeDone(v$3, 29); b("File \"bs_mutable_set_test.ml\", line 55, characters 4-11", +(28 === Bs_internalAVLset.maxNull0(v$3.data))); -Bs_SetIntM.removeOnly(v$3, 0); +Bs_SetIntM.removeDone(v$3, 0); b("File \"bs_mutable_set_test.ml\", line 57, characters 4-11", +(1 === Bs_internalAVLset.minNull0(v$3.data))); @@ -98,7 +98,7 @@ eq("File \"bs_mutable_set_test.ml\", line 58, characters 5-12", Bs_internalAVLse var vv$1 = Array_data_util.randomRange(1, 28); for(var i$3 = 0 ,i_finish$2 = vv$1.length - 1 | 0; i$3 <= i_finish$2; ++i$3){ - Bs_SetIntM.removeOnly(v$3, Caml_array.caml_array_get(vv$1, i$3)); + Bs_SetIntM.removeDone(v$3, Caml_array.caml_array_get(vv$1, i$3)); } eq("File \"bs_mutable_set_test.ml\", line 63, characters 5-12", Bs_internalAVLset.length0(v$3.data), 0); @@ -215,7 +215,7 @@ var cc = Bs_SetIntM.filter(v$4, (function (x) { })); for(var i$4 = 0; i$4 <= 200; ++i$4){ - Bs_SetIntM.removeOnly(v$4, i$4); + Bs_SetIntM.removeDone(v$4, i$4); } eq("File \"bs_mutable_set_test.ml\", line 92, characters 5-12", Bs_internalAVLset.length0(copyV.data), 126); diff --git a/jscomp/test/bs_mutable_set_test.ml b/jscomp/test/bs_mutable_set_test.ml index 765803467d..bc6fd11215 100644 --- a/jscomp/test/bs_mutable_set_test.ml +++ b/jscomp/test/bs_mutable_set_test.ml @@ -14,7 +14,7 @@ let () = let v = N.empty () in for i = 0 to 1_00_000 do (* [%assert (N.checkInvariant !v)]; *) - N.addOnly v i + N.addDone v i done ; b __LOC__ (N.checkInvariant v); b __LOC__ @@ R.forAll 0 1_00_000 (fun [@bs] i -> @@ -25,7 +25,7 @@ let () = let () = let u = I.randomRange 30 100 ++ I.randomRange 40 120 in let v = N.empty () in - N.addArrayOnly v u ; + N.addArrayDone v u ; eq __LOC__ (N.length v) 91 ; eq __LOC__ (N.toArray v) (I.range 30 120) @@ -36,29 +36,29 @@ let () = let u = I.randomRange 50_000 80_000 in for i = 0 to A.length u - 1 do - N.removeOnly v i + N.removeDone v i done; eq __LOC__ (N.length v) 70_000; let count = 100_000 in let vv = I.randomRange 0 count in for i = 0 to A.length vv - 1 do - N.removeOnly v vv.(i) + N.removeDone v vv.(i) done; eq __LOC__ (N.length v) 0; b __LOC__ (N.isEmpty v ) let () = let v = N.ofArray (A.init 30 (fun [@bs]i -> i)) in - N.removeOnly v 30; - N.removeOnly v 29 ; + N.removeDone v 30; + N.removeDone v 29 ; b __LOC__ (Js.eqNull 28 (N.maxNull v )); - N.removeOnly v 0 ; + N.removeDone v 0 ; b __LOC__ (Js.eqNull 1 (N.minNull v)); eq __LOC__ (N.length v ) 28; let vv = I.randomRange 1 28 in for i = 0 to A.length vv - 1 do - N.removeOnly v vv.(i) + N.removeDone v vv.(i) done ; eq __LOC__ (N.length v) 0 @@ -87,7 +87,7 @@ let () = let aa,bb = N.partition v (fun[@bs] x -> x mod 8 = 0) in let cc = N.filter v (fun[@bs] x -> x mod 8 <> 0) in for i = 0 to 200 do - N.removeOnly v i + N.removeDone v i done ; eq __LOC__ (N.length copyV) 126; eq __LOC__ (N.toArray copyV) (A.init 126 (fun[@bs] i -> i * 8)); diff --git a/jscomp/test/bs_poly_mutable_set_test.js b/jscomp/test/bs_poly_mutable_set_test.js index 3fe0303fda..c90aeaefde 100644 --- a/jscomp/test/bs_poly_mutable_set_test.js +++ b/jscomp/test/bs_poly_mutable_set_test.js @@ -39,55 +39,55 @@ b("File \"bs_poly_mutable_set_test.ml\", line 21, characters 4-11", +(29 === Bs_ b("File \"bs_poly_mutable_set_test.ml\", line 22, characters 4-11", +(1 === Bs_internalAVLset.minNull0(u.data))); -Bs_SetM.addOnly(u, 3); +Bs_SetM.addDone(u, 3); for(var i = 0 ,i_finish = r.length - 1 | 0; i <= i_finish; ++i){ - Bs_SetM.removeOnly(u, Caml_array.caml_array_get(r, i)); + Bs_SetM.removeDone(u, Caml_array.caml_array_get(r, i)); } b("File \"bs_poly_mutable_set_test.ml\", line 27, characters 4-11", Bs_internalAVLset.isEmpty0(u.data)); -Bs_SetM.addOnly(u, 0); +Bs_SetM.addDone(u, 0); -Bs_SetM.addOnly(u, 1); +Bs_SetM.addDone(u, 1); -Bs_SetM.addOnly(u, 2); +Bs_SetM.addDone(u, 2); -Bs_SetM.addOnly(u, 0); +Bs_SetM.addDone(u, 0); eq("File \"bs_poly_mutable_set_test.ml\", line 32, characters 5-12", Bs_internalAVLset.length0(u.data), 3); b("File \"bs_poly_mutable_set_test.ml\", line 33, characters 4-11", 1 - Bs_internalAVLset.isEmpty0(u.data)); for(var i$1 = 0; i$1 <= 3; ++i$1){ - Bs_SetM.removeOnly(u, i$1); + Bs_SetM.removeDone(u, i$1); } b("File \"bs_poly_mutable_set_test.ml\", line 37, characters 4-11", Bs_internalAVLset.isEmpty0(u.data)); -Bs_SetM.addArrayOnly(u, Array_data_util.randomRange(0, 20000)); +Bs_SetM.addArrayDone(u, Array_data_util.randomRange(0, 20000)); -Bs_SetM.addArrayOnly(u, Array_data_util.randomRange(0, 200)); +Bs_SetM.addArrayDone(u, Array_data_util.randomRange(0, 200)); eq("File \"bs_poly_mutable_set_test.ml\", line 40, characters 5-12", Bs_internalAVLset.length0(u.data), 20001); -Bs_SetM.removeArrayOnly(u, Array_data_util.randomRange(0, 200)); +Bs_SetM.removeArrayDone(u, Array_data_util.randomRange(0, 200)); eq("File \"bs_poly_mutable_set_test.ml\", line 42, characters 5-12", Bs_internalAVLset.length0(u.data), 19800); -Bs_SetM.removeArrayOnly(u, Array_data_util.randomRange(0, 1000)); +Bs_SetM.removeArrayDone(u, Array_data_util.randomRange(0, 1000)); eq("File \"bs_poly_mutable_set_test.ml\", line 44, characters 5-12", Bs_internalAVLset.length0(u.data), 19000); -Bs_SetM.removeArrayOnly(u, Array_data_util.randomRange(0, 1000)); +Bs_SetM.removeArrayDone(u, Array_data_util.randomRange(0, 1000)); eq("File \"bs_poly_mutable_set_test.ml\", line 46, characters 5-12", Bs_internalAVLset.length0(u.data), 19000); -Bs_SetM.removeArrayOnly(u, Array_data_util.randomRange(1000, 10000)); +Bs_SetM.removeArrayDone(u, Array_data_util.randomRange(1000, 10000)); eq("File \"bs_poly_mutable_set_test.ml\", line 48, characters 5-12", Bs_internalAVLset.length0(u.data), 10000); -Bs_SetM.removeArrayOnly(u, Array_data_util.randomRange(10000, 19999)); +Bs_SetM.removeArrayDone(u, Array_data_util.randomRange(10000, 19999)); eq("File \"bs_poly_mutable_set_test.ml\", line 50, characters 5-12", Bs_internalAVLset.length0(u.data), 1); diff --git a/jscomp/test/bs_poly_mutable_set_test.ml b/jscomp/test/bs_poly_mutable_set_test.ml index 3325234c82..8aa441b078 100644 --- a/jscomp/test/bs_poly_mutable_set_test.ml +++ b/jscomp/test/bs_poly_mutable_set_test.ml @@ -20,33 +20,33 @@ let () = let r = I.randomRange 0 30 in b __LOC__ (Js.eqNull 29 (N.maxNull u)); b __LOC__ (Js.eqNull 1 (N.minNull u)); - N.addOnly u 3; + N.addDone u 3; for i = 0 to A.length r - 1 do - N.removeOnly u (A.get r i) + N.removeDone u (A.get r i) done ; b __LOC__ (N.isEmpty u); - N.addOnly u 0; - N.addOnly u 1; - N.addOnly u 2; - N.addOnly u 0; + N.addDone u 0; + N.addDone u 1; + N.addDone u 2; + N.addDone u 0; eq __LOC__ (N.length u) 3; b __LOC__ (not (N.isEmpty u)); for i = 0 to 3 do - N.removeOnly u i + N.removeDone u i done ; b __LOC__ (N.isEmpty u); - N.addArrayOnly u (I.randomRange 0 20000); - N.addArrayOnly u (I.randomRange 0 200); + N.addArrayDone u (I.randomRange 0 20000); + N.addArrayDone u (I.randomRange 0 200); eq __LOC__ (N.length u) 20001; - N.removeArrayOnly u (I.randomRange 0 200); + N.removeArrayDone u (I.randomRange 0 200); eq __LOC__ (N.length u) 19800; - N.removeArrayOnly u (I.randomRange 0 1000); + N.removeArrayDone u (I.randomRange 0 1000); eq __LOC__ (N.length u) 19000; - N.removeArrayOnly u (I.randomRange 0 1000); + N.removeArrayDone u (I.randomRange 0 1000); eq __LOC__ (N.length u) 19000; - N.removeArrayOnly u (I.randomRange 1000 10000); + N.removeArrayDone u (I.randomRange 1000 10000); eq __LOC__ (N.length u) 10000; - N.removeArrayOnly u (I.randomRange 10000 (20000 -1)); + N.removeArrayDone u (I.randomRange 10000 (20000 -1)); eq __LOC__ (N.length u) 1 ; b __LOC__ (N.mem u 20000) (* for i = *) diff --git a/lib/js/bs_Array.js b/lib/js/bs_Array.js index a44fea1867..adcf1b5468 100644 --- a/lib/js/bs_Array.js +++ b/lib/js/bs_Array.js @@ -21,7 +21,7 @@ function swapUnsafe(xs, i, j) { return /* () */0; } -function shuffleOnly(xs) { +function shuffleDone(xs) { var len = xs.length; for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){ swapUnsafe(xs, i, Js_math.random_int(i, len)); @@ -30,7 +30,7 @@ function shuffleOnly(xs) { } function shuffle(xs) { - shuffleOnly(xs); + shuffleDone(xs); return xs; } @@ -267,7 +267,7 @@ function forAll2(a, b, p) { var concat = Caml_array.caml_array_concat; exports.init = init; -exports.shuffleOnly = shuffleOnly; +exports.shuffleDone = shuffleDone; exports.shuffle = shuffle; exports.zip = zip; exports.makeMatrix = makeMatrix; diff --git a/lib/js/bs_Map.js b/lib/js/bs_Map.js index 060d647ca6..0b97b8e056 100644 --- a/lib/js/bs_Map.js +++ b/lib/js/bs_Map.js @@ -54,42 +54,52 @@ function updateWithOpt0(t, newK, f, cmp) { } } -function remove0(t, x, cmp) { - if (t !== null) { - var l = t.left; - var v = t.key; - var r = t.right; - var c = cmp(x, v); - if (c) { - if (c < 0) { - var ll = remove0(l, x, cmp); +function removeAux0(n, x, cmp) { + var l = n.left; + var v = n.key; + var r = n.right; + var c = cmp(x, v); + if (c) { + if (c < 0) { + if (l !== null) { + var ll = removeAux0(l, x, cmp); if (ll === l) { - return t; + return n; } else { - return Bs_internalAVLtree.bal(ll, v, t.value, r); + return Bs_internalAVLtree.bal(ll, v, n.value, r); } } else { - var rr = remove0(r, x, cmp); - if (rr === r) { - return t; - } else { - return Bs_internalAVLtree.bal(l, v, t.value, rr); - } + return n; } - } else if (l !== null) { - if (r !== null) { - var kr = [r.key]; - var vr = [r.value]; - var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); - return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + } else if (r !== null) { + var rr = removeAux0(r, x, cmp); + if (rr === r) { + return n; } else { - return l; + return Bs_internalAVLtree.bal(l, v, n.value, rr); } } else { - return r; + return n; + } + } else if (l !== null) { + if (r !== null) { + var kr = [r.key]; + var vr = [r.value]; + var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); + return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + } else { + return l; } } else { - return t; + return r; + } +} + +function remove0(n, x, cmp) { + if (n !== null) { + return removeAux0(n, x, cmp); + } else { + return Bs_internalAVLtree.empty0; } } @@ -205,6 +215,36 @@ function merge0(s1, s2, f, cmp) { } } +function removeArrayAux(_t, xs, _i, len, cmp) { + while(true) { + var i = _i; + var t = _t; + if (i < len) { + var ele = xs[i]; + var u = removeAux0(t, ele, cmp); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + + } else { + return u; + } + } else { + return t; + } + }; +} + +function removeArray0(t, keys, cmp) { + var len = keys.length; + if (t !== null) { + return removeArrayAux(t, keys, 0, len, cmp); + } else { + return Bs_internalAVLtree.empty0; + } +} + function ofArray(data, dict) { return { dict: dict, @@ -213,16 +253,39 @@ function ofArray(data, dict) { } function remove(m, x) { - var dict = m.dict; - var data = m.data; - var newData = remove0(data, x, dict[/* cmp */0]); - if (newData === data) { + var odata = m.data; + if (odata !== null) { + var dict = m.dict; + var newData = removeAux0(odata, x, dict[/* cmp */0]); + if (newData === odata) { + return m; + } else { + return { + dict: dict, + data: newData + }; + } + } else { return m; + } +} + +function removeArray(m, xs) { + var odata = m.data; + if (odata !== null) { + var dict = m.dict; + var len = xs.length; + var newData = removeArrayAux(odata, xs, 0, len, dict[/* cmp */0]); + if (newData === odata) { + return m; + } else { + return { + dict: dict, + data: newData + }; + } } else { - return { - dict: dict, - data: newData - }; + return m; } } @@ -532,6 +595,7 @@ exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; exports.remove = remove; +exports.removeArray = removeArray; exports.update = update; exports.updateArray = updateArray; exports.updateWithOpt = updateWithOpt; @@ -548,6 +612,7 @@ exports.mem0 = mem0; exports.update0 = update0; exports.singleton0 = singleton0; exports.remove0 = remove0; +exports.removeArray0 = removeArray0; exports.merge0 = merge0; exports.cmp0 = cmp0; exports.eq0 = eq0; diff --git a/lib/js/bs_MapInt.js b/lib/js/bs_MapInt.js index d1f5bce7fc..bad03a3856 100644 --- a/lib/js/bs_MapInt.js +++ b/lib/js/bs_MapInt.js @@ -49,31 +49,77 @@ function updateWithOpt(t, x, f) { } } +function removeAux(n, x) { + var l = n.left; + var v = n.key; + var r = n.right; + if (x === v) { + if (l !== null) { + if (r !== null) { + var kr = [r.key]; + var vr = [r.value]; + var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); + return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + } else { + return l; + } + } else { + return r; + } + } else if (x < v) { + if (l !== null) { + var ll = removeAux(l, x); + if (ll === l) { + return n; + } else { + return Bs_internalAVLtree.bal(ll, v, n.value, r); + } + } else { + return n; + } + } else if (r !== null) { + var rr = removeAux(r, x); + return Bs_internalAVLtree.bal(l, v, n.value, rr); + } else { + return n; + } +} + function remove(n, x) { if (n !== null) { - var l = n.left; - var v = n.key; - var r = n.right; - if (x === v) { - if (l !== null) { - if (r !== null) { - var kr = [r.key]; - var vr = [r.value]; - var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); - return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + return removeAux(n, x); + } else { + return Bs_internalAVLtree.empty0; + } +} + +function removeArray(t, keys) { + var len = keys.length; + if (t !== null) { + var _t = t; + var xs = keys; + var _i = 0; + var len$1 = len; + while(true) { + var i = _i; + var t$1 = _t; + if (i < len$1) { + var ele = xs[i]; + var u = removeAux(t$1, ele); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + } else { - return l; + return u; } } else { - return r; + return t$1; } - } else if (x < v) { - return Bs_internalAVLtree.bal(remove(l, x), v, n.value, r); - } else { - return Bs_internalAVLtree.bal(l, v, n.value, remove(r, x)); - } + }; } else { - return n; + return Bs_internalAVLtree.empty0; } } @@ -176,6 +222,7 @@ exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; exports.remove = remove; +exports.removeArray = removeArray; exports.update = update; exports.updateWithOpt = updateWithOpt; exports.merge = merge; diff --git a/lib/js/bs_MapIntM.js b/lib/js/bs_MapIntM.js index 1cd4a9c4c4..8b42306381 100644 --- a/lib/js/bs_MapIntM.js +++ b/lib/js/bs_MapIntM.js @@ -51,7 +51,7 @@ function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } -function addOnly(m, k, v) { +function addDone(m, k, v) { var old_data = m.data; var v$1 = Bs_internalMapInt.addMutate(old_data, k, v); if (v$1 !== old_data) { @@ -63,7 +63,7 @@ function addOnly(m, k, v) { } function add(d, k, v) { - addOnly(d, k, v); + addDone(d, k, v); return d; } @@ -159,27 +159,65 @@ function removeMutateAux(nt, x) { } } -function removeMutate(nt, x) { - if (nt !== null) { - return removeMutateAux(nt, x); +function removeDone(d, v) { + var oldRoot = d.data; + if (oldRoot !== null) { + var newRoot = removeMutateAux(oldRoot, v); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } } else { - return nt; + return /* () */0; } } -function removeOnly(d, v) { - var old_data = d.data; - var v$1 = removeMutate(old_data, v); - if (v$1 !== old_data) { - d.data = v$1; - return /* () */0; +function remove(d, v) { + removeDone(d, v); + return d; +} + +function removeArrayMutateAux(_t, xs, _i, len) { + while(true) { + var i = _i; + var t = _t; + if (i < len) { + var ele = xs[i]; + var u = removeMutateAux(t, ele); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + + } else { + return Bs_internalAVLtree.empty0; + } + } else { + return t; + } + }; +} + +function removeArrayDone(d, xs) { + var oldRoot = d.data; + if (oldRoot !== null) { + var len = xs.length; + var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } } else { - return 0; + return /* () */0; } } -function remove(d, v) { - removeOnly(d, v); +function removeArray(d, xs) { + removeArrayDone(d, xs); return d; } @@ -249,8 +287,11 @@ exports.getOpt = getOpt; exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; +exports.removeDone = removeDone; exports.remove = remove; -exports.addOnly = addOnly; +exports.removeArrayDone = removeArrayDone; +exports.removeArray = removeArray; +exports.addDone = addDone; exports.add = add; exports.map = map; exports.mapi = mapi; diff --git a/lib/js/bs_MapM.js b/lib/js/bs_MapM.js index bd151cdf99..1065695b76 100644 --- a/lib/js/bs_MapM.js +++ b/lib/js/bs_MapM.js @@ -41,11 +41,11 @@ function removeMutateAux(nt, x, cmp) { } } -function removeOnly(d, k) { - var dict = d.dict; +function removeDone(d, k) { var oldRoot = d.data; if (oldRoot !== null) { - var newRoot = removeMutateAux(oldRoot, k, dict[/* cmp */0]); + var M = d.dict; + var newRoot = removeMutateAux(oldRoot, k, M[/* cmp */0]); if (newRoot !== oldRoot) { d.data = newRoot; return /* () */0; @@ -58,7 +58,7 @@ function removeOnly(d, k) { } function remove(d, v) { - removeOnly(d, v); + removeDone(d, v); return d; } @@ -83,12 +83,12 @@ function removeArrayMutateAux(_t, xs, _i, len, cmp) { }; } -function removeArrayOnly(d, xs) { +function removeArrayDone(d, xs) { var oldRoot = d.data; if (oldRoot !== null) { var len = xs.length; - var dict = d.dict; - var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len, dict[/* cmp */0]); + var M = d.dict; + var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len, M[/* cmp */0]); if (newRoot !== oldRoot) { d.data = newRoot; return /* () */0; @@ -101,7 +101,7 @@ function removeArrayOnly(d, xs) { } function removeArray(d, xs) { - removeArrayOnly(d, xs); + removeArrayDone(d, xs); return d; } @@ -260,7 +260,7 @@ function ofArray(data, dict) { }; } -function updateOnly(m, e, v) { +function updateDone(m, e, v) { var dict = m.dict; var oldRoot = m.data; var newRoot = Bs_internalAVLtree.updateMutate(oldRoot, e, v, dict[/* cmp */0]); @@ -273,7 +273,7 @@ function updateOnly(m, e, v) { } function update(m, e, v) { - updateOnly(m, e, v); + updateDone(m, e, v); return m; } @@ -305,12 +305,12 @@ exports.getOpt = getOpt; exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; +exports.removeDone = removeDone; exports.remove = remove; -exports.updateOnly = updateOnly; -exports.update = update; -exports.removeOnly = removeOnly; -exports.removeArrayOnly = removeArrayOnly; +exports.removeArrayDone = removeArrayDone; exports.removeArray = removeArray; +exports.updateDone = updateDone; +exports.update = update; exports.map = map; exports.mapi = mapi; /* No side effect */ diff --git a/lib/js/bs_MapString.js b/lib/js/bs_MapString.js index 2d0b61956d..4357f375ba 100644 --- a/lib/js/bs_MapString.js +++ b/lib/js/bs_MapString.js @@ -49,31 +49,77 @@ function updateWithOpt(t, x, f) { } } +function removeAux(n, x) { + var l = n.left; + var v = n.key; + var r = n.right; + if (x === v) { + if (l !== null) { + if (r !== null) { + var kr = [r.key]; + var vr = [r.value]; + var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); + return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + } else { + return l; + } + } else { + return r; + } + } else if (x < v) { + if (l !== null) { + var ll = removeAux(l, x); + if (ll === l) { + return n; + } else { + return Bs_internalAVLtree.bal(ll, v, n.value, r); + } + } else { + return n; + } + } else if (r !== null) { + var rr = removeAux(r, x); + return Bs_internalAVLtree.bal(l, v, n.value, rr); + } else { + return n; + } +} + function remove(n, x) { if (n !== null) { - var l = n.left; - var v = n.key; - var r = n.right; - if (x === v) { - if (l !== null) { - if (r !== null) { - var kr = [r.key]; - var vr = [r.value]; - var r$1 = Bs_internalAVLtree.removeMinAuxWithRef(r, kr, vr); - return Bs_internalAVLtree.bal(l, kr[0], vr[0], r$1); + return removeAux(n, x); + } else { + return Bs_internalAVLtree.empty0; + } +} + +function removeArray(t, keys) { + var len = keys.length; + if (t !== null) { + var _t = t; + var xs = keys; + var _i = 0; + var len$1 = len; + while(true) { + var i = _i; + var t$1 = _t; + if (i < len$1) { + var ele = xs[i]; + var u = removeAux(t$1, ele); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + } else { - return l; + return u; } } else { - return r; + return t$1; } - } else if (x < v) { - return Bs_internalAVLtree.bal(remove(l, x), v, n.value, r); - } else { - return Bs_internalAVLtree.bal(l, v, n.value, remove(r, x)); - } + }; } else { - return n; + return Bs_internalAVLtree.empty0; } } @@ -176,6 +222,7 @@ exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; exports.remove = remove; +exports.removeArray = removeArray; exports.update = update; exports.updateWithOpt = updateWithOpt; exports.merge = merge; diff --git a/lib/js/bs_MapStringM.js b/lib/js/bs_MapStringM.js index 3d49275433..632e6a4a42 100644 --- a/lib/js/bs_MapStringM.js +++ b/lib/js/bs_MapStringM.js @@ -51,7 +51,7 @@ function maxKeyValueNull(m) { return Bs_internalAVLtree.maxKVNull0(m.data); } -function addOnly(m, k, v) { +function addDone(m, k, v) { var old_data = m.data; var v$1 = Bs_internalMapString.addMutate(old_data, k, v); if (v$1 !== old_data) { @@ -63,7 +63,7 @@ function addOnly(m, k, v) { } function add(d, k, v) { - addOnly(d, k, v); + addDone(d, k, v); return d; } @@ -159,27 +159,65 @@ function removeMutateAux(nt, x) { } } -function removeMutate(nt, x) { - if (nt !== null) { - return removeMutateAux(nt, x); +function removeDone(d, v) { + var oldRoot = d.data; + if (oldRoot !== null) { + var newRoot = removeMutateAux(oldRoot, v); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } } else { - return nt; + return /* () */0; } } -function removeOnly(d, v) { - var old_data = d.data; - var v$1 = removeMutate(old_data, v); - if (v$1 !== old_data) { - d.data = v$1; - return /* () */0; +function remove(d, v) { + removeDone(d, v); + return d; +} + +function removeArrayMutateAux(_t, xs, _i, len) { + while(true) { + var i = _i; + var t = _t; + if (i < len) { + var ele = xs[i]; + var u = removeMutateAux(t, ele); + if (u !== null) { + _i = i + 1 | 0; + _t = u; + continue ; + + } else { + return Bs_internalAVLtree.empty0; + } + } else { + return t; + } + }; +} + +function removeArrayDone(d, xs) { + var oldRoot = d.data; + if (oldRoot !== null) { + var len = xs.length; + var newRoot = removeArrayMutateAux(oldRoot, xs, 0, len); + if (newRoot !== oldRoot) { + d.data = newRoot; + return /* () */0; + } else { + return 0; + } } else { - return 0; + return /* () */0; } } -function remove(d, v) { - removeOnly(d, v); +function removeArray(d, xs) { + removeArrayDone(d, xs); return d; } @@ -249,8 +287,11 @@ exports.getOpt = getOpt; exports.getNull = getNull; exports.getWithDefault = getWithDefault; exports.getExn = getExn; +exports.removeDone = removeDone; exports.remove = remove; -exports.addOnly = addOnly; +exports.removeArrayDone = removeArrayDone; +exports.removeArray = removeArray; +exports.addDone = addDone; exports.add = add; exports.map = map; exports.mapi = mapi; diff --git a/lib/js/bs_SetIntM.js b/lib/js/bs_SetIntM.js index 95f88ae39e..336336a18c 100644 --- a/lib/js/bs_SetIntM.js +++ b/lib/js/bs_SetIntM.js @@ -144,7 +144,7 @@ function checkInvariant(d) { return Bs_internalAVLset.checkInvariant(d.data); } -function addOnly(d, k) { +function addDone(d, k) { var old_data = d.data; var v = Bs_internalSetInt.addMutate(old_data, k); if (v !== old_data) { @@ -156,11 +156,11 @@ function addOnly(d, k) { } function add(d, k) { - addOnly(d, k); + addDone(d, k); return d; } -function addArrayOnly(d, arr) { +function addArrayDone(d, arr) { var old_data = d.data; var v = addArrayMutate(old_data, arr); if (v !== old_data) { @@ -180,7 +180,7 @@ function addArray(d, arr) { return d; } -function removeOnly(d, v) { +function removeDone(d, v) { var old_data = d.data; var v$1 = removeMutate(old_data, v); if (v$1 !== old_data) { @@ -192,7 +192,7 @@ function removeOnly(d, v) { } function remove(d, v) { - removeOnly(d, v); + removeDone(d, v); return d; } @@ -360,11 +360,11 @@ function mem(d, x) { exports.empty = empty; exports.isEmpty = isEmpty; exports.mem = mem; -exports.addOnly = addOnly; +exports.addDone = addDone; exports.add = add; exports.singleton = singleton; exports.remove = remove; -exports.removeOnly = removeOnly; +exports.removeDone = removeDone; exports.union = union; exports.inter = inter; exports.diff = diff; @@ -389,6 +389,6 @@ exports.maxNull = maxNull; exports.split = split; exports.findOpt = findOpt; exports.addArray = addArray; -exports.addArrayOnly = addArrayOnly; +exports.addArrayDone = addArrayDone; exports.checkInvariant = checkInvariant; /* No side effect */ diff --git a/lib/js/bs_SetM.js b/lib/js/bs_SetM.js index adae6a387a..450e95ba01 100644 --- a/lib/js/bs_SetM.js +++ b/lib/js/bs_SetM.js @@ -42,7 +42,7 @@ function removeMutateAux(nt, x, cmp) { } } -function removeOnly(d, v) { +function removeDone(d, v) { var dict = d.dict; var oldRoot = d.data; if (oldRoot !== null) { @@ -59,7 +59,7 @@ function removeOnly(d, v) { } function remove(d, v) { - removeOnly(d, v); + removeDone(d, v); return d; } @@ -84,7 +84,7 @@ function removeArrayMutateAux(_t, xs, _i, len, cmp) { }; } -function removeArrayOnly(d, xs) { +function removeArrayDone(d, xs) { var oldRoot = d.data; if (oldRoot !== null) { var len = xs.length; @@ -102,7 +102,7 @@ function removeArrayOnly(d, xs) { } function removeArray(d, xs) { - removeArrayOnly(d, xs); + removeArrayDone(d, xs); return d; } @@ -348,7 +348,7 @@ function ofArray(dict, data) { }; } -function addOnly(m, e) { +function addDone(m, e) { var dict = m.dict; var oldRoot = m.data; var newRoot = Bs_internalAVLset.addMutate(dict[/* cmp */0], oldRoot, e); @@ -361,7 +361,7 @@ function addOnly(m, e) { } function add(m, e) { - addOnly(m, e); + addDone(m, e); return m; } @@ -384,7 +384,7 @@ function addArrayMutate(t, xs, cmp) { return v; } -function addArrayOnly(d, xs) { +function addArrayDone(d, xs) { var dict = d.dict; var oldRoot = d.data; var newRoot = addArrayMutate(oldRoot, xs, dict[/* cmp */0]); @@ -397,7 +397,7 @@ function addArrayOnly(d, xs) { } function addArray(d, xs) { - addArrayOnly(d, xs); + addArrayDone(d, xs); return d; } @@ -531,15 +531,15 @@ exports.ofArray = ofArray; exports.isEmpty = isEmpty; exports.singleton = singleton; exports.mem = mem; -exports.addOnly = addOnly; +exports.addDone = addDone; exports.add = add; exports.addCheck = addCheck; -exports.addArrayOnly = addArrayOnly; +exports.addArrayDone = addArrayDone; exports.addArray = addArray; -exports.removeOnly = removeOnly; +exports.removeDone = removeDone; exports.remove = remove; exports.removeCheck = removeCheck; -exports.removeArrayOnly = removeArrayOnly; +exports.removeArrayDone = removeArrayDone; exports.removeArray = removeArray; exports.union = union; exports.inter = inter; diff --git a/lib/js/bs_SetStringM.js b/lib/js/bs_SetStringM.js index 229538e2bc..900c3799a1 100644 --- a/lib/js/bs_SetStringM.js +++ b/lib/js/bs_SetStringM.js @@ -144,7 +144,7 @@ function checkInvariant(d) { return Bs_internalAVLset.checkInvariant(d.data); } -function addOnly(d, k) { +function addDone(d, k) { var old_data = d.data; var v = Bs_internalSetString.addMutate(old_data, k); if (v !== old_data) { @@ -156,11 +156,11 @@ function addOnly(d, k) { } function add(d, k) { - addOnly(d, k); + addDone(d, k); return d; } -function addArrayOnly(d, arr) { +function addArrayDone(d, arr) { var old_data = d.data; var v = addArrayMutate(old_data, arr); if (v !== old_data) { @@ -180,7 +180,7 @@ function addArray(d, arr) { return d; } -function removeOnly(d, v) { +function removeDone(d, v) { var old_data = d.data; var v$1 = removeMutate(old_data, v); if (v$1 !== old_data) { @@ -192,7 +192,7 @@ function removeOnly(d, v) { } function remove(d, v) { - removeOnly(d, v); + removeDone(d, v); return d; } @@ -360,11 +360,11 @@ function mem(d, x) { exports.empty = empty; exports.isEmpty = isEmpty; exports.mem = mem; -exports.addOnly = addOnly; +exports.addDone = addDone; exports.add = add; exports.singleton = singleton; exports.remove = remove; -exports.removeOnly = removeOnly; +exports.removeDone = removeDone; exports.union = union; exports.inter = inter; exports.diff = diff; @@ -389,6 +389,6 @@ exports.maxNull = maxNull; exports.split = split; exports.findOpt = findOpt; exports.addArray = addArray; -exports.addArrayOnly = addArrayOnly; +exports.addArrayDone = addArrayDone; exports.checkInvariant = checkInvariant; /* No side effect */ From 53468f4c2f9a0e30b2fff1bd351699cfc718f9af Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Wed, 17 Jan 2018 11:54:11 +0800 Subject: [PATCH 5/5] more tests --- jscomp/others/bs.ml | 1 + jscomp/test/.depend | 2 + jscomp/test/Makefile | 1 + jscomp/test/bs_poly_map_test.js | 23 ++++++ jscomp/test/bs_poly_map_test.ml | 8 ++- jscomp/test/bs_poly_mutable_map_test.js | 95 +++++++++++++++++++++++++ jscomp/test/bs_poly_mutable_map_test.ml | 36 ++++++++++ lib/js/bs.js | 3 + 8 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 jscomp/test/bs_poly_mutable_map_test.js create mode 100644 jscomp/test/bs_poly_mutable_map_test.ml diff --git a/jscomp/others/bs.ml b/jscomp/others/bs.ml index 011f1e9682..0c7078de50 100644 --- a/jscomp/others/bs.ml +++ b/jscomp/others/bs.ml @@ -45,6 +45,7 @@ module SortString = Bs_SortString module Stack = Bs_Stack module Range = Bs_Range module Map = Bs_Map +module MapM = Bs_MapM module Set = Bs_Set module SetM = Bs_SetM module MapInt = Bs_MapInt diff --git a/jscomp/test/.depend b/jscomp/test/.depend index 870da80a8a..ef3c0219db 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -107,6 +107,8 @@ bs_mutable_set_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs.cmj \ bs_node_string_buffer_test.cmj : ../others/node.cmj ../runtime/js.cmj bs_poly_map_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs_Array.cmj \ ../others/bs.cmj array_data_util.cmj +bs_poly_mutable_map_test.cmj : mt.cmj ../others/bs_Array.cmj \ + ../others/bs.cmj array_data_util.cmj bs_poly_mutable_set_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs.cmj \ array_data_util.cmj bs_poly_set_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs.cmj \ diff --git a/jscomp/test/Makefile b/jscomp/test/Makefile index 5618c033de..cd7feea107 100644 --- a/jscomp/test/Makefile +++ b/jscomp/test/Makefile @@ -235,6 +235,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_ bs_poly_set_test\ bs_stack_test\ bs_poly_map_test\ + bs_poly_mutable_map_test\ # bs_uncurry_test # needs Lam to get rid of Uncurry arity first # simple_derive_test diff --git a/jscomp/test/bs_poly_map_test.js b/jscomp/test/bs_poly_map_test.js index c2979f26d2..2021c3bf9a 100644 --- a/jscomp/test/bs_poly_map_test.js +++ b/jscomp/test/bs_poly_map_test.js @@ -145,6 +145,29 @@ b("File \"bs_poly_map_test.ml\", line 76, characters 4-11", +(11 === Bs_Map.getN b("File \"bs_poly_map_test.ml\", line 77, characters 4-11", +(Bs_Map.getNull(a4, 3) === null)); +var a7 = Bs_Map.removeArray(a0, /* array */[ + 7, + 8, + 0, + 1, + 3, + 2, + 4, + 922, + 4, + 5, + 6 + ]); + +eq("File \"bs_poly_map_test.ml\", line 80, characters 5-12", Bs_internalAVLtree.keysToArray0(a7.data), /* int array */[ + 9, + 10 + ]); + +var a8 = Bs_Map.removeArray(a7, Array_data_util.randomRange(0, 100)); + +b("File \"bs_poly_map_test.ml\", line 82, characters 4-11", Bs_internalAVLtree.isEmpty0(a8.data)); + Mt.from_pair_suites("bs_poly_map_test.ml", suites[0]); var M = 0; diff --git a/jscomp/test/bs_poly_map_test.ml b/jscomp/test/bs_poly_map_test.ml index 5f5b47c108..b2b1f4dd08 100644 --- a/jscomp/test/bs_poly_map_test.ml +++ b/jscomp/test/bs_poly_map_test.ml @@ -74,5 +74,11 @@ let () = b __LOC__ (Js.Null.test (M.getNull a2 3)); b __LOC__ (Js.eqNull 11 (M.getNull a3 3)); - b __LOC__ (Js.Null.test (M.getNull a4 3)) + b __LOC__ (Js.Null.test (M.getNull a4 3)); + + let a7 = M.removeArray a0 [|7;8;0;1;3;2;4;922;4;5;6;|] in + eq __LOC__ (M.keysToArray a7) [|9;10|]; + let a8 = M.removeArray a7 (I.randomRange 0 100) in + b __LOC__ (M.isEmpty a8) + ;; Mt.from_pair_suites __FILE__ !suites \ No newline at end of file diff --git a/jscomp/test/bs_poly_mutable_map_test.js b/jscomp/test/bs_poly_mutable_map_test.js new file mode 100644 index 0000000000..5bf2e6c550 --- /dev/null +++ b/jscomp/test/bs_poly_mutable_map_test.js @@ -0,0 +1,95 @@ +'use strict'; + +var Mt = require("./mt.js"); +var Bs_Set = require("../../lib/js/bs_Set.js"); +var Bs_MapM = require("../../lib/js/bs_MapM.js"); +var Bs_Array = require("../../lib/js/bs_Array.js"); +var Caml_primitive = require("../../lib/js/caml_primitive.js"); +var Array_data_util = require("./array_data_util.js"); +var Bs_internalAVLtree = require("../../lib/js/bs_internalAVLtree.js"); + +var suites = [/* [] */0]; + +var test_id = [0]; + +function eq(loc, x, y) { + return Mt.eq_suites(test_id, suites, loc, x, y); +} + +function b(loc, v) { + return Mt.bool_suites(test_id, suites, loc, v); +} + +var Icmp = /* module */[/* cmp */Caml_primitive.caml_int_compare]; + +function f(x) { + return Bs_MapM.ofArray(x, Icmp); +} + +function ff(x) { + return Bs_Set.ofArray(Icmp, x); +} + +function randomRange(i, j) { + return Bs_Array.map(Array_data_util.randomRange(i, j), (function (x) { + return /* tuple */[ + x, + x + ]; + })); +} + +var x = randomRange(0, 10); + +var a0 = Bs_MapM.ofArray(x, Icmp); + +Bs_MapM.updateDone(a0, 3, 33); + +eq("File \"bs_poly_mutable_map_test.ml\", line 28, characters 7-14", Bs_MapM.getExn(a0, 3), 33); + +Bs_MapM.removeArrayDone(a0, /* array */[ + 7, + 8, + 0, + 1, + 3, + 2, + 4, + 922, + 4, + 5, + 6 + ]); + +eq("File \"bs_poly_mutable_map_test.ml\", line 31, characters 7-14", Bs_internalAVLtree.keysToArray0(a0.data), /* int array */[ + 9, + 10 + ]); + +Bs_MapM.removeArrayDone(a0, Array_data_util.randomRange(0, 100)); + +b("File \"bs_poly_mutable_map_test.ml\", line 33, characters 6-13", Bs_internalAVLtree.isEmpty0(a0.data)); + +Mt.from_pair_suites("bs_poly_mutable_map_test.ml", suites[0]); + +var M = 0; + +var N = 0; + +var A = 0; + +var I = 0; + +exports.suites = suites; +exports.test_id = test_id; +exports.eq = eq; +exports.b = b; +exports.Icmp = Icmp; +exports.M = M; +exports.N = N; +exports.A = A; +exports.I = I; +exports.f = f; +exports.ff = ff; +exports.randomRange = randomRange; +/* x Not a pure module */ diff --git a/jscomp/test/bs_poly_mutable_map_test.ml b/jscomp/test/bs_poly_mutable_map_test.ml new file mode 100644 index 0000000000..118adf2879 --- /dev/null +++ b/jscomp/test/bs_poly_mutable_map_test.ml @@ -0,0 +1,36 @@ +let suites : Mt.pair_suites ref = ref [] +let test_id = ref 0 +let eq loc x y = Mt.eq_suites ~suites ~test_id loc x y +let b loc v = Mt.bool_suites ~suites ~test_id loc v + +module Icmp = + (val Bs.Cmp.make + (fun[@bs] (x : int) y -> + compare x y + ) + ) +module M = Bs.MapM +module N = Bs.Set + +module A = Bs_Array +module I = Array_data_util +let f x = M.ofArray ~dict:(module Icmp) x +let ff x = N.ofArray (module Icmp) x + + +let randomRange i j = + A.map (I.randomRange i j) (fun[@bs] x -> (x,x)) + + + let () = + let a0 = f (randomRange 0 10) in + M.updateDone a0 3 33; + eq __LOC__ (M.getExn a0 3) 33 ; + M.removeArrayDone a0 [|7;8;0;1;3;2;4;922;4;5;6;|]; + eq __LOC__ (M.keysToArray a0) [|9;10|]; + M.removeArrayDone a0 (I.randomRange 0 100); + b __LOC__ (M.isEmpty a0) + + + +;; Mt.from_pair_suites __FILE__ !suites \ No newline at end of file diff --git a/lib/js/bs.js b/lib/js/bs.js index 153ca4f1fb..1c0e1aca15 100644 --- a/lib/js/bs.js +++ b/lib/js/bs.js @@ -39,6 +39,8 @@ var Range = 0; var $$Map = 0; +var MapM = 0; + var $$Set = 0; var SetM = 0; @@ -74,6 +76,7 @@ exports.SortString = SortString; exports.Stack = Stack; exports.Range = Range; exports.$$Map = $$Map; +exports.MapM = MapM; exports.$$Set = $$Set; exports.SetM = SetM; exports.MapInt = MapInt;