diff --git a/src/domains.jl b/src/domains.jl index be6d4d729..028717df7 100644 --- a/src/domains.jl +++ b/src/domains.jl @@ -36,7 +36,7 @@ Base.isapprox(d1::Domain, d2::Domain; kwargs...) = Base.getindex(d::Domain, ind::Int) = element(d, ind) -Base.getindex(d::Domain, inds::AbstractVector) = [element(d, ind) for ind in inds] +Base.getindex(d::Domain, inds::AbstractVector) = [d[ind] for ind in inds] Base.firstindex(d::Domain) = 1 @@ -48,7 +48,7 @@ Base.iterate(d::Domain, state=1) = state > nelements(d) ? nothing : (d[state], s Base.eltype(d::Domain) = eltype([d[i] for i in 1:nelements(d)]) -Base.keys(d::Domain) = 1:nelements(d) +Base.keys(d::Domain) = Base.OneTo(nelements(d)) Base.parent(d::Domain) = d diff --git a/src/topologies.jl b/src/topologies.jl index 0661d8f71..9e45168be 100644 --- a/src/topologies.jl +++ b/src/topologies.jl @@ -132,6 +132,26 @@ Return the number of facets of the `topology`. """ function nfacets(::Topology) end +# ---------- +# FALLBACKS +# ---------- + +Base.getindex(t::Topology, ind::Int) = element(t, ind) + +Base.getindex(t::Topology, inds::AbstractVector) = [t[ind] for ind in inds] + +Base.firstindex(t::Topology) = 1 + +Base.lastindex(t::Topology) = nelements(t) + +Base.length(t::Topology) = nelements(t) + +Base.iterate(t::Topology, state=1) = state > nelements(t) ? nothing : (t[state], state + 1) + +Base.eltype(t::Topology) = eltype([t[i] for i in 1:nelements(t)]) + +Base.keys(t::Topology) = Base.OneTo(nelements(t)) + # ---------------- # IMPLEMENTATIONS # ---------------- diff --git a/test/topologies.jl b/test/topologies.jl index fdcd51366..3e51a0b7e 100644 --- a/test/topologies.jl +++ b/test/topologies.jl @@ -353,6 +353,17 @@ @test element(t, 1) == connect((1, 2, 5, 4, 13, 14, 17, 16), Hexahedron) @test element(t, 2) == connect((2, 3, 6, 5, 14, 15, 18, 17), Hexahedron) @test element(t, 24) == connect((44, 45, 48, 47, 8, 9, 12, 11), Hexahedron) + + # indexable api + t = GridTopology(10, 10) + @test t[begin] == connect((1, 2, 13, 12), Quadrangle) + @test t[end] == connect((109, 110, 121, 120), Quadrangle) + @test t[10] == connect((10, 11, 22, 21), Quadrangle) + @test length(t) == 100 + @test eltype(t) == Connectivity{Quadrangle,4} + for e in t + @test e isa Connectivity{Quadrangle,4} + end end @testitem "HalfEdgeTopology" setup = [Setup] begin @@ -483,6 +494,18 @@ end t = HalfEdgeTopology(e) n = collect(elements(t)) @test n == connect.([(5, 4, 1), (6, 2, 4), (6, 5, 3), (4, 5, 6)]) + + # indexable api + g = GridTopology(10, 10) + t = convert(HalfEdgeTopology, g) + @test t[begin] == connect((13, 12, 1, 2), Quadrangle) + @test t[end] == connect((110, 121, 120, 109), Quadrangle) + @test t[10] == connect((22, 21, 10, 11), Quadrangle) + @test length(t) == 100 + @test eltype(t) == Connectivity{Quadrangle,4} + for e in t + @test e isa Connectivity{Quadrangle,4} + end end @testitem "SimpleTopology" setup = [Setup] begin @@ -538,4 +561,16 @@ end @test nfaces(t, 2) == 4 @test nfaces(t, 1) == 12 @test nfaces(t, 0) == 9 + + # indexable api + g = GridTopology(10, 10) + t = convert(SimpleTopology, g) + @test t[begin] == connect((1, 2, 13, 12), Quadrangle) + @test t[end] == connect((109, 110, 121, 120), Quadrangle) + @test t[10] == connect((10, 11, 22, 21), Quadrangle) + @test length(t) == 100 + @test eltype(t) == Connectivity{Quadrangle,4} + for e in t + @test e isa Connectivity{Quadrangle,4} + end end