setimpl.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2019 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # An `include` file for the different hash set implementations.
  10. template maxHash(t): untyped = high(t.data)
  11. template dataLen(t): untyped = len(t.data)
  12. include hashcommon
  13. template initImpl(s: typed, size: int) =
  14. let correctSize = slotsNeeded(size)
  15. when s is OrderedSet:
  16. s.first = -1
  17. s.last = -1
  18. s.counter = 0
  19. newSeq(s.data, correctSize)
  20. template rawInsertImpl() {.dirty.} =
  21. if data.len == 0:
  22. initImpl(s, defaultInitialSize)
  23. data[h].key = key
  24. data[h].hcode = hc
  25. proc rawInsert[A](s: var HashSet[A], data: var KeyValuePairSeq[A], key: A,
  26. hc: Hash, h: Hash) =
  27. rawInsertImpl()
  28. proc enlarge[A](s: var HashSet[A]) =
  29. var n: KeyValuePairSeq[A]
  30. newSeq(n, len(s.data) * growthFactor)
  31. swap(s.data, n) # n is now old seq
  32. for i in countup(0, high(n)):
  33. if isFilled(n[i].hcode):
  34. var j = -1 - rawGetKnownHC(s, n[i].key, n[i].hcode)
  35. rawInsert(s, s.data, n[i].key, n[i].hcode, j)
  36. template inclImpl() {.dirty.} =
  37. if s.data.len == 0:
  38. initImpl(s, defaultInitialSize)
  39. var hc: Hash
  40. var index = rawGet(s, key, hc)
  41. if index < 0:
  42. if mustRehash(s):
  43. enlarge(s)
  44. index = rawGetKnownHC(s, key, hc)
  45. rawInsert(s, s.data, key, hc, -1 - index)
  46. inc(s.counter)
  47. template containsOrInclImpl() {.dirty.} =
  48. if s.data.len == 0:
  49. initImpl(s, defaultInitialSize)
  50. var hc: Hash
  51. var index = rawGet(s, key, hc)
  52. if index >= 0:
  53. result = true
  54. else:
  55. if mustRehash(s):
  56. enlarge(s)
  57. index = rawGetKnownHC(s, key, hc)
  58. rawInsert(s, s.data, key, hc, -1 - index)
  59. inc(s.counter)
  60. template doWhile(a, b) =
  61. while true:
  62. b
  63. if not a: break
  64. proc exclImpl[A](s: var HashSet[A], key: A): bool {.inline.} =
  65. var hc: Hash
  66. var i = rawGet(s, key, hc)
  67. var msk = high(s.data)
  68. result = true
  69. if i >= 0:
  70. result = false
  71. dec(s.counter)
  72. while true: # KnuthV3 Algo6.4R adapted for i=i+1 instead of i=i-1
  73. var j = i # The correctness of this depends on (h+1) in nextTry,
  74. var r = j # though may be adaptable to other simple sequences.
  75. s.data[i].hcode = 0 # mark current EMPTY
  76. s.data[i].key = default(typeof(s.data[i].key))
  77. doWhile((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
  78. i = (i + 1) and msk # increment mod table size
  79. if isEmpty(s.data[i].hcode): # end of collision cluster; So all done
  80. return
  81. r = s.data[i].hcode and msk # "home" location of key@i
  82. s.data[j] = move(s.data[i]) # data[i] will be marked EMPTY next loop
  83. template dollarImpl() {.dirty.} =
  84. result = "{"
  85. for key in items(s):
  86. if result.len > 1: result.add(", ")
  87. result.addQuoted(key)
  88. result.add("}")
  89. # --------------------------- OrderedSet ------------------------------
  90. proc rawGet[A](t: OrderedSet[A], key: A, hc: var Hash): int {.inline.} =
  91. rawGetImpl()
  92. proc rawInsert[A](s: var OrderedSet[A], data: var OrderedKeyValuePairSeq[A],
  93. key: A, hc: Hash, h: Hash) =
  94. rawInsertImpl()
  95. data[h].next = -1
  96. if s.first < 0: s.first = h
  97. if s.last >= 0: data[s.last].next = h
  98. s.last = h
  99. proc enlarge[A](s: var OrderedSet[A]) =
  100. var n: OrderedKeyValuePairSeq[A]
  101. newSeq(n, len(s.data) * growthFactor)
  102. var h = s.first
  103. s.first = -1
  104. s.last = -1
  105. swap(s.data, n)
  106. while h >= 0:
  107. var nxt = n[h].next
  108. if isFilled(n[h].hcode):
  109. var j = -1 - rawGetKnownHC(s, n[h].key, n[h].hcode)
  110. rawInsert(s, s.data, n[h].key, n[h].hcode, j)
  111. h = nxt
  112. proc exclImpl[A](s: var OrderedSet[A], key: A): bool {.inline.} =
  113. if len(s.data) == 0:
  114. return true
  115. var n: OrderedKeyValuePairSeq[A]
  116. newSeq(n, len(s.data))
  117. var h = s.first
  118. s.first = -1
  119. s.last = -1
  120. swap(s.data, n)
  121. let hc = genHash(key)
  122. result = true
  123. while h >= 0:
  124. var nxt = n[h].next
  125. if isFilled(n[h].hcode):
  126. if n[h].hcode == hc and n[h].key == key:
  127. dec s.counter
  128. result = false
  129. else:
  130. var j = -1 - rawGetKnownHC(s, n[h].key, n[h].hcode)
  131. rawInsert(s, s.data, n[h].key, n[h].hcode, j)
  132. h = nxt