[bugfix] fix #11588, don't check if SharedTable is initialized

This commit is contained in:
narimiran 2019-06-26 11:10:40 +02:00
commit 326e3ad09d
3 changed files with 27 additions and 11 deletions

View file

@ -19,7 +19,7 @@ include "system/inclrtl"
type type
KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B] KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
KeyValuePairSeq[A, B] = ptr array[10_000_000, KeyValuePair[A, B]] KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]]
SharedTable* [A, B] = object ## generic hash SharedTable SharedTable* [A, B] = object ## generic hash SharedTable
data: KeyValuePairSeq[A, B] data: KeyValuePairSeq[A, B]
counter, dataLen: int counter, dataLen: int
@ -205,6 +205,8 @@ proc del*[A, B](t: var SharedTable[A, B], key: A) =
proc init*[A, B](t: var SharedTable[A, B], initialSize=64) = proc init*[A, B](t: var SharedTable[A, B], initialSize=64) =
## creates a new hash table that is empty. ## creates a new hash table that is empty.
## ##
## This proc must be called before any other usage of `t`.
##
## `initialSize` needs to be a power of two. If you need to accept runtime ## `initialSize` needs to be a power of two. If you need to accept runtime
## values for this you could use the ``nextPowerOfTwo`` proc from the ## values for this you could use the ``nextPowerOfTwo`` proc from the
## `math <math.html>`_ module or the ``rightSize`` proc from this module. ## `math <math.html>`_ module or the ``rightSize`` proc from this module.

View file

@ -30,9 +30,13 @@ proc rawInsert[X, A, B](t: var X, data: var KeyValuePairSeq[A, B],
key: A, val: B, hc: Hash, h: Hash) = key: A, val: B, hc: Hash, h: Hash) =
rawInsertImpl() rawInsertImpl()
template addImpl(enlarge) {.dirty.} = template checkIfInitialized() =
when compiles(defaultInitialSize):
if t.dataLen == 0: if t.dataLen == 0:
initImpl(t, defaultInitialSize) initImpl(t, defaultInitialSize)
template addImpl(enlarge) {.dirty.} =
checkIfInitialized()
if mustRehash(t.dataLen, t.counter): enlarge(t) if mustRehash(t.dataLen, t.counter): enlarge(t)
var hc: Hash var hc: Hash
var j = rawGetDeep(t, key, hc) var j = rawGetDeep(t, key, hc)
@ -40,8 +44,7 @@ template addImpl(enlarge) {.dirty.} =
inc(t.counter) inc(t.counter)
template maybeRehashPutImpl(enlarge) {.dirty.} = template maybeRehashPutImpl(enlarge) {.dirty.} =
if t.dataLen == 0: checkIfInitialized()
initImpl(t, defaultInitialSize)
if mustRehash(t.dataLen, t.counter): if mustRehash(t.dataLen, t.counter):
enlarge(t) enlarge(t)
index = rawGetKnownHC(t, key, hc) index = rawGetKnownHC(t, key, hc)
@ -50,16 +53,14 @@ template maybeRehashPutImpl(enlarge) {.dirty.} =
inc(t.counter) inc(t.counter)
template putImpl(enlarge) {.dirty.} = template putImpl(enlarge) {.dirty.} =
if t.dataLen == 0: checkIfInitialized()
initImpl(t, defaultInitialSize)
var hc: Hash var hc: Hash
var index = rawGet(t, key, hc) var index = rawGet(t, key, hc)
if index >= 0: t.data[index].val = val if index >= 0: t.data[index].val = val
else: maybeRehashPutImpl(enlarge) else: maybeRehashPutImpl(enlarge)
template mgetOrPutImpl(enlarge) {.dirty.} = template mgetOrPutImpl(enlarge) {.dirty.} =
if t.dataLen == 0: checkIfInitialized()
initImpl(t, defaultInitialSize)
var hc: Hash var hc: Hash
var index = rawGet(t, key, hc) var index = rawGet(t, key, hc)
if index < 0: if index < 0:
@ -69,8 +70,7 @@ template mgetOrPutImpl(enlarge) {.dirty.} =
result = t.data[index].val result = t.data[index].val
template hasKeyOrPutImpl(enlarge) {.dirty.} = template hasKeyOrPutImpl(enlarge) {.dirty.} =
if t.dataLen == 0: checkIfInitialized()
initImpl(t, defaultInitialSize)
var hc: Hash var hc: Hash
var index = rawGet(t, key, hc) var index = rawGet(t, key, hc)
if index < 0: if index < 0:

View file

@ -0,0 +1,14 @@
discard """
output: '''
'''
"""
import sharedtables
var table: SharedTable[int, int]
init(table)
table[1] = 10
assert table.mget(1) == 10
assert table.mgetOrPut(3, 7) == 7
assert table.mgetOrPut(3, 99) == 7