Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Feb 20, 2020
1 parent 3385027 commit 51c1921
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
12 changes: 3 additions & 9 deletions lib/pure/collections/tableimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ include hashcommon

template rawGetDeepImpl() {.dirty.} = # Search algo for unconditional add
genHashImpl(key, hc)
var h: Hash = hc and maxHash(t)
var perturb = t.getPerturb(hc)
while true:
let hcode = t.data[h].hcode
if hcode == deletedMarker or hcode == freeMarker:
break
else:
h = nextTry(h, maxHash(t), perturb)
result = h
template mustNextTry(cell, index): bool = cell.hcode != deletedMarker and cell.hcode != freeMarker
result = findCell(t, hc, mustNextTry)


template rawInsertImpl(t) {.dirty.} =
data[h].key = key
Expand Down
25 changes: 11 additions & 14 deletions lib/pure/collections/tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -781,18 +781,17 @@ iterator allValues*[A, B](t: Table[A, B]; key: A): B =
doAssert sortedItems(a.allValues('z')) == @[10, 20, 30]

let hc = genHash(key)
var h: Hash = hc and high(t.data)
let L = len(t)
var perturb = t.getPerturb(hc)

var num = 0
var cache: seq[Hash]
while isFilled(t.data[h].hcode): # `isFilledAndValid` would be incorrect, see test for `allValues`
if t.data[h].hcode == hc and t.data[h].key == key:
if not hasKeyOrPutCache(cache, h):
yield t.data[h].val
assert(len(t) == L, "the length of the table changed while iterating over it")
h = nextTry(h, high(t.data), perturb)
template mustNextTry(cell, index): bool =
if isFilled(cell.hcode):
if cell.hcode == hc and cell.key == key:
if not hasKeyOrPutCache(cache, index):
yield cell.val
assert(len(t) == L, "the length of the table changed while iterating over it")
true
else: false
let index = findCell(t, hc, mustNextTry)



Expand Down Expand Up @@ -1267,10 +1266,8 @@ proc enlarge[A, B](t: var OrderedTable[A, B]) =
var nxt = n[h].next
let eh = n[h].hcode
if isFilledAndValid(eh):
var j: Hash = eh and maxHash(t)
var perturb = t.getPerturb(eh)
while isFilled(t.data[j].hcode):
j = nextTry(j, maxHash(t), perturb)
template mustNextTry(cell, index): bool = isFilled(cell.hcode)
let j = findCell(t, eh, mustNextTry)
rawInsert(t, t.data, n[h].key, n[h].val, n[h].hcode, j)
h = nxt

Expand Down
6 changes: 4 additions & 2 deletions tests/collections/ttables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ block tableconstr:
block ttables2:
proc TestHashIntInt() =
var tab = initTable[int,int]()
for i in 1..1_000_000:
# let n = 1_000_000 # PRTEMP
let n = 100_000
for i in 1..n:
tab[i] = i
for i in 1..1_000_000:
for i in 1..n:
var x = tab[i]
if x != i : echo "not found ", i

Expand Down

0 comments on commit 51c1921

Please sign in to comment.