-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBitMaskSet.ml
659 lines (601 loc) · 23.1 KB
/
BitMaskSet.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
(* ********************************************************************************************** *
* MetaStack Solutions Ltd. *
* ********************************************************************************************** *
* BitMask Sets *
* ********************************************************************************************** *
* Copyright (c) 2013-17 MetaStack Solutions Ltd. *
* ********************************************************************************************** *
* Author: David Allsopp *
* 27-Dec-2013 *
* ********************************************************************************************** *
* Redistribution and use in source and binary forms, with or without modification, are permitted *
* provided that the following two conditions are met: *
* 1. Redistributions of source code must retain the above copyright notice, this list of *
* conditions and the following disclaimer. *
* 2. Neither the name of MetaStack Solutions Ltd. nor the names of its contributors may be *
* used to endorse or promote products derived from this software without specific prior *
* written permission. *
* *
* This software is provided by the Copyright Holder 'as is' and any express or implied *
* warranties, including, but not limited to, the implied warranties of merchantability and *
* fitness for a particular purpose are disclaimed. In no event shall the Copyright Holder be *
* liable for any direct, indirect, incidental, special, exemplary, or consequential damages *
* (including, but not limited to, procurement of substitute goods or services; loss of use, *
* data, or profits; or business interruption) however caused and on any theory of liability, *
* whether in contract, strict liability, or tort (including negligence or otherwise) arising in *
* any way out of the use of this software, even if advised of the possibility of such damage. *
* ********************************************************************************************** *)
(* ********************************************************************************************** *
* Copied from header. *
* ********************************************************************************************** *)
module type S =
sig
include Set.S
val map : (elt -> elt) -> t -> t
val filter_map : (elt -> elt option) -> t -> t
val min_elt_opt : t -> elt option
val max_elt_opt : t -> elt option
val choose_opt : t -> elt option
val find : elt -> t -> elt
val find_opt : elt -> t -> elt option
val find_first : (elt -> bool) -> t -> elt
val find_first_opt : (elt -> bool) -> t -> elt option
val find_last : (elt -> bool) -> t -> elt
val find_last_opt : (elt -> bool) -> t -> elt option
val of_list : elt list -> t
val to_seq_from : elt -> t -> elt Seq.t
val to_seq : t -> elt Seq.t
val add_seq : elt Seq.t -> t -> t
val of_seq : elt Seq.t -> t
val disjoint : t -> t -> bool
val to_rev_seq : t -> elt Seq.t
type storage
val invalid : t -> t
end
module type Storage =
sig
type storage
val zero : storage
val one : storage
val logand : storage -> storage -> storage
val logor : storage -> storage -> storage
val lognot : storage -> storage
val shift_left : storage -> int -> storage
val shift_right_logical : storage -> int -> storage
val compare : storage -> storage -> int
val toString : storage -> string
end
module type BitMask =
sig
include Storage
type t
val mask : storage
end
(* ********************************************************************************************** *
* Implementations of Storage for types int and int64. *
* ********************************************************************************************** *)
module Int =
struct
type storage = int
let zero = 0
let one = 1
let shift_left = (lsl)
let shift_right_logical = (lsr)
let logand = (land)
let logor = (lor)
let lognot = lnot
let compare = compare
let toString = string_of_int
end
module Int64 =
struct
type storage = int64
let zero = 0L
let one = 1L
let shift_left = Int64.shift_left
let shift_right_logical = Int64.shift_right_logical
let logand = Int64.logand
let logor = Int64.logor
let lognot = Int64.lognot
let compare = Int64.compare
let toString = Int64.to_string
end
(* ********************************************************************************************** *
* Make functor. *
* ********************************************************************************************** *)
module Make(Mask : BitMask) : sig
include S
with type storage = Mask.storage
and type t = Mask.storage
and type elt := Mask.t
val create : storage -> t
end = struct
type storage = Mask.storage
type t = Mask.storage
(* ****************************************************************************************** *
* Convert the supplied mask in the functor into the various required values. *
* ****************************************************************************************** *)
let (storage_of_flag, shifts, shiftsInv, topbit, highest, lowest) =
let (shifts, shiftsInv, topbit, highest, lowest) =
let rec f shiftsInv topbit highest lowest l c i =
let v = Mask.shift_left Mask.one i
in
if i > 0 && Mask.compare v Mask.one = 0
then (List.rev shiftsInv, shiftsInv, topbit, highest, lowest)
else if Mask.compare (Mask.logand v Mask.mask) Mask.zero <> 0
then let shiftsInv =
if l > 0
then (c, l)::shiftsInv
else shiftsInv
in
if Mask.compare lowest Mask.zero = 0
then f shiftsInv 0 v v 0 0 (succ i)
else f shiftsInv (succ topbit) v lowest 0 0 (succ i)
else if l > 0
then f shiftsInv topbit highest lowest (succ l) c (succ i)
else f shiftsInv topbit highest lowest 1 (succ topbit) (succ i)
in
f [] (-1) Mask.zero Mask.zero 0 0 0
in
let compute_shift shifts offset =
let rec f a = function
(point, amount)::shifts ->
if offset >= point
then f (a + amount) shifts
else a
| [] ->
a
in
f offset shifts
in
let (storage_of_flag, shifts) =
match shifts with
[] ->
((fun (flag : Mask.t) -> Mask.shift_left Mask.one (Obj.magic flag : int)), [])
| [(0, n)] ->
((fun (flag : Mask.t) -> Mask.shift_left Mask.one (n + (Obj.magic flag : int))), [])
| (0, n)::shifts ->
((fun (flag : Mask.t) ->
let shift = n + compute_shift shifts (Obj.magic flag : int)
in
Mask.shift_left Mask.one shift), shifts)
| _ ->
((fun (flag : Mask.t) ->
Mask.shift_left Mask.one (compute_shift shifts (Obj.magic flag : int))), shifts)
in
(storage_of_flag, shifts, shiftsInv, topbit, highest, lowest)
(* ****************************************************************************************** *
* create, invalid, empty and is_empty are straightforward. *
* ****************************************************************************************** *)
let create mask =
mask
let invalid set =
Mask.logand (Mask.lognot Mask.mask) set
let empty = Mask.zero
let is_empty set =
(Mask.compare set Mask.zero = 0)
(* ****************************************************************************************** *
* Another sequence of straightforward functions. *
* ****************************************************************************************** *)
let mem flag set =
Mask.compare (Mask.logand set (storage_of_flag flag)) Mask.zero <> 0
let find flag set =
if Mask.compare (Mask.logand set (storage_of_flag flag)) Mask.zero = 0
then raise Not_found
else flag
let find_opt flag set =
if Mask.compare (Mask.logand set (storage_of_flag flag)) Mask.zero = 0
then None
else Some flag
let add flag set =
let set' = Mask.logor set (storage_of_flag flag)
in
if Mask.compare set set' = 0
then set
else set'
let of_list l =
List.fold_left (fun s f -> add f s) empty l
let singleton = storage_of_flag
let remove flag set =
let set' = Mask.logand set (Mask.lognot (storage_of_flag flag))
in
if Mask.compare set set' = 0
then set
else set'
let union = Mask.logor
let inter = Mask.logand
let disjoint a b =
Mask.logand a b = Mask.zero
let diff a b =
Mask.logand b (Mask.lognot a)
let compare = Mask.compare
let equal a b =
Mask.compare a b = 0
let subset a b =
Mask.compare (Mask.logand a b) a = 0
(* ****************************************************************************************** *
* deltaShift and deltaShiftInv are used to calculate bit values for the iterators. *
* ****************************************************************************************** *)
let deltaShift i = function
(point, amount)::shifts when i >= point ->
(succ amount, shifts)
| _ as shifts ->
(1, shifts)
let deltaShiftInv i = function
(point, amount)::shifts when i < point ->
(succ amount, shifts)
| _ as shifts ->
(1, shifts)
(* ****************************************************************************************** *
* The iterators count over the bit positions -- for the iterator itself, [i] is the *
* constructor number, [v] is the bit value for that constructor and [s] is the shifts. *
* ****************************************************************************************** *)
let find_first g set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
let elt = (Obj.magic i : Mask.t)
in
if Mask.compare (Mask.logand set v) Mask.zero <> 0 && g elt
then elt
else if Mask.compare v highest = 0
then raise Not_found
else let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
in
f 0 lowest shifts
let find_first_opt g set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
let elt = (Obj.magic i : Mask.t)
in
if Mask.compare (Mask.logand set v) Mask.zero <> 0 && g elt
then Some elt
else if Mask.compare v highest = 0
then None
else let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
in
f 0 lowest shifts
let find_last g set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare v Mask.zero <> 0
then let elt = (Obj.magic i : Mask.t)
in
if Mask.compare (Mask.logand v set) Mask.zero <> 0 && g elt
then elt
else let i = pred i
in
let (shift, s) = deltaShiftInv i s
in
f i (Mask.shift_right_logical v shift) s
else raise Not_found
in
f topbit highest shiftsInv
let find_last_opt g set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare v Mask.zero <> 0
then let elt = (Obj.magic i : Mask.t)
in
if Mask.compare (Mask.logand v set) Mask.zero <> 0 && g elt
then Some elt
else let i = pred i
in
let (shift, s) = deltaShiftInv i s
in
f i (Mask.shift_right_logical v shift) s
else None
in
f topbit highest shiftsInv
let iter g set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
let _ =
if Mask.compare (Mask.logand set v) Mask.zero <> 0
then g (Obj.magic i : Mask.t)
in
if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
in
f 0 lowest shifts
let fold g set acc =
let set = Mask.logand set Mask.mask
in
let rec f a i v s =
let a =
if Mask.compare (Mask.logand set v) Mask.zero <> 0
then g (Obj.magic i : Mask.t) a
else a
in
if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else a
in
f acc 0 lowest shifts
let map g set' =
let set = Mask.logand set' Mask.mask
in
let rec f a i v s =
if Mask.compare v highest <> 0
then let a =
if Mask.compare (Mask.logand set v) Mask.zero <> 0
then Mask.logor a (storage_of_flag (g (Obj.magic i : Mask.t)))
else a
and i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else if Mask.compare a set' = 0
then set'
else a
in
f Mask.zero 0 lowest shifts
let filter_map g set' =
let set = Mask.logand set' Mask.mask
in
let rec f a i v s =
if Mask.compare v highest <> 0
then let a =
if Mask.compare (Mask.logand set v) Mask.zero <> 0
then match g (Obj.magic i : Mask.t) with
Some flag -> Mask.logor a (storage_of_flag flag)
| None -> Mask.logand set (Mask.lognot v)
else a
and i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else if Mask.compare a set' = 0
then set'
else a
in
f Mask.zero 0 lowest shifts
let for_all p set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare (Mask.logand set v) Mask.zero = 0 || p (Obj.magic i : Mask.t)
then if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
else true
else false
in
f 0 lowest shifts
let exists p set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare (Mask.logand set v) Mask.zero = 0 || not (p (Obj.magic i : Mask.t))
then if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
else false
else true
in
f 0 lowest shifts
let filter p set =
let set = Mask.logand set Mask.mask
in
let rec f a i v s =
let a =
if Mask.compare (Mask.logand v set) Mask.zero <> 0 && p (Obj.magic i : Mask.t)
then Mask.logor a v
else a
in
if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else a
in
f Mask.zero 0 lowest shifts
let partition p set =
let set = Mask.logand set Mask.mask
in
let rec f ((l, r) as a) i v s =
let a =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then if p (Obj.magic i : Mask.t)
then (Mask.logor l v, r)
else (l, Mask.logor r v)
else a
in
if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else a
in
f (Mask.zero, Mask.zero) 0 lowest shifts
let cardinal set =
let set = Mask.logand set Mask.mask
in
let rec f a i v =
let a =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then succ a
else a
in
if Mask.compare v highest = 0
then a
else f a (succ i) (Mask.shift_left v 1)
in
f 0 0 lowest
let elements set =
let set = Mask.logand set Mask.mask
in
let rec f a i v s =
if Mask.compare v Mask.zero <> 0
then let a =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then (Obj.magic i : Mask.t)::a
else a
and i = pred i
in
let (shift, s) = deltaShiftInv i s
in
f a i (Mask.shift_right_logical v shift) s
else a
in
f [] topbit highest shiftsInv
let min_elt set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then (Obj.magic i : Mask.t)
else if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
else raise Not_found
in
f 0 lowest shifts
let min_elt_opt set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then Some (Obj.magic i : Mask.t)
else if Mask.compare v highest = 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f i (Mask.shift_left v shift) s
else None
in
f 0 lowest shifts
let max_elt set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare v Mask.zero <> 0
then if Mask.compare (Mask.logand v set) Mask.zero <> 0
then (Obj.magic i : Mask.t)
else let i = pred i
in
let (shift, s) = deltaShiftInv i s
in
f i (Mask.shift_right_logical v shift) s
else raise Not_found
in
f topbit highest shiftsInv
let max_elt_opt set =
let set = Mask.logand set Mask.mask
in
let rec f i v s =
if Mask.compare v Mask.zero <> 0
then if Mask.compare (Mask.logand v set) Mask.zero <> 0
then Some (Obj.magic i : Mask.t)
else let i = pred i
in
let (shift, s) = deltaShiftInv i s
in
f i (Mask.shift_right_logical v shift) s
else None
in
f topbit highest shiftsInv
let choose = min_elt
let choose_opt = min_elt_opt
let split (flag : Mask.t) set =
let flag = (Obj.magic flag : int)
and set = Mask.logand set Mask.mask
in
let rec f ((l, p, r) as a) i v s =
let a =
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then let c = Stdlib.compare i flag
in
if c = 0
then (l, true, r)
else if c < 0
then (Mask.logor v l, p, r)
else (l, p, Mask.logor v r)
else a
in
if Mask.compare v highest <> 0
then let i = succ i
in
let (shift, s) = deltaShift i s
in
f a i (Mask.shift_left v shift) s
else a
in
f (Mask.zero, false, Mask.zero) 0 lowest shifts
let to_seq_from x set =
let set = Mask.logand set Mask.mask
and x = (Obj.magic x : int)
in
let rec f i v s () =
let tail =
if Mask.compare v highest = 0
then Seq.empty
else let j = succ i
in
let (shift, s) = deltaShift j s
in
f j (Mask.shift_left v shift) s
in
if i >= x && Mask.compare (Mask.logand v set) Mask.zero <> 0
then Seq.Cons((Obj.magic i : Mask.t), tail)
else tail ()
in
f 0 lowest shifts
let to_seq set = to_seq_from (Obj.magic 0 : Mask.t) set
let to_rev_seq set =
let set = Mask.logand set Mask.mask
in
let rec f i v s () =
let tail =
if Mask.compare v Mask.zero = 0
then Seq.empty
else let j = pred i
in
let (shift, s) = deltaShiftInv j s
in
f j (Mask.shift_right_logical v shift) s
in
if Mask.compare (Mask.logand v set) Mask.zero <> 0
then Seq.Cons((Obj.magic i : Mask.t), tail)
else tail ()
in
f topbit highest shiftsInv
let add_seq s set = Seq.fold_left (fun set flag -> add flag set) set s
let of_seq s = add_seq s empty
end