89f3f039f0e10170b9cf86b95ad85aa15ec15d9f
1 (**************************************************************************)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the Q Public License version 1.0, with the change *)
11 (* described in file LICENSE. *)
13 (**************************************************************************)
15 (* A compressed (or should we say sparse?) bit set is a list of pairs
16 of integers. The first component of every pair is an index, while
17 the second component is a bit field. The list is sorted by order
18 of increasing indices. *)
33 let is_empty = function
40 let ioffset = i
mod word_size in
41 let iaddr = i
- ioffset
42 and imask
= 1 lsl ioffset in
43 let rec add = function
47 | C
(addr
, ss
, qs
) as s
->
49 (* Insert in front. *)
51 else if iaddr = addr
then
52 (* Found appropriate cell, update bit field. *)
53 let ss'
= ss lor imask
in
59 (* Not there yet, continue. *)
72 let ioffset = i
mod word_size in
73 let iaddr = i
- ioffset
74 and imask
= 1 lsl ioffset in
75 let rec remove = function
78 | C
(addr
, ss, qs) as s
->
81 else if iaddr = addr
then
82 (* Found appropriate cell, update bit field. *)
83 let ss'
= ss land (lnot imask
) in
91 (* Not there yet, continue. *)
92 let qs'
= remove qs in
100 let rec fold f s accu
=
104 | C
(base
, ss, qs) ->
105 let limit = base
+ word_size in
106 let rec loop i
ss accu
=
110 loop (i
+ 1) (ss lsr 1) (if ss land 1 = 1 then f i accu
else accu
)
112 fold f
qs (loop base
ss accu
)
115 fold (fun x
() -> f x
) s
()
118 fold (fun _ m
-> m
+ 1) s
0
121 fold (fun tl hd
-> tl
:: hd
) s
[]
123 let rec subset s1 s2
=
129 | C
(addr1
, ss1
, qs1
), C
(addr2
, ss2
, qs2
) ->
130 if addr1
< addr2
then
132 else if addr1
= addr2
then
133 if (ss1
land ss2
) <> ss1
then
141 subset (singleton i
) s
143 let rec union s1 s2
=
148 | C
(addr1
, ss1
, qs1
), C
(addr2
, ss2
, qs2
) ->
149 if addr1
< addr2
then
150 C
(addr1
, ss1
, union qs1 s2
)
151 else if addr1
> addr2
then
152 let s = union s1 qs2
in
158 let ss = ss1
lor ss2
in
159 let s = union qs1 qs2
in
160 if ss == ss2
&& s == qs2
then
165 let rec inter s1 s2
=
170 | C
(addr1
, ss1
, qs1
), C
(addr2
, ss2
, qs2
) ->
171 if addr1
< addr2
then
173 else if addr1
> addr2
then
176 let ss = ss1
land ss2
in
177 let s = inter qs1 qs2
in
181 if (ss = ss1
) && (s == qs1
) then
186 exception Found
of int
197 let rec compare s1 s2
=
202 | C
(addr1
, ss1
, qs1
), C
(addr2
, ss2
, qs2
) ->
203 if addr1
< addr2
then -1
204 else if addr1
> addr2
then 1
205 else if ss1
< ss2
then -1
206 else if ss1
> ss2
then 1
209 let rec equal s1 s2
=
212 let rec disjoint s1 s2
=
217 | C
(addr1
, ss1
, qs1
), C
(addr2
, ss2
, qs2
) ->
218 if addr1
= addr2
then
219 if (ss1
land ss2
) = 0 then
223 else if addr1
< addr2
then