# # # 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 ast, types import std/hashes when defined(nimPreviewSlimSystem): import std/assertions proc hashTree*(n: PNode): Hash = if n.isNil: return result = ord(n.kind) case n.kind of nkEmpty: discard of nkSym: result = result !& n.sym.id of nkIdent: result = result !& n.ident.h of nkCharLit..nkUInt64Lit: result = result !& hash(n.intVal) of nkFloatLit..nkFloat64Lit: result = result !& hash(cast[uint64](n.floatVal)) of nkStrLit..nkTripleStrLit: result = result !& hash(n.strVal) of nkType, nkNilLit: result = result !& hash(n.typ.itemId) 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)