Skip to content

Commit 2910782

Browse files
committed
some changes based on feedback from oscardssmith
1 parent e027f78 commit 2910782

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

β€Žsrc/base.jlβ€Ž

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,6 @@ the children from the node alone, this should be defined for a wrapper object wh
4444
"""
4545
children(node) = ()
4646

47-
"""
48-
siblings(node)
49-
50-
Get the siblings (i.e. children of the parent of) the node `node`. The fall-back method for this only works for
51-
nodes with the trait [`StoredParents`](@ref).
52-
53-
For a general case iterator see [`Siblings`](@ref).
54-
55-
**OPTIONAL**: This function is optional in all cases.
56-
"""
57-
siblings(node) = siblings(ParentLinks(node), node)
58-
siblings(::StoredParents, node) = (children ∘ parent)(node)
59-
6047
"""
6148
nextsibling(node)
6249
@@ -80,15 +67,15 @@ obtain the previous sibling and all iterators act in the "forward" direction.
8067
function prevsibling end
8168

8269
"""
83-
ischild(node1, node2; equiv=(≑))
70+
ischild(node1, node2; equiv=(===))
8471
8572
Check if `node1` is a child of `node2`.
8673
8774
By default this iterates through `children(node2)`, so performance may be improved by adding a
8875
specialized method for given node type.
8976
9077
Equivalence is established with the `equiv` function. New methods of this function should include this
91-
argument or else it will fall back to `≑`.
78+
argument or else it will fall back to `===`.
9279
"""
9380
ischild(node1, node2; equiv=(≑)) = any(node -> equiv(node, node1), children(node2))
9481

@@ -101,7 +88,7 @@ By default all objects are considered nodes of a trivial tree with no children a
10188
the default method is simply `parent(node) = nothing`.
10289
10390
**OPTIONAL**: The 1-argument version of this function must be implemented for nodes with the [`StoredParents`](@ref)
104-
trait. The 2-argument version is always optional.
91+
trait.
10592
"""
10693
parent(node) = nothing
10794

β€Žsrc/builtins.jlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ ChildIndexing(::Expr) = IndexedChildren()
99
children(p::Pair) = (p[2],)
1010
ChildIndexing(::Pair) = IndexedChildren()
1111

12-
children(dct::AbstractDict) = pairs(dct)
12+
children(dict::AbstractDict) = pairs(dict)

β€Žsrc/cursors.jlβ€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ Abstract type for tree cursors which when constructed from a node can be used to
1616
navigate the entire tree descended from that node.
1717
1818
Tree cursors satisfy the abstract tree interface with a few additional guarantees:
19-
- In addition to [`children`](@ref), [`parent`](@ref), [`nextsibling`](@ref) and
20-
optionally [`prevsibling`](@ref) must be defined.
19+
- Tree cursors always define [`children`](@ref), [`parent`](@ref) and [`nextsibling`](@ref).
20+
[`prevsibling`](@ref) may be defined depending on whether it is defined for the wrapped tree.
2121
- The above functions returning tree nodes are guaranteed to also return tree cursors.
2222
2323
Tree nodes which define `children` and have the traits [`StoredParents`](@ref) and
24-
[`StoredSiblings`](@ref) satisfy the `TreeCursor` interface and can be used as such.
24+
[`StoredSiblings`](@ref) satisfy the `TreeCursor` interface, but calling `TreeCursor(node)` on such
25+
a node wraps them in a [`TrivialCursor`](@ref) to maintain a consistent interface.
2526
2627
## Constructors
2728
All `TreeCursor`s possess (at least) the following constructors

β€Žsrc/iteration.jlβ€Ž

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function next(f, s::PreOrderState)
116116
end
117117
nothing
118118
end
119-
next(s::PreOrderState) = next(x -> true, s)
119+
next(s::PreOrderState) = next(_ -> true, s)
120120

121121

122122
"""
@@ -146,7 +146,7 @@ struct PreOrderDFS{T,F} <: TreeIterator{T}
146146
root::T
147147
end
148148

149-
PreOrderDFS(root) = PreOrderDFS(x -> true, root)
149+
PreOrderDFS(root) = PreOrderDFS(_ -> true, root)
150150

151151
statetype(itr::PreOrderDFS) = PreOrderState
152152

@@ -211,7 +211,7 @@ statetype(itr::PostOrderDFS) = PostOrderState
211211
"""
212212
LeavesState{T<:TreeCursor} <: IteratorState{T}
213213
214-
A [`IteratorState`](@ref) of an iterator which visits all and only the laves of a tree.
214+
A [`IteratorState`](@ref) of an iterator which visits the leaves of a tree.
215215
216216
See [`Leaves`](@ref).
217217
"""
@@ -366,7 +366,7 @@ end
366366

367367
Base.iterate(ti::StatelessBFS) = (ti.root, [])
368368

369-
function _descend_left(inds, next_node, lvl)
369+
function _level(inds, next_node, lvl)
370370
while length(inds) β‰  lvl
371371
ch = children(next_node)
372372
isempty(ch) && break
@@ -390,7 +390,7 @@ function _nextind_or_deadend(node, ind, lvl)
390390
if !isnothing(iterate(ch, ni))
391391
newinds = [active_inds; ni]
392392
next_node = ch[ni]
393-
return _descend_left(newinds, next_node, lvl)
393+
return _level(newinds, next_node, lvl)
394394
end
395395
end
396396
nothing
@@ -404,7 +404,7 @@ function Base.iterate(ti::StatelessBFS, ind)
404404
if isnothing(newinds)
405405
active_lvl += 1
406406
active_lvl > org_lvl + 1 && return nothing
407-
newinds = _descend_left([], ti.root, active_lvl)
407+
newinds = _level([], ti.root, active_lvl)
408408
end
409409
length(newinds) == active_lvl && break
410410
end
@@ -416,7 +416,7 @@ end
416416
"""
417417
MapNode{T,C}
418418
419-
A node in a tree which is returned by [`treemap`](@ref). It consists of a value which is hte result of the function
419+
A node in a tree which is returned by [`treemap`](@ref). It consists of a value which is the result of the function
420420
call and an array of the children, which are also of type `MapNode`.
421421
422422
Every `MapNode` is itself a tree with the [`IndexedChildren`](@ref) trait and therefore supports indexing via
@@ -454,29 +454,29 @@ Base.show(io::IO, ::MIME"text/plain", ΞΌ::MapNode) = print_tree(io, ΞΌ)
454454

455455
#TODO: still experimenting here
456456
"""
457-
treemap(𝒻, node)
457+
treemap(f, node)
458458
459-
Apply the function `𝒻` to every node in the tree with root `node`. `node` must satisfy the AbstractTrees interface.
460-
Instead of returning the result of `𝒻(n)` directly the result will be a tree of [`MapNode`](@ref) objects isomorphic
461-
to the original tree but with values equal to the corresponding `𝒻(n)`.
459+
Apply the function `f` to every node in the tree with root `node`. `node` must satisfy the AbstractTrees interface.
460+
Instead of returning the result of `f(n)` directly the result will be a tree of [`MapNode`](@ref) objects isomorphic
461+
to the original tree but with values equal to the corresponding `f(n)`.
462462
463463
Note that in most common cases tree nodes are of a type which depends on their connectedness and the function
464-
`𝒻` should take this into account. For example the tree `[1, [2, 3]]` contains integer leaves but two
465-
`Vector` nodes. Therefore, the `𝒻` in `treemap(𝒻, [1, [2,3]])` must be a function that is valid for either
464+
`f` should take this into account. For example the tree `[1, [2, 3]]` contains integer leaves but two
465+
`Vector` nodes. Therefore, the `f` in `treemap(f, [1, [2,3]])` must be a function that is valid for either
466466
`Int` or `Vector`. Alternatively, to only operate on leaves do `map(𝒻, Leaves(itr))`.
467467
468468
## Example
469469
```julia
470470
julia> t = [1, [2, 3]];
471471
472-
julia> 𝒻(x) = x isa AbstractArray ? nothing : x + 1;
472+
julia> f(x) = x isa AbstractArray ? nothing : x + 1;
473473
474-
julia> treemap(𝒻, t)
474+
julia> treemap(f, t)
475475
MapNode{Nothing, MapNode}(nothing)
476476
β”œβ”€ MapNode{Int64, MapNode{Union{}}}(2)
477477
└─ MapNode{Nothing, MapNode{Int64, MapNode{Union{}}}}(nothing)
478478
β”œβ”€ MapNode{Int64, MapNode{Union{}}}(3)
479479
└─ MapNode{Int64, MapNode{Union{}}}(4)
480480
```
481481
"""
482-
treemap(𝒻, node) = MapNode(𝒻, node)
482+
treemap(f, node) = MapNode(f, node)

0 commit comments

Comments
Β (0)