# # # The Nim Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # Implements a table from trees to trees. Does structural equivalence checking. import hashes, ast, astalgo, types proc hashTree*(n: PNode): Hash = if n.isNil: return result = ord(n.kind) case n.kind of nkEmpty, nkNilLit, nkType: discard of nkIdent: result = result !& n.ident.h of nkSym: result = result !& n.sym.id of nkCharLit..nkUInt64Lit: if (n.intVal >= low(int)) and (n.intVal <= high(int)): result = result !& int(n.intVal) of nkFloatLit..nkFloat64Lit: if (n.floatVal >= - 1000000.0) and (n.floatVal <= 1000000.0): result = result !& toInt(n.floatVal) of nkStrLit..nkTripleStrLit: result = result !& hash(n.strVal) else: for i in 0..= 0: result = t.data[index].val else: result = low(int) proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode, val: int) = var h: Hash = k and high(data) while data[h].key != nil: h = nextTry(h, high(data)) assert(data[h].key == nil) data[h].h = k data[h].key = key data[h].val = val proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) = let k = hashTree(key) let index = nodeTableRawGet(t, k, key) if index >= 0: assert(t.data[index].key != nil) t.data[index].val = val else: if mustRehash(t.data.len, t.counter): var n = newSeq[TNodePair](t.data.len * GrowthFactor) for i in 0..high(t.data): if t.data[i].key != nil: nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val) t.data = move n nodeTableRawInsert(t.data, k, key, val) inc(t.counter) proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int = let k = hashTree(key) let index = nodeTableRawGet(t, k, key) if index >= 0: assert(t.data[index].key != nil) result = t.data[index].val else: if mustRehash(t.data.len, t.counter): var n = newSeq[TNodePair](t.data.len * GrowthFactor) for i in 0..high(t.data): if t.data[i].key != nil: nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val) t.data = move n nodeTableRawInsert(t.data, k, key, val) result = val inc(t.counter)