remove CCList.Set, move functions to toplevel and rename them

This commit is contained in:
Simon Cruanes 2016-11-03 21:45:15 +01:00
parent 9f34a7f6e3
commit bf609e7f04
2 changed files with 76 additions and 77 deletions

View file

@ -654,17 +654,16 @@ let filter_map f l =
[ 1; 2; 3; 4; 5; 6 ])
*)
module Set = struct
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 ?(eq=(=)) x l =
let add_nodup ?(eq=(=)) x l =
if mem ~eq x l then l else x::l
let remove ?(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
@ -674,11 +673,11 @@ module Set = struct
(*$Q
Q.(pair int (list int)) (fun (x,l) -> \
Set.remove x (Set.add x l) = l)
remove_one x (add_nodup x l) = l)
Q.(pair int (list int)) (fun (x,l) -> \
Set.mem x l || List.length (Set.add x l) = List.length l + 1)
mem x l || List.length (add_nodup x l) = List.length l + 1)
Q.(pair int (list int)) (fun (x,l) -> \
not (Set.mem x l) || List.length (Set.remove x l) = List.length l - 1)
not (mem x l) || List.length (remove_one x l) = List.length l - 1)
*)
let subset ?(eq=(=)) l1 l2 =
@ -694,7 +693,7 @@ module Set = struct
in uniq eq [] l
(*$T
Set.uniq [1;1;2;2;3;4;4;2;4;1;5] |> List.sort Pervasives.compare = [1;2;3;4;5]
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 =
@ -705,7 +704,7 @@ module Set = struct
in union eq [] l1 l2
(*$T
Set.union [1;2;4] [2;3;4;5] = [1;2;3;4;5]
union [1;2;4] [2;3;4;5] = [1;2;3;4;5]
*)
let inter ?(eq=(=)) l1 l2 =
@ -716,9 +715,8 @@ module Set = struct
in inter eq [] l1 l2
(*$T
Set.inter [1;2;4] [2;3;4;5] = [2;4]
inter [1;2;4] [2;3;4;5] = [2;4]
*)
end
module Idx = struct
let mapi f l =

View file

@ -251,15 +251,17 @@ module Idx : sig
too high. *)
end
(** {2 Set Operators} *)
(** {2 Set Operators}
module Set : sig
val add : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
(** [add x set] adds [x] to [set] if it was not already present. Linear time.
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
(** [add_nodup x set] adds [x] to [set] if it was not already present. Linear time.
@since 0.11 *)
val remove : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t
(** [remove x set] removes one occurrence of [x] from [set]. Linear time.
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
@ -269,7 +271,7 @@ module Set : sig
(** Test for inclusion *)
val uniq : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t
(** List uniq. Remove duplicates w.r.t the equality predicate.
(** 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}*)
@ -279,7 +281,6 @@ module Set : sig
val inter : ?eq:('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t
(** List intersection. Complexity is product of length of inputs. *)
end
(** {2 Other Constructors} *)