Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StatelessBFS assumes that children are indexable #113

Open
mortenpi opened this issue Jul 29, 2022 · 1 comment
Open

StatelessBFS assumes that children are indexable #113

mortenpi opened this issue Jul 29, 2022 · 1 comment

Comments

@mortenpi
Copy link
Contributor

Iterating over StatelessBFS calls getindex on the object returned by children():

julia> collect(StatelessBFS(n))
ERROR: MethodError: no method matching getindex(::VectorTreeChildren, ::Int64)
Stacktrace:
 [1] getdescendant
   @ ~/.julia/packages/AbstractTrees/kBTzE/src/indexing.jl:29 [inlined]
 [2] iterate(ti::StatelessBFS{VectorTreeNode}, ind::Vector{Any})
   @ AbstractTrees ~/.julia/packages/AbstractTrees/kBTzE/src/iteration.jl:418

As far as I understand, the default for ChildIndexing is NonIndexedChildren, which should imply that it shouldn't be necessary to implement indexing for children? The other iterators (*OrderDFS, Leaves) work fine with my type.

This should work as an MWE:

struct VectorTreeNode
    children :: Vector{Any}
    VectorTreeNode(cs) = (cs isa Vector) ? new(cs) : new([])
end
struct VectorTreeChildren
    node :: VectorTreeNode
end
Base.IteratorSize(::Type{VectorTreeChildren}) = Base.SizeUnknown()
function Base.iterate(it::VectorTreeChildren, state = 1)
    if state <= length(it.node.children)
        (VectorTreeNode(it.node.children[state]), state + 1)
    else
        nothing
    end
end
using AbstractTrees
AbstractTrees.children(n::VectorTreeNode) = VectorTreeChildren(n)
n = VectorTreeNode([1,[2,3],4])
collect(StatelessBFS(n))

Note: there is also #15 which seems to be a similar issue, but is quite outdated I think -- Leaves works and the indexability trait (ChildIndexing) exists now.

@oscardssmith
Copy link
Member

Good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants