Skip to content

StatelessBFS assumes that children are indexable #113

Open
@mortenpi

Description

@mortenpi

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions