treetab.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Implements a table from trees to trees. Does structural equivalence checking.
  10. import
  11. hashes, ast, astalgo, types
  12. proc hashTree*(n: PNode): Hash =
  13. if n.isNil:
  14. return
  15. result = ord(n.kind)
  16. case n.kind
  17. of nkEmpty, nkNilLit, nkType:
  18. discard
  19. of nkIdent:
  20. result = result !& n.ident.h
  21. of nkSym:
  22. result = result !& n.sym.id
  23. of nkCharLit..nkUInt64Lit:
  24. if (n.intVal >= low(int)) and (n.intVal <= high(int)):
  25. result = result !& int(n.intVal)
  26. of nkFloatLit..nkFloat64Lit:
  27. if (n.floatVal >= - 1000000.0) and (n.floatVal <= 1000000.0):
  28. result = result !& toInt(n.floatVal)
  29. of nkStrLit..nkTripleStrLit:
  30. result = result !& hash(n.strVal)
  31. else:
  32. for i in 0..<n.len:
  33. result = result !& hashTree(n[i])
  34. result = !$result
  35. #echo "hashTree ", result
  36. #echo n
  37. proc treesEquivalent(a, b: PNode): bool =
  38. if a == b:
  39. result = true
  40. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  41. case a.kind
  42. of nkEmpty, nkNilLit, nkType: result = true
  43. of nkSym: result = a.sym.id == b.sym.id
  44. of nkIdent: result = a.ident.id == b.ident.id
  45. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  46. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  47. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  48. else:
  49. if a.len == b.len:
  50. for i in 0..<a.len:
  51. if not treesEquivalent(a[i], b[i]): return
  52. result = true
  53. if result: result = sameTypeOrNil(a.typ, b.typ)
  54. proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int =
  55. var h: Hash = k and high(t.data)
  56. while t.data[h].key != nil:
  57. if (t.data[h].h == k) and treesEquivalent(t.data[h].key, key):
  58. return h
  59. h = nextTry(h, high(t.data))
  60. result = -1
  61. proc nodeTableGet*(t: TNodeTable, key: PNode): int =
  62. var index = nodeTableRawGet(t, hashTree(key), key)
  63. if index >= 0: result = t.data[index].val
  64. else: result = low(int)
  65. proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode,
  66. val: int) =
  67. var h: Hash = k and high(data)
  68. while data[h].key != nil: h = nextTry(h, high(data))
  69. assert(data[h].key == nil)
  70. data[h].h = k
  71. data[h].key = key
  72. data[h].val = val
  73. proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) =
  74. let k = hashTree(key)
  75. let index = nodeTableRawGet(t, k, key)
  76. if index >= 0:
  77. assert(t.data[index].key != nil)
  78. t.data[index].val = val
  79. else:
  80. if mustRehash(t.data.len, t.counter):
  81. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  82. for i in 0..high(t.data):
  83. if t.data[i].key != nil:
  84. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  85. t.data = move n
  86. nodeTableRawInsert(t.data, k, key, val)
  87. inc(t.counter)
  88. proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int =
  89. let k = hashTree(key)
  90. let index = nodeTableRawGet(t, k, key)
  91. if index >= 0:
  92. assert(t.data[index].key != nil)
  93. result = t.data[index].val
  94. else:
  95. if mustRehash(t.data.len, t.counter):
  96. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  97. for i in 0..high(t.data):
  98. if t.data[i].key != nil:
  99. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  100. t.data = move n
  101. nodeTableRawInsert(t.data, k, key, val)
  102. result = val
  103. inc(t.counter)