tableimpl.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 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 table implementations.
  10. include hashcommon
  11. template rawGetDeepImpl() {.dirty.} = # Search algo for unconditional add
  12. genHashImpl(key, hc)
  13. var h: Hash = hc and maxHash(t)
  14. while isFilled(t.data[h].hcode):
  15. h = nextTry(h, maxHash(t))
  16. result = h
  17. template rawInsertImpl() {.dirty.} =
  18. data[h].key = key
  19. data[h].val = val
  20. data[h].hcode = hc
  21. proc rawGetDeep[X, A](t: X, key: A, hc: var Hash): int {.inline.} =
  22. rawGetDeepImpl()
  23. proc rawInsert[X, A, B](t: var X, data: var KeyValuePairSeq[A, B],
  24. key: A, val: B, hc: Hash, h: Hash) =
  25. rawInsertImpl()
  26. template checkIfInitialized() =
  27. when compiles(defaultInitialSize):
  28. if t.dataLen == 0:
  29. initImpl(t, defaultInitialSize)
  30. template addImpl(enlarge) {.dirty.} =
  31. checkIfInitialized()
  32. if mustRehash(t.dataLen, t.counter): enlarge(t)
  33. var hc: Hash
  34. var j = rawGetDeep(t, key, hc)
  35. rawInsert(t, t.data, key, val, hc, j)
  36. inc(t.counter)
  37. template maybeRehashPutImpl(enlarge) {.dirty.} =
  38. checkIfInitialized()
  39. if mustRehash(t.dataLen, t.counter):
  40. enlarge(t)
  41. index = rawGetKnownHC(t, key, hc)
  42. index = -1 - index # important to transform for mgetOrPutImpl
  43. rawInsert(t, t.data, key, val, hc, index)
  44. inc(t.counter)
  45. template putImpl(enlarge) {.dirty.} =
  46. checkIfInitialized()
  47. var hc: Hash
  48. var index = rawGet(t, key, hc)
  49. if index >= 0: t.data[index].val = val
  50. else: maybeRehashPutImpl(enlarge)
  51. template mgetOrPutImpl(enlarge) {.dirty.} =
  52. checkIfInitialized()
  53. var hc: Hash
  54. var index = rawGet(t, key, hc)
  55. if index < 0:
  56. # not present: insert (flipping index)
  57. maybeRehashPutImpl(enlarge)
  58. # either way return modifiable val
  59. result = t.data[index].val
  60. template hasKeyOrPutImpl(enlarge) {.dirty.} =
  61. checkIfInitialized()
  62. var hc: Hash
  63. var index = rawGet(t, key, hc)
  64. if index < 0:
  65. result = false
  66. maybeRehashPutImpl(enlarge)
  67. else: result = true
  68. template delImplIdx(t, i) =
  69. let msk = maxHash(t)
  70. if i >= 0:
  71. dec(t.counter)
  72. block outer:
  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. t.data[i].hcode = 0 # mark current EMPTY
  77. t.data[i].key = default(type(t.data[i].key))
  78. t.data[i].val = default(type(t.data[i].val))
  79. while true:
  80. i = (i + 1) and msk # increment mod table size
  81. if isEmpty(t.data[i].hcode): # end of collision cluster; So all done
  82. break outer
  83. r = t.data[i].hcode and msk # "home" location of key@i
  84. if not ((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
  85. break
  86. when defined(js):
  87. t.data[j] = t.data[i]
  88. else:
  89. shallowCopy(t.data[j], t.data[i]) # data[j] will be marked EMPTY next loop
  90. template delImpl() {.dirty.} =
  91. var hc: Hash
  92. var i = rawGet(t, key, hc)
  93. delImplIdx(t, i)
  94. template clearImpl() {.dirty.} =
  95. for i in 0 ..< t.dataLen:
  96. when compiles(t.data[i].hcode): # CountTable records don't contain a hcode
  97. t.data[i].hcode = 0
  98. t.data[i].key = default(type(t.data[i].key))
  99. t.data[i].val = default(type(t.data[i].val))
  100. t.counter = 0
  101. template ctAnd(a, b): bool =
  102. # pending https://github.com/nim-lang/Nim/issues/13502
  103. when a:
  104. when b: true
  105. else: false
  106. else: false
  107. template initImpl(result: typed, size: int) =
  108. when ctAnd(declared(SharedTable), type(result) is SharedTable):
  109. init(result, size)
  110. else:
  111. assert isPowerOfTwo(size)
  112. result.counter = 0
  113. newSeq(result.data, size)
  114. when compiles(result.first):
  115. result.first = -1
  116. result.last = -1
  117. template insertImpl() = # for CountTable
  118. if t.dataLen == 0: initImpl(t, defaultInitialSize)
  119. if mustRehash(len(t.data), t.counter): enlarge(t)
  120. ctRawInsert(t, t.data, key, val)
  121. inc(t.counter)
  122. template getOrDefaultImpl(t, key): untyped =
  123. mixin rawGet
  124. var hc: Hash
  125. var index = rawGet(t, key, hc)
  126. if index >= 0: result = t.data[index].val
  127. template getOrDefaultImpl(t, key, default: untyped): untyped =
  128. mixin rawGet
  129. var hc: Hash
  130. var index = rawGet(t, key, hc)
  131. result = if index >= 0: t.data[index].val else: default
  132. template dollarImpl(): untyped {.dirty.} =
  133. if t.len == 0:
  134. result = "{:}"
  135. else:
  136. result = "{"
  137. for key, val in pairs(t):
  138. if result.len > 1: result.add(", ")
  139. result.addQuoted(key)
  140. result.add(": ")
  141. result.addQuoted(val)
  142. result.add("}")
  143. template equalsImpl(s, t: typed) =
  144. if s.counter == t.counter:
  145. # different insertion orders mean different 'data' seqs, so we have
  146. # to use the slow route here:
  147. for key, val in s:
  148. if not t.hasKey(key): return false
  149. if t.getOrDefault(key) != val: return false
  150. return true