Source file ptset.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
type elt = int
type t = Empty | Leaf of int | Branch of int * int * t * t
let empty = Empty
let is_empty = function Empty -> true | _ -> false
let singleton k = Leaf k
let zero_bit k m = k land m == 0
let rec mem k = function
| Empty -> false
| Leaf j -> k == j
| Branch (_, m, l, r) -> mem k (if zero_bit k m then l else r)
let lowest_bit x = x land -x
let branching_bit p0 p1 = lowest_bit (p0 lxor p1)
let mask p m = p land (m - 1)
let join (p0, t0, p1, t1) =
let m = branching_bit p0 p1 in
if zero_bit p0 m then Branch (mask p0 m, m, t0, t1)
else Branch (mask p0 m, m, t1, t0)
let match_prefix k p m = mask k m == p
let add k t =
let rec ins = function
| Empty -> Leaf k
| Leaf j as t -> if j == k then t else join (k, Leaf k, j, t)
| Branch (p, m, t0, t1) as t ->
if match_prefix k p m then
if zero_bit k m then Branch (p, m, ins t0, t1)
else Branch (p, m, t0, ins t1)
else join (k, Leaf k, p, t)
in
ins t
let branch = function
| _, _, Empty, t -> t
| _, _, t, Empty -> t
| p, m, t0, t1 -> Branch (p, m, t0, t1)
let remove k t =
let rec rmv = function
| Empty -> Empty
| Leaf j as t -> if k == j then Empty else t
| Branch (p, m, t0, t1) as t ->
if match_prefix k p m then
if zero_bit k m then branch (p, m, rmv t0, t1)
else branch (p, m, t0, rmv t1)
else t
in
rmv t
let rec merge = function
| Empty, t -> t
| t, Empty -> t
| Leaf k, t -> add k t
| t, Leaf k -> add k t
| (Branch (p, m, s0, s1) as s), (Branch (q, n, t0, t1) as t) ->
if m == n && match_prefix q p m then
Branch (p, m, merge (s0, t0), merge (s1, t1))
else if m < n && match_prefix q p m then
if zero_bit q m then Branch (p, m, merge (s0, t), s1)
else Branch (p, m, s0, merge (s1, t))
else if m > n && match_prefix p q n then
if zero_bit p n then Branch (q, n, merge (s, t0), t1)
else Branch (q, n, t0, merge (s, t1))
else
join (p, s, q, t)
let union s t = merge (s, t)
let rec subset s1 s2 =
match (s1, s2) with
| Empty, _ -> true
| _, Empty -> false
| Leaf k1, _ -> mem k1 s2
| Branch _, Leaf _ -> false
| Branch (p1, m1, l1, r1), Branch (p2, m2, l2, r2) ->
if m1 == m2 && p1 == p2 then subset l1 l2 && subset r1 r2
else if m1 > m2 && match_prefix p1 p2 m2 then
if zero_bit p1 m2 then subset l1 l2 && subset r1 l2
else subset l1 r2 && subset r1 r2
else false
let rec intersect s1 s2 =
match (s1, s2) with
| Empty, _ -> false
| _, Empty -> false
| Leaf k1, _ -> mem k1 s2
| _, Leaf k2 -> mem k2 s1
| Branch (p1, m1, l1, r1), Branch (p2, m2, l2, r2) ->
if m1 == m2 && p1 == p2 then intersect l1 l2 || intersect r1 r2
else if m1 < m2 && match_prefix p2 p1 m1 then
intersect (if zero_bit p2 m1 then l1 else r1) s2
else if m1 > m2 && match_prefix p1 p2 m2 then
intersect s1 (if zero_bit p1 m2 then l2 else r2)
else false
let rec inter s1 s2 =
match (s1, s2) with
| Empty, _ -> Empty
| _, Empty -> Empty
| Leaf k1, _ -> if mem k1 s2 then s1 else Empty
| _, Leaf k2 -> if mem k2 s1 then s2 else Empty
| Branch (p1, m1, l1, r1), Branch (p2, m2, l2, r2) ->
if m1 == m2 && p1 == p2 then merge (inter l1 l2, inter r1 r2)
else if m1 < m2 && match_prefix p2 p1 m1 then
inter (if zero_bit p2 m1 then l1 else r1) s2
else if m1 > m2 && match_prefix p1 p2 m2 then
inter s1 (if zero_bit p1 m2 then l2 else r2)
else Empty
let rec diff s1 s2 =
match (s1, s2) with
| Empty, _ -> Empty
| _, Empty -> s1
| Leaf k1, _ -> if mem k1 s2 then Empty else s1
| _, Leaf k2 -> remove k2 s1
| Branch (p1, m1, l1, r1), Branch (p2, m2, l2, r2) ->
if m1 == m2 && p1 == p2 then merge (diff l1 l2, diff r1 r2)
else if m1 < m2 && match_prefix p2 p1 m1 then
if zero_bit p2 m1 then merge (diff l1 s2, r1) else merge (l1, diff r1 s2)
else if m1 > m2 && match_prefix p1 p2 m2 then
if zero_bit p1 m2 then diff s1 l2 else diff s1 r2
else s1
let rec cardinal = function
| Empty -> 0
| Leaf _ -> 1
| Branch (_, _, t0, t1) -> cardinal t0 + cardinal t1
let rec iter f = function
| Empty -> ()
| Leaf k -> f k
| Branch (_, _, t0, t1) ->
iter f t0;
iter f t1
let map f t =
let res = ref Empty in
iter (fun el -> res := add (f el) !res) t;
!res
let rec fold f s accu =
match s with
| Empty -> accu
| Leaf k -> f k accu
| Branch (_, _, t0, t1) -> fold f t0 (fold f t1 accu)
let rec for_all p = function
| Empty -> true
| Leaf k -> p k
| Branch (_, _, t0, t1) -> for_all p t0 && for_all p t1
let rec exists p = function
| Empty -> false
| Leaf k -> p k
| Branch (_, _, t0, t1) -> exists p t0 || exists p t1
let filter p s =
let rec filt acc = function
| Empty -> acc
| Leaf k -> if p k then add k acc else acc
| Branch (_, _, t0, t1) -> filt (filt acc t0) t1
in
filt Empty s
let partition p s =
let rec part ((t, f) as acc) = function
| Empty -> acc
| Leaf k -> if p k then (add k t, f) else (t, add k f)
| Branch (_, _, t0, t1) -> part (part acc t0) t1
in
part (Empty, Empty) s
let rec choose = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_, _, t0, _) -> choose t0
let rec choose_opt = function
| Empty -> None
| Leaf k -> Some k
| Branch (_, _, t0, _) -> choose_opt t0
let split x s =
let coll k (l, b, r) =
if k < x then (add k l, b, r)
else if k > x then (l, b, add k r)
else (l, true, r)
in
fold coll s (Empty, false, Empty)
let elements s =
let rec elements_aux acc = function
| Empty -> acc
| Leaf k -> k :: acc
| Branch (_, _, l, r) -> elements_aux (elements_aux acc l) r
in
elements_aux [] s
let rec min_elt = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_, _, s, t) -> min (min_elt s) (min_elt t)
let min_elt_opt = function
| Empty -> None
| Leaf k -> Some k
| Branch (_, _, s, t) -> Some (min (min_elt s) (min_elt t))
let rec max_elt = function
| Empty -> raise Not_found
| Leaf k -> k
| Branch (_, _, s, t) -> max (max_elt s) (max_elt t)
let max_elt_opt = function
| Empty -> None
| Leaf k -> Some k
| Branch (_, _, s, t) -> Some (max (max_elt s) (max_elt t))
let find e t = if mem e t then e else raise Not_found
let find_opt e t = if mem e t then Some e else None
let rec find_first f = function
| Leaf k when f k -> k
| Empty | Leaf _ -> raise Not_found
| Branch (_, _, s, t) -> (
match (find_first_opt f s, find_first_opt f t) with
| Some vs, Some vt -> min vs vt
| Some v, None | None, Some v -> v
| None, None -> raise Not_found)
and find_first_opt f = function
| Leaf k when f k -> Some k
| Empty | Leaf _ -> None
| Branch (_, _, s, t) -> (
match (find_first_opt f s, find_first_opt f t) with
| Some vs, Some vt -> Some (min vs vt)
| (Some _ as opt_v), None | None, (Some _ as opt_v) -> opt_v
| None, None -> None)
let rec find_last f = function
| Leaf k when f k -> k
| Empty | Leaf _ -> raise Not_found
| Branch (_, _, s, t) -> (
match (find_last_opt f s, find_last_opt f t) with
| Some vs, Some vt -> max vs vt
| Some v, None | None, Some v -> v
| None, None -> raise Not_found)
and find_last_opt f = function
| Leaf k when f k -> Some k
| Empty | Leaf _ -> None
| Branch (_, _, s, t) -> (
match (find_last_opt f s, find_last_opt f t) with
| Some vs, Some vt -> Some (max vs vt)
| (Some _ as opt_v), None | None, (Some _ as opt_v) -> opt_v
| None, None -> None)
let equal = ( = )
let compare = compare
let of_list lst =
let coll acc el = add el acc in
List.fold_left coll empty lst