Skip to content

Commit 51c1921

Browse files
committed
_
1 parent 3385027 commit 51c1921

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

lib/pure/collections/tableimpl.nim

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,9 @@ include hashcommon
1313

1414
template rawGetDeepImpl() {.dirty.} = # Search algo for unconditional add
1515
genHashImpl(key, hc)
16-
var h: Hash = hc and maxHash(t)
17-
var perturb = t.getPerturb(hc)
18-
while true:
19-
let hcode = t.data[h].hcode
20-
if hcode == deletedMarker or hcode == freeMarker:
21-
break
22-
else:
23-
h = nextTry(h, maxHash(t), perturb)
24-
result = h
16+
template mustNextTry(cell, index): bool = cell.hcode != deletedMarker and cell.hcode != freeMarker
17+
result = findCell(t, hc, mustNextTry)
18+
2519

2620
template rawInsertImpl(t) {.dirty.} =
2721
data[h].key = key

lib/pure/collections/tables.nim

+11-14
Original file line numberDiff line numberDiff line change
@@ -781,18 +781,17 @@ iterator allValues*[A, B](t: Table[A, B]; key: A): B =
781781
doAssert sortedItems(a.allValues('z')) == @[10, 20, 30]
782782

783783
let hc = genHash(key)
784-
var h: Hash = hc and high(t.data)
785784
let L = len(t)
786-
var perturb = t.getPerturb(hc)
787-
788-
var num = 0
789785
var cache: seq[Hash]
790-
while isFilled(t.data[h].hcode): # `isFilledAndValid` would be incorrect, see test for `allValues`
791-
if t.data[h].hcode == hc and t.data[h].key == key:
792-
if not hasKeyOrPutCache(cache, h):
793-
yield t.data[h].val
794-
assert(len(t) == L, "the length of the table changed while iterating over it")
795-
h = nextTry(h, high(t.data), perturb)
786+
template mustNextTry(cell, index): bool =
787+
if isFilled(cell.hcode):
788+
if cell.hcode == hc and cell.key == key:
789+
if not hasKeyOrPutCache(cache, index):
790+
yield cell.val
791+
assert(len(t) == L, "the length of the table changed while iterating over it")
792+
true
793+
else: false
794+
let index = findCell(t, hc, mustNextTry)
796795

797796

798797

@@ -1267,10 +1266,8 @@ proc enlarge[A, B](t: var OrderedTable[A, B]) =
12671266
var nxt = n[h].next
12681267
let eh = n[h].hcode
12691268
if isFilledAndValid(eh):
1270-
var j: Hash = eh and maxHash(t)
1271-
var perturb = t.getPerturb(eh)
1272-
while isFilled(t.data[j].hcode):
1273-
j = nextTry(j, maxHash(t), perturb)
1269+
template mustNextTry(cell, index): bool = isFilled(cell.hcode)
1270+
let j = findCell(t, eh, mustNextTry)
12741271
rawInsert(t, t.data, n[h].key, n[h].val, n[h].hcode, j)
12751272
h = nxt
12761273

tests/collections/ttables.nim

+4-2
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ block tableconstr:
170170
block ttables2:
171171
proc TestHashIntInt() =
172172
var tab = initTable[int,int]()
173-
for i in 1..1_000_000:
173+
# let n = 1_000_000 # PRTEMP
174+
let n = 100_000
175+
for i in 1..n:
174176
tab[i] = i
175-
for i in 1..1_000_000:
177+
for i in 1..n:
176178
var x = tab[i]
177179
if x != i : echo "not found ", i
178180

0 commit comments

Comments
 (0)