Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement indexable api for topology #1122

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/topologies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@
"""
function nfacets(::Topology) end

# ----------
# FALLBACKS
# ----------

Base.getindex(t::Topology, ind::Int) = element(t, ind)

Base.getindex(t::Topology, inds::AbstractVector) = [element(t, ind) for ind in inds]

Check warning on line 141 in src/topologies.jl

View check run for this annotation

Codecov / codecov/patch

src/topologies.jl#L141

Added line #L141 was not covered by tests
juliohm marked this conversation as resolved.
Show resolved Hide resolved

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) = 1:nelements(t)

Check warning on line 153 in src/topologies.jl

View check run for this annotation

Codecov / codecov/patch

src/topologies.jl#L153

Added line #L153 was not covered by tests
juliohm marked this conversation as resolved.
Show resolved Hide resolved

# ----------------
# IMPLEMENTATIONS
# ----------------
Expand Down
35 changes: 35 additions & 0 deletions test/topologies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading