add some tests and functions to CCHashSet

This commit is contained in:
Simon Cruanes 2015-08-02 21:23:49 +02:00
parent 0d0a8f8764
commit 7642d662cb
2 changed files with 44 additions and 0 deletions

View file

@ -12,6 +12,9 @@ module type S = sig
val create : int -> t val create : int -> t
(** [create n] makes a new set with the given capacity [n] *) (** [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 val clear : t -> unit
(** [clear s] removes all elements from [s] *) (** [clear s] removes all elements from [s] *)
@ -58,6 +61,9 @@ module type S = sig
val subset : t -> t -> bool val subset : t -> t -> bool
(** [subset a b] returns [true] if all elements of [a] are in [b] *) (** [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 for_all : (elt -> bool) -> t -> bool
val exists : (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 create = Tbl.create
let singleton x =
let s = create 8 in
Tbl.replace s x x;
s
let clear = Tbl.clear let clear = Tbl.clear
let copy = Tbl.copy let copy = Tbl.copy
@ -112,6 +123,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct
let cardinal = Tbl.length let cardinal = Tbl.length
(*$T
let module IS = Make(CCInt) in \
IS.cardinal (IS.create 10) = 0
*)
let mem = Tbl.mem let mem = Tbl.mem
let find_exn = Tbl.find 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) try Some (Tbl.find s x)
with Not_found -> None 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 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 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; iter (fun x -> if mem a x then insert res x) b;
res 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 = let inter_mut ~into a =
iter iter
(fun x -> (fun x ->
@ -140,6 +166,11 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct
copy_into ~into:res b; copy_into ~into:res b;
res 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 = let union_mut ~into a =
copy_into ~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; (fun x -> remove res x) b;
res 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 exception FastExit
let for_all p s = let for_all p s =
@ -166,6 +202,8 @@ module Make(E : ELEMENT) : S with type elt = E.t = struct
let subset a b = let subset a b =
for_all (fun x -> mem b x) a for_all (fun x -> mem b x) a
let equal a b = subset a b && subset b a
let elements s = let elements s =
Tbl.fold (fun x _ acc -> x::acc) s [] Tbl.fold (fun x _ acc -> x::acc) s []

View file

@ -16,6 +16,9 @@ module type S = sig
val create : int -> t val create : int -> t
(** [create n] makes a new set with the given capacity [n] *) (** [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 val clear : t -> unit
(** [clear s] removes all elements from [s] *) (** [clear s] removes all elements from [s] *)
@ -62,6 +65,9 @@ module type S = sig
val subset : t -> t -> bool val subset : t -> t -> bool
(** [subset a b] returns [true] if all elements of [a] are in [b] *) (** [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 for_all : (elt -> bool) -> t -> bool
val exists : (elt -> bool) -> t -> bool val exists : (elt -> bool) -> t -> bool