1. Sébastien Ferré
  2. ocaml-lib

Source

ocaml-lib / intset.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

(** {1 Optimized integer sets} *)

module type T =
  sig
    type t
    val empty : t
    val is_empty : t -> bool
    val cardinal : t -> int
    val mem : int -> t -> bool
    val singleton : int -> t
    val add : int -> t -> t
    val remove : int -> t -> t
    val subset : t -> t -> bool
    val union : t -> t -> t
    val inter : t -> t -> t
    val diff : t -> t -> t
    val union_r : t list -> t
    val inter_r : t list -> t
    val fold : ('a -> int -> 'a) -> 'a -> t -> 'a
    val iter : (int -> unit) -> t -> unit
    val map : (int -> 'a) -> t -> 'a list
    val filter : (int -> bool) -> t -> t
    val elements : t -> int LSet.t
    val memory_size : t -> int (* in words *)
  end

module type T_BOUNDED =
  sig
    val bound : int
    include T
  end

module Cis : T =
(* Cis implementation of extents *)
  struct
    type t = Cis.t
    let empty = Cis.empty
    let is_empty = Cis.is_empty
    let cardinal = Cis.cardinal
    let mem = Cis.mem
    let singleton = Cis.singleton
    let add = Cis.add
    let remove = Cis.remove
    let subset = Cis.subset
    let union = Cis.union
    let inter = Cis.inter
    let diff = Cis.diff
    let union_r l = List.fold_left (fun res set -> union res set) empty l
    let inter_r = function
      | [] -> raise (Invalid_argument "Node.Ext.inter_r : empty list of sets")
      | set::sets -> List.fold_right (fun set res -> inter set res) sets set
    let fold = Cis.fold_left
    let iter = Cis.iter
    let map f ext = Cis.fold_left (fun res x -> f x::res) [] ext
    let filter p ext = List.fold_left (fun res x -> if p x then Cis.add x res else res) Cis.empty (Cis.elements ext)
    let elements = Cis.elements
    let memory_size = Cis.memory_size
  end

module LSet : T =
(* LSet implementation of extents *)
  struct
    type t = int LSet.t
    let empty = LSet.empty ()
    let is_empty = LSet.is_empty
    let cardinal = LSet.cardinal
    let mem = LSet.mem
    let singleton = LSet.singleton
    let add = LSet.add
    let remove = LSet.remove
    let subset = LSet.subset
    let union = LSet.union
    let inter = LSet.inter
    let diff = LSet.diff
    let union_r = LSet.union_r
    let inter_r = LSet.inter_r
    let fold = List.fold_left
    let iter = List.iter
    let map = List.map
    let filter = List.filter
    let elements l = l
    let memory_size l = 1 + (4 * LSet.cardinal l)
  end

module Bitmap31 : T_BOUNDED = (* intsets on [0..31[ *)
  struct
    let bound = 31

    type t = int

    let card_byte =
      let t = Array.make 256 0 in
      for i = 0 to 255 do
	for j = 0 to 7 do
	  if (i lsr j) land 1 <> 0
	  then t.(i) <- t.(i) + 1
	done
      done;
      t

    let empty = 0

    let is_empty i = i = 0

    let cardinal i =
      card_byte.(i land 0xFF)
	+ card_byte.((i lsr 8) land 0xFF)
	+ card_byte.((i lsr 16) land 0xFF)
	+ card_byte.((i lsr 24) land 0xFF)

    let mem x i =
      (i lsr x) land 1 <> 0

    let singleton x =
      assert (x < bound);
      (1 lsl x)

    let add x i =
      assert (x < bound);
      i lor (1 lsl x)

    let remove x i =
      i land ((-1) - (1 lsl x))

    let subset i1 i2 =  i1 land (lnot i2) = 0

    let union i1 i2 = i1 lor i2

    let inter i1 i2 = i1 land i2

    let diff i1 i2 = i1 land (lnot i2)

    let union_r l = List.fold_left (fun res set -> union res set) empty l

    let inter_r = function
      | [] -> raise (Invalid_argument "Intset.Bitmap31.inter_r : empty list of sets")
      | set::sets -> List.fold_right (fun set res -> inter set res) sets set

    let fold f init i =
      let res = ref init in
      for x = 0 to 31 - 1 do
	if mem x i then
	  res := f !res x
      done;
      !res

    let iter f i =
      for x = 0 to 31 - 1 do
	if mem x i then
	  f x
      done

    let map f e = fold (fun res x -> f x::res) [] e
	
    let filter p i =
      let res = ref i in
      for x = 0 to 31 - 1 do
	if mem x !res && not (p x)
	then res := remove x !res
      done;
      !res

    let elements e =
      List.rev (fold (fun res x -> x::res) [] e)

    let memory_size e = 1
  end


module Bitmap (X : T_BOUNDED) : T_BOUNDED =
  struct
    let bound = 31 * X.bound

    type t = Obj.t
       (* empty set : negative integer *)
       (* singleton : non-negative integer representing unique element *)
       (* other sets : blocks *)
	  (* field 0 is bitmap of non-empty subsets *)
	  (* each 1-bit of field 0 refers to a subset *)
	  (* the number of 1-bits in 'e.(0) lsr (x/X.bound)' gives the field containing the bit x *)

    type kind = Empty | Single of int | Other of Obj.t (* block *)

    let kind e =
      if Obj.is_int e
      then
	let i = (Obj.obj e : int) in
	if i < 0 then Empty else Single i
      else
	Other e

    (* computing efficiently number of 1-bits in bytes and words *)

    let card_byte =
      let t = Array.make 256 0 in
      for i = 0 to 255 do
	for j = 0 to 7 do
	  if (i lsr j) land 1 <> 0
	  then t.(i) <- t.(i) + 1
	done
      done;
      t

    let cardinal_word i =
      card_byte.(i land 0xFF)
	+ card_byte.((i lsr 8) land 0xFF)
	+ card_byte.((i lsr 16) land 0xFF)
	+ card_byte.((i lsr 24) land 0xFF)

    (* low level access to representations as arrays *)
	
    let make m n = (* m is the initial mask, and n the number of fields, comprising the mask *)
      assert (n > 0);
      let e = Obj.new_block 0 n in
      Obj.set_field e 0 (Obj.repr m);
      e

    let copy e = Obj.dup e

    let sub e pos len =
      let e' = Obj.new_block 0 len in
      for i = 0 to len - 1 do
	Obj.set_field e' i (Obj.field e (pos + i))
      done;
      e'

    let length e = Obj.size e

    let get_mask e = (Obj.obj (Obj.field e 0) : int)

    let set_mask e m = Obj.set_field e 0 (Obj.repr m)

    let get_field e i = (Obj.obj (Obj.field e i) : X.t)

    let set_field e i s = Obj.set_field e i (Obj.repr s)


    let get_i x1 e =
      let b = (get_mask e) lsr x1 in
      assert (b land 1 <> 0);
      cardinal_word b

    let get_subset x1 e =
      let b = (get_mask e) lsr x1 in
      if b land 1 = 0
      then X.empty
      else get_field e (cardinal_word b)

    let get_index x e = (* returns coordinates of bit x in e *)
      let x1, x2 = x / X.bound, x mod X.bound in
      let b = (get_mask e) lsr x1 in
      if b land 1 = 0
      then (cardinal_word b + 1, x1, x2, false)
      else (cardinal_word b, x1, x2, true)
      
    (* bitwise operations *)

    let test i x2 =
      (i lsr x2) land 1 <> 0

    let set i x2 =
      i lor (1 lsl x2)

    let reset i x2 =
      i land ((-1) - (1 lsl x2))

    (* intset interface *)

    let empty = Obj.repr (-1)

    let is_empty e =
      match kind e with
      | Empty -> true
      | Single _ -> false
      | Other e -> get_mask e = 0

    let cardinal e =
      match kind e with
      | Empty -> 0
      | Single _ -> 1
      | Other e ->
	  let n = length e in
	  let res = ref 0 in
	  for i = 1 to n-1 do
	    res := !res + X.cardinal (get_field e i)
	  done;
	  !res

    let mem x e =
      match kind e with
      | Empty -> false
      | Single y -> x = y
      | Other e ->
	  let i, _, x2, present = get_index x e in
	  present && X.mem x2 (get_field e i)

    let singleton x =
      assert (x < bound);
      Obj.repr x

    let add x e =
      assert (x < bound);
      match kind e with
      | Empty -> singleton x
      | Single y ->
	  let x1, x2 = x / X.bound, x mod X.bound in
	  let y1, y2 = y / X.bound, y mod X.bound in
	  if x1 = y1
	  then begin
	    let e = make (1 lsl x1) 2 in
	    set_field e 1 (X.add x2 (X.singleton y2));
	    e end
	  else begin
	    let e = make ((1 lsl x1) lor (1 lsl y1)) 3 in
	    let ix, iy = if x1 < y1 then 2, 1 else 1, 2 in
	    set_field e ix (X.singleton x2);
	    set_field e iy (X.singleton y2);
	    e
	  end
      | Other e ->
	  let i, x1, x2, present = get_index x e in
	  if present
	  then
	    if X.mem x2 (get_field e i)
	    then e
	    else begin
	      let e' = copy e in (* unsafe not to make a copy *)
	      set_field e' i (X.add x2 (get_field e' i));
	      e'
	    end
	  else begin
	    let n = length e in
	    let e' = make (set (get_mask e) x1) (n+1) in
	    for k = 1 to i-1 do
	      Obj.set_field e' k (Obj.field e k)
	      (* set_field e' k (get_field e k) *)
	    done;
	    for k = n downto i+1 do
	      Obj.set_field e' k (Obj.field e (k-1))
	      (* set_field e' k (get_field e (k-1)) *)
	    done;
	    set_field e' i (X.singleton x2);
	    e' end

    let remove x e =
      match kind e with
      | Empty -> e
      | Single y ->
	  if x = y
	  then empty
	  else e
      | Other e ->
	  let i, x1, x2, present = get_index x e in
	  if present
	  then
	    if X.mem x2 (get_field e i)
	    then
	      let e'_i = X.remove x2 (get_field e i) in
	      if X.is_empty e'_i
	      then begin
		let n = length e in
		let e' = make (reset (get_mask e) x1) (n-1) in
		for k = 1 to i-1 do
		  Obj.set_field e' k (Obj.field e k)
		  (* set_field e' k (get_field e k) *)
		done;
		for k = i to n-2 do
		  Obj.set_field e' k (Obj.field e (k+1))
		  (* set_field e' k (get_field e (k+1)) *)
		done;
		e' end
	      else begin
		let e' = copy e in
		set_field e' i e'_i;
		e'
	      end
	    else e
	  else e

    let subset e1 e2 =
      let subset_word i1 i2 = i1 land (lnot i2) = 0 in
      match kind e1, kind e2 with
      | Empty, _ -> true
      | _, Empty -> false
      | Single x1, _ -> mem x1 e2
      | Other e1, Single y ->
	  let y1, y2 = y / X.bound, y mod X.bound in
	  if subset_word (get_mask e1) (1 lsl y1)
	  then X.subset (get_subset y1 e1) (X.singleton y2)
	  else false
      | Other e1, Other e2 ->
	  if not (subset_word (get_mask e1) (get_mask e2))
	  then false
	  else begin
	    let res = ref true in
	    let x1 = ref 0 in
	    while !res && !x1 < 31 do
	      res := test (get_mask e1) !x1 && X.subset (get_subset !x1 e1) (get_subset !x1 e2);
	      incr x1
	    done;
	    !res
	  end

    let union e1 e2 =
      let union_word i1 i2 = i1 lor i2 in
      match kind e1, kind e2 with
      | Empty, _ -> e2
      | _, Empty -> e1
      | Single x, _ -> add x e2
      | _, Single y -> add y e1
      | Other e1, Other e2 ->
	  let e_mask = union_word (get_mask e1) (get_mask e2) in
	  let n = cardinal_word e_mask in
	  let e = make e_mask (n+1) in
	  for x1 = 0 to 31 - 1 do
	    if test (get_mask e) x1
	    then set_field e (get_i x1 e) (X.union (get_subset x1 e1) (get_subset x1 e2))
	  done;
	  e

    let inter e1 e2 =
      let inter_word i1 i2 = i1 land i2 in
      match kind e1, kind e2 with
      | Empty, _ -> e1
      | _, Empty -> e2
      | Single x, _ -> if mem x e2 then e1 else empty
      | _, Single y -> if mem y e1 then e2 else empty
      | Other e1, Other e2 ->
	  let e_mask = inter_word (get_mask e1) (get_mask e2) in
	  let n = cardinal_word e_mask in
	  let e = make e_mask (n+1) in
	  for x1 = 31 - 1 downto 0 do
	    if test (get_mask e) x1
	    then
	      let e_x1 = X.inter (get_subset x1 e1) (get_subset x1 e2) in
	      if X.is_empty e_x1
	      then set_mask e (reset (get_mask e) x1)
	      else set_field e (get_i x1 e) e_x1
	  done;
	  sub e 0 (1 + cardinal_word (get_mask e)) (* for removing extra words *)

    let diff e1 e2 =
(*      let diff_word i1 i2 = i1 land (lnot i2) in *)
      match kind e1, kind e2 with
      | Empty, _ -> e1
      | _, Empty -> e1
      | Single x, _ -> if mem x e2 then empty else e1
      | _, Single y -> remove y e1
      | Other e1, Other e2 ->
	  let e_mask = get_mask e1 in
	  let n = cardinal_word e_mask in
	  let e = make e_mask (n+1) in
	  for x1 = 31 - 1 downto 0 do
	    if test (get_mask e) x1
	    then
	      let e_x1 = X.diff (get_subset x1 e1) (get_subset x1 e2) in
	      if X.is_empty e_x1
	      then set_mask e (reset (get_mask e) x1)
	      else set_field e (get_i x1 e) e_x1
	  done;
	  sub e 0 (1 + cardinal_word (get_mask e)) (* for removing extra words *)

    let union_r l = List.fold_left (fun res set -> union res set) empty l

    let inter_r = function
      | [] -> raise (Invalid_argument "Intset.Bitmap961.inter_r : empty list of sets")
      | set::sets -> List.fold_right (fun set res -> inter set res) sets set

    let fold f init e =
      match kind e with
      | Empty -> init
      | Single x -> f init x
      | Other e ->
	  let e_mask = get_mask e in
	  let res = ref init in
	  for x1 = 0 to 31 - 1 do
	    if test e_mask x1 then begin
	      let i = get_subset x1 e in
	      let x0 = X.bound * x1 in
	      res := X.fold (fun res2 x2 -> f res2 (x0 + x2)) !res i
	    end
	  done;
	  !res
	
    let iter f e =
      match kind e with
      | Empty -> ()
      | Single x -> f x
      | Other e ->
	  let e_mask = get_mask e in
	  for x1 = 0 to 31 - 1 do
	    if test e_mask x1 then begin
	      let i = get_subset x1 e in
	      let x0 = X.bound * x1 in
	      X.iter (fun x2 -> f (x0 + x2)) i
	    end
	  done

    let map f e = fold (fun res x -> f x::res) [] e

    let filter p e1 =
      match kind e1 with
      | Empty -> e1
      | Single x -> if p x then e1 else empty
      | Other e1 ->
	  let e_mask = get_mask e1 in
	  let n = cardinal_word e_mask in
	  let e = make e_mask (n+1) in
	  for x1 = 31 - 1 downto 0 do
	    if test (get_mask e) x1
	    then
	      let x0 = X.bound * x1 in
	      let e_x1 = X.filter (fun x2 -> p (x0 + x2)) (get_subset x1 e1) in
	      if X.is_empty e_x1
	      then set_mask e (reset (get_mask e) x1)
	      else set_field e (get_i x1 e) e_x1
	  done;
	  sub e 0 (1 + cardinal_word (get_mask e)) (* for removing extra words *)

    let elements e =
      List.rev (fold (fun res x -> x::res) [] e)

    let memory_size e =
      match kind e with
      | Empty -> 1
      | Single _ -> 1
      | Other e ->
	  let n = length e in
	  let res = ref (1 + n) in
	  for i = 1 to n-1 do
	    res := !res + X.memory_size (get_field e i)
	  done;
	  !res
  end