treetab.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ast, astalgo, types
  11. import std/hashes
  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. else:
  56. result = false
  57. if result: result = sameTypeOrNil(a.typ, b.typ)
  58. else:
  59. result = false
  60. proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int =
  61. var h: Hash = k and high(t.data)
  62. while t.data[h].key != nil:
  63. if (t.data[h].h == k) and treesEquivalent(t.data[h].key, key):
  64. return h
  65. h = nextTry(h, high(t.data))
  66. result = -1
  67. proc nodeTableGet*(t: TNodeTable, key: PNode): int =
  68. var index = nodeTableRawGet(t, hashTree(key), key)
  69. if index >= 0: result = t.data[index].val
  70. else: result = low(int)
  71. proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode,
  72. val: int) =
  73. var h: Hash = k and high(data)
  74. while data[h].key != nil: h = nextTry(h, high(data))
  75. assert(data[h].key == nil)
  76. data[h].h = k
  77. data[h].key = key
  78. data[h].val = val
  79. proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) =
  80. let k = hashTree(key)
  81. let index = nodeTableRawGet(t, k, key)
  82. if index >= 0:
  83. assert(t.data[index].key != nil)
  84. t.data[index].val = val
  85. else:
  86. if mustRehash(t.data.len, t.counter):
  87. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  88. for i in 0..high(t.data):
  89. if t.data[i].key != nil:
  90. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  91. t.data = move n
  92. nodeTableRawInsert(t.data, k, key, val)
  93. inc(t.counter)
  94. proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int =
  95. let k = hashTree(key)
  96. let index = nodeTableRawGet(t, k, key)
  97. if index >= 0:
  98. assert(t.data[index].key != nil)
  99. result = t.data[index].val
  100. else:
  101. if mustRehash(t.data.len, t.counter):
  102. var n = newSeq[TNodePair](t.data.len * GrowthFactor)
  103. for i in 0..high(t.data):
  104. if t.data[i].key != nil:
  105. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  106. t.data = move n
  107. nodeTableRawInsert(t.data, k, key, val)
  108. result = val
  109. inc(t.counter)