Skip to content

Commit 724e547

Browse files
committed
Fix for indexing ViewAxis
1 parent 8618ea1 commit 724e547

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/axis.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ ViewAxis(Inds, IdxMap) = ViewAxis(Inds, Axis(IdxMap))
125125
ViewAxis(Inds) = Inds
126126

127127
Base.length(ax::ViewAxis{Inds}) where Inds = length(Inds)
128+
# Fix https://github.com/Deltares/Ribasim/issues/2028
129+
Base.getindex(::ViewAxis{Inds, IdxMap, <:ComponentArrays.Shaped1DAxis}, idx::Integer) where {Inds,IdxMap} = Inds[idx]
128130

129131
const View = ViewAxis
130132
const NullOrFlatView{Inds,IdxMap} = ViewAxis{Inds,IdxMap,<:NullorFlatAxis}

test/runtests.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,36 @@ end
851851
@test all(Xstack4_dcolon[:a, :, :] .== Xstack4_noca_dcolon[1, :, :])
852852
@test all(Xstack4_dcolon[:b, :, :] .== Xstack4_noca_dcolon[2:3, :, :])
853853
end
854+
855+
# Test fix https://github.com/Deltares/Ribasim/issues/2028
856+
a = range(0.0, 1.0, length=0) |> collect
857+
b = range(0.0, 1.0; length=2) |> collect
858+
c = range(0.0, 1.0, length=3) |> collect
859+
d = range(0.0, 1.0; length=0) |> collect
860+
u = ComponentVector(a=a, b=b, c=c, d=d)
861+
862+
function get_state_index(
863+
idx::Int,
864+
::ComponentVector{A, B, <:Tuple{<:Axis{NT}}},
865+
component_name::Symbol
866+
) where {A, B, NT}
867+
for (comp, range) in pairs(NT)
868+
if comp == component_name
869+
return range[idx]
870+
end
871+
end
872+
return nothing
873+
end
874+
875+
@test_throws BoundsError get_state_index(1, u, :a)
876+
@test_throws BoundsError get_state_index(2, u, :a)
877+
@test get_state_index(1, u, :b) == 1
878+
@test get_state_index(2, u, :b) == 2
879+
@test get_state_index(1, u, :c) == 3
880+
@test get_state_index(2, u, :c) == 4
881+
@test get_state_index(3, u, :c) == 5
882+
@test_throws BoundsError get_state_index(1, u, :d)
883+
@test_throws BoundsError get_state_index(2, u, :d)
854884
end
855885

856886
@testset "axpy! / axpby!" begin

0 commit comments

Comments
 (0)