From 83d23e711bf9572a15e63cffb2cb6a1d21e4e3c3 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Sat, 30 Dec 2017 16:08:28 +0800 Subject: [PATCH 1/3] better API design --- jscomp/others/bs_Array.ml | 2 +- jscomp/others/bs_Array.mli | 2 +- jscomp/others/bs_Set.ml | 20 ++++---- jscomp/others/bs_Set.mli | 28 +++++------ jscomp/others/bs_SetInt.ml | 2 +- jscomp/others/bs_SetInt.mli | 47 +++++++----------- jscomp/others/bs_SetIntM.ml | 2 +- jscomp/others/bs_SetString.ml | 2 +- jscomp/others/bs_SetString.mli | 47 +++++++----------- jscomp/others/bs_internalAVLset.ml | 79 ++++++++++++++++++------------ jscomp/others/bs_internalSetInt.ml | 3 +- jscomp/others/set.cppo.ml | 6 +-- jscomp/others/set.cppo.mli | 47 +++++++----------- jscomp/test/bs_set_int_test.js | 8 +-- jscomp/test/bs_set_int_test.ml | 4 +- lib/js/bs_Array.js | 2 +- lib/js/bs_Set.js | 34 ++++++------- lib/js/bs_SetInt.js | 4 +- lib/js/bs_SetString.js | 4 +- lib/js/bs_internalAVLset.js | 50 ++++++++++--------- lib/js/bs_internalSetInt.js | 3 +- 21 files changed, 193 insertions(+), 203 deletions(-) diff --git a/jscomp/others/bs_Array.ml b/jscomp/others/bs_Array.ml index b798a8ddcbd..47b50bc96da 100644 --- a/jscomp/others/bs_Array.ml +++ b/jscomp/others/bs_Array.ml @@ -167,7 +167,7 @@ let foldLeft a x f = let foldRight a x f = let r = ref x in for i = length a - 1 downto 0 do - r := f (unsafe_get a i) !r [@bs] + r := f !r (unsafe_get a i) [@bs] done; !r diff --git a/jscomp/others/bs_Array.mli b/jscomp/others/bs_Array.mli index 6dcce9f1f00..44c9b5f8a13 100644 --- a/jscomp/others/bs_Array.mli +++ b/jscomp/others/bs_Array.mli @@ -96,7 +96,7 @@ val mapi : 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array val foldLeft : 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a -val foldRight : 'b array -> 'a -> ('b -> 'a -> 'a [@bs]) -> 'a +val foldRight : 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a val forAll : 'a array -> ('a -> bool [@bs]) -> bool diff --git a/jscomp/others/bs_Set.ml b/jscomp/others/bs_Set.ml index 496e8e79e52..3b9f92d208c 100644 --- a/jscomp/others/bs_Set.ml +++ b/jscomp/others/bs_Set.ml @@ -23,7 +23,7 @@ let exists0 = N.exists0 let filter0 = N.filter0 let partition0 = N.partition0 let length0 = N.length0 -let elements0 = N.elements0 +let toList0 = N.toList0 let toArray0 = N.toArray0 (* Insertion of one element *) @@ -268,26 +268,26 @@ let subset (type elt) (type id) (m : (elt,id) t) (n : (elt,id) t) = let module M = (val dict) in subset0 ~cmp:M.cmp mdata ndata -let iter f m = iter0 f (B.data m) +let iter m f = iter0 (B.data m) f -let fold f m acc = fold0 f (B.data m) acc +let fold m acc f = fold0 (B.data m) acc f -let forAll f m = forAll0 f (B.data m) +let forAll m f = forAll0 (B.data m) f -let exists f m = exists0 f (B.data m) +let exists m f = exists0 (B.data m) f -let filter f m = +let filter m f = let data, dict = B.(data m, dict m) in - B.bag ~dict ~data:(filter0 f data) + B.bag ~dict ~data:(filter0 data f ) -let partition f m = +let partition m f = let mdata, dict = B.(data m, dict m) in - let l,r = partition0 f mdata in + let l,r = partition0 mdata f in B.bag ~data:l ~dict, B.bag ~data:r ~dict let length m = length0 (B.data m) -let elements m = elements0 (B.data m) +let toList m = toList0 (B.data m) let toArray m = toArray0 (B.data m) let min m = min0 (B.data m) diff --git a/jscomp/others/bs_Set.mli b/jscomp/others/bs_Set.mli index 3665c0544fa..087fa724ab8 100644 --- a/jscomp/others/bs_Set.mli +++ b/jscomp/others/bs_Set.mli @@ -97,34 +97,34 @@ val subset: (** [subset s1 s2] tests whether the set [s1] is a subset of the set [s2]. *) -val iter0: ('elt -> unit [@bs]) -> ('elt, 'id) t0 -> unit -val iter: ('elt -> unit [@bs]) -> ('elt, 'id) t -> unit +val iter0: ('elt, 'id) t0 -> ('elt -> unit [@bs]) -> unit +val iter: ('elt, 'id) t -> ('elt -> unit [@bs]) -> unit (** [iter f s] applies [f] in turn to all elements of [s]. The elements of [s] are presented to [f] in increasing order with respect to the ordering over the type of the elements. *) -val fold0: ('elt -> 'a -> 'a [@bs]) -> ('elt, 'id) t0 -> 'a -> 'a -val fold: ('elt -> 'a -> 'a [@bs]) -> ('elt, 'id) t -> 'a -> 'a +val fold0: ('elt, 'id) t0 -> 'a -> ('a -> 'elt -> 'a [@bs]) -> 'a +val fold: ('elt, 'id) t -> 'a -> ('a -> 'elt -> 'a [@bs]) -> 'a (** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], where [x1 ... xN] are the elements of [s], in increasing order. *) -val forAll0: ('elt -> bool [@bs]) -> ('elt, 'id) t0 -> bool -val forAll:('elt -> bool [@bs]) -> ('elt, 'id) t -> bool +val forAll0: ('elt, 'id) t0 -> ('elt -> bool [@bs]) -> bool +val forAll: ('elt, 'id) t -> ('elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set satisfy the predicate [p]. *) -val exists0: ('elt -> bool [@bs]) -> ('elt, 'id) t0 -> bool -val exists: ('elt -> bool [@bs]) -> ('elt, 'id) t -> bool +val exists0: ('elt, 'id) t0 -> ('elt -> bool [@bs]) -> bool +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 filter0: ('elt -> bool [@bs]) -> ('elt, 'id) t0 -> ('elt, 'id) t0 -val filter: ('elt -> bool [@bs]) -> ('elt, 'id) t -> ('elt, 'id) t +val filter0: ('elt, 'id) t0 -> ('elt -> bool [@bs]) -> ('elt, 'id) t0 +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]. *) -val partition0: ('elt -> bool [@bs]) -> ('elt, 'id) t0 -> ('elt, 'id) t0 * ('elt, 'id) t0 -val partition: ('elt -> bool [@bs]) -> ('elt, 'id) t -> ('elt, 'id) t * ('elt, 'id) t +val partition0: ('elt, 'id) t0 -> ('elt -> bool [@bs]) -> ('elt, 'id) t0 * ('elt, 'id) t0 +val partition: ('elt, 'id) t -> ('elt -> bool [@bs]) -> ('elt, 'id) t * ('elt, 'id) t (** [partition p s] returns a pair of sets [(s1, s2)], where [s1] is the set of all the elements of [s] that satisfy the predicate [p], and [s2] is the set of all the elements of @@ -134,8 +134,8 @@ val length0: ('elt, 'id) t0 -> int val length: ('elt, 'id) t -> int (** Return the number of elements of a set. *) -val elements0: ('elt, 'id) t0 -> 'elt list -val elements: ('elt, 'id) t -> 'elt list +val toList0: ('elt, 'id) t0 -> 'elt list +val toList: ('elt, 'id) t -> 'elt list (** Return the list of all elements of the given set. The returned list is sorted in increasing order with respect to the ordering [Ord.compare], where [Ord] is the argument diff --git a/jscomp/others/bs_SetInt.ml b/jscomp/others/bs_SetInt.ml index 45ce63553cb..3c3457827da 100644 --- a/jscomp/others/bs_SetInt.ml +++ b/jscomp/others/bs_SetInt.ml @@ -17,7 +17,7 @@ let exists = N.exists0 let filter = N.filter0 let partition = N.partition0 let length = N.length0 -let elements = N.elements0 +let toList = N.toList0 let toArray = N.toArray0 let checkInvariant = N.checkInvariant diff --git a/jscomp/others/bs_SetInt.mli b/jscomp/others/bs_SetInt.mli index db89b7ff83a..15453ef2bb6 100644 --- a/jscomp/others/bs_SetInt.mli +++ b/jscomp/others/bs_SetInt.mli @@ -8,68 +8,63 @@ type t (** The type of sets. *) val empty: t -(** The empty set. *) + val isEmpty: t -> bool -(** Test whether a set is empty or not. *) val mem: t -> elt -> bool -(** [mem x s] tests whether [x] belongs to the set [s]. *) + val add: t -> elt -> t -(** [add x s] returns a set containing all elements of [s], - plus [x]. If [x] was already in [s], [s] is returned unchanged. *) +(** If [x] was already in [s], [s] is returned unchanged. *) val singleton: elt -> t (** [singleton x] returns the one-element set containing only [x]. *) val remove: t -> elt -> t -(** [remove x s] returns a set containing all elements of [s], - except [x]. If [x] was not in [s], [s] is returned unchanged. *) +(** If [x] was not in [s], [s] is returned unchanged. *) val union: t -> t -> t -(** Set union. *) val inter: t -> t -> t -(** Set intersection. *) val diff: t -> t -> t -(** Set difference. *) + val cmp: t -> t -> int (** Total ordering between sets. Can be used as the ordering function for doing sets of sets. *) val eq: t -> t -> bool -(** [equal s1 s2] tests whether the sets [s1] and [s2] are +(** [eq s1 s2] tests whether the sets [s1] and [s2] are equal, that is, contain equal elements. *) val subset: t -> t -> bool (** [subset s1 s2] tests whether the set [s1] is a subset of the set [s2]. *) -val iter: (elt -> unit [@bs]) -> t -> unit +val iter: t -> (elt -> unit [@bs]) -> unit (** [iter f s] applies [f] in turn to all elements of [s]. The elements of [s] are presented to [f] in increasing order with respect to the ordering over the type of the elements. *) -val fold: (elt -> 'a -> 'a [@bs]) -> t -> 'a -> 'a +val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a (** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], where [x1 ... xN] are the elements of [s], in increasing order. *) -val forAll: (elt -> bool [@bs]) -> t -> bool +val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set - satisfy the predicate [p]. *) + satisfy the predicate [p]. Order unspecified. *) -val exists: (elt -> bool [@bs]) -> t -> bool +val exists: t -> (elt -> bool [@bs]) -> bool (** [exists p s] checks if at least one element of - the set satisfies the predicate [p]. *) + the set satisfies the predicate [p]. Oder unspecified. *) -val filter: (elt -> bool [@bs]) -> t -> t +val filter: t -> (elt -> bool [@bs]) -> t (** [filter p s] returns the set of all elements in [s] that satisfy predicate [p]. *) -val partition: (elt -> bool [@bs]) -> t -> t * t +val partition: t -> (elt -> bool [@bs]) -> t * t (** [partition p s] returns a pair of sets [(s1, s2)], where [s1] is the set of all the elements of [s] that satisfy the predicate [p], and [s2] is the set of all the elements of @@ -77,7 +72,7 @@ val partition: (elt -> bool [@bs]) -> t -> t * t val length: t -> int -val elements: t -> elt list +val toList: t -> elt list (** Return the list of all elements of the given set. The returned list is sorted in increasing order with respect to the ordering [Ord.compare], where [Ord] is the argument @@ -87,12 +82,9 @@ val toArray: t -> elt array val min: t -> elt option (** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering), or raise - [Not_found] if the set is empty. *) + (with respect to the [Ord.compare] ordering) *) val max: t -> elt option -(** Same as {!Set.S.min_elt}, but returns the largest element of the - given set. *) val split: elt -> t -> t * bool * t @@ -105,11 +97,8 @@ val split: elt -> t -> t * bool * t or [true] if [s] contains an element equal to [x]. *) val findOpt: elt -> t -> elt option -(** [find x s] returns the element of [s] equal to [x] (according - to [Ord.compare]), or raise [Not_found] if no such element - exists. - @since 4.01.0 *) + val ofArray : elt array -> t -val checkInvariant : t -> bool \ No newline at end of file +val checkInvariant : t -> bool diff --git a/jscomp/others/bs_SetIntM.ml b/jscomp/others/bs_SetIntM.ml index 33ea06d1faf..b99a2938ef0 100644 --- a/jscomp/others/bs_SetIntM.ml +++ b/jscomp/others/bs_SetIntM.ml @@ -22,7 +22,7 @@ let exists = N.exists0 let filter = N.filter0 let partition = N.partition0 let length = N.length0 -let elements = N.elements0 +let toList = N.toList0 let toArray = N.toArray0 let checkInvariant = N.checkInvariant diff --git a/jscomp/others/bs_SetString.ml b/jscomp/others/bs_SetString.ml index 074e3c0c633..9c3d531df11 100644 --- a/jscomp/others/bs_SetString.ml +++ b/jscomp/others/bs_SetString.ml @@ -17,7 +17,7 @@ let exists = N.exists0 let filter = N.filter0 let partition = N.partition0 let length = N.length0 -let elements = N.elements0 +let toList = N.toList0 let toArray = N.toArray0 let checkInvariant = N.checkInvariant diff --git a/jscomp/others/bs_SetString.mli b/jscomp/others/bs_SetString.mli index b34103f303d..e0321394306 100644 --- a/jscomp/others/bs_SetString.mli +++ b/jscomp/others/bs_SetString.mli @@ -8,68 +8,63 @@ type t (** The type of sets. *) val empty: t -(** The empty set. *) + val isEmpty: t -> bool -(** Test whether a set is empty or not. *) val mem: t -> elt -> bool -(** [mem x s] tests whether [x] belongs to the set [s]. *) + val add: t -> elt -> t -(** [add x s] returns a set containing all elements of [s], - plus [x]. If [x] was already in [s], [s] is returned unchanged. *) +(** If [x] was already in [s], [s] is returned unchanged. *) val singleton: elt -> t (** [singleton x] returns the one-element set containing only [x]. *) val remove: t -> elt -> t -(** [remove x s] returns a set containing all elements of [s], - except [x]. If [x] was not in [s], [s] is returned unchanged. *) +(** If [x] was not in [s], [s] is returned unchanged. *) val union: t -> t -> t -(** Set union. *) val inter: t -> t -> t -(** Set intersection. *) val diff: t -> t -> t -(** Set difference. *) + val cmp: t -> t -> int (** Total ordering between sets. Can be used as the ordering function for doing sets of sets. *) val eq: t -> t -> bool -(** [equal s1 s2] tests whether the sets [s1] and [s2] are +(** [eq s1 s2] tests whether the sets [s1] and [s2] are equal, that is, contain equal elements. *) val subset: t -> t -> bool (** [subset s1 s2] tests whether the set [s1] is a subset of the set [s2]. *) -val iter: (elt -> unit [@bs]) -> t -> unit +val iter: t -> (elt -> unit [@bs]) -> unit (** [iter f s] applies [f] in turn to all elements of [s]. The elements of [s] are presented to [f] in increasing order with respect to the ordering over the type of the elements. *) -val fold: (elt -> 'a -> 'a [@bs]) -> t -> 'a -> 'a +val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a (** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], where [x1 ... xN] are the elements of [s], in increasing order. *) -val forAll: (elt -> bool [@bs]) -> t -> bool +val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set - satisfy the predicate [p]. *) + satisfy the predicate [p]. Order unspecified. *) -val exists: (elt -> bool [@bs]) -> t -> bool +val exists: t -> (elt -> bool [@bs]) -> bool (** [exists p s] checks if at least one element of - the set satisfies the predicate [p]. *) + the set satisfies the predicate [p]. Oder unspecified. *) -val filter: (elt -> bool [@bs]) -> t -> t +val filter: t -> (elt -> bool [@bs]) -> t (** [filter p s] returns the set of all elements in [s] that satisfy predicate [p]. *) -val partition: (elt -> bool [@bs]) -> t -> t * t +val partition: t -> (elt -> bool [@bs]) -> t * t (** [partition p s] returns a pair of sets [(s1, s2)], where [s1] is the set of all the elements of [s] that satisfy the predicate [p], and [s2] is the set of all the elements of @@ -77,7 +72,7 @@ val partition: (elt -> bool [@bs]) -> t -> t * t val length: t -> int -val elements: t -> elt list +val toList: t -> elt list (** Return the list of all elements of the given set. The returned list is sorted in increasing order with respect to the ordering [Ord.compare], where [Ord] is the argument @@ -87,12 +82,9 @@ val toArray: t -> elt array val min: t -> elt option (** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering), or raise - [Not_found] if the set is empty. *) + (with respect to the [Ord.compare] ordering) *) val max: t -> elt option -(** Same as {!Set.S.min_elt}, but returns the largest element of the - given set. *) val split: elt -> t -> t * bool * t @@ -105,11 +97,8 @@ val split: elt -> t -> t * bool * t or [true] if [s] contains an element equal to [x]. *) val findOpt: elt -> t -> elt option -(** [find x s] returns the element of [s] equal to [x] (according - to [Ord.compare]), or raise [Not_found] if no such element - exists. - @since 4.01.0 *) + val ofArray : elt array -> t -val checkInvariant : t -> bool \ No newline at end of file +val checkInvariant : t -> bool diff --git a/jscomp/others/bs_internalAVLset.ml b/jscomp/others/bs_internalAVLset.ml index dd3dcb6dbbb..f324a0fed15 100644 --- a/jscomp/others/bs_internalAVLset.ml +++ b/jscomp/others/bs_internalAVLset.ml @@ -31,8 +31,8 @@ let rec copy n = | Some n -> let l,r = left n, right n in return @@ node - ~left:(copy l) ~right:(copy r) - ~key:(key n) ~h:(h n) + ~left:(copy l) ~right:(copy r) + ~key:(key n) ~h:(h n) (* Creates a new node with left son l, value v and right son r. We must have all elements of l < v < all elements of r. l and r must be balanced and | height l - height r | <= 2. @@ -185,46 +185,57 @@ let rec cons_enum s e = -> cons_enum (left n) (More( key n, right n, e)) -let rec iter0 f n = +let rec iter0 n f = match toOpt n with | None -> () - | Some n (* Node(l, v, r, _) *) -> iter0 f (left n); f (key n) [@bs]; iter0 f (right n) + | Some n -> + iter0 (left n) f; f (key n) [@bs]; iter0 (right n) f -let rec fold0 f s accu = +let rec fold0 s accu f = match toOpt s with | None -> accu - | Some n (* Node(l, v, r, _) *) -> fold0 f (right n) (f (key n) (fold0 f (left n) accu) [@bs]) + | Some n -> + let l,k,r = left n, key n , right n in + fold0 + r + (f (fold0 l accu f) k [@bs]) f -let rec forAll0 p n = +let rec forAll0 n p = match toOpt n with | None -> true - | Some n (* (l, v, r, _) *) -> p (key n) [@bs] && forAll0 p (left n) && forAll0 p (right n) + | Some n -> + p (key n) [@bs] && + forAll0 (left n) p && + forAll0 (right n) p -let rec exists0 p n = +let rec exists0 n p = match toOpt n with | None -> false - | Some n (* (l, v, r, _) *) -> p (key n) [@bs] || exists0 p (left n) || exists0 p (right n) + | Some n -> + p (key n) [@bs] || + exists0 (left n) p || + exists0 (right n) p -let rec filter0 p n = +let rec filter0 n p = match toOpt n with | None -> empty - | Some n (* (l, v, r, _) *) -> + | Some n -> (* call [p] in the expected left-to-right order *) - let l' = filter0 p (left n) in + let newL = filter0 (left n) p in let v = key n in let pv = p v [@bs] in - let r' = filter0 p (right n) in - if pv then join l' v r' else concat l' r' + let newR = filter0 (right n) p in + if pv then join newL v newR else concat newL newR -let rec partition0 p n = +let rec partition0 n p = match toOpt n with | None -> (empty, empty) - | Some n (* (l, v, r, _)*) -> + | Some n -> (* call [p] in the expected left-to-right order *) - let (lt, lf) = partition0 p (left n) in + let (lt, lf) = partition0 (left n) p in let v = key n in let pv = p v [@bs] in - let (rt, rf) = partition0 p (right n) in + let (rt, rf) = partition0 (right n) p in if pv then (join lt v rt, concat lf rf) else (concat lt rt, join lf v rf) @@ -251,9 +262,13 @@ let rec length0 n = let rec elements_aux accu n = match toOpt n with | None -> accu - | Some n (* Node(l, v, r, _) *) -> elements_aux (key n :: elements_aux accu (right n)) (left n) + | Some n -> + let l,k,r = left n, key n, right n in + elements_aux + (k :: elements_aux accu r) + l -let elements0 s = +let toList0 s = elements_aux [] s let rec checkInvariant (v : _ t0) = @@ -282,9 +297,9 @@ let rec fillArray n i arr = (* TODO: binary search tree to array efficiency *) let toArray0 n = - match toOpt n with - | None -> [||] - | Some n -> + match toOpt n with + | None -> [||] + | Some n -> let size = cardinalAux n in let v = Bs.Array.makeUninitializedUnsafe size in ignore (fillArray n 0 v : int); (* may add assertion *) @@ -303,7 +318,7 @@ let rotateWithLeftChild k2 = (rightSet k1 (return k2 )); let hlk2, hrk2 = (height (left k2), (height (right k2))) in (hSet k2 - (Pervasives.max hlk2 hrk2 + 1)); + (Pervasives.max hlk2 hrk2 + 1)); let hlk1, hk2 = (height (left k1), (h k2)) in (hSet k1 (Pervasives.max hlk1 hk2 + 1)); k1 @@ -335,7 +350,7 @@ let heightUpdateMutate t = let hlt, hrt = (height (left t),(height (right t))) in hSet t (Pervasives.max hlt hrt + 1); t - + let balMutate nt = let l, r = (left nt, right nt) in let hl, hr = (height l, height r) in @@ -346,7 +361,7 @@ let balMutate nt = heightUpdateMutate (rotateWithLeftChild nt) else heightUpdateMutate (doubleWithLeftChild nt) - ) + ) else if hr > 2 + hl then let r = unsafeCoerce r in @@ -357,11 +372,11 @@ let balMutate nt = heightUpdateMutate (doubleWithRightChild nt) ) else - begin - hSet nt (max hl hr + 1); - nt - end - + begin + hSet nt (max hl hr + 1); + nt + end + let rec removeMinAuxMutate n = let rn, ln = right n, left n in match toOpt ln with diff --git a/jscomp/others/bs_internalSetInt.ml b/jscomp/others/bs_internalSetInt.ml index e7e27415fad..ca5603c05f0 100644 --- a/jscomp/others/bs_internalSetInt.ml +++ b/jscomp/others/bs_internalSetInt.ml @@ -238,7 +238,8 @@ let rec removeMutateAux nt (x : elt)= let l,r = N.(left nt, right nt) in match N.(toOpt l, toOpt r) with | Some _, Some nr -> - N.rightSet nt (N.removeMinAuxMutateWithRoot nt nr ); + N.keySet nt (N.min0Aux nr ); + N.rightSet nt ( removeMutateAux nr x ); (* TODO specalized by removeMinAuxMutate*) N.return (N.balMutate nt) | None, Some _ -> r diff --git a/jscomp/others/set.cppo.ml b/jscomp/others/set.cppo.ml index 70119f86fa5..5a59d8a4f26 100644 --- a/jscomp/others/set.cppo.ml +++ b/jscomp/others/set.cppo.ml @@ -1,10 +1,10 @@ #ifdef TYPE_STRING type elt = string - #elif defined TYPE_INT +#elif defined TYPE_INT type elt = int - #else +#else [%error "unknown type"] - #endif +#endif module N = Bs_internalAVLset diff --git a/jscomp/others/set.cppo.mli b/jscomp/others/set.cppo.mli index b41585cf661..e7778fc4c88 100644 --- a/jscomp/others/set.cppo.mli +++ b/jscomp/others/set.cppo.mli @@ -12,68 +12,63 @@ type t (** The type of sets. *) val empty: t -(** The empty set. *) + val isEmpty: t -> bool -(** Test whether a set is empty or not. *) val mem: t -> elt -> bool -(** [mem x s] tests whether [x] belongs to the set [s]. *) + val add: t -> elt -> t -(** [add x s] returns a set containing all elements of [s], - plus [x]. If [x] was already in [s], [s] is returned unchanged. *) +(** If [x] was already in [s], [s] is returned unchanged. *) val singleton: elt -> t (** [singleton x] returns the one-element set containing only [x]. *) val remove: t -> elt -> t -(** [remove x s] returns a set containing all elements of [s], - except [x]. If [x] was not in [s], [s] is returned unchanged. *) +(** If [x] was not in [s], [s] is returned unchanged. *) val union: t -> t -> t -(** Set union. *) val inter: t -> t -> t -(** Set intersection. *) val diff: t -> t -> t -(** Set difference. *) + val cmp: t -> t -> int (** Total ordering between sets. Can be used as the ordering function for doing sets of sets. *) val eq: t -> t -> bool -(** [equal s1 s2] tests whether the sets [s1] and [s2] are +(** [eq s1 s2] tests whether the sets [s1] and [s2] are equal, that is, contain equal elements. *) val subset: t -> t -> bool (** [subset s1 s2] tests whether the set [s1] is a subset of the set [s2]. *) -val iter: (elt -> unit [@bs]) -> t -> unit +val iter: t -> (elt -> unit [@bs]) -> unit (** [iter f s] applies [f] in turn to all elements of [s]. The elements of [s] are presented to [f] in increasing order with respect to the ordering over the type of the elements. *) -val fold: (elt -> 'a -> 'a [@bs]) -> t -> 'a -> 'a +val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a (** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], where [x1 ... xN] are the elements of [s], in increasing order. *) -val forAll: (elt -> bool [@bs]) -> t -> bool +val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set - satisfy the predicate [p]. *) + satisfy the predicate [p]. Order unspecified. *) -val exists: (elt -> bool [@bs]) -> t -> bool +val exists: t -> (elt -> bool [@bs]) -> bool (** [exists p s] checks if at least one element of - the set satisfies the predicate [p]. *) + the set satisfies the predicate [p]. Oder unspecified. *) -val filter: (elt -> bool [@bs]) -> t -> t +val filter: t -> (elt -> bool [@bs]) -> t (** [filter p s] returns the set of all elements in [s] that satisfy predicate [p]. *) -val partition: (elt -> bool [@bs]) -> t -> t * t +val partition: t -> (elt -> bool [@bs]) -> t * t (** [partition p s] returns a pair of sets [(s1, s2)], where [s1] is the set of all the elements of [s] that satisfy the predicate [p], and [s2] is the set of all the elements of @@ -81,7 +76,7 @@ val partition: (elt -> bool [@bs]) -> t -> t * t val length: t -> int -val elements: t -> elt list +val toList: t -> elt list (** Return the list of all elements of the given set. The returned list is sorted in increasing order with respect to the ordering [Ord.compare], where [Ord] is the argument @@ -91,12 +86,9 @@ val toArray: t -> elt array val min: t -> elt option (** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering), or raise - [Not_found] if the set is empty. *) + (with respect to the [Ord.compare] ordering) *) val max: t -> elt option -(** Same as {!Set.S.min_elt}, but returns the largest element of the - given set. *) val split: elt -> t -> t * bool * t @@ -109,11 +101,8 @@ val split: elt -> t -> t * bool * t or [true] if [s] contains an element equal to [x]. *) val findOpt: elt -> t -> elt option -(** [find x s] returns the element of [s] equal to [x] (according - to [Ord.compare]), or raise [Not_found] if no such element - exists. - @since 4.01.0 *) + val ofArray : elt array -> t -val checkInvariant : t -> bool \ No newline at end of file +val checkInvariant : t -> bool diff --git a/jscomp/test/bs_set_int_test.js b/jscomp/test/bs_set_int_test.js index db1e49fe997..85d4b0393f1 100644 --- a/jscomp/test/bs_set_int_test.js +++ b/jscomp/test/bs_set_int_test.js @@ -90,9 +90,9 @@ var i = range(100, 1500); b("File \"bs_set_int_test.ml\", line 43, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i), v)); -var match = Bs_SetInt.partition((function (x) { +var match = Bs_SetInt.partition(v, (function (x) { return +(x % 3 === 0); - }), v); + })); var l = Bs_SetInt.empty; @@ -172,9 +172,9 @@ var minv = Bs_SetInt.min(v$1); var maxv = Bs_SetInt.max(v$1); -eq("File \"bs_set_int_test.ml\", line 79, characters 5-12", Bs_SetInt.fold((function (x, y) { +eq("File \"bs_set_int_test.ml\", line 79, characters 5-12", Bs_SetInt.fold(v$1, 0, (function (x, y) { return x + y | 0; - }), v$1, 0), $$Array.fold_left((function (prim, prim$1) { + })), $$Array.fold_left((function (prim, prim$1) { return prim + prim$1 | 0; }), 0, ss)); diff --git a/jscomp/test/bs_set_int_test.ml b/jscomp/test/bs_set_int_test.ml index 2897789a42d..bfb253f911c 100644 --- a/jscomp/test/bs_set_int_test.ml +++ b/jscomp/test/bs_set_int_test.ml @@ -41,7 +41,7 @@ let revRange i j = let () = let v = (ofA (Array.append (range 100 1000) (revRange 400 1500))) in b __LOC__ (v =~ (range 100 1500)); - let l, r = N.partition (fun[@bs] x -> x mod 3 = 0) v in + let l, r = N.partition v (fun[@bs] x -> x mod 3 = 0) in let nl, nr = let l,r = ref N.empty, ref N.empty in for i = 100 to 1500 do @@ -76,7 +76,7 @@ let () = let ss = [|1;222;3;4;2;0;33;-1|] in let v = ofA [|1;222;3;4;2;0;33;-1|] in let minv, maxv = N.min v, N.max v in - eq __LOC__ (N.fold (fun [@bs] x y -> x + y) v 0) (Array.fold_left (+) 0 ss) ; + eq __LOC__ (N.fold v 0 (fun [@bs] x y -> x + y) ) (Array.fold_left (+) 0 ss) ; eq __LOC__ minv (Some (-1)); eq __LOC__ maxv (Some 222); let v = N.remove v 3 in diff --git a/lib/js/bs_Array.js b/lib/js/bs_Array.js index d66bcd23e49..e769904a063 100644 --- a/lib/js/bs_Array.js +++ b/lib/js/bs_Array.js @@ -208,7 +208,7 @@ function foldLeft(a, x, f) { function foldRight(a, x, f) { var r = x; for(var i = a.length - 1 | 0; i >= 0; --i){ - r = f(a[i], r); + r = f(r, a[i]); } return r; } diff --git a/lib/js/bs_Set.js b/lib/js/bs_Set.js index cbf8c88f242..764c73af3c6 100644 --- a/lib/js/bs_Set.js +++ b/lib/js/bs_Set.js @@ -268,35 +268,35 @@ function eq(m, n) { return eq0(dict[/* cmp */0], mdata, ndata); } -function iter(f, m) { - return Bs_internalAVLset.iter0(f, m.data); +function iter(m, f) { + return Bs_internalAVLset.iter0(m.data, f); } -function fold(f, m, acc) { - return Bs_internalAVLset.fold0(f, m.data, acc); +function fold(m, acc, f) { + return Bs_internalAVLset.fold0(m.data, acc, f); } -function forAll(f, m) { - return Bs_internalAVLset.forAll0(f, m.data); +function forAll(m, f) { + return Bs_internalAVLset.forAll0(m.data, f); } -function exists(f, m) { - return Bs_internalAVLset.exists0(f, m.data); +function exists(m, f) { + return Bs_internalAVLset.exists0(m.data, f); } -function filter(f, m) { +function filter(m, f) { var data = m.data; var dict = m.dict; return { dict: dict, - data: Bs_internalAVLset.filter0(f, data) + data: Bs_internalAVLset.filter0(data, f) }; } -function partition(f, m) { +function partition(m, f) { var mdata = m.data; var dict = m.dict; - var match = Bs_internalAVLset.partition0(f, mdata); + var match = Bs_internalAVLset.partition0(mdata, f); return /* tuple */[ { dict: dict, @@ -313,8 +313,8 @@ function length(m) { return Bs_internalAVLset.length0(m.data); } -function elements(m) { - return Bs_internalAVLset.elements0(m.data); +function toList(m) { + return Bs_internalAVLset.toList0(m.data); } function toArray(m) { @@ -366,7 +366,7 @@ var partition0 = Bs_internalAVLset.partition0; var length0 = Bs_internalAVLset.length0; -var elements0 = Bs_internalAVLset.elements0; +var toList0 = Bs_internalAVLset.toList0; var toArray0 = Bs_internalAVLset.toArray0; @@ -408,8 +408,8 @@ exports.partition0 = partition0; exports.partition = partition; exports.length0 = length0; exports.length = length; -exports.elements0 = elements0; -exports.elements = elements; +exports.toList0 = toList0; +exports.toList = toList; exports.toArray0 = toArray0; exports.toArray = toArray; exports.min0 = min0; diff --git a/lib/js/bs_SetInt.js b/lib/js/bs_SetInt.js index ba8f59b61ae..87b10a4ac2a 100644 --- a/lib/js/bs_SetInt.js +++ b/lib/js/bs_SetInt.js @@ -41,7 +41,7 @@ var partition = Bs_internalAVLset.partition0; var length = Bs_internalAVLset.length0; -var elements = Bs_internalAVLset.elements0; +var toList = Bs_internalAVLset.toList0; var toArray = Bs_internalAVLset.toArray0; @@ -76,7 +76,7 @@ exports.exists = exists; exports.filter = filter; exports.partition = partition; exports.length = length; -exports.elements = elements; +exports.toList = toList; exports.toArray = toArray; exports.min = min; exports.max = max; diff --git a/lib/js/bs_SetString.js b/lib/js/bs_SetString.js index 2d99eb431a6..60d7c875ede 100644 --- a/lib/js/bs_SetString.js +++ b/lib/js/bs_SetString.js @@ -41,7 +41,7 @@ var partition = Bs_internalAVLset.partition0; var length = Bs_internalAVLset.length0; -var elements = Bs_internalAVLset.elements0; +var toList = Bs_internalAVLset.toList0; var toArray = Bs_internalAVLset.toArray0; @@ -76,7 +76,7 @@ exports.exists = exists; exports.filter = filter; exports.partition = partition; exports.length = length; -exports.elements = elements; +exports.toList = toList; exports.toArray = toArray; exports.min = min; exports.max = max; diff --git a/lib/js/bs_internalAVLset.js b/lib/js/bs_internalAVLset.js index eb56de8631f..f313c6f0bff 100644 --- a/lib/js/bs_internalAVLset.js +++ b/lib/js/bs_internalAVLset.js @@ -246,11 +246,11 @@ function cons_enum(_s, _e) { }; } -function iter0(f, _n) { +function iter0(_n, f) { while(true) { var n = _n; if (n !== null) { - iter0(f, n.left); + iter0(n.left, f); f(n.key); _n = n.right; continue ; @@ -261,13 +261,16 @@ function iter0(f, _n) { }; } -function fold0(f, _s, _accu) { +function fold0(_s, _accu, f) { while(true) { var accu = _accu; var s = _s; if (s !== null) { - _accu = f(s.key, fold0(f, s.left, accu)); - _s = s.right; + var l = s.left; + var k = s.key; + var r = s.right; + _accu = f(fold0(l, accu, f), k); + _s = r; continue ; } else { @@ -276,12 +279,12 @@ function fold0(f, _s, _accu) { }; } -function forAll0(p, _n) { +function forAll0(_n, p) { while(true) { var n = _n; if (n !== null) { if (p(n.key)) { - if (forAll0(p, n.left)) { + if (forAll0(n.left, p)) { _n = n.right; continue ; @@ -297,13 +300,13 @@ function forAll0(p, _n) { }; } -function exists0(p, _n) { +function exists0(_n, p) { while(true) { var n = _n; if (n !== null) { if (p(n.key)) { return /* true */1; - } else if (exists0(p, n.left)) { + } else if (exists0(n.left, p)) { return /* true */1; } else { _n = n.right; @@ -316,30 +319,30 @@ function exists0(p, _n) { }; } -function filter0(p, n) { +function filter0(n, p) { if (n !== null) { - var l$prime = filter0(p, n.left); + var newL = filter0(n.left, p); var v = n.key; var pv = p(v); - var r$prime = filter0(p, n.right); + var newR = filter0(n.right, p); if (pv) { - return join(l$prime, v, r$prime); + return join(newL, v, newR); } else { - return concat(l$prime, r$prime); + return concat(newL, newR); } } else { return null; } } -function partition0(p, n) { +function partition0(n, p) { if (n !== null) { - var match = partition0(p, n.left); + var match = partition0(n.left, p); var lf = match[1]; var lt = match[0]; var v = n.key; var pv = p(v); - var match$1 = partition0(p, n.right); + var match$1 = partition0(n.right, p); var rf = match$1[1]; var rt = match$1[0]; if (pv) { @@ -382,10 +385,13 @@ function elements_aux(_accu, _n) { var n = _n; var accu = _accu; if (n !== null) { - _n = n.left; + var l = n.left; + var k = n.key; + var r = n.right; + _n = l; _accu = /* :: */[ - n.key, - elements_aux(accu, n.right) + k, + elements_aux(accu, r) ]; continue ; @@ -395,7 +401,7 @@ function elements_aux(_accu, _n) { }; } -function elements0(s) { +function toList0(s) { return elements_aux(/* [] */0, s); } @@ -596,7 +602,7 @@ exports.partition0 = partition0; exports.cardinalAux = cardinalAux; exports.length0 = length0; exports.elements_aux = elements_aux; -exports.elements0 = elements0; +exports.toList0 = toList0; exports.checkInvariant = checkInvariant; exports.fillArray = fillArray; exports.toArray0 = toArray0; diff --git a/lib/js/bs_internalSetInt.js b/lib/js/bs_internalSetInt.js index 79384088c60..aeea34baef8 100644 --- a/lib/js/bs_internalSetInt.js +++ b/lib/js/bs_internalSetInt.js @@ -371,7 +371,8 @@ function removeMutateAux(nt, x) { var r = nt.right; if (l !== null) { if (r !== null) { - nt.right = Bs_internalAVLset.removeMinAuxMutateWithRoot(nt, r); + nt.key = Bs_internalAVLset.min0Aux(r); + nt.right = removeMutateAux(r, x); return Bs_internalAVLset.balMutate(nt); } else { return l; From 9546b83b1b7588dddc0c3d6190e49f5b5b6a2dc1 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Sat, 30 Dec 2017 16:23:48 +0800 Subject: [PATCH 2/3] add minNull/maxNull --- jscomp/others/bs_Set.ml | 9 ++- jscomp/others/bs_Set.mli | 19 ++---- jscomp/others/bs_SetInt.ml | 6 +- jscomp/others/bs_SetInt.mli | 12 ++-- jscomp/others/bs_SetIntM.ml | 4 +- jscomp/others/bs_SetString.ml | 6 +- jscomp/others/bs_SetString.mli | 12 ++-- jscomp/others/bs_internalAVLset.ml | 13 ++++- jscomp/others/set.cppo.mli | 12 ++-- jscomp/test/bs_set_int_test.js | 92 +++++++++++------------------- jscomp/test/bs_set_int_test.ml | 19 ++---- lib/js/bs_Set.js | 20 +++---- lib/js/bs_SetInt.js | 14 +++-- lib/js/bs_SetString.js | 14 +++-- lib/js/bs_internalAVLset.js | 26 +++++++-- 15 files changed, 137 insertions(+), 141 deletions(-) diff --git a/jscomp/others/bs_Set.ml b/jscomp/others/bs_Set.ml index 3b9f92d208c..f2d72d324f2 100644 --- a/jscomp/others/bs_Set.ml +++ b/jscomp/others/bs_Set.ml @@ -14,8 +14,8 @@ type ('elt,'id) t = (('elt,'id) Bs_Cmp.t , ('elt,'id) t0) B.bag let empty0 = N.empty0 let isEmpty0 = N.isEmpty0 let singleton0 = N.singleton0 -let min0 = N.min0 -let max0 = N.max0 +let minOpt0 = N.minOpt0 +let maxOpt0 = N.maxOpt0 let iter0 = N.iter0 let fold0 = N.fold0 let forAll0 = N.forAll0 @@ -289,9 +289,8 @@ let length m = length0 (B.data m) let toList m = toList0 (B.data m) let toArray m = toArray0 (B.data m) -let min m = min0 (B.data m) - -let max m = max0 (B.data m) +let minOpt m = minOpt0 (B.data m) +let maxOpt m = maxOpt0 (B.data m) let split (type elt) (type id) e (m : (elt,id) t) = let dict, data = B.(dict m, data m) in diff --git a/jscomp/others/bs_Set.mli b/jscomp/others/bs_Set.mli index 087fa724ab8..7af98df4a99 100644 --- a/jscomp/others/bs_Set.mli +++ b/jscomp/others/bs_Set.mli @@ -136,23 +136,14 @@ val length: ('elt, 'id) t -> int val toList0: ('elt, 'id) t0 -> 'elt list val toList: ('elt, 'id) t -> 'elt list -(** Return the list of all elements of the given set. - The returned list is sorted in increasing order with respect - to the ordering [Ord.compare], where [Ord] is the argument - given to {!Set.Make}. *) +(** In increasing order*) val toArray0: ('elt, 'id) t0 -> 'elt array val toArray: ('elt, 'id) t -> 'elt array -val min0: ('elt, 'id) t0 -> 'elt option -val min: ('elt, 'id) t -> 'elt option -(** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering), or raise - [Not_found] if the set is empty. *) - -val max0: ('elt, 'id) t0 -> 'elt option -val max: ('elt, 'id) t -> 'elt option -(** Same as {!Set.S.min_elt}, but returns the largest element of the - given set. *) +val minOpt0: ('elt, 'id) t0 -> 'elt option +val minOpt: ('elt, 'id) t -> 'elt option +val maxOpt0: ('elt, 'id) t0 -> 'elt option +val maxOpt: ('elt, 'id) t -> 'elt option val split0: diff --git a/jscomp/others/bs_SetInt.ml b/jscomp/others/bs_SetInt.ml index 3c3457827da..43954a9cdc6 100644 --- a/jscomp/others/bs_SetInt.ml +++ b/jscomp/others/bs_SetInt.ml @@ -8,8 +8,10 @@ type t = I.t let empty = N.empty0 let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let min = N.min0 -let max = N.max0 +let minOpt = N.minOpt0 +let minNull = N.minNull0 +let maxOpt = N.maxOpt0 +let maxNull = N.maxNull0 let iter = N.iter0 let fold = N.fold0 let forAll = N.forAll0 diff --git a/jscomp/others/bs_SetInt.mli b/jscomp/others/bs_SetInt.mli index 15453ef2bb6..140663d5903 100644 --- a/jscomp/others/bs_SetInt.mli +++ b/jscomp/others/bs_SetInt.mli @@ -49,8 +49,7 @@ val iter: t -> (elt -> unit [@bs]) -> unit with respect to the ordering over the type of the elements. *) val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a -(** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], - where [x1 ... xN] are the elements of [s], in increasing order. *) +(** Iterate in increasing order. *) val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set @@ -80,11 +79,10 @@ val toList: t -> elt list val toArray: t -> elt array -val min: t -> elt option -(** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering) *) - -val max: t -> elt option +val minOpt: t -> elt option +val minNull: t -> elt Js.null +val maxOpt: t -> elt option +val maxNull: t -> elt Js.null val split: elt -> t -> t * bool * t diff --git a/jscomp/others/bs_SetIntM.ml b/jscomp/others/bs_SetIntM.ml index b99a2938ef0..6b3ecf8f07e 100644 --- a/jscomp/others/bs_SetIntM.ml +++ b/jscomp/others/bs_SetIntM.ml @@ -13,8 +13,8 @@ let empty = N.empty0 (* No value restriction ? *) let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let min = N.min0 -let max = N.max0 +let minOpt = N.minOpt0 +let maxOpt = N.maxOpt0 let iter = N.iter0 let fold = N.fold0 let forAll = N.forAll0 diff --git a/jscomp/others/bs_SetString.ml b/jscomp/others/bs_SetString.ml index 9c3d531df11..4756fdd177a 100644 --- a/jscomp/others/bs_SetString.ml +++ b/jscomp/others/bs_SetString.ml @@ -8,8 +8,10 @@ type t = I.t let empty = N.empty0 let isEmpty = N.isEmpty0 let singleton = N.singleton0 -let min = N.min0 -let max = N.max0 +let minOpt = N.minOpt0 +let maxOpt = N.maxOpt0 +let minNull = N.minNull0 +let maxNull = N.maxNull0 let iter = N.iter0 let fold = N.fold0 let forAll = N.forAll0 diff --git a/jscomp/others/bs_SetString.mli b/jscomp/others/bs_SetString.mli index e0321394306..fa4d0ab972b 100644 --- a/jscomp/others/bs_SetString.mli +++ b/jscomp/others/bs_SetString.mli @@ -49,8 +49,7 @@ val iter: t -> (elt -> unit [@bs]) -> unit with respect to the ordering over the type of the elements. *) val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a -(** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], - where [x1 ... xN] are the elements of [s], in increasing order. *) +(** Iterate in increasing order. *) val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set @@ -80,11 +79,10 @@ val toList: t -> elt list val toArray: t -> elt array -val min: t -> elt option -(** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering) *) - -val max: t -> elt option +val minOpt: t -> elt option +val minNull: t -> elt Js.null +val maxOpt: t -> elt option +val maxNull: t -> elt Js.null val split: elt -> t -> t * bool * t diff --git a/jscomp/others/bs_internalAVLset.ml b/jscomp/others/bs_internalAVLset.ml index f324a0fed15..c00d0d3b7df 100644 --- a/jscomp/others/bs_internalAVLset.ml +++ b/jscomp/others/bs_internalAVLset.ml @@ -128,21 +128,30 @@ let rec min0Aux n = | None -> key n | Some n -> min0Aux n -let rec min0 n = +let minOpt0 n = match toOpt n with None -> None | Some n -> Some (min0Aux n) +let minNull0 n = + match toOpt n with + | None -> Js.null + | Some n -> return (min0Aux n) + let rec max0Aux n = match toOpt (right n) with | None -> key n | Some n -> max0Aux n -let rec max0 n = +let maxOpt0 n = match toOpt n with | None -> None | Some n -> Some (max0Aux n) +let maxNull0 n = + match toOpt n with + | None -> Js.null + | Some n -> return (max0Aux n) (* Remove the smallest element of the given set *) let rec removeMinAux n = diff --git a/jscomp/others/set.cppo.mli b/jscomp/others/set.cppo.mli index e7778fc4c88..04b1323c070 100644 --- a/jscomp/others/set.cppo.mli +++ b/jscomp/others/set.cppo.mli @@ -53,8 +53,7 @@ val iter: t -> (elt -> unit [@bs]) -> unit with respect to the ordering over the type of the elements. *) val fold: t -> 'a -> ('a -> elt -> 'a [@bs]) -> 'a -(** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)], - where [x1 ... xN] are the elements of [s], in increasing order. *) +(** Iterate in increasing order. *) val forAll: t -> (elt -> bool [@bs]) -> bool (** [for_all p s] checks if all elements of the set @@ -84,11 +83,10 @@ val toList: t -> elt list val toArray: t -> elt array -val min: t -> elt option -(** Return the smallest element of the given set - (with respect to the [Ord.compare] ordering) *) - -val max: t -> elt option +val minOpt: t -> elt option +val minNull: t -> elt Js.null +val maxOpt: t -> elt option +val maxNull: t -> elt Js.null val split: elt -> t -> t * bool * t diff --git a/jscomp/test/bs_set_int_test.js b/jscomp/test/bs_set_int_test.js index 85d4b0393f1..8f7cb9aba54 100644 --- a/jscomp/test/bs_set_int_test.js +++ b/jscomp/test/bs_set_int_test.js @@ -3,7 +3,6 @@ var Mt = require("./mt.js"); var List = require("../../lib/js/list.js"); var $$Array = require("../../lib/js/array.js"); -var Block = require("../../lib/js/block.js"); var Bs_Array = require("../../lib/js/bs_Array.js"); var Bs_SetInt = require("../../lib/js/bs_SetInt.js"); @@ -12,34 +11,11 @@ var suites = [/* [] */0]; var test_id = [0]; function eq(loc, x, y) { - test_id[0] = test_id[0] + 1 | 0; - suites[0] = /* :: */[ - /* tuple */[ - loc + (" id " + test_id[0]), - (function () { - return /* Eq */Block.__(0, [ - x, - y - ]); - }) - ], - suites[0] - ]; - return /* () */0; + return Mt.eq_suites(test_id, suites, loc, x, y); } function b(loc, v) { - test_id[0] = test_id[0] + 1 | 0; - suites[0] = /* :: */[ - /* tuple */[ - loc + (" id " + test_id[0]), - (function () { - return /* Ok */Block.__(4, [v]); - }) - ], - suites[0] - ]; - return /* () */0; + return Mt.bool_suites(test_id, suites, loc, v); } function $eq$tilde(s, i) { @@ -50,7 +26,7 @@ function $eq$star(a, b) { return Bs_SetInt.eq(Bs_SetInt.ofArray(a), Bs_SetInt.ofArray(b)); } -b("File \"bs_set_int_test.ml\", line 24, characters 4-11", $eq$star(/* int array */[ +b("File \"bs_set_int_test.ml\", line 17, characters 4-11", $eq$star(/* int array */[ 1, 2, 3 @@ -70,7 +46,7 @@ var u = Bs_SetInt.inter(Bs_SetInt.ofArray(/* int array */[ 5 ])); -b("File \"bs_set_int_test.ml\", line 30, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(/* int array */[3]), u)); +b("File \"bs_set_int_test.ml\", line 23, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(/* int array */[3]), u)); function range(i, j) { return $$Array.init((j - i | 0) + 1 | 0, (function (k) { @@ -88,7 +64,7 @@ var v = Bs_SetInt.ofArray($$Array.append(range(100, 1000), revRange(400, 1500))) var i = range(100, 1500); -b("File \"bs_set_int_test.ml\", line 43, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i), v)); +b("File \"bs_set_int_test.ml\", line 36, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i), v)); var match = Bs_SetInt.partition(v, (function (x) { return +(x % 3 === 0); @@ -106,45 +82,45 @@ for(var i$1 = 100; i$1 <= 1500; ++i$1){ } } -b("File \"bs_set_int_test.ml\", line 54, characters 4-11", Bs_SetInt.eq(match[0], l)); +b("File \"bs_set_int_test.ml\", line 47, characters 4-11", Bs_SetInt.eq(match[0], l)); -b("File \"bs_set_int_test.ml\", line 55, characters 4-11", Bs_SetInt.eq(match[1], r)); +b("File \"bs_set_int_test.ml\", line 48, characters 4-11", Bs_SetInt.eq(match[1], r)); var i$2 = range(50, 100); var s = Bs_SetInt.inter(Bs_SetInt.ofArray(range(1, 100)), Bs_SetInt.ofArray(range(50, 200))); -b("File \"bs_set_int_test.ml\", line 58, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$2), s)); +b("File \"bs_set_int_test.ml\", line 51, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$2), s)); var i$3 = range(1, 200); var s$1 = Bs_SetInt.union(Bs_SetInt.ofArray(range(1, 100)), Bs_SetInt.ofArray(range(50, 200))); -b("File \"bs_set_int_test.ml\", line 61, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$3), s$1)); +b("File \"bs_set_int_test.ml\", line 54, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$3), s$1)); var i$4 = range(1, 49); var s$2 = Bs_SetInt.diff(Bs_SetInt.ofArray(range(1, 100)), Bs_SetInt.ofArray(range(50, 200))); -b("File \"bs_set_int_test.ml\", line 64, characters 6-13", Bs_SetInt.eq(Bs_SetInt.ofArray(i$4), s$2)); +b("File \"bs_set_int_test.ml\", line 57, characters 6-13", Bs_SetInt.eq(Bs_SetInt.ofArray(i$4), s$2)); var i$5 = revRange(50, 100); var s$3 = Bs_SetInt.inter(Bs_SetInt.ofArray(revRange(1, 100)), Bs_SetInt.ofArray(revRange(50, 200))); -b("File \"bs_set_int_test.ml\", line 67, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$5), s$3)); +b("File \"bs_set_int_test.ml\", line 60, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$5), s$3)); var i$6 = revRange(1, 200); var s$4 = Bs_SetInt.union(Bs_SetInt.ofArray(revRange(1, 100)), Bs_SetInt.ofArray(revRange(50, 200))); -b("File \"bs_set_int_test.ml\", line 70, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$6), s$4)); +b("File \"bs_set_int_test.ml\", line 63, characters 4-11", Bs_SetInt.eq(Bs_SetInt.ofArray(i$6), s$4)); var i$7 = revRange(1, 49); var s$5 = Bs_SetInt.diff(Bs_SetInt.ofArray(revRange(1, 100)), Bs_SetInt.ofArray(revRange(50, 200))); -b("File \"bs_set_int_test.ml\", line 73, characters 6-13", Bs_SetInt.eq(Bs_SetInt.ofArray(i$7), s$5)); +b("File \"bs_set_int_test.ml\", line 66, characters 6-13", Bs_SetInt.eq(Bs_SetInt.ofArray(i$7), s$5)); var ss = /* array */[ 1, @@ -168,49 +144,49 @@ var v$1 = Bs_SetInt.ofArray(/* array */[ -1 ]); -var minv = Bs_SetInt.min(v$1); +var minv = Bs_SetInt.minOpt(v$1); -var maxv = Bs_SetInt.max(v$1); +var maxv = Bs_SetInt.maxOpt(v$1); -eq("File \"bs_set_int_test.ml\", line 79, characters 5-12", Bs_SetInt.fold(v$1, 0, (function (x, y) { +eq("File \"bs_set_int_test.ml\", line 72, characters 5-12", Bs_SetInt.fold(v$1, 0, (function (x, y) { return x + y | 0; })), $$Array.fold_left((function (prim, prim$1) { return prim + prim$1 | 0; }), 0, ss)); -eq("File \"bs_set_int_test.ml\", line 80, characters 5-12", minv, /* Some */[-1]); +eq("File \"bs_set_int_test.ml\", line 73, characters 5-12", minv, /* Some */[-1]); -eq("File \"bs_set_int_test.ml\", line 81, characters 5-12", maxv, /* Some */[222]); +eq("File \"bs_set_int_test.ml\", line 74, characters 5-12", maxv, /* Some */[222]); var v$2 = Bs_SetInt.remove(v$1, 3); -var minv$1 = Bs_SetInt.min(v$2); +var minv$1 = Bs_SetInt.minOpt(v$2); -var maxv$1 = Bs_SetInt.max(v$2); +var maxv$1 = Bs_SetInt.maxOpt(v$2); -eq("File \"bs_set_int_test.ml\", line 84, characters 5-12", minv$1, /* Some */[-1]); +eq("File \"bs_set_int_test.ml\", line 77, characters 5-12", minv$1, /* Some */[-1]); -eq("File \"bs_set_int_test.ml\", line 85, characters 5-12", maxv$1, /* Some */[222]); +eq("File \"bs_set_int_test.ml\", line 78, characters 5-12", maxv$1, /* Some */[222]); var v$3 = Bs_SetInt.remove(v$2, 222); -var minv$2 = Bs_SetInt.min(v$3); +var minv$2 = Bs_SetInt.minOpt(v$3); -var maxv$2 = Bs_SetInt.max(v$3); +var maxv$2 = Bs_SetInt.maxOpt(v$3); -eq("File \"bs_set_int_test.ml\", line 88, characters 5-12", minv$2, /* Some */[-1]); +eq("File \"bs_set_int_test.ml\", line 81, characters 5-12", minv$2, /* Some */[-1]); -eq("File \"bs_set_int_test.ml\", line 89, characters 5-12", maxv$2, /* Some */[33]); +eq("File \"bs_set_int_test.ml\", line 82, characters 5-12", maxv$2, /* Some */[33]); var v$4 = Bs_SetInt.remove(v$3, -1); -var minv$3 = Bs_SetInt.min(v$4); +var minv$3 = Bs_SetInt.minOpt(v$4); -var maxv$3 = Bs_SetInt.max(v$4); +var maxv$3 = Bs_SetInt.maxOpt(v$4); -eq("File \"bs_set_int_test.ml\", line 92, characters 5-12", minv$3, /* Some */[0]); +eq("File \"bs_set_int_test.ml\", line 85, characters 5-12", minv$3, /* Some */[0]); -eq("File \"bs_set_int_test.ml\", line 93, characters 5-12", maxv$3, /* Some */[33]); +eq("File \"bs_set_int_test.ml\", line 86, characters 5-12", maxv$3, /* Some */[33]); var v$5 = Bs_SetInt.remove(v$4, 0); @@ -224,7 +200,7 @@ var v$9 = Bs_SetInt.remove(v$8, 4); var v$10 = Bs_SetInt.remove(v$9, 1); -b("File \"bs_set_int_test.ml\", line 100, characters 4-11", Bs_SetInt.isEmpty(v$10)); +b("File \"bs_set_int_test.ml\", line 93, characters 4-11", Bs_SetInt.isEmpty(v$10)); var v$11 = Bs_Array.init(1000000, (function (i) { return i; @@ -234,15 +210,15 @@ Bs_Array.shuffleInPlace(v$11); var u$1 = Bs_SetInt.ofArray(v$11); -b("File \"bs_set_int_test.ml\", line 108, characters 4-11", Bs_SetInt.checkInvariant(u$1)); +b("File \"bs_set_int_test.ml\", line 101, characters 4-11", Bs_SetInt.checkInvariant(u$1)); var firstHalf = Bs_Array.sub(v$11, 0, 2000); var xx = Bs_Array.foldLeft(firstHalf, u$1, Bs_SetInt.remove); -b("File \"bs_set_int_test.ml\", line 112, characters 4-11", Bs_SetInt.checkInvariant(u$1)); +b("File \"bs_set_int_test.ml\", line 105, characters 4-11", Bs_SetInt.checkInvariant(u$1)); -b("File \"bs_set_int_test.ml\", line 113, characters 4-11", Bs_SetInt.eq(Bs_SetInt.union(Bs_SetInt.ofArray(firstHalf), xx), u$1)); +b("File \"bs_set_int_test.ml\", line 106, characters 4-11", Bs_SetInt.eq(Bs_SetInt.union(Bs_SetInt.ofArray(firstHalf), xx), u$1)); Mt.from_pair_suites("bs_set_int_test.ml", suites[0]); diff --git a/jscomp/test/bs_set_int_test.ml b/jscomp/test/bs_set_int_test.ml index bfb253f911c..64f122c3667 100644 --- a/jscomp/test/bs_set_int_test.ml +++ b/jscomp/test/bs_set_int_test.ml @@ -1,15 +1,8 @@ let suites : Mt.pair_suites ref = ref [] let test_id = ref 0 -let eq loc x y = - incr test_id ; - suites := - (loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites +let eq loc x y = Mt.eq_suites ~suites ~test_id loc x y -let b loc v = - incr test_id ; - suites := - (loc ^" id " ^ (string_of_int !test_id), - (fun _ -> Mt.Ok v)) :: !suites +let b loc v = Mt.bool_suites ~suites ~test_id loc v module N = Bs.SetInt @@ -75,20 +68,20 @@ let () = let () = let ss = [|1;222;3;4;2;0;33;-1|] in let v = ofA [|1;222;3;4;2;0;33;-1|] in - let minv, maxv = N.min v, N.max v in + let minv, maxv = N.minOpt v, N.maxOpt v in eq __LOC__ (N.fold v 0 (fun [@bs] x y -> x + y) ) (Array.fold_left (+) 0 ss) ; eq __LOC__ minv (Some (-1)); eq __LOC__ maxv (Some 222); let v = N.remove v 3 in - let minv, maxv = N.min v, N.max v in + let minv, maxv = N.minOpt v, N.maxOpt v in eq __LOC__ minv (Some (-1)); eq __LOC__ maxv (Some 222); let v = N.remove v 222 in - let minv, maxv = N.min v, N.max v in + let minv, maxv = N.minOpt v, N.maxOpt v in eq __LOC__ minv (Some (-1)); eq __LOC__ maxv (Some 33); let v = N.remove v (-1) in - let minv, maxv = N.min v, N.max v in + let minv, maxv = N.minOpt v, N.maxOpt v in eq __LOC__ minv (Some (0)); eq __LOC__ maxv (Some 33); let v = N.remove v 0 in diff --git a/lib/js/bs_Set.js b/lib/js/bs_Set.js index 764c73af3c6..2e69a4778d0 100644 --- a/lib/js/bs_Set.js +++ b/lib/js/bs_Set.js @@ -321,12 +321,12 @@ function toArray(m) { return Bs_internalAVLset.toArray0(m.data); } -function min(m) { - return Bs_internalAVLset.min0(m.data); +function minOpt(m) { + return Bs_internalAVLset.minOpt0(m.data); } -function max(m) { - return Bs_internalAVLset.max0(m.data); +function maxOpt(m) { + return Bs_internalAVLset.maxOpt0(m.data); } function split(e, m) { @@ -370,9 +370,9 @@ var toList0 = Bs_internalAVLset.toList0; var toArray0 = Bs_internalAVLset.toArray0; -var min0 = Bs_internalAVLset.min0; +var minOpt0 = Bs_internalAVLset.minOpt0; -var max0 = Bs_internalAVLset.max0; +var maxOpt0 = Bs_internalAVLset.maxOpt0; exports.empty0 = empty0; exports.empty = empty; @@ -412,10 +412,10 @@ exports.toList0 = toList0; exports.toList = toList; exports.toArray0 = toArray0; exports.toArray = toArray; -exports.min0 = min0; -exports.min = min; -exports.max0 = max0; -exports.max = max; +exports.minOpt0 = minOpt0; +exports.minOpt = minOpt; +exports.maxOpt0 = maxOpt0; +exports.maxOpt = maxOpt; exports.split0 = split0; exports.split = split; /* No side effect */ diff --git a/lib/js/bs_SetInt.js b/lib/js/bs_SetInt.js index 87b10a4ac2a..f9ac6f46cd8 100644 --- a/lib/js/bs_SetInt.js +++ b/lib/js/bs_SetInt.js @@ -45,9 +45,13 @@ var toList = Bs_internalAVLset.toList0; var toArray = Bs_internalAVLset.toArray0; -var min = Bs_internalAVLset.min0; +var minOpt = Bs_internalAVLset.minOpt0; -var max = Bs_internalAVLset.max0; +var minNull = Bs_internalAVLset.minNull0; + +var maxOpt = Bs_internalAVLset.maxOpt0; + +var maxNull = Bs_internalAVLset.maxNull0; var split = Bs_internalSetInt.split; @@ -78,8 +82,10 @@ exports.partition = partition; exports.length = length; exports.toList = toList; exports.toArray = toArray; -exports.min = min; -exports.max = max; +exports.minOpt = minOpt; +exports.minNull = minNull; +exports.maxOpt = maxOpt; +exports.maxNull = maxNull; exports.split = split; exports.findOpt = findOpt; exports.ofArray = ofArray; diff --git a/lib/js/bs_SetString.js b/lib/js/bs_SetString.js index 60d7c875ede..d4b226c4e50 100644 --- a/lib/js/bs_SetString.js +++ b/lib/js/bs_SetString.js @@ -45,9 +45,13 @@ var toList = Bs_internalAVLset.toList0; var toArray = Bs_internalAVLset.toArray0; -var min = Bs_internalAVLset.min0; +var minOpt = Bs_internalAVLset.minOpt0; -var max = Bs_internalAVLset.max0; +var minNull = Bs_internalAVLset.minNull0; + +var maxOpt = Bs_internalAVLset.maxOpt0; + +var maxNull = Bs_internalAVLset.maxNull0; var split = Bs_internalSetString.split; @@ -78,8 +82,10 @@ exports.partition = partition; exports.length = length; exports.toList = toList; exports.toArray = toArray; -exports.min = min; -exports.max = max; +exports.minOpt = minOpt; +exports.minNull = minNull; +exports.maxOpt = maxOpt; +exports.maxNull = maxNull; exports.split = split; exports.findOpt = findOpt; exports.ofArray = ofArray; diff --git a/lib/js/bs_internalAVLset.js b/lib/js/bs_internalAVLset.js index f313c6f0bff..c3adfd3297b 100644 --- a/lib/js/bs_internalAVLset.js +++ b/lib/js/bs_internalAVLset.js @@ -155,7 +155,7 @@ function min0Aux(_n) { }; } -function min0(n) { +function minOpt0(n) { if (n !== null) { return /* Some */[min0Aux(n)]; } else { @@ -163,6 +163,14 @@ function min0(n) { } } +function minNull0(n) { + if (n !== null) { + return min0Aux(n); + } else { + return null; + } +} + function max0Aux(_n) { while(true) { var n = _n; @@ -177,7 +185,7 @@ function max0Aux(_n) { }; } -function max0(n) { +function maxOpt0(n) { if (n !== null) { return /* Some */[max0Aux(n)]; } else { @@ -185,6 +193,14 @@ function max0(n) { } } +function maxNull0(n) { + if (n !== null) { + return max0Aux(n); + } else { + return null; + } +} + function removeMinAux(n) { var rn = n.right; var ln = n.left; @@ -584,9 +600,11 @@ exports.add_min_element = add_min_element; exports.add_max_element = add_max_element; exports.join = join; exports.min0Aux = min0Aux; -exports.min0 = min0; +exports.minOpt0 = minOpt0; +exports.minNull0 = minNull0; exports.max0Aux = max0Aux; -exports.max0 = max0; +exports.maxOpt0 = maxOpt0; +exports.maxNull0 = maxNull0; exports.removeMinAux = removeMinAux; exports.merge = merge; exports.concat = concat; From 46beefe62a302f714dc65d99d11adfff9c8285f6 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Sat, 30 Dec 2017 16:28:33 +0800 Subject: [PATCH 3/3] update tests --- jscomp/test/.depend | 4 ++-- jscomp/test/bs_set_int_test.js | 34 +++++++++++++++++++--------------- jscomp/test/bs_set_int_test.ml | 8 +++++--- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/jscomp/test/.depend b/jscomp/test/.depend index 35ca83754b0..b0e13c7bf91 100644 --- a/jscomp/test/.depend +++ b/jscomp/test/.depend @@ -107,8 +107,8 @@ bs_queue_test.cmj : ../runtime/js.cmj ../others/bs.cmj bs_rbset_int_bench.cmj : rbset.cmj bs_rest_test.cmj : bs_set_bench.cmj : ../others/bs.cmj -bs_set_int_test.cmj : mt.cmj ../stdlib/list.cmj ../others/bs.cmj \ - ../stdlib/array.cmj +bs_set_int_test.cmj : mt.cmj ../stdlib/list.cmj ../runtime/js.cmj \ + ../others/bs.cmj ../stdlib/array.cmj bs_sort_test.cmj : mt.cmj ../others/bs_Range.cmj ../others/bs.cmj \ array_data_util.cmj bs_splice_partial.cmj : ../runtime/js.cmj diff --git a/jscomp/test/bs_set_int_test.js b/jscomp/test/bs_set_int_test.js index 8f7cb9aba54..44f3d12ff95 100644 --- a/jscomp/test/bs_set_int_test.js +++ b/jscomp/test/bs_set_int_test.js @@ -144,19 +144,23 @@ var v$1 = Bs_SetInt.ofArray(/* array */[ -1 ]); -var minv = Bs_SetInt.minOpt(v$1); +var minv = Bs_SetInt.minNull(v$1); -var maxv = Bs_SetInt.maxOpt(v$1); +var maxv = Bs_SetInt.maxNull(v$1); -eq("File \"bs_set_int_test.ml\", line 72, characters 5-12", Bs_SetInt.fold(v$1, 0, (function (x, y) { +function approx(loc, x, y) { + return b(loc, +(x === y)); +} + +eq("File \"bs_set_int_test.ml\", line 74, characters 5-12", Bs_SetInt.fold(v$1, 0, (function (x, y) { return x + y | 0; })), $$Array.fold_left((function (prim, prim$1) { return prim + prim$1 | 0; }), 0, ss)); -eq("File \"bs_set_int_test.ml\", line 73, characters 5-12", minv, /* Some */[-1]); +approx("File \"bs_set_int_test.ml\", line 75, characters 9-16", -1, minv); -eq("File \"bs_set_int_test.ml\", line 74, characters 5-12", maxv, /* Some */[222]); +approx("File \"bs_set_int_test.ml\", line 76, characters 9-16", 222, maxv); var v$2 = Bs_SetInt.remove(v$1, 3); @@ -164,9 +168,9 @@ var minv$1 = Bs_SetInt.minOpt(v$2); var maxv$1 = Bs_SetInt.maxOpt(v$2); -eq("File \"bs_set_int_test.ml\", line 77, characters 5-12", minv$1, /* Some */[-1]); +eq("File \"bs_set_int_test.ml\", line 79, characters 5-12", minv$1, /* Some */[-1]); -eq("File \"bs_set_int_test.ml\", line 78, characters 5-12", maxv$1, /* Some */[222]); +eq("File \"bs_set_int_test.ml\", line 80, characters 5-12", maxv$1, /* Some */[222]); var v$3 = Bs_SetInt.remove(v$2, 222); @@ -174,9 +178,9 @@ var minv$2 = Bs_SetInt.minOpt(v$3); var maxv$2 = Bs_SetInt.maxOpt(v$3); -eq("File \"bs_set_int_test.ml\", line 81, characters 5-12", minv$2, /* Some */[-1]); +eq("File \"bs_set_int_test.ml\", line 83, characters 5-12", minv$2, /* Some */[-1]); -eq("File \"bs_set_int_test.ml\", line 82, characters 5-12", maxv$2, /* Some */[33]); +eq("File \"bs_set_int_test.ml\", line 84, characters 5-12", maxv$2, /* Some */[33]); var v$4 = Bs_SetInt.remove(v$3, -1); @@ -184,9 +188,9 @@ var minv$3 = Bs_SetInt.minOpt(v$4); var maxv$3 = Bs_SetInt.maxOpt(v$4); -eq("File \"bs_set_int_test.ml\", line 85, characters 5-12", minv$3, /* Some */[0]); +eq("File \"bs_set_int_test.ml\", line 87, characters 5-12", minv$3, /* Some */[0]); -eq("File \"bs_set_int_test.ml\", line 86, characters 5-12", maxv$3, /* Some */[33]); +eq("File \"bs_set_int_test.ml\", line 88, characters 5-12", maxv$3, /* Some */[33]); var v$5 = Bs_SetInt.remove(v$4, 0); @@ -200,7 +204,7 @@ var v$9 = Bs_SetInt.remove(v$8, 4); var v$10 = Bs_SetInt.remove(v$9, 1); -b("File \"bs_set_int_test.ml\", line 93, characters 4-11", Bs_SetInt.isEmpty(v$10)); +b("File \"bs_set_int_test.ml\", line 95, characters 4-11", Bs_SetInt.isEmpty(v$10)); var v$11 = Bs_Array.init(1000000, (function (i) { return i; @@ -210,15 +214,15 @@ Bs_Array.shuffleInPlace(v$11); var u$1 = Bs_SetInt.ofArray(v$11); -b("File \"bs_set_int_test.ml\", line 101, characters 4-11", Bs_SetInt.checkInvariant(u$1)); +b("File \"bs_set_int_test.ml\", line 103, characters 4-11", Bs_SetInt.checkInvariant(u$1)); var firstHalf = Bs_Array.sub(v$11, 0, 2000); var xx = Bs_Array.foldLeft(firstHalf, u$1, Bs_SetInt.remove); -b("File \"bs_set_int_test.ml\", line 105, characters 4-11", Bs_SetInt.checkInvariant(u$1)); +b("File \"bs_set_int_test.ml\", line 107, characters 4-11", Bs_SetInt.checkInvariant(u$1)); -b("File \"bs_set_int_test.ml\", line 106, characters 4-11", Bs_SetInt.eq(Bs_SetInt.union(Bs_SetInt.ofArray(firstHalf), xx), u$1)); +b("File \"bs_set_int_test.ml\", line 108, characters 4-11", Bs_SetInt.eq(Bs_SetInt.union(Bs_SetInt.ofArray(firstHalf), xx), u$1)); Mt.from_pair_suites("bs_set_int_test.ml", suites[0]); diff --git a/jscomp/test/bs_set_int_test.ml b/jscomp/test/bs_set_int_test.ml index 64f122c3667..5c3766e3782 100644 --- a/jscomp/test/bs_set_int_test.ml +++ b/jscomp/test/bs_set_int_test.ml @@ -68,10 +68,12 @@ let () = let () = let ss = [|1;222;3;4;2;0;33;-1|] in let v = ofA [|1;222;3;4;2;0;33;-1|] in - let minv, maxv = N.minOpt v, N.maxOpt v in + let minv, maxv = N.minNull v, N.maxNull v in + let approx loc (x : int) y = + b loc (Js.eqNull x y) in eq __LOC__ (N.fold v 0 (fun [@bs] x y -> x + y) ) (Array.fold_left (+) 0 ss) ; - eq __LOC__ minv (Some (-1)); - eq __LOC__ maxv (Some 222); + approx __LOC__ (-1) minv ; + approx __LOC__ 222 maxv; let v = N.remove v 3 in let minv, maxv = N.minOpt v, N.maxOpt v in eq __LOC__ minv (Some (-1));