Skip to content

Commit 2f20ed1

Browse files
committed
changed Base.values method to nodevalues; a bunch of new docs
1 parent 590e690 commit 2f20ed1

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

docs/src/index.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,30 @@ NodeTypeUnknown
8787
HasNodeType
8888
```
8989

90-
This can be used e.g. to guarantee the `eltype` of `TreeIterator`s. It should be defined especially
91-
for trees in which all nodes have the same type, which are the only cases in which iteration over a
92-
tree can be type stable.
90+
Providing the `HasNodeType` trait will guarantee that all nodes connected to the node must be of the
91+
type returned by [`nodetype`](@ref).
92+
93+
An important use case of this trait is to guarantee the return types of a `TreeIterator`. Tree
94+
nodes with `NodeTypeUnknown` cannot have type-stable iteration over the entire tree.
95+
96+
For example
97+
```julia
98+
struct ExampleNode
99+
x::Int
100+
children::Vector{ExampleNode}
101+
end
102+
103+
AbstractTrees.nodevalue(x::ExampleNode) = x.x
104+
AbstractTrees.children(x::ExampleNode) = x.children
105+
106+
AbstractTrees.NodeType(::Type{<:ExampleNode}) = HasNodeType()
107+
AbstractTrees.nodetype(::Type{<:ExampleNode}) = ExampleNode
108+
```
109+
In this example, iteration over a tree of `ExampleNode`s is type-stable with `eltype`
110+
`ExampleNode`.
111+
112+
Providing the `nodetype(::Type)` method is preferable to defining `nodetype(::ExampleNode)` because
113+
it ensures that `nodetype` can be involved at compile time even if values are not known.
93114

94115

95116
## The Indexed Tree Interface
@@ -124,9 +145,25 @@ prevsiblingindex
124145
rootindex
125146
```
126147

148+
## Additional Functions
149+
```@docs
150+
getdescendant
151+
nodevalues
152+
ischild
153+
isroot
154+
intree
155+
isdescendant
156+
treebreadth
157+
treeheight
158+
descendleft
159+
getroot
160+
```
161+
127162
## Example Implementations
128163
- All objects in base which define the abstract trees interface are defined in
129164
[`builtins.jl`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/src/builtins.jl).
130-
- [`IDTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/idtree.jl)
131-
- [`OneNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/onenode.jl)
132-
- [`OneTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/onetree.jl)
165+
- [`IDTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/idtree.jl)
166+
- [`OneNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/onenode.jl)
167+
- [`OneTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/onetree.jl)
168+
- [`FSNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/fstree.jl)
169+
- [`BinaryNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/binarytree.jl)

src/AbstractTrees.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export ParentLinks, StoredParents, ImplicitParents
2525
export SiblingLinks, StoredSiblings, ImplicitSiblings
2626
export ChildIndexing, IndexedChildren, NonIndexedChildren
2727
export NodeType, HasNodeType, NodeTypeUnknown
28-
export nodetype, nodevalue, children, parentlinks, siblinglinks, childindexing, childtype, childrentype
28+
export nodetype, nodevalue, nodevalues, children, parentlinks, siblinglinks, childindexing, childtype, childrentype
2929
#extended interface
3030
export nextsibling, prevsibling
3131

src/iteration.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ function Base.iterate(ti::TreeIterator, s::Union{Nothing,IteratorState}=initial(
7676
end
7777

7878
"""
79-
values(itr::TreeIterator)
79+
nodevalues(itr::TreeIterator)
8080
8181
An iterator which returns the `nodevalue` of each node in the tree, equivalent to
8282
`Iterators.map(nodevalue, itr)`.
8383
"""
84-
Base.values(itr::TreeIterator) = Iterators.map(nodevalue, itr)
84+
nodevalues(itr::TreeIterator) = Iterators.map(nodevalue, itr)
8585

8686
"""
8787
PreOrderState{T<:TreeCursor} <: IteratorState{T}

test/builtins.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ end
8080
a = [1,[2,[3]]]
8181
f = n -> n isa AbstractArray ? (nothing, children(n)) : (n+1, children(n))
8282
b = treemap(f, a)
83-
@test nodevalue.(PreOrderDFS(b)) == [nothing, 2, nothing, 3, nothing, 4]
83+
@test collect(nodevalues(PreOrderDFS(b))) == [nothing, 2, nothing, 3, nothing, 4]
8484
g = n -> isempty(children(n)) ? (nodevalue(n), ()) : (nothing, [0; children(n)])
8585
b = treemap(g, a)
8686
@test nodevalue.(PostOrderDFS(b)) == [0, 1, 0, 2, 0, 3, nothing, nothing, nothing]

0 commit comments

Comments
 (0)