summary refs log tree commit diff stats
path: root/lib/pure/collections/intsets.nim
blob: 91b6b55e87288bf62d8d5b06318fe661f016ef00 (plain) (blame)
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
#
#
#            Nim's Runtime Library
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## The ``intsets`` module implements an efficient `int` set implemented as a
## `sparse bit set`:idx:.
##
## **Note**: Currently the assignment operator ``=`` for ``IntSet``
## performs some rather meaningless shallow copy. Since Nim currently does
## not allow the assignment operator to be overloaded, use `assign proc
## <#assign,IntSet,IntSet>`_ to get a deep copy.
##
## **See also:**
## * `sets module <sets.html>`_ for more general hash sets


import
  hashes, math

type
  BitScalar = uint

const
  InitIntSetSize = 8              # must be a power of two!
  TrunkShift = 9
  BitsPerTrunk = 1 shl TrunkShift # needs to be a power of 2 and
                                  # divisible by 64
  TrunkMask = BitsPerTrunk - 1
  IntsPerTrunk = BitsPerTrunk div (sizeof(BitScalar) * 8)
  IntShift = 5 + ord(sizeof(BitScalar) == 8) # 5 or 6, depending on int width
  IntMask = 1 shl IntShift - 1

type
  PTrunk = ref Trunk
  Trunk = object
    next: PTrunk                                # all nodes are connected with this pointer
    key: int                                    # start address at bit 0
    bits: array[0..IntsPerTrunk - 1, BitScalar] # a bit vector

  TrunkSeq = seq[PTrunk]
  IntSet* = object       ## An efficient set of `int` implemented as a sparse bit set.
    elems: int           # only valid for small numbers
    counter, max: int
    head: PTrunk
    data: TrunkSeq
    a: array[0..33, int] # profiling shows that 34 elements are enough

proc mustRehash(length, counter: int): bool {.inline.} =
  assert(length > counter)
  result = (length * 2 < counter * 3) or (length - counter < 4)

proc nextTry(h, maxHash: Hash): Hash {.inline.} =
  result = ((5 * h) + 1) and maxHash

proc intSetGet(t: IntSet, key: int): PTrunk =
  var h = key and t.max
  while t.data[h] != nil:
    if t.data[h].key == key:
      return t.data[h]
    h = nextTry(h, t.max)
  result = nil

proc intSetRawInsert(t: IntSet, data: var TrunkSeq, desc: PTrunk) =
  var h = desc.key and t.max
  while data[h] != nil:
    assert(data[h] != desc)
    h = nextTry(h, t.max)
  assert(data[h] == nil)
  data[h] = desc

proc intSetEnlarge(t: var IntSet) =
  var n: TrunkSeq
  var oldMax = t.max
  t.max = ((t.max + 1) * 2) - 1
  newSeq(n, t.max + 1)
  for i in countup(0, oldMax):
    if t.data[i] != nil: intSetRawInsert(t, n, t.data[i])
  swap(t.data, n)

proc intSetPut(t: var IntSet, key: int): PTrunk =
  var h = key and t.max
  while t.data[h] != nil:
    if t.data[h].key == key:
      return t.data[h]
    h = nextTry(h, t.max)
  if mustRehash(t.max + 1, t.counter): intSetEnlarge(t)
  inc(t.counter)
  h = key and t.max
  while t.data[h] != nil: h = nextTry(h, t.max)
  assert(t.data[h] == nil)
  new(result)
  result.next = t.head
  result.key = key
  t.head = result
  t.data[h] = result

proc bitincl(s: var IntSet, key: int) {.inline.} =
  var t = intSetPut(s, `shr`(key, TrunkShift))
  var u = key and TrunkMask
  t.bits[u shr IntShift] = t.bits[u shr IntShift] or
      (BitScalar(1) shl (u and IntMask))

proc exclImpl(s: var IntSet, key: int) =
  if s.elems <= s.a.len:
    for i in 0..<s.elems:
      if s.a[i] == key:
        s.a[i] = s.a[s.elems-1]
        dec s.elems
        return
  else:
    var t = intSetGet(s, key shr TrunkShift)
    if t != nil:
      var u = key and TrunkMask
      t.bits[u shr IntShift] = t.bits[u shr IntShift] and
          not(BitScalar(1) shl (u and IntMask))

template dollarImpl(): untyped =
  result = "{"
  for key in items(s):
    if result.len > 1: result.add(", ")
    result.add($key)
  result.add("}")


iterator items*(s: IntSet): int {.inline.} =
  ## Iterates over any included element of `s`.
  if s.elems <= s.a.len:
    for i in 0..<s.elems:
      yield s.a[i]
  else:
    var r = s.head
    while r != nil:
      var i = 0
      while i <= high(r.bits):
        var w: uint = r.bits[i]
        # taking a copy of r.bits[i] here is correct, because
        # modifying operations are not allowed during traversation
        var j = 0
        while w != 0: # test all remaining bits for zero
          if (w and 1) != 0: # the bit is set!
            yield (r.key shl TrunkShift) or (i shl IntShift +% j)
          inc(j)
          w = w shr 1
        inc(i)
      r = r.next


proc initIntSet*: IntSet =
  ## Returns an empty IntSet.
  runnableExamples:
    var a = initIntSet()
    assert len(a) == 0

  # newSeq(result.data, InitIntSetSize)
  # result.max = InitIntSetSize-1
  result = IntSet(
    elems: 0,
    counter: 0,
    max: 0,
    head: nil,
    data: when defined(nimNoNilSeqs): @[] else: nil)
  #  a: array[0..33, int] # profiling shows that 34 elements are enough

proc contains*(s: IntSet, key: int): bool =
  ## Returns true if `key` is in `s`.
  ##
  ## This allows the usage of `in` operator.
  runnableExamples:
    var a = initIntSet()
    for x in [1, 3, 5]:
      a.incl(x)
    assert a.contains(3)
    assert 3 in a
    assert(not a.contains(8))
    assert 8 notin a

  if s.elems <= s.a.len:
    for i in 0..<s.elems:
      if s.a[i] == key: return true
  else:
    var t = intSetGet(s, `shr`(key, TrunkShift))
    if t != nil:
      var u = key and TrunkMask
      result = (t.bits[u shr IntShift] and
                (BitScalar(1) shl (u and IntMask))) != 0
    else:
      result = false

proc incl*(s: var IntSet, key: int) =
  ## Includes an element `key` in `s`.
  ##
  ## This doesn't do anything if `key` is already in `s`.
  ##
  ## See also:
  ## * `excl proc <#excl,IntSet,int>`_ for excluding an element
  ## * `incl proc <#incl,IntSet,IntSet>`_ for including other set
  ## * `containsOrIncl proc <#containsOrIncl,IntSet,int>`_
  runnableExamples:
    var a = initIntSet()
    a.incl(3)
    a.incl(3)
    assert len(a) == 1

  if s.elems <= s.a.len:
    for i in 0..<s.elems:
      if s.a[i] == key: return
    if s.elems < s.a.len:
      s.a[s.elems] = key
      inc s.elems
      return
    newSeq(s.data, InitIntSetSize)
    s.max = InitIntSetSize-1
    for i in 0..<s.elems:
      bitincl(s, s.a[i])
    s.elems = s.a.len + 1
    # fall through:
  bitincl(s, key)

proc incl*(s: var IntSet, other: IntSet) =
  ## Includes all elements from `other` into `s`.
  ##
  ## This is the in-place version of `s + other <#+,IntSet,IntSet>`_.
  ##
  ## See also:
  ## * `excl proc <#excl,IntSet,IntSet>`_ for excluding other set
  ## * `incl proc <#incl,IntSet,int>`_ for including an element
  ## * `containsOrIncl proc <#containsOrIncl,IntSet,int>`_
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1)
    b.incl(5)
    a.incl(b)
    assert len(a) == 2
    assert 5 in a

  for item in other: incl(s, item)

proc containsOrIncl*(s: var IntSet, key: int): bool =
  ## Includes `key` in the set `s` and tells if `key` was already in `s`.
  ##
  ## The difference with regards to the `incl proc <#incl,IntSet,int>`_ is
  ## that this proc returns `true` if `s` already contained `key`. The
  ## proc will return `false` if `key` was added as a new value to `s` during
  ## this call.
  ##
  ## See also:
  ## * `incl proc <#incl,IntSet,int>`_ for including an element
  ## * `missingOrExcl proc <#missingOrExcl,IntSet,int>`_
  runnableExamples:
    var a = initIntSet()
    assert a.containsOrIncl(3) == false
    assert a.containsOrIncl(3) == true
    assert a.containsOrIncl(4) == false

  if s.elems <= s.a.len:
    for i in 0..<s.elems:
      if s.a[i] == key:
        return true
    incl(s, key)
    result = false
  else:
    var t = intSetGet(s, `shr`(key, TrunkShift))
    if t != nil:
      var u = key and TrunkMask
      result = (t.bits[u shr IntShift] and BitScalar(1) shl (u and IntMask)) != 0
      if not result:
        t.bits[u shr IntShift] = t.bits[u shr IntShift] or
            (BitScalar(1) shl (u and IntMask))
    else:
      incl(s, key)
      result = false

proc excl*(s: var IntSet, key: int) =
  ## Excludes `key` from the set `s`.
  ##
  ## This doesn't do anything if `key` is not found in `s`.
  ##
  ## See also:
  ## * `incl proc <#incl,IntSet,int>`_ for including an element
  ## * `excl proc <#excl,IntSet,IntSet>`_ for excluding other set
  ## * `missingOrExcl proc <#missingOrExcl,IntSet,int>`_
  runnableExamples:
    var a = initIntSet()
    a.incl(3)
    a.excl(3)
    a.excl(3)
    a.excl(99)
    assert len(a) == 0
  exclImpl(s, key)

proc excl*(s: var IntSet, other: IntSet) =
  ## Excludes all elements from `other` from `s`.
  ##
  ## This is the in-place version of `s - other <#-,IntSet,IntSet>`_.
  ##
  ## See also:
  ## * `incl proc <#incl,IntSet,IntSet>`_ for including other set
  ## * `excl proc <#excl,IntSet,int>`_ for excluding an element
  ## * `missingOrExcl proc <#missingOrExcl,IntSet,int>`_
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1)
    a.incl(5)
    b.incl(5)
    a.excl(b)
    assert len(a) == 1
    assert 5 notin a

  for item in other: excl(s, item)

proc missingOrExcl*(s: var IntSet, key: int): bool =
  ## Excludes `key` in the set `s` and tells if `key` was already missing from `s`.
  ##
  ## The difference with regards to the `excl proc <#excl,IntSet,int>`_ is
  ## that this proc returns `true` if `key` was missing from `s`.
  ## The proc will return `false` if `key` was in `s` and it was removed
  ## during this call.
  ##
  ## See also:
  ## * `excl proc <#excl,IntSet,int>`_ for excluding an element
  ## * `excl proc <#excl,IntSet,IntSet>`_ for excluding other set
  ## * `containsOrIncl proc <#containsOrIncl,IntSet,int>`_
  runnableExamples:
    var a = initIntSet()
    a.incl(5)
    assert a.missingOrExcl(5) == false
    assert a.missingOrExcl(5) == true

  var count = s.elems
  exclImpl(s, key)
  result = count == s.elems

proc clear*(result: var IntSet) =
  ## Clears the IntSet back to an empty state.
  runnableExamples:
    var a = initIntSet()
    a.incl(5)
    a.incl(7)
    clear(a)
    assert len(a) == 0

  # setLen(result.data, InitIntSetSize)
  # for i in 0..InitIntSetSize-1: result.data[i] = nil
  # result.max = InitIntSetSize-1
  when defined(nimNoNilSeqs):
    result.data = @[]
  else:
    result.data = nil
  result.max = 0
  result.counter = 0
  result.head = nil
  result.elems = 0

proc isNil*(x: IntSet): bool {.inline.} = x.head.isNil and x.elems == 0

proc assign*(dest: var IntSet, src: IntSet) =
  ## Copies `src` to `dest`.
  ## `dest` does not need to be initialized by `initIntSet proc <#initIntSet>`_.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    b.incl(5)
    b.incl(7)
    a.assign(b)
    assert len(a) == 2

  if src.elems <= src.a.len:
    when defined(nimNoNilSeqs):
      dest.data = @[]
    else:
      dest.data = nil
    dest.max = 0
    dest.counter = src.counter
    dest.head = nil
    dest.elems = src.elems
    dest.a = src.a
  else:
    dest.counter = src.counter
    dest.max = src.max
    newSeq(dest.data, src.data.len)

    var it = src.head
    while it != nil:
      var h = it.key and dest.max
      while dest.data[h] != nil: h = nextTry(h, dest.max)
      assert(dest.data[h] == nil)
      var n: PTrunk
      new(n)
      n.next = dest.head
      n.key = it.key
      n.bits = it.bits
      dest.head = n
      dest.data[h] = n
      it = it.next

proc union*(s1, s2: IntSet): IntSet =
  ## Returns the union of the sets `s1` and `s2`.
  ##
  ## The same as `s1 + s2 <#+,IntSet,IntSet>`_.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1); a.incl(2); a.incl(3)
    b.incl(3); b.incl(4); b.incl(5)
    assert union(a, b).len == 5
    ## {1, 2, 3, 4, 5}

  result.assign(s1)
  incl(result, s2)

proc intersection*(s1, s2: IntSet): IntSet =
  ## Returns the intersection of the sets `s1` and `s2`.
  ##
  ## The same as `s1 * s2 <#*,IntSet,IntSet>`_.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1); a.incl(2); a.incl(3)
    b.incl(3); b.incl(4); b.incl(5)
    assert intersection(a, b).len == 1
    ## {3}

  result = initIntSet()
  for item in s1:
    if contains(s2, item):
      incl(result, item)

proc difference*(s1, s2: IntSet): IntSet =
  ## Returns the difference of the sets `s1` and `s2`.
  ##
  ## The same as `s1 - s2 <#-,IntSet,IntSet>`_.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1); a.incl(2); a.incl(3)
    b.incl(3); b.incl(4); b.incl(5)
    assert difference(a, b).len == 2
    ## {1, 2}

  result = initIntSet()
  for item in s1:
    if not contains(s2, item):
      incl(result, item)

proc symmetricDifference*(s1, s2: IntSet): IntSet =
  ## Returns the symmetric difference of the sets `s1` and `s2`.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1); a.incl(2); a.incl(3)
    b.incl(3); b.incl(4); b.incl(5)
    assert symmetricDifference(a, b).len == 4
    ## {1, 2, 4, 5}

  result.assign(s1)
  for item in s2:
    if containsOrIncl(result, item): excl(result, item)

proc `+`*(s1, s2: IntSet): IntSet {.inline.} =
  ## Alias for `union(s1, s2) <#union,IntSet,IntSet>`_.
  result = union(s1, s2)

proc `*`*(s1, s2: IntSet): IntSet {.inline.} =
  ## Alias for `intersection(s1, s2) <#intersection,IntSet,IntSet>`_.
  result = intersection(s1, s2)

proc `-`*(s1, s2: IntSet): IntSet {.inline.} =
  ## Alias for `difference(s1, s2) <#difference,IntSet,IntSet>`_.
  result = difference(s1, s2)

proc disjoint*(s1, s2: IntSet): bool =
  ## Returns true if the sets `s1` and `s2` have no items in common.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1); a.incl(2)
    b.incl(2); b.incl(3)
    assert disjoint(a, b) == false
    b.excl(2)
    assert disjoint(a, b) == true

  for item in s1:
    if contains(s2, item):
      return false
  return true

proc len*(s: IntSet): int {.inline.} =
  ## Returns the number of elements in `s`.
  if s.elems < s.a.len:
    result = s.elems
  else:
    result = 0
    for _ in s:
      inc(result)

proc card*(s: IntSet): int {.inline.} =
  ## Alias for `len() <#len,IntSet>`_.
  result = s.len()

proc `<=`*(s1, s2: IntSet): bool =
  ## Returns true if `s1` is subset of `s2`.
  ##
  ## A subset `s1` has all of its elements in `s2`, and `s2` doesn't necessarily
  ## have more elements than `s1`. That is, `s1` can be equal to `s2`.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1)
    b.incl(1); b.incl(2)
    assert a <= b
    a.incl(2)
    assert a <= b
    a.incl(3)
    assert(not (a <= b))

  for item in s1:
    if not s2.contains(item):
      return false
  return true

proc `<`*(s1, s2: IntSet): bool =
  ## Returns true if `s1` is proper subset of `s2`.
  ##
  ## A strict or proper subset `s1` has all of its elements in `s2`, but `s2` has
  ## more elements than `s1`.
  runnableExamples:
    var
      a = initIntSet()
      b = initIntSet()
    a.incl(1)
    b.incl(1); b.incl(2)
    assert a < b
    a.incl(2)
    assert(not (a < b))
  return s1 <= s2 and not (s2 <= s1)

proc `==`*(s1, s2: IntSet): bool =
  ## Returns true if both `s1` and `s2` have the same elements and set size.
  return s1 <= s2 and s2 <= s1

proc `$`*(s: IntSet): string =
  ## The `$` operator for int sets.
  ##
  ## Converts the set `s` to a string, mostly for logging and printing purposes.
  dollarImpl()



when isMainModule:
  import sequtils, algorithm

  var x = initIntSet()
  x.incl(1)
  x.incl(2)
  x.incl(7)
  x.incl(1056)

  x.incl(1044)
  x.excl(1044)

  assert x.containsOrIncl(888) == false
  assert 888 in x
  assert x.containsOrIncl(888) == true

  assert x.missingOrExcl(888) == false
  assert 888 notin x
  assert x.missingOrExcl(888) == true

  var xs = toSeq(items(x))
  xs.sort(cmp[int])
  assert xs == @[1, 2, 7, 1056]

  var y: IntSet
  assign(y, x)
  var ys = toSeq(items(y))
  ys.sort(cmp[int])
  assert ys == @[1, 2, 7, 1056]

  assert x == y

  var z: IntSet
  for i in 0..1000:
    incl z, i
    assert z.len() == i+1
  for i in 0..1000:
    assert z.contains(i)

  var w = initIntSet()
  w.incl(1)
  w.incl(4)
  w.incl(50)
  w.incl(1001)
  w.incl(1056)

  var xuw = x.union(w)
  var xuws = toSeq(items(xuw))
  xuws.sort(cmp[int])
  assert xuws == @[1, 2, 4, 7, 50, 1001, 1056]

  var xiw = x.intersection(w)
  var xiws = toSeq(items(xiw))
  xiws.sort(cmp[int])
  assert xiws == @[1, 1056]

  var xdw = x.difference(w)
  var xdws = toSeq(items(xdw))
  xdws.sort(cmp[int])
  assert xdws == @[2, 7]

  var xsw = x.symmetricDifference(w)
  var xsws = toSeq(items(xsw))
  xsws.sort(cmp[int])
  assert xsws == @[2, 4, 7, 50, 1001]

  x.incl(w)
  xs = toSeq(items(x))
  xs.sort(cmp[int])
  assert xs == @[1, 2, 4, 7, 50, 1001, 1056]

  assert w <= x

  assert w < x

  assert(not disjoint(w, x))

  var u = initIntSet()
  u.incl(3)
  u.incl(5)
  u.incl(500)
  assert disjoint(u, x)

  var v = initIntSet()
  v.incl(2)
  v.incl(50)

  x.excl(v)
  xs = toSeq(items(x))
  xs.sort(cmp[int])
  assert xs == @[1, 4, 7, 1001, 1056]