mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-03 10:24:44 +00:00
[bugfix] fix #11588, don't check if SharedTable is initialized
This commit is contained in:
@@ -19,7 +19,7 @@ include "system/inclrtl"
|
||||
|
||||
type
|
||||
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
|
||||
data: KeyValuePairSeq[A, B]
|
||||
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) =
|
||||
## 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
|
||||
## values for this you could use the ``nextPowerOfTwo`` proc from the
|
||||
## `math <math.html>`_ module or the ``rightSize`` proc from this module.
|
||||
|
||||
@@ -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) =
|
||||
rawInsertImpl()
|
||||
|
||||
template checkIfInitialized() =
|
||||
when compiles(defaultInitialSize):
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
|
||||
template addImpl(enlarge) {.dirty.} =
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
checkIfInitialized()
|
||||
if mustRehash(t.dataLen, t.counter): enlarge(t)
|
||||
var hc: Hash
|
||||
var j = rawGetDeep(t, key, hc)
|
||||
@@ -40,8 +44,7 @@ template addImpl(enlarge) {.dirty.} =
|
||||
inc(t.counter)
|
||||
|
||||
template maybeRehashPutImpl(enlarge) {.dirty.} =
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
checkIfInitialized()
|
||||
if mustRehash(t.dataLen, t.counter):
|
||||
enlarge(t)
|
||||
index = rawGetKnownHC(t, key, hc)
|
||||
@@ -50,16 +53,14 @@ template maybeRehashPutImpl(enlarge) {.dirty.} =
|
||||
inc(t.counter)
|
||||
|
||||
template putImpl(enlarge) {.dirty.} =
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
checkIfInitialized()
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index >= 0: t.data[index].val = val
|
||||
else: maybeRehashPutImpl(enlarge)
|
||||
|
||||
template mgetOrPutImpl(enlarge) {.dirty.} =
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
checkIfInitialized()
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index < 0:
|
||||
@@ -69,8 +70,7 @@ template mgetOrPutImpl(enlarge) {.dirty.} =
|
||||
result = t.data[index].val
|
||||
|
||||
template hasKeyOrPutImpl(enlarge) {.dirty.} =
|
||||
if t.dataLen == 0:
|
||||
initImpl(t, defaultInitialSize)
|
||||
checkIfInitialized()
|
||||
var hc: Hash
|
||||
var index = rawGet(t, key, hc)
|
||||
if index < 0:
|
||||
|
||||
14
tests/stdlib/tsharedtable.nim
Normal file
14
tests/stdlib/tsharedtable.nim
Normal 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
|
||||
Reference in New Issue
Block a user