package containers

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file CCHashSet.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(* This file is free software, part of containers. See file "license" for more details. *)

(** {1 Mutable Set} *)

type 'a sequence = ('a -> unit) -> unit
type 'a printer = Format.formatter -> 'a -> unit

module type S = sig
  type t
  type elt

  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] *)

  val copy : t -> t
  (** Fresh copy *)

  val copy_into : into:t -> t -> unit
  (** [copy_into ~into s] copies all elements of [s] into [into] *)

  val insert : t -> elt -> unit
  (** [insert s x] adds [x] into [s] *)

  val remove : t -> elt -> unit
  (** Remove the element, if it were in there *)

  val cardinal : t -> int
  (** [cardinal s] returns the number of elements in [s] *)

  val mem : t -> elt -> bool
  (** [mem s x] returns [true] iff [x] is in [s] *)

  val find_exn : t -> elt -> elt
  (** [find_exn s x] returns [y] if [x] and [y] are equal, and [mem s y].
      @raise Not_found if [x] not in [s] *)

  val find : t -> elt -> elt option
  (** Safe version of {!find_exn} *)

  val inter : t -> t -> t
  (** [inter a b] returns [a ∩ b] *)

  val inter_mut : into:t -> t -> unit
  (** [inter_mut ~into a] changes [into] into [a ∩ into] *)

  val union : t -> t -> t
  (** [union a b] returns [a ∪ b] *)

  val union_mut : into:t -> t -> unit
  (** [union_mut ~into a] changes [into] into [a ∪ into] *)

  val diff : t -> t -> t
  (** [diff a b] returns [a - b] *)

  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

  val iter : (elt -> unit) -> t -> unit
  (** Iterate on values *)

  val fold : ('a -> elt -> 'a) -> 'a -> t -> 'a
  (** Fold on values *)

  val elements : t -> elt list
  (** List of elements *)

  val of_list : elt list -> t

  val to_seq : t -> elt sequence

  val of_seq : elt sequence -> t

  val add_seq : t -> elt sequence -> unit

  val pp : ?sep:string -> elt printer -> t printer
  (** [pp pp_elt] returns a set printer, given a printer for
      individual elements *)
end

module type ELEMENT = sig
  type t
  val equal : t -> t -> bool
  val hash : t -> int (** Positive value *)
end

module Make(E : ELEMENT) : S with type elt = E.t = struct
  module Tbl = Hashtbl.Make(E)

  type elt = E.t

  type t = elt Tbl.t  (* map [x -> x], for find *)

  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

  let copy_into ~into s =
    Tbl.iter (fun x _ -> Tbl.replace into x x) s

  let insert s x = Tbl.replace s x x

  let remove = Tbl.remove

  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

  let find s x =
    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

  let inter a b =
    let res = create (min (cardinal a) (cardinal b)) in
    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 ->
         if not (mem a x) then remove into x
      ) into

  let union a b =
    let res = copy a in
    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

  let diff a b =
    let res = copy a in
    iter
      (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 =
    try
      Tbl.iter (fun x _ -> if not (p x) then raise FastExit) s;
      true
    with FastExit -> false

  let exists p s =
    try
      Tbl.iter (fun x _ -> if p x then raise FastExit) s;
      false
    with FastExit -> true

  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 []

  let of_list l =
    let res = create (List.length l) in
    List.iter (insert res) l;
    res

  let to_seq s yield = iter yield s

  let add_seq s seq = seq (insert s)

  let of_seq seq =
    let s = create 32 in
    seq (insert s);
    s

  let pp ?(sep=",") pp_x out s =
    Format.pp_print_string out "{";
    let first = ref true in
    Tbl.iter
      (fun x _ ->
         if !first
         then first := false
         else (
           Format.pp_print_string out sep;
           Format.pp_print_cut out ();
         );
         pp_x out x
      ) s;
    Format.pp_print_string out "}"
end
OCaml

Innovation. Community. Security.