Merge branch 'master' of ssh://git.nguyen.vg/tatoo
[tatoo.git] / src / ptset.ml
1 (* Original file: *)
2 (***********************************************************************)
3 (*                                                                     *)
4 (*  Copyright (C) Jean-Christophe Filliatre                            *)
5 (*                                                                     *)
6 (*  This software is free software; you can redistribute it and/or     *)
7 (*  modify it under the terms of the GNU Library General Public        *)
8 (*  License version 2.1, with the special exception on linking         *)
9 (*  described in file http://www.lri.fr/~filliatr/ftp/ocaml/ds/LICENSE *)
10 (*                                                                     *)
11 (*  This software is distributed in the hope that it will be useful,   *)
12 (*  but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
13 (*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               *)
14 (*                                                                     *)
15 (***********************************************************************)
16
17 (* Modified by Kim Nguyen *)
18 (* The Patricia trees are themselves deeply hash-consed. The module
19    provides a Make (and Weak) functor to build hash-consed patricia
20    trees whose elements are Abstract hash-consed values.
21 *)
22
23 INCLUDE "utils.ml"
24
25 include Sigs.PTSET
26
27 module type HConsBuilder =
28   functor (H : Sigs.AUX.HashedType) -> Hcons.S with type data = H.t
29
30 module Builder (HCB : HConsBuilder) (H : Hcons.Abstract) :
31   S with type elt = H.t =
32 struct
33   type elt = H.t
34
35   type 'a set =
36     | Empty
37     | Leaf of elt
38     | Branch of int * int * 'a * 'a
39
40   module rec Node : Hcons.S with type data = Data.t = HCB(Data)
41                             and Data : Sigs.AUX.HashedType with type t = Node.t set =
42   struct
43     type t =  Node.t set
44     let equal x y =
45       match x,y with
46       | Empty,Empty -> true
47       | Leaf k1, Leaf k2 ->  k1 == k2
48       | Branch(b1,i1,l1,r1), Branch(b2,i2,l2,r2) ->
49           b1 == b2 && i1 == i2 && (Node.equal l1 l2) && (Node.equal r1 r2)
50
51       | _ -> false
52
53     let hash = function
54     | Empty -> 0
55     | Leaf i -> HASHINT2 (PRIME1, Uid.to_int (H.uid i))
56     | Branch (b,i,l,r) ->
57         HASHINT4(b, i, Uid.to_int l.Node.id, Uid.to_int r.Node.id)
58   end
59
60   include Node
61
62   let empty = Node.make Empty
63
64   let is_empty s = (Node.node s) == Empty
65
66   let branch p m l r = Node.make (Branch(p,m,l,r))
67
68   let leaf k = Node.make (Leaf k)
69
70                             (* To enforce the invariant that a branch contains two non empty
71                                sub-trees *)
72   let branch_ne p m t0 t1 =
73     if (is_empty t0) then t1
74     else if is_empty t1 then t0 else branch p m t0 t1
75
76                             (******** from here on, only use the smart constructors ************)
77
78   let zero_bit k m = (k land m) == 0
79
80   let singleton k = leaf k
81
82   let is_singleton n =
83     match Node.node n with Leaf _ -> true
84     | _ -> false
85
86   let mem (k:elt) n =
87     let kid = Uid.to_int (H.uid k) in
88     let rec loop n = match Node.node n with
89     | Empty -> false
90     | Leaf j ->  k == j
91     | Branch (p, _, l, r) -> if kid <= p then loop l else loop r
92     in loop n
93
94   let rec min_elt n = match Node.node n with
95   | Empty -> raise Not_found
96   | Leaf k -> k
97   | Branch (_,_,s,_) -> min_elt s
98
99   let rec max_elt n = match Node.node n with
100   | Empty -> raise Not_found
101   | Leaf k -> k
102   | Branch (_,_,_,t) -> max_elt t
103
104   let elements s =
105     let rec elements_aux acc n = match Node.node n with
106     | Empty -> acc
107     | Leaf k -> k :: acc
108     | Branch (_,_,l,r) -> elements_aux (elements_aux acc r) l
109     in
110     elements_aux [] s
111
112   let mask k m  = (k lor (m-1)) land (lnot m)
113
114   let naive_highest_bit x =
115     assert (x < 256);
116     let rec loop i =
117       if i = 0 then 1 else if x lsr i = 1 then 1 lsl i else loop (i-1)
118     in
119     loop 7
120
121   let hbit = Array.init 256 naive_highest_bit
122                             (*
123                               external clz : int -> int = "caml_clz" "noalloc"
124                               external leading_bit : int -> int = "caml_leading_bit" "noalloc"
125                             *)
126   let highest_bit x =
127     try
128       let n = (x) lsr 24 in
129       if n != 0 then  hbit.(n) lsl 24
130       else let n = (x) lsr 16 in if n != 0 then hbit.(n) lsl 16
131         else let n = (x) lsr 8 in if n != 0 then hbit.(n) lsl 8
132           else hbit.(x)
133     with
134       _ -> raise (Invalid_argument ("highest_bit " ^ (string_of_int x)))
135
136   let highest_bit64 x =
137     let n = x lsr 32 in if n != 0 then highest_bit n lsl 32
138       else highest_bit x
139
140   let branching_bit p0 p1 = highest_bit64 (p0 lxor p1)
141
142   let join p0 t0 p1 t1 =
143     let m = branching_bit p0 p1  in
144     let msk = mask p0 m in
145     if zero_bit p0 m then
146     branch_ne msk m t0 t1
147     else
148     branch_ne msk m t1 t0
149
150   let match_prefix k p m = (mask k m) == p
151
152   let add k t =
153     let kid = Uid.to_int (H.uid k) in
154     assert (kid >=0);
155     let rec ins n = match Node.node n with
156     | Empty -> leaf k
157     | Leaf j ->  if j == k then n else join kid (leaf k) (Uid.to_int (H.uid j)) n
158     | Branch (p,m,t0,t1)  ->
159         if match_prefix kid p m then
160         if zero_bit kid m then
161         branch_ne p m (ins t0) t1
162         else
163         branch_ne p m t0 (ins t1)
164         else
165         join kid (leaf k)  p n
166     in
167     ins t
168
169   let remove k t =
170     let kid = Uid.to_int(H.uid k) in
171     let rec rmv n = match Node.node n with
172     | Empty -> empty
173     | Leaf j  -> if  k == j then empty else n
174     | Branch (p,m,t0,t1) ->
175         if match_prefix kid p m then
176         if zero_bit kid m then
177         branch_ne p m (rmv t0) t1
178         else
179         branch_ne p m t0 (rmv t1)
180         else
181         n
182     in
183     rmv t
184
185                             (* should run in O(1) thanks to Hash consing *)
186
187   let equal a b = Node.equal a b
188
189   let compare a b = (Uid.to_int (Node.uid a)) - (Uid.to_int (Node.uid b))
190
191   let rec merge s t =
192     if (equal s t) (* This is cheap thanks to hash-consing *)
193     then s
194     else
195     match Node.node s, Node.node t with
196     | Empty, _  -> t
197     | _, Empty  -> s
198     | Leaf k, _ -> add k t
199     | _, Leaf k -> add k s
200     | Branch (p,m,s0,s1), Branch (q,n,t0,t1) ->
201         if m == n && match_prefix q p m then
202         branch p  m  (merge s0 t0) (merge s1 t1)
203         else if m > n && match_prefix q p m then
204         if zero_bit q m then
205         branch_ne p m (merge s0 t) s1
206         else
207         branch_ne p m s0 (merge s1 t)
208         else if m < n && match_prefix p q n then
209         if zero_bit p n then
210         branch_ne q n (merge s t0) t1
211         else
212         branch_ne q n t0 (merge s t1)
213         else
214                                   (* The prefixes disagree. *)
215         join p s q t
216
217
218
219
220   let rec subset s1 s2 = (equal s1 s2) ||
221     match (Node.node s1,Node.node s2) with
222     | Empty, _ -> true
223     | _, Empty -> false
224     | Leaf k1, _ -> mem k1 s2
225     | Branch _, Leaf _ -> false
226     | Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
227         if m1 == m2 && p1 == p2 then
228         subset l1 l2 && subset r1 r2
229         else if m1 < m2 && match_prefix p1 p2 m2 then
230         if zero_bit p1 m2 then
231         subset l1 l2 && subset r1 l2
232         else
233         subset l1 r2 && subset r1 r2
234         else
235         false
236
237
238   let union s1 s2 = merge s1 s2
239                             (* Todo replace with e Memo Module *)
240
241   let rec inter s1 s2 =
242     if equal s1 s2
243     then s1
244     else
245     match (Node.node s1,Node.node s2) with
246     | Empty, _ -> empty
247     | _, Empty -> empty
248     | Leaf k1, _ -> if mem k1 s2 then s1 else empty
249     | _, Leaf k2 -> if mem k2 s1 then s2 else empty
250     | Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
251         if m1 == m2 && p1 == p2 then
252         merge (inter l1 l2)  (inter r1 r2)
253         else if m1 > m2 && match_prefix p2 p1 m1 then
254         inter (if zero_bit p2 m1 then l1 else r1) s2
255         else if m1 < m2 && match_prefix p1 p2 m2 then
256         inter s1 (if zero_bit p1 m2 then l2 else r2)
257         else
258         empty
259
260   let rec diff s1 s2 =
261     if equal s1 s2
262     then empty
263     else
264     match (Node.node s1,Node.node s2) with
265     | Empty, _ -> empty
266     | _, Empty -> s1
267     | Leaf k1, _ -> if mem k1 s2 then empty else s1
268     | _, Leaf k2 -> remove k2 s1
269     | Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
270         if m1 == m2 && p1 == p2 then
271         merge (diff l1 l2) (diff r1 r2)
272         else if m1 > m2 && match_prefix p2 p1 m1 then
273         if zero_bit p2 m1 then
274         merge (diff l1 s2) r1
275         else
276         merge l1 (diff r1 s2)
277         else if m1 < m2 && match_prefix p1 p2 m2 then
278         if zero_bit p1 m2 then diff s1 l2 else diff s1 r2
279         else
280         s1
281
282
283   (*s All the following operations ([cardinal], [iter], [fold], [for_all],
284     [exists], [filter], [partition], [choose], [elements]) are
285     implemented as for any other kind of binary trees. *)
286
287   let rec cardinal n = match Node.node n with
288   | Empty -> 0
289   | Leaf _ -> 1
290   | Branch (_,_,t0,t1) -> cardinal t0 + cardinal t1
291
292   let rec iter f n = match Node.node n with
293   | Empty -> ()
294   | Leaf k -> f k
295   | Branch (_,_,t0,t1) -> iter f t0; iter f t1
296
297   let rec fold f s accu = match Node.node s with
298   | Empty -> accu
299   | Leaf k -> f k accu
300   | Branch (_,_,t0,t1) -> fold f t0 (fold f t1 accu)
301
302
303   let rec for_all p n = match Node.node n with
304   | Empty -> true
305   | Leaf k -> p k
306   | Branch (_,_,t0,t1) -> for_all p t0 && for_all p t1
307
308   let rec exists p n = match Node.node n with
309   | Empty -> false
310   | Leaf k -> p k
311   | Branch (_,_,t0,t1) -> exists p t0 || exists p t1
312
313   let rec filter pr n = match Node.node n with
314   | Empty -> empty
315   | Leaf k -> if pr k then n else empty
316   | Branch (p,m,t0,t1) -> branch_ne p m (filter pr t0) (filter pr t1)
317
318   let partition p s =
319     let rec part (t,f as acc) n = match Node.node n with
320     | Empty -> acc
321     | Leaf k -> if p k then (add k t, f) else (t, add k f)
322     | Branch (_,_,t0,t1) -> part (part acc t0) t1
323     in
324     part (empty, empty) s
325
326   let rec choose n = match Node.node n with
327   | Empty -> raise Not_found
328   | Leaf k -> k
329   | Branch (_, _,t0,_) -> choose t0   (* we know that [t0] is non-empty *)
330
331
332   let split x s =
333     let coll k (l, b, r) =
334       if k < x then add k l, b, r
335       else if k > x then l, b, add k r
336       else l, true, r
337     in
338     fold coll s (empty, false, empty)
339
340   (*s Additional functions w.r.t to [Set.S]. *)
341
342   let rec intersect s1 s2 = (equal s1 s2) ||
343     match (Node.node s1,Node.node s2) with
344     | Empty, _ -> false
345     | _, Empty -> false
346     | Leaf k1, _ -> mem k1 s2
347     | _, Leaf k2 -> mem k2 s1
348     | Branch (p1,m1,l1,r1), Branch (p2,m2,l2,r2) ->
349         if m1 == m2 && p1 == p2 then
350         intersect l1 l2 || intersect r1 r2
351         else if m1 < m2 && match_prefix p2 p1 m1 then
352         intersect (if zero_bit p2 m1 then l1 else r1) s2
353         else if m1 > m2 && match_prefix p1 p2 m2 then
354         intersect s1 (if zero_bit p1 m2 then l2 else r2)
355         else
356         false
357
358
359   let from_list l = List.fold_left (fun acc e -> add e acc) empty l
360
361
362 end
363
364 module Make = Builder(Hcons.Make)
365 module Weak = Builder(Hcons.Weak)
366
367 module PosInt
368   =
369 struct
370   include Make(Hcons.PosInt)
371   let print ppf s =
372     Format.pp_print_string ppf "{ ";
373     iter (fun i -> Format.fprintf ppf "%i " i) s;
374     Format.pp_print_string ppf "}";
375     Format.pp_print_flush ppf ()
376 end