summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authornarimiran <narimiran@disroot.org>2019-06-26 11:10:40 +0200
committernarimiran <narimiran@disroot.org>2019-06-26 17:16:55 +0200
commit326e3ad09de975de93c2653eae555ae1456720a8 (patch)
tree996bc515add316c32376bddf7af8cccba3b499cd /lib
parentb7f8031e98ea0d6e5be4c07eb096eda68294ed40 (diff)
downloadNim-326e3ad09de975de93c2653eae555ae1456720a8.tar.gz
[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: