Rewrite cyclicTree. Performance improved by approx 50%.

This commit is contained in:
Matthew Baulch 2016-08-27 19:01:59 +10:00
commit 8f86e0f86b

View file

@ -12,29 +12,19 @@
import import
ast, astalgo, lexer, msgs, strutils, wordrecg ast, astalgo, lexer, msgs, strutils, wordrecg
proc hasSon(father, son: PNode): bool = proc cyclicTreeAux(n: PNode, visited: var seq[PNode]): bool =
for i in countup(0, sonsLen(father) - 1): if n == nil: return
if father.sons[i] == son: for v in visited:
return true if v == n: return true
result = false
proc cyclicTreeAux(n, s: PNode): bool =
if n == nil:
return false
if hasSon(s, n):
return true
var m = sonsLen(s)
addSon(s, n)
if not (n.kind in {nkEmpty..nkNilLit}): if not (n.kind in {nkEmpty..nkNilLit}):
for i in countup(0, sonsLen(n) - 1): visited.add(n)
if cyclicTreeAux(n.sons[i], s): for nSon in n.sons:
return true if cyclicTreeAux(nSon, visited): return true
result = false discard visited.pop()
delSon(s, m)
proc cyclicTree*(n: PNode): bool = proc cyclicTree*(n: PNode): bool =
var s = newNodeI(nkEmpty, n.info) var visited: seq[PNode] = @[]
result = cyclicTreeAux(n, s) cyclicTreeAux(n, visited)
proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool = proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
result = false result = false