From 7642d662cbb37d3b0f3fe263dc967f2e70300fd8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 2 Aug 2015 21:23:49 +0200 Subject: [PATCH] add some tests and functions to `CCHashSet` --- src/data/CCHashSet.ml | 38 ++++++++++++++++++++++++++++++++++++++ src/data/CCHashSet.mli | 6 ++++++ 2 files changed, 44 insertions(+) diff --git a/src/data/CCHashSet.ml b/src/data/CCHashSet.ml index 20a350d6..6d8520cb 100644 --- a/src/data/CCHashSet.ml +++ b/src/data/CCHashSet.ml @@ -12,6 +12,9 @@ module type S = sig val create : int -> t (** [create n] makes a new set with the given capacity [n] *) + val singleton : elt -> t + (** [singleton x] is the singleton [{x}] *) + val clear : t -> unit (** [clear s] removes all elements from [s] *) @@ -58,6 +61,9 @@ module type S = sig val subset : t -> t -> bool (** [subset a b] returns [true] if all elements of [a] are in [b] *) + val equal : t -> t -> bool + (** [equal a b] is extensional equality ([a] and [b] have the same elements) *) + val for_all : (elt -> bool) -> t -> bool val exists : (elt -> bool) -> t -> bool @@ -99,6 +105,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct let create = Tbl.create + let singleton x = + let s = create 8 in + Tbl.replace s x x; + s + let clear = Tbl.clear let copy = Tbl.copy @@ -112,6 +123,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct let cardinal = Tbl.length + (*$T + let module IS = Make(CCInt) in \ + IS.cardinal (IS.create 10) = 0 + *) + let mem = Tbl.mem let find_exn = Tbl.find @@ -120,6 +136,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct try Some (Tbl.find s x) with Not_found -> None + (*$T + let module IS = Make(CCInt) in IS.find (IS.of_list [1;2;3]) 3 = Some 3 + let module IS = Make(CCInt) in IS.find (IS.of_list [1;2;3]) 5 = None + *) + let iter f s = Tbl.iter (fun x _ -> f x) s let fold f acc s = Tbl.fold (fun x _ acc -> f acc x) s acc @@ -129,6 +150,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct iter (fun x -> if mem a x then insert res x) b; res + (*$T + let module IS = Make(CCInt) in \ + IS.(equal (inter (of_list [1;2;3]) (of_list [2;5;4])) (of_list [2])) + *) + let inter_mut ~into a = iter (fun x -> @@ -140,6 +166,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct copy_into ~into:res b; res + (*$T + let module IS = Make(CCInt) in \ + IS.(equal (union (of_list [1;2;3]) (of_list [2;5;4])) (of_list [1;2;3;4;5])) + *) + let union_mut ~into a = copy_into ~into a @@ -149,6 +180,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct (fun x -> remove res x) b; res + (*$T + let module IS = Make(CCInt) in \ + IS.(equal (diff (of_list [1;2;3]) (of_list [2;4;5])) (of_list [1;3])) + *) + exception FastExit let for_all p s = @@ -166,6 +202,8 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct let subset a b = for_all (fun x -> mem b x) a + let equal a b = subset a b && subset b a + let elements s = Tbl.fold (fun x _ acc -> x::acc) s [] diff --git a/src/data/CCHashSet.mli b/src/data/CCHashSet.mli index ae60ed02..04f458fa 100644 --- a/src/data/CCHashSet.mli +++ b/src/data/CCHashSet.mli @@ -16,6 +16,9 @@ module type S = sig val create : int -> t (** [create n] makes a new set with the given capacity [n] *) + val singleton : elt -> t + (** [singleton x] is the singleton [{x}] *) + val clear : t -> unit (** [clear s] removes all elements from [s] *) @@ -62,6 +65,9 @@ module type S = sig val subset : t -> t -> bool (** [subset a b] returns [true] if all elements of [a] are in [b] *) + val equal : t -> t -> bool + (** [equal a b] is extensional equality ([a] and [b] have the same elements) *) + val for_all : (elt -> bool) -> t -> bool val exists : (elt -> bool) -> t -> bool