Skip to content

Commit d8be1a9

Browse files
juliohmJoshuaLampert
authored andcommitted
Implement indexable api for topology (#1122)
* Implement indexable api for topology * Update src/topologies.jl Co-authored-by: Joshua Lampert <[email protected]> * Update src/topologies.jl Co-authored-by: Joshua Lampert <[email protected]> * Apply suggestions to Domain --------- Co-authored-by: Joshua Lampert <[email protected]>
1 parent dfaf12d commit d8be1a9

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/domains.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Base.isapprox(d1::Domain, d2::Domain; kwargs...) =
3636

3737
Base.getindex(d::Domain, ind::Int) = element(d, ind)
3838

39-
Base.getindex(d::Domain, inds::AbstractVector) = [element(d, ind) for ind in inds]
39+
Base.getindex(d::Domain, inds::AbstractVector) = [d[ind] for ind in inds]
4040

4141
Base.firstindex(d::Domain) = 1
4242

@@ -48,7 +48,7 @@ Base.iterate(d::Domain, state=1) = state > nelements(d) ? nothing : (d[state], s
4848

4949
Base.eltype(d::Domain) = eltype([d[i] for i in 1:nelements(d)])
5050

51-
Base.keys(d::Domain) = 1:nelements(d)
51+
Base.keys(d::Domain) = Base.OneTo(nelements(d))
5252

5353
Base.parent(d::Domain) = d
5454

src/topologies.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ Return the number of facets of the `topology`.
132132
"""
133133
function nfacets(::Topology) end
134134

135+
# ----------
136+
# FALLBACKS
137+
# ----------
138+
139+
Base.getindex(t::Topology, ind::Int) = element(t, ind)
140+
141+
Base.getindex(t::Topology, inds::AbstractVector) = [t[ind] for ind in inds]
142+
143+
Base.firstindex(t::Topology) = 1
144+
145+
Base.lastindex(t::Topology) = nelements(t)
146+
147+
Base.length(t::Topology) = nelements(t)
148+
149+
Base.iterate(t::Topology, state=1) = state > nelements(t) ? nothing : (t[state], state + 1)
150+
151+
Base.eltype(t::Topology) = eltype([t[i] for i in 1:nelements(t)])
152+
153+
Base.keys(t::Topology) = Base.OneTo(nelements(t))
154+
135155
# ----------------
136156
# IMPLEMENTATIONS
137157
# ----------------

test/topologies.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,17 @@
353353
@test element(t, 1) == connect((1, 2, 5, 4, 13, 14, 17, 16), Hexahedron)
354354
@test element(t, 2) == connect((2, 3, 6, 5, 14, 15, 18, 17), Hexahedron)
355355
@test element(t, 24) == connect((44, 45, 48, 47, 8, 9, 12, 11), Hexahedron)
356+
357+
# indexable api
358+
t = GridTopology(10, 10)
359+
@test t[begin] == connect((1, 2, 13, 12), Quadrangle)
360+
@test t[end] == connect((109, 110, 121, 120), Quadrangle)
361+
@test t[10] == connect((10, 11, 22, 21), Quadrangle)
362+
@test length(t) == 100
363+
@test eltype(t) == Connectivity{Quadrangle,4}
364+
for e in t
365+
@test e isa Connectivity{Quadrangle,4}
366+
end
356367
end
357368

358369
@testitem "HalfEdgeTopology" setup = [Setup] begin
@@ -483,6 +494,18 @@ end
483494
t = HalfEdgeTopology(e)
484495
n = collect(elements(t))
485496
@test n == connect.([(5, 4, 1), (6, 2, 4), (6, 5, 3), (4, 5, 6)])
497+
498+
# indexable api
499+
g = GridTopology(10, 10)
500+
t = convert(HalfEdgeTopology, g)
501+
@test t[begin] == connect((13, 12, 1, 2), Quadrangle)
502+
@test t[end] == connect((110, 121, 120, 109), Quadrangle)
503+
@test t[10] == connect((22, 21, 10, 11), Quadrangle)
504+
@test length(t) == 100
505+
@test eltype(t) == Connectivity{Quadrangle,4}
506+
for e in t
507+
@test e isa Connectivity{Quadrangle,4}
508+
end
486509
end
487510

488511
@testitem "SimpleTopology" setup = [Setup] begin
@@ -538,4 +561,16 @@ end
538561
@test nfaces(t, 2) == 4
539562
@test nfaces(t, 1) == 12
540563
@test nfaces(t, 0) == 9
564+
565+
# indexable api
566+
g = GridTopology(10, 10)
567+
t = convert(SimpleTopology, g)
568+
@test t[begin] == connect((1, 2, 13, 12), Quadrangle)
569+
@test t[end] == connect((109, 110, 121, 120), Quadrangle)
570+
@test t[10] == connect((10, 11, 22, 21), Quadrangle)
571+
@test length(t) == 100
572+
@test eltype(t) == Connectivity{Quadrangle,4}
573+
for e in t
574+
@test e isa Connectivity{Quadrangle,4}
575+
end
541576
end

0 commit comments

Comments
 (0)