Skip to content

Commit 784c305

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 784c305

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

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

test/runtests.jl

Lines changed: 1 addition & 1 deletion
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

Lines changed: 28 additions & 0 deletions
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)