tableimpl.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. # hcode for real keys cannot be zero. hcode==0 signifies an empty slot. These
  11. # two procs retain clarity of that encoding without the space cost of an enum.
  12. proc isEmpty(hcode: Hash): bool {.inline.} =
  13. result = hcode == 0
  14. proc isFilled(hcode: Hash): bool {.inline.} =
  15. result = hcode != 0
  16. const
  17. growthFactor = 2
  18. proc mustRehash(length, counter: int): bool {.inline.} =
  19. assert(length > counter)
  20. result = (length * 2 < counter * 3) or (length - counter < 4)
  21. proc nextTry(h, maxHash: Hash): Hash {.inline.} =
  22. result = (h + 1) and maxHash
  23. template rawGetKnownHCImpl() {.dirty.} =
  24. var h: Hash = hc and maxHash(t) # start with real hash value
  25. while isFilled(t.data[h].hcode):
  26. # Compare hc THEN key with boolean short circuit. This makes the common case
  27. # zero ==key's for missing (e.g.inserts) and exactly one ==key for present.
  28. # It does slow down succeeding lookups by one extra Hash cmp&and..usually
  29. # just a few clock cycles, generally worth it for any non-integer-like A.
  30. if t.data[h].hcode == hc and t.data[h].key == key:
  31. return h
  32. h = nextTry(h, maxHash(t))
  33. result = -1 - h # < 0 => MISSING; insert idx = -1 - result
  34. template genHashImpl(key, hc: typed) =
  35. hc = hash(key)
  36. if hc == 0: # This almost never taken branch should be very predictable.
  37. hc = 314159265 # Value doesn't matter; Any non-zero favorite is fine.
  38. template genHash(key: typed): Hash =
  39. var res: Hash
  40. genHashImpl(key, res)
  41. res
  42. template rawGetImpl() {.dirty.} =
  43. genHashImpl(key, hc)
  44. rawGetKnownHCImpl()
  45. template rawGetDeepImpl() {.dirty.} = # Search algo for unconditional add
  46. genHashImpl(key, hc)
  47. var h: Hash = hc and maxHash(t)
  48. while isFilled(t.data[h].hcode):
  49. h = nextTry(h, maxHash(t))
  50. result = h
  51. template rawInsertImpl() {.dirty.} =
  52. data[h].key = key
  53. data[h].val = val
  54. data[h].hcode = hc
  55. proc rawGetKnownHC[X, A](t: X, key: A, hc: Hash): int {.inline.} =
  56. rawGetKnownHCImpl()
  57. proc rawGetDeep[X, A](t: X, key: A, hc: var Hash): int {.inline.} =
  58. rawGetDeepImpl()
  59. proc rawGet[X, A](t: X, key: A, hc: var Hash): int {.inline.} =
  60. rawGetImpl()
  61. proc rawInsert[X, A, B](t: var X, data: var KeyValuePairSeq[A, B],
  62. key: A, val: B, hc: Hash, h: Hash) =
  63. rawInsertImpl()
  64. template addImpl(enlarge) {.dirty.} =
  65. if mustRehash(t.dataLen, t.counter): enlarge(t)
  66. var hc: Hash
  67. var j = rawGetDeep(t, key, hc)
  68. rawInsert(t, t.data, key, val, hc, j)
  69. inc(t.counter)
  70. template maybeRehashPutImpl(enlarge) {.dirty.} =
  71. if mustRehash(t.dataLen, t.counter):
  72. enlarge(t)
  73. index = rawGetKnownHC(t, key, hc)
  74. index = -1 - index # important to transform for mgetOrPutImpl
  75. rawInsert(t, t.data, key, val, hc, index)
  76. inc(t.counter)
  77. template putImpl(enlarge) {.dirty.} =
  78. var hc: Hash
  79. var index = rawGet(t, key, hc)
  80. if index >= 0: t.data[index].val = val
  81. else: maybeRehashPutImpl(enlarge)
  82. template mgetOrPutImpl(enlarge) {.dirty.} =
  83. var hc: Hash
  84. var index = rawGet(t, key, hc)
  85. if index < 0:
  86. # not present: insert (flipping index)
  87. maybeRehashPutImpl(enlarge)
  88. # either way return modifiable val
  89. result = t.data[index].val
  90. template hasKeyOrPutImpl(enlarge) {.dirty.} =
  91. var hc: Hash
  92. var index = rawGet(t, key, hc)
  93. if index < 0:
  94. result = false
  95. maybeRehashPutImpl(enlarge)
  96. else: result = true
  97. template default[T](t: typedesc[T]): T =
  98. var v: T
  99. v
  100. template delImplIdx(t, i) =
  101. let msk = maxHash(t)
  102. if i >= 0:
  103. dec(t.counter)
  104. block outer:
  105. while true: # KnuthV3 Algo6.4R adapted for i=i+1 instead of i=i-1
  106. var j = i # The correctness of this depends on (h+1) in nextTry,
  107. var r = j # though may be adaptable to other simple sequences.
  108. t.data[i].hcode = 0 # mark current EMPTY
  109. t.data[i].key = default(type(t.data[i].key))
  110. t.data[i].val = default(type(t.data[i].val))
  111. while true:
  112. i = (i + 1) and msk # increment mod table size
  113. if isEmpty(t.data[i].hcode): # end of collision cluster; So all done
  114. break outer
  115. r = t.data[i].hcode and msk # "home" location of key@i
  116. if not ((i >= r and r > j) or (r > j and j > i) or (j > i and i >= r)):
  117. break
  118. when defined(js):
  119. t.data[j] = t.data[i]
  120. else:
  121. shallowCopy(t.data[j], t.data[i]) # data[j] will be marked EMPTY next loop
  122. template delImpl() {.dirty.} =
  123. var hc: Hash
  124. var i = rawGet(t, key, hc)
  125. delImplIdx(t, i)
  126. template clearImpl() {.dirty.} =
  127. for i in 0 ..< t.data.len:
  128. when compiles(t.data[i].hcode): # CountTable records don't contain a hcode
  129. t.data[i].hcode = 0
  130. t.data[i].key = default(type(t.data[i].key))
  131. t.data[i].val = default(type(t.data[i].val))
  132. t.counter = 0