From 72cb078fa37c88e92e1aaeb33d12ba6541350a7e Mon Sep 17 00:00:00 2001 From: Jacques-Pascal Deplaix Date: Mon, 6 Nov 2017 19:44:19 +0100 Subject: [PATCH] Avoid unsafe polymorphic functions and operators --- _oasis | 11 ++++++---- src/core/CCBool.ml | 2 +- src/core/CCChar.ml | 16 ++++++-------- src/core/CCEqual.ml | 10 ++++----- src/core/CCFloat.ml | 20 ++++++++--------- src/core/CCInt.ml | 2 +- src/core/CCInt64.ml | 2 +- src/core/CCList.ml | 44 +++++++++++++++++++------------------- src/core/CCList.mli | 42 ++++++++++++++++++------------------ src/core/CCListLabels.ml | 44 +++++++++++++++++++------------------- src/core/CCListLabels.mli | 42 ++++++++++++++++++------------------ src/core/CCParse.ml | 10 ++++----- src/core/CCRandom.ml | 6 +++--- src/core/CCRandom.mli | 2 +- src/core/CCString.cppo.ml | 22 ++++++++----------- src/core/CCVector.ml | 2 +- src/core/CCVector.mli | 2 +- src/data/CCCache.ml | 6 +++++- src/data/CCDeque.ml | 7 +++--- src/data/CCDeque.mli | 4 ++-- src/data/CCFQueue.ml | 6 +++++- src/data/CCHashTrie.ml | 6 ++++-- src/data/CCIntMap.ml | 8 ++++--- src/data/CCMixmap.ml | 2 +- src/data/CCMixtbl.ml | 2 +- src/data/CCRAL.ml | 4 ++-- src/data/CCRAL.mli | 4 ++-- src/data/CCSimple_queue.ml | 6 +++--- src/data/CCTrie.ml | 6 +++++- src/data/CCTrie.mli | 1 + src/iter/CCKTree.ml | 4 ++-- src/sexp/CCSexp.ml | 2 +- src/sexp/CCSexp_lex.mll | 3 +-- src/threads/CCPool.ml | 6 +++++- src/threads/CCTimer.ml | 2 ++ 35 files changed, 188 insertions(+), 170 deletions(-) diff --git a/_oasis b/_oasis index 99dfa6f1..aabfca6a 100644 --- a/_oasis +++ b/_oasis @@ -50,14 +50,14 @@ Library "containers" Library "containers_unix" Path: src/unix Modules: CCUnix - BuildDepends: bytes, result, unix + BuildDepends: containers, unix FindlibParent: containers FindlibName: unix Library "containers_sexp" Path: src/sexp Modules: CCSexp, CCSexp_lex - BuildDepends: bytes, result + BuildDepends: containers FindlibParent: containers FindlibName: sexp @@ -66,10 +66,12 @@ Library "containers_data" Modules: CCMultiMap, CCMultiSet, CCTrie, CCFlatHashtbl, CCCache, CCPersistentHashtbl, CCDeque, CCFQueue, CCBV, CCMixtbl, CCMixmap, CCRingBuffer, CCIntMap, CCPersistentArray, - CCMixset, CCGraph, CCHashSet, CCBitField, + CCMixset, + #CCGraph, + CCHashSet, CCBitField, CCHashTrie, CCWBTree, CCRAL, CCSimple_queue, CCImmutArray, CCHet, CCZipper - BuildDepends: bytes + BuildDepends: containers # BuildDepends: bytes, bisect_ppx FindlibParent: containers FindlibName: data @@ -77,6 +79,7 @@ Library "containers_data" Library "containers_iter" Path: src/iter Modules: CCKTree, CCKList, CCLazy_list + BuildDepends: containers FindlibParent: containers FindlibName: iter diff --git a/src/core/CCBool.ml b/src/core/CCBool.ml index 087d0101..89a6a9e5 100644 --- a/src/core/CCBool.ml +++ b/src/core/CCBool.ml @@ -3,7 +3,7 @@ type t = bool -let equal (a:bool) b = a=b +let equal (a:bool) b = Pervasives.(=) a b let compare (a:bool) b = Pervasives.compare a b diff --git a/src/core/CCChar.ml b/src/core/CCChar.ml index 55900bd6..1825afbc 100644 --- a/src/core/CCChar.ml +++ b/src/core/CCChar.ml @@ -6,7 +6,7 @@ include Char -let equal (a:char) b = a=b +let equal (a:char) b = Pervasives.(=) a b let pp = Buffer.add_char let print = Format.pp_print_char @@ -15,12 +15,10 @@ let of_int_exn = Char.chr let of_int c = try Some (of_int_exn c) with _ -> None let to_int = Char.code -let lowercase_ascii c = - if c >= 'A' && c <= 'Z' - then Char.unsafe_chr (Char. code c + 32) - else c +let lowercase_ascii = function + | 'A'..'Z' as c -> Char.unsafe_chr (Char. code c + 32) + | c -> c -let uppercase_ascii c = - if c >= 'a' && c <= 'z' - then Char.unsafe_chr (Char.code c - 32) - else c +let uppercase_ascii = function + | 'a'..'z' as c -> Char.unsafe_chr (Char.code c - 32) + | c -> c diff --git a/src/core/CCEqual.ml b/src/core/CCEqual.ml index f879bd05..769dbbb7 100644 --- a/src/core/CCEqual.ml +++ b/src/core/CCEqual.ml @@ -5,12 +5,12 @@ type 'a t = 'a -> 'a -> bool -let poly = (=) +let poly = Pervasives.(=) -let int : int t = (=) -let string : string t = (=) -let bool : bool t = (=) -let float : float t = (=) +let int : int t = CCInt.equal +let string : string t = CCString.equal +let bool : bool t = CCBool.equal +let float : float t = CCFloat.equal let unit () () = true let rec list f l1 l2 = match l1, l2 with diff --git a/src/core/CCFloat.ml b/src/core/CCFloat.ml index 2e7fcc41..cb4062b9 100644 --- a/src/core/CCFloat.ml +++ b/src/core/CCFloat.ml @@ -9,6 +9,16 @@ type fpclass = Pervasives.fpclass = | FP_infinite | FP_nan +module Infix = struct + let (=) = Pervasives.(=) + let (<>) = Pervasives.(<>) + let (<) = Pervasives.(<) + let (>) = Pervasives.(>) + let (<=) = Pervasives.(<=) + let (>=) = Pervasives.(>=) +end +include Infix + let nan = Pervasives.nan let infinity = Pervasives.infinity @@ -84,13 +94,3 @@ let random_range i j st = i +. random (j-.i) st let equal_precision ~epsilon a b = abs_float (a-.b) < epsilon let classify = Pervasives.classify_float - -module Infix = struct - let (=) = Pervasives.(=) - let (<>) = Pervasives.(<>) - let (<) = Pervasives.(<) - let (>) = Pervasives.(>) - let (<=) = Pervasives.(<=) - let (>=) = Pervasives.(>=) -end -include Infix diff --git a/src/core/CCInt.ml b/src/core/CCInt.ml index eb37fccc..f4b8adb0 100644 --- a/src/core/CCInt.ml +++ b/src/core/CCInt.ml @@ -77,7 +77,7 @@ let floor_div a n = let rem a n = let y = a mod n in - if (y < 0) <> (n < 0) && y <> 0 then + if not (CCBool.equal (y < 0) (n < 0)) && y <> 0 then y + n else y diff --git a/src/core/CCInt64.ml b/src/core/CCInt64.ml index f9ab3841..9ac33506 100644 --- a/src/core/CCInt64.ml +++ b/src/core/CCInt64.ml @@ -28,7 +28,7 @@ let (lsr) = shift_right_logical let (asr) = shift_right -let equal (x:t) y = x=y +let equal (x:t) y = Pervasives.(=) x y let hash x = Pervasives.abs (to_int x) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index f7552587..98c8ecb1 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -537,7 +537,7 @@ let map_product_l f l = cmp_lii_unord (cartesian_product l) (map_product_l CCFun.id l)) *) -let sorted_merge ?(cmp=Pervasives.compare) l1 l2 = +let sorted_merge ~cmp l1 l2 = let rec recurse cmp acc l1 l2 = match l1,l2 with | [], _ -> List.rev_append acc l2 | _, [] -> List.rev_append acc l1 @@ -560,7 +560,7 @@ let sorted_merge ?(cmp=Pervasives.compare) l1 l2 = List.length (sorted_merge l1 l2) = List.length l1 + List.length l2) *) -let sort_uniq (type elt) ?(cmp=Pervasives.compare) l = +let sort_uniq (type elt) ~cmp l = let module S = Set.Make(struct type t = elt let compare = cmp @@ -574,7 +574,7 @@ let sort_uniq (type elt) ?(cmp=Pervasives.compare) l = sort_uniq [10;10;10;10;1;10] = [1;10] *) -let is_sorted ?(cmp=Pervasives.compare) l = +let is_sorted ~cmp l = let rec aux cmp = function | [] | [_] -> true | x :: ((y :: _) as tail) -> cmp x y <= 0 && aux cmp tail @@ -586,7 +586,7 @@ let is_sorted ?(cmp=Pervasives.compare) l = is_sorted (List.sort Pervasives.compare l)) *) -let sorted_insert ?(cmp=Pervasives.compare) ?(uniq=false) x l = +let sorted_insert ~cmp ?(uniq=false) x l = let rec aux cmp uniq x left l = match l with | [] -> List.rev_append left [x] | y :: tail -> @@ -615,7 +615,7 @@ let sorted_insert ?(cmp=Pervasives.compare) ?(uniq=false) x l = List.mem x (sorted_insert x l)) *) -let uniq_succ ?(eq=(=)) l = +let uniq_succ ~eq l = let rec f acc l = match l with | [] -> List.rev acc | [x] -> List.rev (x::acc) @@ -628,7 +628,7 @@ let uniq_succ ?(eq=(=)) l = uniq_succ [1;1;2;3;1;6;6;4;6;1] = [1;2;3;1;6;4;6;1] *) -let group_succ ?(eq=(=)) l = +let group_succ ~eq l = let rec f ~eq acc cur l = match cur, l with | [], [] -> List.rev acc | _::_, [] -> List.rev (List.rev cur :: acc) @@ -647,7 +647,7 @@ let group_succ ?(eq=(=)) l = = [[1, 1; 1, 2; 1, 3]; [2, 0]] *) -let sorted_merge_uniq ?(cmp=Pervasives.compare) l1 l2 = +let sorted_merge_uniq ~cmp l1 l2 = let push ~cmp acc x = match acc with | [] -> [x] | y :: _ when cmp x y > 0 -> x :: acc @@ -746,7 +746,7 @@ let sublists_of_len ?(last=fun _ -> None) ?offset n l = (* add sub-lists of [l] to [acc] *) let rec aux acc l = let group = take n l in - if group=[] then acc (* this was the last group, we are done *) + if is_empty group then acc (* this was the last group, we are done *) else if List.length group < n (* last group, with missing elements *) then match last group with | None -> acc @@ -880,7 +880,7 @@ let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l find_map (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None *) -let remove ?(eq=(=)) ~x l = +let remove ~eq ~x l = let rec remove' eq x acc l = match l with | [] -> List.rev acc | y :: tail when eq x y -> remove' eq x acc tail @@ -952,16 +952,16 @@ let all_ok l = (Error "e2") (all_ok [Ok 1; Error "e2"; Error "e3"; Ok 4]) *) -let mem ?(eq=(=)) x l = +let mem ~eq x l = let rec search eq x l = match l with | [] -> false | y::l' -> eq x y || search eq x l' in search eq x l -let add_nodup ?(eq=(=)) x l = +let add_nodup ~eq x l = if mem ~eq x l then l else x::l -let remove_one ?(eq=(=)) x l = +let remove_one ~eq x l = let rec remove_one ~eq x acc l = match l with | [] -> assert false | y :: tl when eq x y -> List.rev_append acc tl @@ -978,12 +978,12 @@ let remove_one ?(eq=(=)) x l = not (mem x l) || List.length (remove_one x l) = List.length l - 1) *) -let subset ?(eq=(=)) l1 l2 = +let subset ~eq l1 l2 = List.for_all (fun t -> mem ~eq t l2) l1 -let uniq ?(eq=(=)) l = +let uniq ~eq l = let rec uniq eq acc l = match l with | [] -> List.rev acc | x::xs when List.exists (eq x) xs -> uniq eq acc xs @@ -999,7 +999,7 @@ let uniq ?(eq=(=)) l = sort_uniq l = (uniq l |> sort Pervasives.compare)) *) -let union ?(eq=(=)) l1 l2 = +let union ~eq l1 l2 = let rec union eq acc l1 l2 = match l1 with | [] -> List.rev_append acc l2 | x::xs when mem ~eq x l2 -> union eq acc xs l2 @@ -1010,7 +1010,7 @@ let union ?(eq=(=)) l1 l2 = union [1;2;4] [2;3;4;5] = [1;2;3;4;5] *) -let inter ?(eq=(=)) l1 l2 = +let inter ~eq l1 l2 = let rec inter eq acc l1 l2 = match l1 with | [] -> List.rev acc | x::xs when mem ~eq x l2 -> inter eq (x::acc) xs l2 @@ -1201,9 +1201,9 @@ module Assoc = struct | (y,z)::l' -> if eq x y then z else search_exn eq l' x - let get_exn ?(eq=(=)) x l = search_exn eq l x + let get_exn ~eq x l = search_exn eq l x - let get ?(eq=(=)) x l = + let get ~eq x l = try Some (search_exn eq l x) with Not_found -> None @@ -1224,7 +1224,7 @@ module Assoc = struct then f x (Some y') (List.rev_append acc l') else search_set eq ((x',y')::acc) l' x ~f - let set ?(eq=(=)) x y l = + let set ~eq x y l = search_set eq [] l x ~f:(fun x _ l -> (x,y)::l) @@ -1235,7 +1235,7 @@ module Assoc = struct = [1, "1"; 2, "2"; 3, "3"] *) - let mem ?(eq=(=)) x l = + let mem ~eq x l = try ignore (search_exn eq l x); true with Not_found -> false @@ -1244,7 +1244,7 @@ module Assoc = struct not (Assoc.mem 4 [1,"1"; 2,"2"; 3, "3"]) *) - let update ?(eq=(=)) ~f x l = + let update ~eq ~f x l = search_set eq [] l x ~f:(fun x opt_y rest -> match f opt_y with @@ -1262,7 +1262,7 @@ module Assoc = struct ~f:(function None -> Some "3" | _ -> assert false) |> lsort) *) - let remove ?(eq=(=)) x l = + let remove ~eq x l = search_set eq [] l x ~f:(fun _ opt_y rest -> match opt_y with | None -> l (* keep as is *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index f83407bf..9e069337 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -256,7 +256,7 @@ val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option (** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l], and [p x] holds. Otherwise returns [None] *) -val remove : ?eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t +val remove : eq:('a -> 'a -> bool) -> x:'a -> 'a t -> 'a t (** [remove ~x l] removes every instance of [x] from [l]. Tailrec. @param eq equality function @since 0.11 *) @@ -284,23 +284,23 @@ val all_ok : ('a, 'err) Result.result t -> ('a t, 'err) Result.result or [Error e] otherwise (with the first error met). @since 1.3 *) -val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list +val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** Merges elements from both sorted list *) -val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list +val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list (** Sort the list and remove duplicate elements *) -val sorted_merge_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list +val sorted_merge_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** [sorted_merge_uniq l1 l2] merges the sorted lists [l1] and [l2] and removes duplicates @since 0.10 *) -val is_sorted : ?cmp:('a -> 'a -> int) -> 'a list -> bool +val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool (** [is_sorted l] returns [true] iff [l] is sorted (according to given order) @param cmp the comparison function (default [Pervasives.compare]) @since 0.17 *) -val sorted_insert : ?cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list +val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list (** [sorted_insert x l] inserts [x] into [l] such that, if [l] was sorted, then [sorted_insert x l] is sorted too. @param uniq if true and [x] is already in sorted position in [l], then @@ -313,14 +313,14 @@ val sorted_insert : ?cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a is_sorted (sorted_insert x l)) *) -val uniq_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list +val uniq_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list (** [uniq_succ l] removes duplicate elements that occur one next to the other. Examples: [uniq_succ [1;2;1] = [1;2;1]] [uniq_succ [1;1;2] = [1;2]] @since 0.10 *) -val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list +val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list (** [group_succ ~eq l] groups together consecutive elements that are equal according to [eq] @since 0.11 *) @@ -362,30 +362,30 @@ val remove_at_idx : int -> 'a t -> 'a t Those operations maintain the invariant that the list does not contain duplicates (if it already satisfies it) *) -val add_nodup : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t +val add_nodup : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [add_nodup x set] adds [x] to [set] if it was not already present. Linear time. @since 0.11 *) -val remove_one : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t +val remove_one : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +val mem : eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool (** Membership to the list. Linear time *) -val subset : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool +val subset : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool (** Test for inclusion *) -val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t +val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t (** Remove duplicates w.r.t the equality predicate. Complexity is quadratic in the length of the list, but the order of elements is preserved. If you wish for a faster de-duplication but do not care about the order, use {!sort_uniq}*) -val union : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t +val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t (** List union. Complexity is product of length of inputs. *) -val inter : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t +val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t (** List intersection. Complexity is product of length of inputs. *) (** {2 Other Constructors} *) @@ -423,28 +423,28 @@ val repeat : int -> 'a t -> 'a t module Assoc : sig type ('a, 'b) t = ('a*'b) list - val get : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b option + val get : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b option (** Find the element *) - val get_exn : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b + val get_exn : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b (** Same as [get], but unsafe @raise Not_found if the element is not present *) - val set : ?eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t + val set : eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present) *) - val mem : ?eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool + val mem : eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l] @since 0.16 *) val update : - ?eq:('a->'a->bool) -> f:('b option -> 'b option) -> 'a -> ('a,'b) t -> ('a,'b) t + eq:('a->'a->bool) -> f:('b option -> 'b option) -> 'a -> ('a,'b) t -> ('a,'b) t (** [update k ~f l] updates [l] on the key [k], by calling [f (get l k)] and removing [k] if it returns [None], mapping [k] to [v'] if it returns [Some v'] @since 0.16 *) - val remove : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t + val remove : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t (** [remove x l] removes the first occurrence of [k] from [l]. @since 0.17 *) end diff --git a/src/core/CCListLabels.ml b/src/core/CCListLabels.ml index 61df2913..d90aa088 100644 --- a/src/core/CCListLabels.ml +++ b/src/core/CCListLabels.ml @@ -331,7 +331,7 @@ let pure = return let (<*>) funs l = product (fun f x -> f x) funs l -let sorted_merge ?(cmp=Pervasives.compare) l1 l2 = +let sorted_merge ~cmp l1 l2 = let rec recurse cmp acc l1 l2 = match l1,l2 with | [], _ -> List.rev_append acc l2 | _, [] -> List.rev_append acc l1 @@ -354,7 +354,7 @@ let sorted_merge ?(cmp=Pervasives.compare) l1 l2 = List.length (sorted_merge l1 l2) = List.length l1 + List.length l2) *) -let sort_uniq (type elt) ?(cmp=Pervasives.compare) l = +let sort_uniq (type elt) ~cmp l = let module S = Set.Make(struct type t = elt let compare = cmp @@ -368,7 +368,7 @@ let sort_uniq (type elt) ?(cmp=Pervasives.compare) l = sort_uniq [10;10;10;10;1;10] = [1;10] *) -let is_sorted ?(cmp=Pervasives.compare) l = +let is_sorted ~cmp l = let rec aux cmp = function | [] | [_] -> true | x :: ((y :: _) as tail) -> cmp x y <= 0 && aux cmp tail @@ -380,7 +380,7 @@ let is_sorted ?(cmp=Pervasives.compare) l = is_sorted (List.sort Pervasives.compare l)) *) -let sorted_insert ?(cmp=Pervasives.compare) ?(uniq=false) x l = +let sorted_insert ~cmp ?(uniq=false) x l = let rec aux cmp uniq x left l = match l with | [] -> List.rev_append left [x] | y :: tail -> @@ -409,7 +409,7 @@ let sorted_insert ?(cmp=Pervasives.compare) ?(uniq=false) x l = List.mem x (sorted_insert x l)) *) -let uniq_succ ?(eq=(=)) l = +let uniq_succ ~eq l = let rec f acc l = match l with | [] -> List.rev acc | [x] -> List.rev (x::acc) @@ -422,7 +422,7 @@ let uniq_succ ?(eq=(=)) l = uniq_succ [1;1;2;3;1;6;6;4;6;1] = [1;2;3;1;6;4;6;1] *) -let group_succ ?(eq=(=)) l = +let group_succ ~eq l = let rec f ~eq acc cur l = match cur, l with | [], [] -> List.rev acc | _::_, [] -> List.rev (List.rev cur :: acc) @@ -441,7 +441,7 @@ let group_succ ?(eq=(=)) l = = [[1, 1; 1, 2; 1, 3]; [2, 0]] *) -let sorted_merge_uniq ?(cmp=Pervasives.compare) l1 l2 = +let sorted_merge_uniq ~cmp l1 l2 = let push ~cmp acc x = match acc with | [] -> [x] | y :: _ when cmp x y > 0 -> x :: acc @@ -540,7 +540,7 @@ let sublists_of_len ?(last=fun _ -> None) ?offset n l = (* add sub-lists of [l] to [acc] *) let rec aux acc l = let group = take n l in - if group=[] then acc (* this was the last group, we are done *) + if is_empty group then acc (* this was the last group, we are done *) else if List.length group < n (* last group, with missing elements *) then match last group with | None -> acc @@ -654,7 +654,7 @@ let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l find_map (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None *) -let remove ?(eq=(=)) ~x l = +let remove ~eq ~x l = let rec remove' eq x acc l = match l with | [] -> List.rev acc | y :: tail when eq x y -> remove' eq x acc tail @@ -684,16 +684,16 @@ let filter_map f l = [ 1; 2; 3; 4; 5; 6 ]) *) -let mem ?(eq=(=)) x l = +let mem ~eq x l = let rec search eq x l = match l with | [] -> false | y::l' -> eq x y || search eq x l' in search eq x l -let add_nodup ?(eq=(=)) x l = +let add_nodup ~eq x l = if mem ~eq x l then l else x::l -let remove_one ?(eq=(=)) x l = +let remove_one ~eq x l = let rec remove_one ~eq x acc l = match l with | [] -> assert false | y :: tl when eq x y -> List.rev_append acc tl @@ -710,12 +710,12 @@ let remove_one ?(eq=(=)) x l = not (mem x l) || List.length (remove_one x l) = List.length l - 1) *) -let subset ?(eq=(=)) l1 l2 = +let subset ~eq l1 l2 = List.for_all (fun t -> mem ~eq t l2) l1 -let uniq ?(eq=(=)) l = +let uniq ~eq l = let rec uniq eq acc l = match l with | [] -> List.rev acc | x::xs when List.exists (eq x) xs -> uniq eq acc xs @@ -726,7 +726,7 @@ let uniq ?(eq=(=)) l = uniq [1;1;2;2;3;4;4;2;4;1;5] |> List.sort Pervasives.compare = [1;2;3;4;5] *) -let union ?(eq=(=)) l1 l2 = +let union ~eq l1 l2 = let rec union eq acc l1 l2 = match l1 with | [] -> List.rev_append acc l2 | x::xs when mem ~eq x l2 -> union eq acc xs l2 @@ -737,7 +737,7 @@ let union ?(eq=(=)) l1 l2 = union [1;2;4] [2;3;4;5] = [1;2;3;4;5] *) -let inter ?(eq=(=)) l1 l2 = +let inter ~eq l1 l2 = let rec inter eq acc l1 l2 = match l1 with | [] -> List.rev acc | x::xs when mem ~eq x l2 -> inter eq (x::acc) xs l2 @@ -928,9 +928,9 @@ module Assoc = struct | (y,z)::l' -> if eq x y then z else search_exn eq l' x - let get_exn ?(eq=(=)) x l = search_exn eq l x + let get_exn ~eq x l = search_exn eq l x - let get ?(eq=(=)) x l = + let get ~eq x l = try Some (search_exn eq l x) with Not_found -> None @@ -951,7 +951,7 @@ module Assoc = struct then f x (Some y') (List.rev_append acc l') else search_set eq ((x',y')::acc) l' x ~f - let set ?(eq=(=)) x y l = + let set ~eq x y l = search_set eq [] l x ~f:(fun x _ l -> (x,y)::l) @@ -962,7 +962,7 @@ module Assoc = struct = [1, "1"; 2, "2"; 3, "3"] *) - let mem ?(eq=(=)) x l = + let mem ~eq x l = try ignore (search_exn eq l x); true with Not_found -> false @@ -971,7 +971,7 @@ module Assoc = struct not (Assoc.mem 4 [1,"1"; 2,"2"; 3, "3"]) *) - let update ?(eq=(=)) ~f x l = + let update ~eq ~f x l = search_set eq [] l x ~f:(fun x opt_y rest -> match f opt_y with @@ -989,7 +989,7 @@ module Assoc = struct ~f:(function None -> Some "3" | _ -> assert false) |> lsort) *) - let remove ?(eq=(=)) x l = + let remove ~eq x l = search_set eq [] l x ~f:(fun _ opt_y rest -> match opt_y with | None -> l (* keep as is *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 535eab06..45c86e22 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -178,7 +178,7 @@ val find_idx : f:('a -> bool) -> 'a t -> (int * 'a) option (** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l], and [p x] holds. Otherwise returns [None] *) -val remove : ?eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t +val remove : eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t (** [remove ~key l] removes every instance of [key] from [l]. Tailrec. @param eq equality function @since 0.11 *) @@ -186,23 +186,23 @@ val remove : ?eq:('a -> 'a -> bool) -> key:'a -> 'a t -> 'a t val filter_map : f:('a -> 'b option) -> 'a t -> 'b t (** Map and remove elements at the same time *) -val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list +val sorted_merge : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** Merges elements from both sorted list *) -val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list +val sort_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list (** Sort the list and remove duplicate elements *) -val sorted_merge_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list +val sorted_merge_uniq : cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list (** [sorted_merge_uniq l1 l2] merges the sorted lists [l1] and [l2] and removes duplicates @since 0.10 *) -val is_sorted : ?cmp:('a -> 'a -> int) -> 'a list -> bool +val is_sorted : cmp:('a -> 'a -> int) -> 'a list -> bool (** [is_sorted l] returns [true] iff [l] is sorted (according to given order) @param cmp the comparison function (default [Pervasives.compare]) @since 0.17 *) -val sorted_insert : ?cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list +val sorted_insert : cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a list (** [sorted_insert x l] inserts [x] into [l] such that, if [l] was sorted, then [sorted_insert x l] is sorted too. @param uniq if true and [x] is already in sorted position in [l], then @@ -215,14 +215,14 @@ val sorted_insert : ?cmp:('a -> 'a -> int) -> ?uniq:bool -> 'a -> 'a list -> 'a is_sorted (sorted_insert x l)) *) -val uniq_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list +val uniq_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list (** [uniq_succ l] removes duplicate elements that occur one next to the other. Examples: [uniq_succ [1;2;1] = [1;2;1]] [uniq_succ [1;1;2] = [1;2]] @since 0.10 *) -val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list +val group_succ : eq:('a -> 'a -> bool) -> 'a list -> 'a list list (** [group_succ ~eq l] groups together consecutive elements that are equal according to [eq] @since 0.11 *) @@ -259,30 +259,30 @@ val remove_at_idx : int -> 'a t -> 'a t Those operations maintain the invariant that the list does not contain duplicates (if it already satisfies it) *) -val add_nodup : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t +val add_nodup : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [add_nodup x set] adds [x] to [set] if it was not already present. Linear time. @since 0.11 *) -val remove_one : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t +val remove_one : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +val mem : eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool (** Membership to the list. Linear time *) -val subset : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool +val subset : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool (** Test for inclusion *) -val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t +val uniq : eq:('a -> 'a -> bool) -> 'a t -> 'a t (** Remove duplicates w.r.t the equality predicate. Complexity is quadratic in the length of the list, but the order of elements is preserved. If you wish for a faster de-duplication but do not care about the order, use {!sort_uniq}*) -val union : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t +val union : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t (** List union. Complexity is product of length of inputs. *) -val inter : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t +val inter : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t (** List intersection. Complexity is product of length of inputs. *) (** {2 Other Constructors} *) @@ -320,28 +320,28 @@ val repeat : int -> 'a t -> 'a t module Assoc : sig type ('a, 'b) t = ('a*'b) list - val get : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b option + val get : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b option (** Find the element *) - val get_exn : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b + val get_exn : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> 'b (** Same as [get], but unsafe @raise Not_found if the element is not present *) - val set : ?eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t + val set : eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present) *) - val mem : ?eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool + val mem : eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l] @since 0.16 *) val update : - ?eq:('a->'a->bool) -> f:('b option -> 'b option) -> 'a -> ('a,'b) t -> ('a,'b) t + eq:('a->'a->bool) -> f:('b option -> 'b option) -> 'a -> ('a,'b) t -> ('a,'b) t (** [update k ~f l] updates [l] on the key [k], by calling [f (get l k)] and removing [k] if it returns [None], mapping [k] to [v'] if it returns [Some v'] @since 0.16 *) - val remove : ?eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t + val remove : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t (** [remove x l] removes the first occurrence of [k] from [l]. @since 0.17 *) end diff --git a/src/core/CCParse.ml b/src/core/CCParse.ml index f581784a..01511e0a 100644 --- a/src/core/CCParse.ml +++ b/src/core/CCParse.ml @@ -87,7 +87,7 @@ let next st ~ok ~err = else ( let c = st.str.[st.i] in st.i <- st.i + 1; - if c='\n' + if CCChar.equal c '\n' then (st.lnum <- st.lnum + 1; st.cnum <- 1) else st.cnum <- st.cnum + 1; ok c @@ -146,7 +146,7 @@ let char c = let msg = Printf.sprintf "expected '%c'" c in fun st ~ok ~err -> next st ~err - ~ok:(fun c' -> if c=c' then ok c else fail_ ~err st (const_ msg)) + ~ok:(fun c' -> if CCChar.equal c c' then ok c else fail_ ~err st (const_ msg)) let char_if p st ~ok ~err = next st ~err @@ -164,7 +164,7 @@ let chars_if p st ~ok ~err:_ = let chars1_if p st ~ok ~err = chars_if p st ~err ~ok:(fun s -> - if s = "" + if CCString.is_empty s then fail_ ~err st (const_ "unexpected sequence of chars") else ok s) @@ -231,7 +231,7 @@ let string s st ~ok ~err = else next st ~err ~ok:(fun c -> - if c = s.[i] + if CCChar.equal c s.[i] then check (i+1) else fail_ ~err st (fun () -> Printf.sprintf "expected \"%s\"" s)) in @@ -386,7 +386,7 @@ module U = struct skip_white <* string stop let int = - chars1_if (fun c -> is_num c || c='-') + chars1_if (fun c -> is_num c || CCChar.equal c '-') >>= fun s -> try return (int_of_string s) with Failure _ -> fail "expected an int" diff --git a/src/core/CCRandom.ml b/src/core/CCRandom.ml index bcc83111..0bf23101 100644 --- a/src/core/CCRandom.ml +++ b/src/core/CCRandom.ml @@ -77,7 +77,7 @@ let replicate n g st = in aux [] n (* Sample without replacement using rejection sampling. *) -let sample_without_replacement (type elt) ?(compare=compare) k (rng:elt t) st= +let sample_without_replacement (type elt) ~compare k (rng:elt t) st= let module S = Set.Make(struct type t=elt let compare = compare end) in let rec aux s k = if k <= 0 then @@ -118,7 +118,7 @@ let _diff_list ~last l = let split_list i ~len st = if len <= 1 then invalid_arg "Random.split_list"; if i >= len then - let xs = sample_without_replacement (len-1) (int_range 1 (i-1)) st in + let xs = sample_without_replacement ~compare:CCInt.compare (len-1) (int_range 1 (i-1)) st in _diff_list ( 0::xs ) ~last:i else None @@ -221,7 +221,7 @@ let uniformity_test ?(size_hint=10) k rng st = let confidence = 4. in let std = confidence *. (sqrt (kf *. variance)) in let predicate _key n acc = - acc && abs_float (average -. float_of_int n) < std in + CCFloat.Infix.(acc && abs_float (average -. float_of_int n) < std) in Hashtbl.fold predicate histogram true (*$T split_list diff --git a/src/core/CCRandom.mli b/src/core/CCRandom.mli index ffb32cfb..de76e90d 100644 --- a/src/core/CCRandom.mli +++ b/src/core/CCRandom.mli @@ -56,7 +56,7 @@ val replicate : int -> 'a t -> 'a list t randomly using [g] *) val sample_without_replacement: - ?compare:('a -> 'a -> int) -> int -> 'a t -> 'a list t + compare:('a -> 'a -> int) -> int -> 'a t -> 'a list t (** [sample_without_replacement n g] makes a list of [n] elements which are all generated randomly using [g] with the added constraint that none of the generated random values are equal diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index 9eb3c329..5e8b94b1 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -32,7 +32,7 @@ module type S = sig val print : Format.formatter -> t -> unit end -let equal (a:string) b = a=b +let equal (a:string) b = Pervasives.(=) a b let compare = String.compare @@ -66,7 +66,7 @@ let _is_sub ~sub i s j ~len = let rec check k = if k = len then true - else sub.[i+k] = s.[j+k] && check (k+1) + else Char.equal sub.[i+k] s.[j+k] && check (k+1) in j+len <= String.length s && check 0 @@ -114,7 +114,7 @@ module Find = struct let j = ref 0 in while !i < len do match !j with - | _ when get str (!i-1) = get str !j -> + | _ when Char.equal (get str (!i-1)) (get str !j) -> (* substring starting at !j continues matching current char *) incr j; failure.(!i) <- !j; @@ -146,7 +146,7 @@ module Find = struct while !j < pat_len && !i + !j < len do let c = String.get s (!i + !j) in let expected = String.get pattern.str !j in - if c = expected + if Char.equal c expected then ( (* char matches *) incr j; @@ -181,7 +181,7 @@ module Find = struct while !j < pat_len && !i + !j < len do let c = String.get s (len - !i - !j - 1) in let expected = String.get pattern.str (String.length pattern.str - !j - 1) in - if c = expected + if Char.equal c expected then ( (* char matches *) incr j; @@ -280,7 +280,7 @@ let replace_at_ ~pos ~len ~by s = Buffer.contents b let replace ?(which=`All) ~sub ~by s = - if sub="" then invalid_arg "CCString.replace"; + if is_empty sub then invalid_arg "CCString.replace"; match which with | `Left -> let i = find ~sub s ~start:0 in @@ -459,7 +459,7 @@ let edit_distance s1 s2 = then length s2 else if length s2 = 0 then length s1 - else if s1 = s2 + else if equal s1 s2 then 0 else begin (* distance vectors (v0=previous, v1=current) *) @@ -778,14 +778,10 @@ let lowercase_ascii = map CCChar.lowercase_ascii #endif let equal_caseless s1 s2: bool = - let char_lower c = - if c >= 'A' && c <= 'Z' - then Char.unsafe_chr (Char. code c + 32) - else c - in String.length s1 = String.length s2 && for_all2 - (fun c1 c2 -> char_lower c1 = char_lower c2) + (fun c1 c2 -> + CCChar.equal (CCChar.lowercase_ascii c1) (CCChar.lowercase_ascii c2)) s1 s2 let pp buf s = diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 13586ae7..30cb602c 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -513,7 +513,7 @@ let for_all p v = else p v.vec.(i) && check (i+1) in check 0 -let member ?(eq=(=)) x v = +let member ~eq x v = exists (eq x) v let find_exn p v = diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index edb60897..b570905e 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -118,7 +118,7 @@ val shrink : ('a, rw) t -> int -> unit (** Shrink to the given size (remove elements above this size). Does nothing if the parameter is bigger than the current size. *) -val member : ?eq:('a -> 'a -> bool) -> 'a -> ('a, _) t -> bool +val member : eq:('a -> 'a -> bool) -> 'a -> ('a, _) t -> bool (** Is the element a member of the vector? *) val sort : ('a -> 'a -> int) -> ('a, _) t -> ('a, 'mut) t diff --git a/src/data/CCCache.ml b/src/data/CCCache.ml index ec956458..1ad77bdc 100644 --- a/src/data/CCCache.ml +++ b/src/data/CCCache.ml @@ -161,9 +161,13 @@ module Replacing = struct | Pair _ | Empty -> raise Not_found + let is_empty = function + | Empty -> true + | Pair _ -> false + let set c x y = let i = c.hash x mod Array.length c.arr in - if c.arr.(i) = Empty then c.c_size <- c.c_size + 1; + if is_empty c.arr.(i) then c.c_size <- c.c_size + 1; c.arr.(i) <- Pair (x,y) let iter c f = diff --git a/src/data/CCDeque.ml b/src/data/CCDeque.ml index 9453e464..00fe6a01 100644 --- a/src/data/CCDeque.ml +++ b/src/data/CCDeque.ml @@ -76,7 +76,7 @@ let is_zero_ n = match n.cell with let is_empty d = let res = d.size = 0 in - assert (res = is_zero_ d.cur); + assert (CCBool.equal res (is_zero_ d.cur)); res let push_front d x = @@ -377,7 +377,7 @@ let copy d = assert_equal ~cmp q q' *) -let equal ?(eq=(=)) a b = +let equal ~eq a b = let rec aux eq a b = match a() , b() with | None, None -> true | None, Some _ @@ -385,7 +385,7 @@ let equal ?(eq=(=)) a b = | Some x, Some y -> eq x y && aux eq a b in aux eq (to_gen a) (to_gen b) -let compare ?(cmp=Pervasives.compare) a b = +let compare ~cmp a b = let rec aux cmp a b = match a() , b() with | None, None -> 0 | None, Some _ -> -1 @@ -412,4 +412,3 @@ let print pp_x out d = pp_x out x ) d; Format.fprintf out "}@]" - diff --git a/src/data/CCDeque.mli b/src/data/CCDeque.mli index c0bde886..4571f63a 100644 --- a/src/data/CCDeque.mli +++ b/src/data/CCDeque.mli @@ -21,13 +21,13 @@ val clear : _ t -> unit val is_empty : 'a t -> bool (** Is the deque empty? *) -val equal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool +val equal : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool (** [equal a b] checks whether [a] and [b] contain the same sequence of elements. @param eq comparison function for elements @since 0.13 *) -val compare : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int +val compare : cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int (** [compare a b] compares lexicographically [a] and [b] @param cmp comparison function for elements @since 0.13 *) diff --git a/src/data/CCFQueue.ml b/src/data/CCFQueue.ml index de6848f1..e2bb56f1 100644 --- a/src/data/CCFQueue.ml +++ b/src/data/CCFQueue.ml @@ -34,10 +34,14 @@ let empty = Shallow Zero exception Empty +let not_zero = function + | Zero -> false + | _ -> true + let _single x = Shallow (One x) let _double x y = Shallow (Two (x,y)) let _deep n hd middle tl = - assert (hd<>Zero && tl<>Zero); + assert (not_zero hd && not_zero tl); Deep (n, hd, middle, tl) let is_empty = function diff --git a/src/data/CCHashTrie.ml b/src/data/CCHashTrie.ml index 926d13c8..e21d08e1 100644 --- a/src/data/CCHashTrie.ml +++ b/src/data/CCHashTrie.ml @@ -291,6 +291,7 @@ module Make(Key : KEY) type t = private int val make : Key.t -> t val zero : t (* special "hash" *) + val equal : t -> t -> bool val is_0 : t -> bool val rem : t -> int (* [A.length_log] last bits *) val quotient : t -> t (* remove [A.length_log] last bits *) @@ -298,6 +299,7 @@ module Make(Key : KEY) type t = int let make = Key.hash let zero = 0 + let equal = (=) let is_0 h = h==0 let rem h = h land (A.length - 1) let quotient h = h lsr A.length_log @@ -407,14 +409,14 @@ module Make(Key : KEY) let rec add_ ~id k v ~h m = match m with | E -> S (h, k, v) | S (h', k', v') -> - if h=h' + if Hash.equal h h' then if Key.equal k k' then S (h, k, v) (* replace *) else L (h, Cons (k, v, Cons (k', v', Nil))) else make_array_ ~id ~leaf:(Cons (k', v', Nil)) ~h_leaf:h' k v ~h | L (h', l) -> - if h=h' + if Hash.equal h h' then L (h, add_list_ k v l) else (* split into N *) make_array_ ~id ~leaf:l ~h_leaf:h' k v ~h diff --git a/src/data/CCIntMap.ml b/src/data/CCIntMap.ml index cf2e6f82..fb17ec00 100644 --- a/src/data/CCIntMap.ml +++ b/src/data/CCIntMap.ml @@ -11,6 +11,7 @@ module Bit : sig type t = private int val highest : int -> t val min_int : t + val equal : t -> t -> bool val is_0 : bit:t -> int -> bool val is_1 : bit:t -> int -> bool val mask : mask:t -> int -> int (* zeroes the bit, puts all lower bits to 1 *) @@ -20,6 +21,7 @@ end = struct type t = int let min_int = min_int + let equal = (=) let rec highest_bit_naive x m = if x=m then m @@ -241,7 +243,7 @@ let rec equal ~eq a b = a==b || match a, b with | E, E -> true | L (ka, va), L (kb, vb) -> ka = kb && eq va vb | N (pa, sa, la, ra), N (pb, sb, lb, rb) -> - pa=pb && sa=sb && equal ~eq la lb && equal ~eq ra rb + pa=pb && Bit.equal sa sb && equal ~eq la lb && equal ~eq ra rb | E, _ | N _, _ | L _, _ -> false @@ -295,7 +297,7 @@ let rec union f t1 t2 = (* insert k, v into o *) insert_ (fun ~old v -> f k old v) k v o | N (p1, m1, l1, r1), N (p2, m2, l2, r2) -> - if p1 = p2 && m1 = m2 + if p1 = p2 && Bit.equal m1 m2 then mk_node_ p1 m1 (union f l1 l2) (union f r1 r2) else if Bit.gt m1 m2 && is_prefix_ ~prefix:p1 p2 ~bit:m1 then if Bit.is_0 p2 ~bit:m1 @@ -353,7 +355,7 @@ let rec inter f a b = with Not_found -> E end | N (p1, m1, l1, r1), N (p2, m2, l2, r2) -> - if p1 = p2 && m1 = m2 + if p1 = p2 && Bit.equal m1 m2 then mk_node_ p1 m1 (inter f l1 l2) (inter f r1 r2) else if Bit.gt m1 m2 && is_prefix_ ~prefix:p1 p2 ~bit:m1 then if Bit.is_0 p2 ~bit:m1 diff --git a/src/data/CCMixmap.ml b/src/data/CCMixmap.ml index 8c162a36..83dbdcbe 100644 --- a/src/data/CCMixmap.ml +++ b/src/data/CCMixmap.ml @@ -125,7 +125,7 @@ module Make(X : ORD) : S with type key = X.t = struct let mem ~inj x map = try - inj.get (M.find x map) <> None + CCOpt.is_some (inj.get (M.find x map)) with Not_found -> false let iter_keys ~f map = diff --git a/src/data/CCMixtbl.ml b/src/data/CCMixtbl.ml index 8d1d9f73..7b8e8f8b 100644 --- a/src/data/CCMixtbl.ml +++ b/src/data/CCMixtbl.ml @@ -86,7 +86,7 @@ let copy tbl = Hashtbl.copy tbl let mem ~inj tbl x = try - inj.get (Hashtbl.find tbl x) <> None + CCOpt.is_some (inj.get (Hashtbl.find tbl x)) with Not_found -> false (*$R diff --git a/src/data/CCRAL.ml b/src/data/CCRAL.ml index 5d9aa245..4783e3b4 100644 --- a/src/data/CCRAL.ml +++ b/src/data/CCRAL.ml @@ -371,7 +371,7 @@ let drop_while ~f l = let take_drop n l = take n l, drop n l -let equal ?(eq=(=)) l1 l2 = +let equal ~eq l1 l2 = let rec aux ~eq l1 l2 = match l1, l2 with | Nil, Nil -> true | Cons (size1, t1, l1'), Cons (size2, t2, l2') -> @@ -543,7 +543,7 @@ let rec of_list_map ~f l = match l with let y = f x in cons y (of_list_map ~f l') -let compare ?(cmp=Pervasives.compare) l1 l2 = +let compare ~cmp l1 l2 = let rec cmp_gen ~cmp g1 g2 = match g1(), g2() with | None, None -> 0 | Some _, None -> 1 diff --git a/src/data/CCRAL.mli b/src/data/CCRAL.mli index 0e76eca3..70f7f12b 100644 --- a/src/data/CCRAL.mli +++ b/src/data/CCRAL.mli @@ -109,9 +109,9 @@ val rev_map : f:('a -> 'b) -> 'a t -> 'b t val rev : 'a t -> 'a t (** Reverse the list *) -val equal : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool +val equal : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool -val compare : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int +val compare : cmp:('a -> 'a -> int) -> 'a t -> 'a t -> int (** Lexicographic comparison *) (** {2 Utils} *) diff --git a/src/data/CCSimple_queue.ml b/src/data/CCSimple_queue.ml index ed9b639c..67e02bbb 100644 --- a/src/data/CCSimple_queue.ml +++ b/src/data/CCSimple_queue.ml @@ -23,7 +23,7 @@ let make_ hd tl = match hd with | [] -> {hd=List.rev tl; tl=[] } | _::_ -> {hd; tl; } -let is_empty q = q.hd = [] +let is_empty q = CCList.is_empty q.hd let push x q = make_ q.hd (x :: q.tl) @@ -31,7 +31,7 @@ let snoc q x = push x q let peek_exn q = match q.hd with - | [] -> assert (q.tl = []); invalid_arg "Queue.peek" + | [] -> assert (CCList.is_empty q.tl); invalid_arg "Queue.peek" | x::_ -> x let peek q = match q.hd with @@ -40,7 +40,7 @@ let peek q = match q.hd with let pop_exn q = match q.hd with - | [] -> assert (q.tl = []); invalid_arg "Queue.peek" + | [] -> assert (CCList.is_empty q.tl); invalid_arg "Queue.peek" | x::hd' -> let q' = make_ hd' q.tl in x, q' diff --git a/src/data/CCTrie.ml b/src/data/CCTrie.ml index d7f8b9f7..b3799adf 100644 --- a/src/data/CCTrie.ml +++ b/src/data/CCTrie.ml @@ -19,6 +19,7 @@ module type WORD = sig val compare : char_ -> char_ -> int val to_seq : t -> char_ sequence val of_list : char_ list -> t + val equal : t -> t -> bool end module type S = sig @@ -527,7 +528,7 @@ module Make(W : WORD) | Empty -> 0 | Cons (_, t') -> size t' | Node (v, map) -> - let s = if v=None then 0 else 1 in + let s = if CCOpt.is_none v then 0 else 1 in M.fold (fun _ t' acc -> size t' + acc) map s @@ -745,6 +746,7 @@ module MakeArray(X : ORDERED) = Make(struct let compare = X.compare let to_seq a k = Array.iter k a let of_list = Array.of_list + let equal = CCArray.equal (fun x y -> X.compare x y = 0) end) module MakeList(X : ORDERED) = Make(struct @@ -753,6 +755,7 @@ module MakeList(X : ORDERED) = Make(struct let compare = X.compare let to_seq a k = List.iter k a let of_list l = l + let equal = CCList.equal (fun x y -> X.compare x y = 0) end) module String = Make(struct @@ -764,4 +767,5 @@ module String = Make(struct let buf = Buffer.create (List.length l) in List.iter (fun c -> Buffer.add_char buf c) l; Buffer.contents buf + let equal = CCString.equal end) diff --git a/src/data/CCTrie.mli b/src/data/CCTrie.mli index 0cb34515..81f55d89 100644 --- a/src/data/CCTrie.mli +++ b/src/data/CCTrie.mli @@ -19,6 +19,7 @@ module type WORD = sig val compare : char_ -> char_ -> int val to_seq : t -> char_ sequence val of_list : char_ list -> t + val equal : t -> t -> bool end module type S = sig diff --git a/src/iter/CCKTree.ml b/src/iter/CCKTree.ml index eae5a29c..0d4489bb 100644 --- a/src/iter/CCKTree.ml +++ b/src/iter/CCKTree.ml @@ -141,13 +141,13 @@ module FQ = struct let empty = _make [] [] - let is_empty q = q.hd = [] + let is_empty q = CCList.is_empty q.hd let push q x = _make q.hd (x::q.tl) let pop_exn q = match q.hd with - | [] -> assert (q.tl = []); raise Empty + | [] -> assert (CCList.is_empty q.tl); raise Empty | x::hd' -> let q' = _make hd' q.tl in x, q' diff --git a/src/sexp/CCSexp.ml b/src/sexp/CCSexp.ml index 6e5d96a0..c781b4d3 100644 --- a/src/sexp/CCSexp.ml +++ b/src/sexp/CCSexp.ml @@ -13,7 +13,7 @@ type t = [ ] type sexp = t -let equal a b = a = b +let equal a b = Pervasives.(=) a b let compare a b = Pervasives.compare a b diff --git a/src/sexp/CCSexp_lex.mll b/src/sexp/CCSexp_lex.mll index c67e66eb..250f6c31 100644 --- a/src/sexp/CCSexp_lex.mll +++ b/src/sexp/CCSexp_lex.mll @@ -22,7 +22,7 @@ (* remove quotes + unescape *) let remove_quotes lexbuf s = - assert (s.[0] = '"' && s.[String.length s - 1] = '"'); + assert (CCChar.equal s.[0] '"' && CCChar.equal s.[String.length s - 1] '"'); let buf = Buffer.create (String.length s) in let st = ref Not_escaped in for i = 1 to String.length s-2 do @@ -72,4 +72,3 @@ rule token = parse | string { ATOM (remove_quotes lexbuf (Lexing.lexeme lexbuf)) } | _ as c { error lexbuf (Printf.sprintf "lexing failed on char `%c`" c) } - diff --git a/src/threads/CCPool.ml b/src/threads/CCPool.ml index 914461cc..ffa1ced4 100644 --- a/src/threads/CCPool.ml +++ b/src/threads/CCPool.ml @@ -314,11 +314,15 @@ module Make(P : PARAM) = struct | Run cell -> with_lock_ cell (fun cell -> cell.state) + let not_waiting = function + | Waiting -> false + | _ -> true + let is_done = function | Return _ | FailNow _ -> true | Run cell -> - with_lock_ cell (fun c -> c.state <> Waiting) + with_lock_ cell (fun c -> not_waiting c.state) (** {2 Combinators *) diff --git a/src/threads/CCTimer.ml b/src/threads/CCTimer.ml index f2c37cb8..8bd5a7d6 100644 --- a/src/threads/CCTimer.ml +++ b/src/threads/CCTimer.ml @@ -6,6 +6,8 @@ type job = | Job : float * (unit -> 'a) -> job +open CCFloat.Infix + module TaskHeap = CCHeap.Make(struct type t = job let leq (Job(f1,_)) (Job (f2,_)) = f1 <= f2