Skip to content

Commit

Permalink
improved node_depth speed, fixed test for sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rcnlee committed Oct 5, 2017
1 parent 4e2f3a3 commit 8bd4a92
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/ExprRules.jl
Expand Up @@ -321,12 +321,12 @@ end
Return the depth of node for an expression tree rooted at root. Depth is 1 when root == node.
"""
function node_depth(root::RuleNode, node::RuleNode)
root == node && return 1
c_max = 0
root === node && return 1
for c in root.children
c_max = max(c_max, node_depth(c, node))
d = node_depth(c, node)
d > 0 && (return d+1)
end
c_max > 0 ? c_max+1 : 0
return 0
end

"""
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Expand Up @@ -72,12 +72,12 @@ let
@test !completed #should throw an error for not found

@test length(unique([sample(rulenode, 1) for i=1:10])) == 1
@test length(unique([sample(rulenode, 2) for i=1:10])) == 2
@test length(unique([sample(rulenode, 3) for i=1:10])) == 3
@test length(unique([sample(rulenode, 2) for i=1:10])) <= 2
@test length(unique([sample(rulenode, 3) for i=1:10])) <= 3

@test length(unique([sample(NodeLoc, rulenode, 1) for i=1:10])) == 1
@test length(unique([sample(NodeLoc, rulenode, 2) for i=1:10])) == 2
@test length(unique([sample(NodeLoc, rulenode, 3) for i=1:10])) == 3
@test length(unique([sample(NodeLoc, rulenode, 2) for i=1:10])) <= 2
@test length(unique([sample(NodeLoc, rulenode, 3) for i=1:10])) <= 3

@test sample(NodeLoc, rulenode, :R, grammar, 1) == NodeLoc(rulenode, 0)
@test sample(NodeLoc, rulenode, :R, grammar, 2) == NodeLoc(rulenode, 0)
Expand Down

0 comments on commit 8bd4a92

Please sign in to comment.