summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-06-26 23:11:34 +0200
committerGitHub <noreply@github.com>2019-06-26 23:11:34 +0200
commit48cbf1c4960848dac9b2f63e17c57a837a234163 (patch)
tree19631fb0faf70b0f2ad42e6cd7a572d43cfe277e /lib
parentce2777af5a22f01196f409de0182d9ee4b40b6e1 (diff)
parent326e3ad09de975de93c2653eae555ae1456720a8 (diff)
downloadNim-48cbf1c4960848dac9b2f63e17c57a837a234163.tar.gz
Merge pull request #11598 from narimiran/fix-sharedtables
[bugfix] fix #11588, don't check if SharedTable is initialized
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/collections/sharedtables.nim4
-rw-r--r--lib/pure/collections/tableimpl.nim20
2 files changed, 13 insertions, 11 deletions
diff --git a/lib/pure/collections/sharedtables.nim b/lib/pure/collections/sharedtables.nim
index 6180929b9..c4e641d70 100644
--- a/lib/pure/collections/sharedtables.nim
+++ b/lib/pure/collections/sharedtables.nim
@@ -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.
diff --git a/lib/pure/collections/tableimpl.nim b/lib/pure/collections/tableimpl.nim
index 6fce5c989..7404a484b 100644
--- a/lib/pure/collections/tableimpl.nim
+++ b/lib/pure/collections/tableimpl.nim
@@ -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: