Debian Codegraph @

clib/hMap.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module type HashedType =
12 sig
13 type t
14 val compare : t -> t -> int
15 val hash : t -> int
16 end
17
18 module SetMake(M : HashedType) =
19 struct
20 (** Hash Sets use hashes to prevent doing too many comparison tests. They
21 associate to each hash the set of keys having that hash.
22
23 Invariants:
24
25 1. There is no empty set in the intmap.
26 2. All values in the same set have the same hash, which is the int to
27 which it is associated in the intmap.
28 *)
29
30 module Set = Set.Make(M)
31
32 type elt = M.t
33
34 type t = Set.t Int.Map.t
35
36 let empty = Int.Map.empty
37
38 let is_empty = Int.Map.is_empty
39
40 let mem x s =
41 if Int.Map.is_empty s then false
42 else
43 let h = M.hash x in
44 try
45 let m = Int.Map.find h s in
46 Set.mem x m
47 with Not_found -> false
48
49 let add x s =
50 let h = M.hash x in
51 Int.Map.update h (function
52 | None -> Some (Set.singleton x)
53 | Some m -> Some (Set.add x m))
54 s
55
56 let singleton x =
57 let h = M.hash x in
58 let m = Set.singleton x in
59 Int.Map.singleton h m
60
61 let remove x s =
62 if Int.Map.is_empty s then s
63 else
64 let h = M.hash x in
65 Int.Map.update h (function
66 | None -> None
67 | Some m ->
68 let m = Set.remove x m in
69 if Set.is_empty m then None
70 else Some m)
71 s
72
73 let height s = Int.Map.height s
74
75 let is_smaller s1 s2 = height s1 <= height s2 + 3
76
77 (** Assumes s1 << s2 *)
78 let fast_union s1 s2 =
79 let fold h s accu =
80 try Int.Map.modify h (fun _ s' -> Set.fold Set.add s s') accu
81 with Not_found -> Int.Map.add h s accu
82 in
83 Int.Map.fold fold s1 s2
84
85 let union s1 s2 =
86 if is_smaller s1 s2 then fast_union s1 s2
87 else if is_smaller s2 s1 then fast_union s2 s1
88 else
89 let fu _ m1 m2 = match m1, m2 with
90 | None, None -> None
91 | (Some _ as m), None | None, (Some _ as m) -> m
92 | Some m1, Some m2 -> Some (Set.union m1 m2)
93 in
94 Int.Map.merge fu s1 s2
95
96 (** Assumes s1 << s2 *)
97 let fast_inter s1 s2 =
98 let fold h s accu =
99 try
100 let s' = Int.Map.find h s2 in
101 let si = Set.filter (fun e -> Set.mem e s') s in
102 if Set.is_empty si then accu
103 else Int.Map.add h si accu
104 with Not_found -> accu
105 in
106 Int.Map.fold fold s1 Int.Map.empty
107
108 let inter s1 s2 =
109 if is_smaller s1 s2 then fast_inter s1 s2
110 else if is_smaller s2 s1 then fast_inter s2 s1
111 else
112 let fu _ m1 m2 = match m1, m2 with
113 | None, None -> None
114 | Some _, None | None, Some _ -> None
115 | Some m1, Some m2 ->
116 let m = Set.inter m1 m2 in
117 if Set.is_empty m then None else Some m
118 in
119 Int.Map.merge fu s1 s2
120
121 (** Assumes s1 << s2 *)
122 let fast_diff_l s1 s2 =
123 let fold h s accu =
124 try
125 let s' = Int.Map.find h s2 in
126 let si = Set.filter (fun e -> not (Set.mem e s')) s in
127 if Set.is_empty si then accu
128 else Int.Map.add h si accu
129 with Not_found -> Int.Map.add h s accu
130 in
131 Int.Map.fold fold s1 Int.Map.empty
132
133 (** Assumes s2 << s1 *)
134 let fast_diff_r s1 s2 =
135 let fold h s accu =
136 try
137 let s' = Int.Map.find h accu in
138 let si = Set.filter (fun e -> not (Set.mem e s)) s' in
139 if Set.is_empty si then Int.Map.remove h accu
140 else Int.Map.set h si accu
141 with Not_found -> accu
142 in
143 Int.Map.fold fold s2 s1
144
145 let diff s1 s2 =
146 if is_smaller s1 s2 then fast_diff_l s1 s2
147 else if is_smaller s2 s2 then fast_diff_r s1 s2
148 else
149 let fu _ m1 m2 = match m1, m2 with
150 | None, None -> None
151 | (Some _ as m), None -> m
152 | None, Some _ -> None
153 | Some m1, Some m2 ->
154 let m = Set.diff m1 m2 in
155 if Set.is_empty m then None else Some m
156 in
157 Int.Map.merge fu s1 s2
158
159 let compare s1 s2 = Int.Map.compare Set.compare s1 s2
160
161 let equal s1 s2 = Int.Map.equal Set.equal s1 s2
162
163 let subset s1 s2 =
164 let check h m1 =
165 let m2 = try Int.Map.find h s2 with Not_found -> Set.empty in
166 Set.subset m1 m2
167 in
168 Int.Map.for_all check s1
169
170 let iter f s =
171 let fi _ m = Set.iter f m in
172 Int.Map.iter fi s
173
174 let fold f s accu =
175 let ff _ m accu = Set.fold f m accu in
176 Int.Map.fold ff s accu
177
178 let for_all f s =
179 let ff _ m = Set.for_all f m in
180 Int.Map.for_all ff s
181
182 let exists f s =
183 let fe _ m = Set.exists f m in
184 Int.Map.exists fe s
185
186 let filter f s =
187 let ff m = Set.filter f m in
188 let s = Int.Map.map ff s in
189 Int.Map.filter (fun _ m -> not (Set.is_empty m)) s
190
191 let filter_map f s =
192 Int.Map.filter_map (fun _ m ->
193 let m = Set.filter_map f m in
194 if Set.is_empty m then None else Some m)
195 s
196
197 let partition f s =
198 let fold h m (sl, sr) =
199 let (ml, mr) = Set.partition f m in
200 let sl = if Set.is_empty ml then sl else Int.Map.add h ml sl in
201 let sr = if Set.is_empty mr then sr else Int.Map.add h mr sr in
202 (sl, sr)
203 in
204 Int.Map.fold fold s (Int.Map.empty, Int.Map.empty)
205
206 let cardinal s =
207 let fold _ m accu = accu + Set.cardinal m in
208 Int.Map.fold fold s 0
209
210 let elements s =
211 let fold _ m accu = Set.fold (fun x accu -> x :: accu) m accu in
212 Int.Map.fold fold s []
213
214 let choose s =
215 let (_, m) = Int.Map.choose s in
216 Set.choose m
217
218 end
219
220 module Make(M : HashedType) =
221 struct
222 (** This module is essentially the same as SetMake, except that we have maps
223 instead of sets in the intmap. Invariants are the same. *)
224 module Set = SetMake(M)
225 module Map = CMap.Make(M)
226
227 type key = M.t
228
229 type 'a t = 'a Map.t Int.Map.t
230
231 let empty = Int.Map.empty
232
233 let is_empty = Int.Map.is_empty
234
235 let mem k s =
236 if Int.Map.is_empty s then false
237 else
238 let h = M.hash k in
239 try
240 let m = Int.Map.find h s in
241 Map.mem k m
242 with Not_found -> false
243
244 let add k x s =
245 let h = M.hash k in
246 Int.Map.update h (function
247 | None -> Some (Map.singleton k x)
248 | Some m -> Some (Map.add k x m))
249 s
250
251 let singleton k x =
252 let h = M.hash k in
253 Int.Map.singleton h (Map.singleton k x)
254
255 let remove k s =
256 if Int.Map.is_empty s then s
257 else
258 let h = M.hash k in
259 Int.Map.update h (function
260 | None -> None
261 | Some m ->
262 let m = Map.remove k m in
263 if Map.is_empty m then None
264 else Some m)
265 s
266
267 let merge f s1 s2 =
268 let fm h m1 m2 = match m1, m2 with
269 | None, None -> None
270 | Some m, None ->
271 let m = Map.merge f m Map.empty in
272 if Map.is_empty m then None
273 else Some m
274 | None, Some m ->
275 let m = Map.merge f Map.empty m in
276 if Map.is_empty m then None
277 else Some m
278 | Some m1, Some m2 ->
279 let m = Map.merge f m1 m2 in
280 if Map.is_empty m then None
281 else Some m
282 in
283 Int.Map.merge fm s1 s2
284
285 let union f s1 s2 =
286 let fm h m1 m2 =
287 let m = Map.union f m1 m2 in
288 if Map.is_empty m then None
289 else Some m
290 in
291 Int.Map.union fm s1 s2
292
293 let compare f s1 s2 =
294 let fc m1 m2 = Map.compare f m1 m2 in
295 Int.Map.compare fc s1 s2
296
297 let equal f s1 s2 =
298 let fe m1 m2 = Map.equal f m1 m2 in
299 Int.Map.equal fe s1 s2
300
301 let iter f s =
302 let fi _ m = Map.iter f m in
303 Int.Map.iter fi s
304
305 let fold f s accu =
306 let ff _ m accu = Map.fold f m accu in
307 Int.Map.fold ff s accu
308
309 let for_all f s =
310 let ff _ m = Map.for_all f m in
311 Int.Map.for_all ff s
312
313 let exists f s =
314 let fe _ m = Map.exists f m in
315 Int.Map.exists fe s
316
317 let filter f s =
318 let ff m = Map.filter f m in
319 let s = Int.Map.map ff s in
320 Int.Map.filter (fun _ m -> not (Map.is_empty m)) s
321
322 let filter_map f s =
323 let ff m = Map.filter_map f m in
324 let s = Int.Map.map ff s in
325 Int.Map.filter (fun _ m -> not (Map.is_empty m)) s
326
327 let partition f s =
328 let fold h m (sl, sr) =
329 let (ml, mr) = Map.partition f m in
330 let sl = if Map.is_empty ml then sl else Int.Map.add h ml sl in
331 let sr = if Map.is_empty mr then sr else Int.Map.add h mr sr in
332 (sl, sr)
333 in
334 Int.Map.fold fold s (Int.Map.empty, Int.Map.empty)
335
336 let cardinal s =
337 let fold _ m accu = accu + Map.cardinal m in
338 Int.Map.fold fold s 0
339
340 let bindings s =
341 let fold _ m accu = Map.fold (fun k x accu -> (k, x) :: accu) m accu in
342 Int.Map.fold fold s []
343
344 let choose s =
345 let (_, m) = Int.Map.choose s in
346 Map.choose m
347
348 let choose_opt s =
349 try Some (choose s)
350 with Not_found -> None
351
352 let find k s =
353 if Int.Map.is_empty s then raise Not_found
354 else
355 let h = M.hash k in
356 let m = Int.Map.find h s in
357 Map.find k m
358
359 let find_opt k s =
360 if Int.Map.is_empty s then None
361 else
362 let h = M.hash k in
363 match Int.Map.find_opt h s with
364 | None -> None
365 | Some m -> Map.find_opt k m
366
367 let get k s =
368 let h = M.hash k in
369 let m = Int.Map.get h s in
370 Map.get k m
371
372 let map f s =
373 let fs m = Map.map f m in
374 Int.Map.map fs s
375
376 let mapi f s =
377 let fs m = Map.mapi f m in
378 Int.Map.map fs s
379
380 let modify k f s =
381 if Int.Map.is_empty s then raise Not_found
382 else
383 let h = M.hash k in
384 Int.Map.modify h (fun _ m -> Map.modify k f m) s
385
386 let bind f s =
387 let fb m = Map.bind f m in
388 Int.Map.map fb s
389
390 let domain s = Int.Map.map Map.domain s
391
392 let set k x s =
393 if Int.Map.is_empty s then raise Not_found
394 else
395 let h = M.hash k in
396 Int.Map.modify h (fun _ m -> Map.set k x m) s
397
398 module Smart =
399 struct
400
401 let map f s =
402 let fs m = Map.Smart.map f m in
403 Int.Map.Smart.map fs s
404
405 let mapi f s =
406 let fs m = Map.Smart.mapi f m in
407 Int.Map.Smart.map fs s
408
409 end
410
411 let height s = Int.Map.height s
412
413 (* Not as efficient as the original version *)
414 let filter_range f s =
415 filter (fun x _ -> f x = 0) s
416
417 let of_list l =
418 let fold accu (x, v) = add x v accu in
419 List.fold_left fold empty l
420
421 let update k f m =
422 if Int.Map.is_empty m then
423 begin match f None with
424 | None -> m
425 | Some v -> singleton k v
426 end
427 else
428 let aux = function
429 | None -> (match f None with
430 | None -> None
431 | Some v -> Some (Map.singleton k v))
432 | Some m ->
433 let m = Map.update k f m in
434 if Map.is_empty m then None
435 else Some m
436 in
437 Int.Map.update (M.hash k) aux m
438
439 module Monad(M : CMap.MonadS) =
440 struct
441 module IntM = Int.Map.Monad(M)
442 module ExtM = Map.Monad(M)
443
444 let fold f s accu =
445 let ff _ m accu = ExtM.fold f m accu in
446 IntM.fold ff s accu
447
448 let mapi f s =
449 IntM.mapi (fun _ m -> ExtM.mapi f m) s
450 end
451
452 let symmetric_diff_fold f lm rm acc =
453 Int.Map.symmetric_diff_fold
454 (fun _ l r -> match l, r with
455 | Some m, None -> Map.fold (fun k v acc -> f k (Some v) None acc) m
456 | None, Some m -> Map.fold (fun k v acc -> f k None (Some v) acc) m
457 | Some lm, Some rm -> Map.symmetric_diff_fold f lm rm
458 | None, None -> assert false)
459 lm rm acc
460
461 end