summary refs log tree commit diff stats
path: root/lib/pure/collections
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-06-11 17:03:45 +0200
committerAraq <rumpf_a@web.de>2011-06-11 17:03:45 +0200
commitec2bd53ead04bc2ea6b8d6186f5f28f253ff859d (patch)
tree55fdf00baef570e1ca1c1ca5c8f75f50f787ffd7 /lib/pure/collections
parent922e216b86c1502567ee9a7ea7f3e684ed396bb5 (diff)
downloadNim-ec2bd53ead04bc2ea6b8d6186f5f28f253ff859d.tar.gz
implemented tables.add
Diffstat (limited to 'lib/pure/collections')
-rw-r--r--lib/pure/collections/sets.nim4
-rw-r--r--lib/pure/collections/tables.nim22
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim
index 331881d88..a577964c9 100644
--- a/lib/pure/collections/sets.nim
+++ b/lib/pure/collections/sets.nim
@@ -26,7 +26,7 @@ type
   TSlotEnum = enum seEmpty, seFilled, seDeleted
   TKeyValuePair[A] = tuple[slot: TSlotEnum, key: A]
   TKeyValuePairSeq[A] = seq[TKeyValuePair[A]]
-  TSet* {.final, myShallow.}[A] = object
+  TSet* {.final, myShallow.}[A] = object ## a generic hash set
     data: TKeyValuePairSeq[A]
     counter: int
 
@@ -141,7 +141,7 @@ proc `$`*[A](s: TSet[A]): string =
   ## The `$` operator for hash sets.
   dollarImpl()
 
-# ------------------------------ ordered table ------------------------------
+# ------------------------------ ordered set ------------------------------
 
 type
   TOrderedKeyValuePair[A] = tuple[
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index d1ed7586a..d353db3fb 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -102,19 +102,37 @@ proc Enlarge[A, B](t: var TTable[A, B]) =
     if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
   swap(t.data, n)
 
+template AddImpl() =
+  if mustRehash(len(t.data), t.counter): Enlarge(t)
+  RawInsert(t, t.data, key, val)
+  inc(t.counter)
+
 template PutImpl() =
   var index = RawGet(t, key)
   if index >= 0:
     t.data[index].val = val
   else:
+    AddImpl()
+
+template HasKeyOrPutImpl() =
+  var index = RawGet(t, key)
+  if index >= 0:
+    t.data[index].val = val
+    result = true
+  else:
     if mustRehash(len(t.data), t.counter): Enlarge(t)
     RawInsert(t, t.data, key, val)
     inc(t.counter)
+    result = false
 
 proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
   ## puts a (key, value)-pair into `t`.
   putImpl()
 
+proc add*[A, B](t: var TTable[A, B], key: A, val: B) =
+  ## puts a new (key, value)-pair into `t` even if ``t[key]`` already exists.
+  AddImpl()
+  
 proc del*[A, B](t: var TTable[A, B], key: A) =
   ## deletes `key` from hash table `t`.
   var index = RawGet(t, key)
@@ -230,6 +248,10 @@ proc `[]=`*[A, B](t: var TOrderedTable[A, B], key: A, val: B) =
   ## puts a (key, value)-pair into `t`.
   putImpl()
 
+proc add*[A, B](t: var TOrderedTable[A, B], key: A, val: B) =
+  ## puts a new (key, value)-pair into `t` even if ``t[key]`` already exists.
+  AddImpl()
+
 proc initOrderedTable*[A, B](initialSize=64): TOrderedTable[A, B] =
   ## creates a new ordered hash table that is empty. `initialSize` needs to be
   ## a power of two.
us@nand.sh> 2021-11-03 17:02:00 +0530 Document symbol prioritization' href='/andinus/fornax/commit/README?id=dbb7b95b35a2be5af0e2068129cd8af2be827503'>dbb7b95 ^
6acf653 ^
846b64b ^
6acf653 ^
58a57ab



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98