implemented tables.add

This commit is contained in:
Araq 2011-06-11 17:03:45 +02:00
commit ec2bd53ead
4 changed files with 26 additions and 11 deletions

View file

@ -342,23 +342,15 @@ proc transformAddrDeref(c: PTransf, n: PNode, a, b: TNodeKind): PTransNode =
var x = copyTree(n) var x = copyTree(n)
x.sons[0].sons[0] = m.sons[0] x.sons[0].sons[0] = m.sons[0]
result = transform(c, x.sons[0]) result = transform(c, x.sons[0])
#result = newTransNode(n.sons[0])
#result[0] = transform(c, m.sons[0])
else: else:
result = transformSons(c, n) result = transformSons(c, n)
of nkHiddenStdConv, nkHiddenSubConv, nkConv: of nkHiddenStdConv, nkHiddenSubConv, nkConv:
var m = n.sons[0].sons[1] var m = n.sons[0].sons[1]
if (m.kind == a) or (m.kind == b): if (m.kind == a) or (m.kind == b):
# addr ( nkConv ( deref ( x ) ) ) --> nkConv(x) # addr ( nkConv ( deref ( x ) ) ) --> nkConv(x)
var x = copyTree(n) var x = copyTree(n)
x.sons[0].sons[1] = m.sons[0] x.sons[0].sons[1] = m.sons[0]
result = transform(c, x.sons[0]) result = transform(c, x.sons[0])
#result = newTransNode(n.sons[0])
#result[1] = transform(c, m.sons[0])
else: else:
result = transformSons(c, n) result = transformSons(c, n)
else: else:

View file

@ -26,7 +26,7 @@ type
TSlotEnum = enum seEmpty, seFilled, seDeleted TSlotEnum = enum seEmpty, seFilled, seDeleted
TKeyValuePair[A] = tuple[slot: TSlotEnum, key: A] TKeyValuePair[A] = tuple[slot: TSlotEnum, key: A]
TKeyValuePairSeq[A] = seq[TKeyValuePair[A]] TKeyValuePairSeq[A] = seq[TKeyValuePair[A]]
TSet* {.final, myShallow.}[A] = object TSet* {.final, myShallow.}[A] = object ## a generic hash set
data: TKeyValuePairSeq[A] data: TKeyValuePairSeq[A]
counter: int counter: int
@ -141,7 +141,7 @@ proc `$`*[A](s: TSet[A]): string =
## The `$` operator for hash sets. ## The `$` operator for hash sets.
dollarImpl() dollarImpl()
# ------------------------------ ordered table ------------------------------ # ------------------------------ ordered set ------------------------------
type type
TOrderedKeyValuePair[A] = tuple[ TOrderedKeyValuePair[A] = tuple[

View file

@ -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) if t.data[i].slot == seFilled: RawInsert(t, n, t.data[i].key, t.data[i].val)
swap(t.data, n) 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() = template PutImpl() =
var index = RawGet(t, key) var index = RawGet(t, key)
if index >= 0: if index >= 0:
t.data[index].val = val 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: else:
if mustRehash(len(t.data), t.counter): Enlarge(t) if mustRehash(len(t.data), t.counter): Enlarge(t)
RawInsert(t, t.data, key, val) RawInsert(t, t.data, key, val)
inc(t.counter) inc(t.counter)
result = false
proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) = proc `[]=`*[A, B](t: var TTable[A, B], key: A, val: B) =
## puts a (key, value)-pair into `t`. ## puts a (key, value)-pair into `t`.
putImpl() 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) = proc del*[A, B](t: var TTable[A, B], key: A) =
## deletes `key` from hash table `t`. ## deletes `key` from hash table `t`.
var index = RawGet(t, key) 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`. ## puts a (key, value)-pair into `t`.
putImpl() 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] = proc initOrderedTable*[A, B](initialSize=64): TOrderedTable[A, B] =
## creates a new ordered hash table that is empty. `initialSize` needs to be ## creates a new ordered hash table that is empty. `initialSize` needs to be
## a power of two. ## a power of two.

View file

@ -26,10 +26,11 @@ Bugs
- the parser allows empty object case branches - the parser allows empty object case branches
- pegs: the anchor '^' does not work because many procs use a linear search - pegs: the anchor '^' does not work because many procs use a linear search
and matchLen() and matchLen()
- BUG: generic assign still buggy - bug: generic assign still buggy
- Optimization: If we use a temporary for the result anyway the code gen - Optimization: If we use a temporary for the result anyway the code gen
should make use of this fact to generate better code... should make use of this fact to generate better code...
- bug: invoking a generic iterator twice triggers a code gen bug - bug: invoking a generic iterator twice triggers a code gen bug
- bug: forward proc for generic seems broken
- sorting with leads to a strange memory corruption! - sorting with leads to a strange memory corruption!
--> system.swap or genericAssign is broken! And indeed, if reference counts --> system.swap or genericAssign is broken! And indeed, if reference counts
are not modified and the GC is triggered in between a swap, bad things are not modified and the GC is triggered in between a swap, bad things