Skip to content

Commit 8cabb2f

Browse files
committed
feat(examples): Add an example using the Julia type system.
Building over JuliaCollections#32, this provides an example implementation for a tree based on the Julia type system.
1 parent af3fabd commit 8cabb2f

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

Project.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ version = "0.4.5"
55

66
[compat]
77
Aqua = "0.8"
8+
InteractiveUtils = "1"
89
Test = "1"
910
julia = "1"
1011

1112
[extras]
1213
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
14+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1315
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1416

1517
[targets]
16-
test = ["Aqua", "Test"]
18+
test = ["Aqua", "Test", "InteractiveUtils"]

docs/src/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,4 @@ repr_node
203203
- [`OneTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/onetree.jl)
204204
- [`FSNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/fstree.jl)
205205
- [`BinaryNode`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/binarytree.jl)
206+
- [`TypeTree`](https://github.com/JuliaCollections/AbstractTrees.jl/blob/master/test/examples/juliatypes.jl)

test/examples/juliatypes.jl

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using AbstractTrees
2+
using InteractiveUtils: subtypes, supertype, supertypes
3+
"""
4+
TypeTree
5+
6+
The first thing you might think of is using Type{T} as a node directly. However,
7+
this will create many difficulties with components of AbstractTrees.jl that rely
8+
on the type system (in particular `childrentype`). A simple workaround is to
9+
use a wrapper type.
10+
"""
11+
struct TypeTree
12+
t::Type
13+
end
14+
function AbstractTrees.children(t::TypeTree)
15+
t.t === Function ? Vector{TypeTree}() : map(x->TypeTree(x), filter(x -> x !== Any,subtypes(t.t)))
16+
end
17+
AbstractTrees.printnode(io::IO,t::TypeTree) = print(io,t.t)
18+
AbstractTrees.nodevalue(t::TypeTree) = t.t
19+
AbstractTrees.parent(t::TypeTree) = TypeTree(supertype(t.t))
20+
AbstractTrees.ParentLinks(::Type{TypeTree}) = StoredParents()
21+
22+
module JuliaTypesExamples
23+
abstract type AbstractSuperType end
24+
struct DirectDescendant <: AbstractSuperType end
25+
abstract type AbstractFooBar <: AbstractSuperType end
26+
struct Foo <: AbstractFooBar end
27+
struct Bar <: AbstractFooBar end
28+
end

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ if Base.VERSION >= v"1.6"
88
@testset "Printing" begin include("printing.jl") end
99
end
1010

11-
Aqua.test_all(AbstractTrees)
11+
Aqua.test_all(AbstractTrees)

test/trees.jl

+28
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,31 @@ include(joinpath(@__DIR__, "examples", "binarytree.jl"))
116116
@test nodevalue.(sbfs) == [0, 1, 2, 3]
117117
end
118118

119+
include(joinpath(@__DIR__, "examples", "juliatypes.jl"))
120+
121+
@testset "TypeTree" begin
122+
t = TypeTree(JuliaTypesExamples.AbstractSuperType)
123+
124+
ls = collect(Leaves(t))
125+
@test issetequal(nodevalue.(ls), [JuliaTypesExamples.DirectDescendant, JuliaTypesExamples.Foo, JuliaTypesExamples.Bar])
126+
127+
predfs = nodevalue.(collect(PreOrderDFS(t)))
128+
for (i,T) in enumerate(predfs)
129+
@test !any(T .<: predfs[i+1:end])
130+
end
131+
132+
postdfs = nodevalue.(collect(PostOrderDFS(t)))
133+
for (i,T) in enumerate(postdfs)
134+
@test !any(T .<: postdfs[begin:i-1])
135+
end
136+
137+
sbfs = nodevalue.(collect(StatelessBFS(t)))
138+
for (i,T) in enumerate(sbfs)
139+
parents = collect(supertypes(T)[2:end])
140+
for j in (i+1):length(sbfs)
141+
later_parents = collect(supertypes(sbfs[j])[2:end])
142+
@test (parents later_parents) == parents
143+
end
144+
end
145+
end
146+

0 commit comments

Comments
 (0)