setimpl.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. result = false
  56. if mustRehash(s):
  57. enlarge(s)
  58. index = rawGetKnownHC(s, key, hc)
  59. rawInsert(s, s.data, key, hc, -1 - index)
  60. inc(s.counter)
  61. template doWhile(a, b) =
  62. while true:
  63. b
  64. if not a: break
  65. proc exclImpl[A](s: var HashSet[A], key: A): bool {.inline.} =
  66. var hc: Hash
  67. var i = rawGet(s, key, hc)
  68. var msk = high(s.data)
  69. result = true
  70. if i >= 0:
  71. result = false
  72. dec(s.counter)
  73. while true: # KnuthV3 Algo6.4R adapted for i=i+1 instead of i=i-1
  74. var j = i # The correctness of this depends on (h+1) in nextTry,
  75. var r = j # though may be adaptable to other simple sequences.
  76. s.data[i].hcode = 0 # mark current EMPTY
  77. s.data[i].key = default(typeof(s.data[i].key))
  78. doWhile((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
  79. i = (i + 1) and msk # increment mod table size
  80. if isEmpty(s.data[i].hcode): # end of collision cluster; So all done
  81. return
  82. r = s.data[i].hcode and msk # "home" location of key@i
  83. s.data[j] = move(s.data[i]) # data[i] will be marked EMPTY next loop
  84. template dollarImpl() {.dirty.} =
  85. result = "{"
  86. for key in items(s):
  87. if result.len > 1: result.add(", ")
  88. result.addQuoted(key)
  89. result.add("}")
  90. # --------------------------- OrderedSet ------------------------------
  91. proc rawGet[A](t: OrderedSet[A], key: A, hc: var Hash): int {.inline.} =
  92. rawGetImpl()
  93. proc rawInsert[A](s: var OrderedSet[A], data: var OrderedKeyValuePairSeq[A],
  94. key: A, hc: Hash, h: Hash) =
  95. rawInsertImpl()
  96. data[h].next = -1
  97. if s.first < 0: s.first = h
  98. if s.last >= 0: data[s.last].next = h
  99. s.last = h
  100. proc enlarge[A](s: var OrderedSet[A]) =
  101. var n: OrderedKeyValuePairSeq[A]
  102. newSeq(n, len(s.data) * growthFactor)
  103. var h = s.first
  104. s.first = -1
  105. s.last = -1
  106. swap(s.data, n)
  107. while h >= 0:
  108. var nxt = n[h].next
  109. if isFilled(n[h].hcode):
  110. var j = -1 - rawGetKnownHC(s, n[h].key, n[h].hcode)
  111. rawInsert(s, s.data, n[h].key, n[h].hcode, j)
  112. h = nxt
  113. proc exclImpl[A](s: var OrderedSet[A], key: A): bool {.inline.} =
  114. if len(s.data) == 0:
  115. return true
  116. var n: OrderedKeyValuePairSeq[A]
  117. newSeq(n, len(s.data))
  118. var h = s.first
  119. s.first = -1
  120. s.last = -1
  121. swap(s.data, n)
  122. let hc = genHash(key)
  123. result = true
  124. while h >= 0:
  125. var nxt = n[h].next
  126. if isFilled(n[h].hcode):
  127. if n[h].hcode == hc and n[h].key == key:
  128. dec s.counter
  129. result = false
  130. else:
  131. var j = -1 - rawGetKnownHC(s, n[h].key, n[h].hcode)
  132. rawInsert(s, s.data, n[h].key, n[h].hcode, j)
  133. h = nxt