treetab.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. when defined(nimPreviewSlimSystem):
  13. import std/assertions
  14. proc hashTree*(n: PNode): Hash =
  15. if n.isNil:
  16. return
  17. result = ord(n.kind)
  18. case n.kind
  19. of nkEmpty, nkNilLit, nkType:
  20. discard
  21. of nkIdent:
  22. result = result !& n.ident.h
  23. of nkSym:
  24. result = result !& n.sym.id
  25. of nkCharLit..nkUInt64Lit:
  26. if (n.intVal >= low(int)) and (n.intVal <= high(int)):
  27. result = result !& int(n.intVal)
  28. of nkFloatLit..nkFloat64Lit:
  29. if (n.floatVal >= - 1000000.0) and (n.floatVal <= 1000000.0):
  30. result = result !& toInt(n.floatVal)
  31. of nkStrLit..nkTripleStrLit:
  32. result = result !& hash(n.strVal)
  33. else:
  34. for i in 0..<n.len:
  35. result = result !& hashTree(n[i])
  36. result = !$result
  37. #echo "hashTree ", result
  38. #echo n
  39. proc treesEquivalent(a, b: PNode): bool =
  40. if a == b:
  41. result = true
  42. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  43. case a.kind
  44. of nkEmpty, nkNilLit, nkType: result = true
  45. of nkSym: result = a.sym.id == b.sym.id
  46. of nkIdent: result = a.ident.id == b.ident.id
  47. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  48. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  49. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  50. else:
  51. if a.len == b.len:
  52. for i in 0..<a.len:
  53. if not treesEquivalent(a[i], b[i]): return
  54. result = true
  55. if result: result = sameTypeOrNil(a.typ, b.typ)
  56. proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int =
  57. var h: Hash = k and high(t.data)
  58. while t.data[h].key != nil:
  59. if (t.data[h].h == k) and treesEquivalent(t.data[h].key, key):
  60. return h
  61. h = nextTry(h, high(t.data))
  62. result = -1
  63. proc nodeTableGet*(t: TNodeTable, key: PNode): int =
  64. var index = nodeTableRawGet(t, hashTree(key), key)
  65. if index >= 0: result = t.data[index].val
  66. else: result = low(int)
  67. proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode,
  68. val: int) =
  69. var h: Hash = k and high(data)
  70. while data[h].key != nil: h = nextTry(h, high(data))
  71. assert(data[h].key == nil)
  72. data[h].h = k
  73. data[h].key = key
  74. data[h].val = val
  75. proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) =
  76. let k = hashTree(key)
  77. let index = nodeTableRawGet(t, k, key)
  78. if index >= 0:
  79. assert(t.data[index].key != nil)
  80. t.data[index].val = val
  81. else:
  82. if mustRehash(t.data.len, t.counter):
  83. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  84. for i in 0..high(t.data):
  85. if t.data[i].key != nil:
  86. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  87. t.data = move n
  88. nodeTableRawInsert(t.data, k, key, val)
  89. inc(t.counter)
  90. proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int =
  91. let k = hashTree(key)
  92. let index = nodeTableRawGet(t, k, key)
  93. if index >= 0:
  94. assert(t.data[index].key != nil)
  95. result = t.data[index].val
  96. else:
  97. if mustRehash(t.data.len, t.counter):
  98. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  99. for i in 0..high(t.data):
  100. if t.data[i].key != nil:
  101. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  102. t.data = move n
  103. nodeTableRawInsert(t.data, k, key, val)
  104. result = val
  105. inc(t.counter)