/
iteration.jl
504 lines (384 loc) · 13.7 KB
/
iteration.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"""
IteratorState{T<:TreeCursor}
The state of a [`TreeIterator`](@ref) object. These are simple wrappers of [`TreeCursor`](@ref) objects which define
a method for [`next`](@ref). `TreeIterator`s are in turn simple wrappers of `IteratorState`s.
Each `IteratorState` fully determines the current iteration state and therefore the next state can be obtained
with `next` (`nothing` is returned after the final state is reached).
"""
abstract type IteratorState{T<:TreeCursor} end
# we define this explicitly to avoid run-time dispatch
"""
instance(::Type{<:IteratorState}, node; kw...)
Create an instance of the given [`IteratorState`](@ref) around node `node`. This is mostly just a constructor
for `IteratorState` except that if `node` is `nothing` it will return `nothing`.
"""
function instance(::Type{S}, node; kw...) where {S<:IteratorState}
isnothing(node) ? nothing : S(node; kw...)
end
"""
initial(::Type{<:IteratorState}, node)
Obtain the initial [`IteratorState`](@ref) of the provided type for the node `node`.
"""
function initial end
"""
next(s::IteratorState)
next(f, s::IteratorState)
Obtain the next [`IteratorState`](@ref) after the current one. If `s` is the final state, this will return
`nothing`.
This provides an alternative iteration protocol which only uses the states directly as opposed to
[`Base.iterate`](@ref) which takes an iterator object and the current state as separate arguments.
"""
function next end
"""
statetype(::Type{<:TreeIterator})
Gives the type of [`IteratorState`](@ref) which is the state of the provided [`TreeIterator`](@ref).
"""
function statetype end
"""
TreeIterator{T}
An iterator of a tree that implements the AbstractTrees interface. Every `TreeIterator` is simply a wrapper
of an [`IteratorState`](@ref) which fully determine the iteration state and implement their own alternative
protocol using [`next`](@ref).
## Constructors
All `TreeIterator`s have a one argument constructor `T(node)` which starts iteration from `node`.
"""
abstract type TreeIterator{T} end
_iterator_eltype(::NodeTypeUnknown) = EltypeUnknown()
_iterator_eltype(::HasNodeType) = HasEltype()
Base.IteratorEltype(::Type{<:TreeIterator{T}}) where {T} = _iterator_eltype(NodeType(T))
Base.eltype(::Type{<:TreeIterator{T}}) where {T} = nodetype(T)
Base.eltype(ti::TreeIterator) = eltype(typeof(ti))
Base.IteratorSize(::Type{<:TreeIterator}) = SizeUnknown()
function Base.iterate(ti::TreeIterator, s=initial(statetype(ti), ti.root))
isnothing(s) && return nothing
(nodevalue(s.cursor), next(s))
end
"""
nodevalues(itr::TreeIterator)
An iterator which returns the `nodevalue` of each node in the tree, equivalent to
`Iterators.map(nodevalue, itr)`.
"""
nodevalues(itr::TreeIterator) = Iterators.map(nodevalue, itr)
"""
PreOrderState{T<:TreeCursor} <: IteratorState{T}
The iteration state of a tree iterator which guarantees that parent nodes will be visited *before* their children,
i.e. which descends a tree from root to leaves.
This state implements a [`next`](@ref) method which accepts a filter function as its first argument, allowing
tree branches to be skipped.
See [`PreOrderDFS`](@ref).
"""
struct PreOrderState{T<:TreeCursor} <: IteratorState{T}
cursor::T
PreOrderState(csr::TreeCursor) = new{typeof(csr)}(csr)
end
PreOrderState(node) = (PreOrderState ∘ TreeCursor)(node)
initial(::Type{PreOrderState}, node) = PreOrderState(node)
function next(f, s::PreOrderState)
if f(nodevalue(s.cursor))
ch = children(s.cursor)
isempty(ch) || return instance(PreOrderState, first(ch))
end
csr = s.cursor
while !isroot(csr)
n = nextsibling(csr)
if isnothing(n)
csr = parent(csr)
else
return PreOrderState(n)
end
end
nothing
end
next(s::PreOrderState) = next(_ -> true, s)
"""
PreOrderDFS{T,F} <: TreeIterator{T}
Iterator to visit the nodes of a tree, guaranteeing that parents
will be visited before their children.
Optionally takes a filter function that determines whether the iterator
should continue iterating over a node's children (if it has any) or should
consider that node a leaf.
e.g. for the tree
```
Any[Any[1,2],Any[3,4]]
├─ Any[1,2]
| ├─ 1
| └─ 2
└─ Any[3,4]
├─ 3
└─ 4
```
we will get `[[[1, 2], [3, 4]], [1, 2], 1, 2, [3, 4], 3, 4]`.
"""
struct PreOrderDFS{T,F} <: TreeIterator{T}
filter::F
root::T
end
PreOrderDFS(root) = PreOrderDFS(_ -> true, root)
statetype(itr::PreOrderDFS) = PreOrderState
function Base.iterate(ti::PreOrderDFS, s::Union{Nothing,IteratorState}=initial(statetype(ti), ti.root))
isnothing(s) && return nothing
(nodevalue(s.cursor), next(ti.filter, s))
end
"""
PostOrderState{T<:TreeCursor} <: IteratorState{T}
The state of a tree iterator which guarantees that parents are visited *after* their children, i.e. ascends
a tree from leaves to root.
See [`PostOrderDFS`](@ref).
"""
struct PostOrderState{T<:TreeCursor} <: IteratorState{T}
cursor::T
PostOrderState(csr::TreeCursor) = new{typeof(csr)}(csr)
end
PostOrderState(node) = (PostOrderState ∘ TreeCursor)(node)
initial(::Type{PostOrderState}, node) = (PostOrderState ∘ descendleft ∘ TreeCursor)(node)
function next(s::PostOrderState)
n = nextsibling(s.cursor)
if isnothing(n)
instance(PostOrderState, parent(s.cursor))
else
# descendleft is not allowed to return nothing
PostOrderState(descendleft(n))
end
end
"""
PostOrderDFS{T} <: TreeIterator{T}
Iterator to visit the nodes of a tree, guaranteeing that children
will be visited before their parents.
e.g. for the tree
```
Any[1,Any[2,3]]
├─ 1
└─ Any[2,3]
├─ 2
└─ 3
```
we will get `[1, 2, 3, [2, 3], [1, [2, 3]]]`.
"""
struct PostOrderDFS{T} <: TreeIterator{T}
root::T
end
statetype(itr::PostOrderDFS) = PostOrderState
"""
LeavesState{T<:TreeCursor} <: IteratorState{T}
A [`IteratorState`](@ref) of an iterator which visits the leaves of a tree.
See [`Leaves`](@ref).
"""
struct LeavesState{T<:TreeCursor} <: IteratorState{T}
cursor::T
LeavesState(csr::TreeCursor) = new{typeof(csr)}(csr)
end
LeavesState(node) = (LeavesState ∘ TreeCursor)(node)
initial(::Type{LeavesState}, node) = (LeavesState ∘ descendleft ∘ TreeCursor)(node)
function next(s::LeavesState)
csr = s.cursor
while !isnothing(csr) && !isroot(csr)
n = nextsibling(csr)
if isnothing(n)
csr = parent(csr)
else
# descendleft is not allowed to return nothing
return LeavesState(descendleft(n))
end
end
nothing
end
"""
Leaves{T} <: TreeIterator{T}
Iterator to visit the leaves of a tree, e.g. for the tree
```
Any[1,Any[2,3]]
├─ 1
└─ Any[2,3]
├─ 2
└─ 3
```
we will get `[1,2,3]`.
"""
struct Leaves{T} <: TreeIterator{T}
root::T
end
statetype(itr::Leaves) = LeavesState
"""
SiblingState{T<:TreeCursor} <: IteratorState{T}
A [`IteratorState`](@ref) of an iterator which visits all of the tree siblings after the current sibling.
See [`Siblings`](@ref).
"""
struct SiblingState{T<:TreeCursor} <: IteratorState{T}
cursor::T
SiblingState(csr::TreeCursor) = new{typeof(csr)}(csr)
end
SiblingState(node) = (SiblingState ∘ TreeCursor)(node)
initial(::Type{SiblingState}, node) = SiblingState(node)
next(s::SiblingState) = nextsibling(s.cursor)
"""
Siblings{T} <: TreeIterator{T}
A [`TreeIterator`](@ref) which visits the siblings of a node after the provided node.
"""
struct Siblings{T} <: TreeIterator{T}
root::T
end
statetype(itr::Siblings) = SiblingState
function _ascend(getparent, select, node)
isroot(node) && (select(node); return node)
p = getparent(node)
while select(node) && !isroot(node)
node = p
p = getparent(node)
end
node
end
"""
ascend(select, node)
ascend(select, tree, node)
Ascend a tree starting from node `node` and at each node choosing whether or not to terminate by calling
`select(n)` where `n` is the current node (`false` terminates).
For the two argument method, parents are obtained by calling `parent(tree, node)`, otherwise by calling
`parent(node)`.
Note that the parent is computed before `select` is executed, allowing modification to its argument
(as long as the tree structure is not altered).
"""
ascend(select, tree, node) = _ascend(n -> parent(tree, n), select, node)
ascend(select, node) = _ascend(parent, select, node)
"""
descend(select, tree)
Descends the tree, at each node choosing the child given by select callback
or the current node if 0 is returned.
Requires tree nodes to have [`IndexedChildren`](@ref).
"""
function descend(select, ::IndexedChildren, tree)
idx = select(tree)
idx == 0 && return tree
node = children(tree)[idx]
while true
idx = select(node)
idx == 0 && return node
node = children(node)[idx]
end
end
descend(select, node) = descend(select, ChildIndexing(node), node)
"""
StatelessBFS{T} <: TreeIterator{T}
Iterator to visit the nodes of a tree, all nodes of a level will be visited
before their children
This iterator requires [`getdescendant`](@ref) to be valid for all nodes in the
tree, but the nodes do not necessarily need the [`IndexedChildren`](@ref) trait.
e.g. for the tree
```
Any[1,Any[2,3]]
├─ 1
└─ Any[2,3]
├─ 2
└─ 3
```
we will get `[[1, [2,3]], 1, [2, 3], 2, 3]`.
WARNING: This is \$O(n^2)\$, only use this if you know you need it, as opposed to
a more standard statefull approach.
"""
struct StatelessBFS{T} <: TreeIterator{T}
root::T
end
Base.iterate(ti::StatelessBFS) = (ti.root, [])
function _descend_left(inds, next_node, level)
while length(inds) ≠ level
ch = children(next_node)
isempty(ch) && break
push!(inds, 1)
next_node = first(ch)
end
inds
end
function _nextind_or_deadend(node, ind, level)
current_lvl = active_lvl = length(ind)
active_inds = ind
# go up until there is a sibling to the right
while current_lvl > 0
active_inds = ind[1:(current_lvl-1)]
parent = getdescendant(node, active_inds)
cur_child = ind[current_lvl]
ch = children(parent)
ni = nextind(ch, cur_child)
current_lvl -= 1
if !isnothing(iterate(ch, ni))
newinds = [active_inds; ni]
next_node = ch[ni]
return _descend_left(newinds, next_node, level)
end
end
nothing
end
function Base.iterate(ti::StatelessBFS, ind)
org_lvl = active_lvl = length(ind)
newinds = ind
while true
newinds = _nextind_or_deadend(ti.root, newinds, active_lvl)
if isnothing(newinds)
active_lvl += 1
active_lvl > org_lvl + 1 && return nothing
newinds = _descend_left([], ti.root, active_lvl)
end
length(newinds) == active_lvl && break
end
(getdescendant(ti.root, newinds), newinds)
end
"""
MapNode{T,C}
A node in a tree which is returned by [`treemap`](@ref). It consists of a value which is the result of the function
call and an array of the children, which are also of type `MapNode`.
Every `MapNode` is itself a tree with the [`IndexedChildren`](@ref) trait and therefore supports indexing via
[`getdescendant`](@ref).
Use [`AbstractTrees.nodevalue`](@ref) or `mapnode.value` to obtain the wrapped value.
"""
struct MapNode{T,C} <: AbstractNode{T}
value::T
children::C
function MapNode(f, node)
(v, ch) = f(node)
ch′ = map(c -> MapNode(f, c), ch)
new{typeof(v),typeof(ch′)}(v, ch′)
end
end
MapNode(node) = MapNode(n -> (nodevalue(n), children(n)), nodevalue(node))
children(μ::MapNode) = μ.children
childrentype(::Type{MapNode{T,C}}) where {T,C} = C
nodevalue(μ::MapNode) = μ.value
ChildIndexing(::MapNode) = IndexedChildren()
"""
treemap(f, node)
Apply the function `f` to every node in the tree with root `node`, where `f` is a function that returns `(v, ch)` where
`v` is a new value for the node (i.e. as returned by [`nodevalue`](@ref) and `ch` is the new children of the node.
`f` will be called recursively so that all the children returned by `f` for a parent node will again be called for
each child. This means that to maintain the structure of a tree but merely map to new values, one should define
`f = node -> (g(node), children(node))` for some function `g` which returns a value for the node.
The nodes of the output tree will all be represented by [`MapNode`](@ref) objects wrapping the returned values.
This is necessary in order to guarantee that the output types can describe any tree topology.
Note that in most common cases tree nodes are of a type which depends on their connectedness and the function
`f` should take this into account. For example the tree `[1, [2, 3]]` contains integer leaves but two
`Vector` nodes. Therefore, the `f` in `treemap(f, [1, [2,3]])` must be a function that is valid for either
`Int` or `Vector`. Alternatively, to only operate on leaves do `map(𝒻, Leaves(itr))`.
It's very easy to write an `f` that makes `treemap` stack-overflow. To avoid this, ensure that `f` eventually
termiantes, i.e. that sometimes it returns empty `children`. For example, if `f(n) = (nothing, [0; children(n)])` will
stack-overflow because every node will have at least 1 child.
To create a tree with [`HasNodeType`](@ref) which enables efficient iteration, see [`StableNode`](@ref) instead.
## Examples
```julia
julia> t = [1, [2, 3]];
julia> f(n) = n isa AbstractArray ? (nothing, children(n)) : (n+1, children(n))
julia> treemap(f, t)
nothing
├─ 2
└─ nothing
├─ 3
└─ 4
julia> g(n) = isempty(children(n)) ? (nodevalue(n), []) : (nodevalue(n), [0; children(n)])
g (generic function with 1 method)
julia> treemap(g, t)
Any[1, [2, 3]]
├─ 0
├─ 1
└─ [2, 3]
├─ 0
├─ 2
└─ 3
```
"""
treemap(f, node) = MapNode(f, node)