From db1023acb5ca4cba167468c3a3f646d5ad660c0c Mon Sep 17 00:00:00 2001 From: Daniel Ingraham Date: Wed, 6 Mar 2024 11:39:29 -0500 Subject: [PATCH 1/5] Add `length` method for `ViewAxis` --- src/axis.jl | 2 ++ test/runtests.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/axis.jl b/src/axis.jl index 6c150ecd..15c43ba5 100644 --- a/src/axis.jl +++ b/src/axis.jl @@ -122,6 +122,8 @@ ViewAxis{Inds,IdxMap,Ax}() where {Inds,IdxMap,Ax} = ViewAxis(Inds, Ax()) ViewAxis(Inds, IdxMap) = ViewAxis(Inds, Axis(IdxMap)) ViewAxis(Inds) = Inds +Base.length(ax::ViewAxis{Inds}) where Inds = length(Inds) + const View = ViewAxis const NullOrFlatView{Inds,IdxMap} = ViewAxis{Inds,IdxMap,<:NullorFlatAxis} diff --git a/test/runtests.jl b/test/runtests.jl index 184cd06b..beeb357d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -178,6 +178,8 @@ end x = ComponentArray(b=1, a=2) @test merge(NamedTuple(), x) == NamedTuple(x) @test kw_fun(; x...) == 2 + + @test length(ViewAxis(2:7, ShapedAxis((2,3)))) == 6 end @testset "Get" begin @@ -388,6 +390,8 @@ end end end + + @testset "Similar" begin @test similar(ca) isa typeof(ca) @test similar(ca2) isa typeof(ca2) From ed9f14cfe80081a2b474701044d0a8da0fa5b6ac Mon Sep 17 00:00:00 2001 From: Daniel Ingraham Date: Thu, 7 Mar 2024 16:30:45 -0500 Subject: [PATCH 2/5] Add `Base.length(ci::ComponentIndex)` --- src/componentindex.jl | 2 ++ test/runtests.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/componentindex.jl b/src/componentindex.jl index b63317cd..a821468c 100644 --- a/src/componentindex.jl +++ b/src/componentindex.jl @@ -13,6 +13,8 @@ const NullComponentIndex{Idx} = ComponentIndex{Idx, NullAxis} Base.:(==)(ci1::ComponentIndex, ci2::ComponentIndex) = ci1.idx == ci2.idx && ci1.ax == ci2.ax +Base.length(ci::ComponentIndex) = length(ci.idx) + """ KeepIndex(idx) diff --git a/test/runtests.jl b/test/runtests.jl index beeb357d..6d53dae2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -366,6 +366,12 @@ end @test ax[(:a, :c)] == ax[[:a, :c]] == ComponentArrays.ComponentIndex([1, 3, 4], Axis(a = 1, c = r2v(2:3))) ax2 = getaxes(ca2)[1] @test ax2[(:a, :c)] == ax2[[:a, :c]] == ComponentArrays.ComponentIndex([1, 3:8...], Axis(a = 1, c = ViewAxis(2:7, ShapedAxis((2,3))))) + + @test length(ComponentArrays.ComponentIndex(1, ComponentArrays.NullAxis())) == 1 + @test length(ComponentArrays.ComponentIndex(3:4, ShapedAxis(size(3:4)))) == 2 + @test length(ComponentArrays.ComponentIndex(5:8, Axis(a = r2v(1:3), b = 4))) == 4 + @test length(ComponentArrays.ComponentIndex([1, 3, 4], Axis(a = 1, c = r2v(2:3)))) == 3 + @test length(ComponentArrays.ComponentIndex([1, 3:8...], Axis(a = 1, c = ViewAxis(2:7, ShapedAxis((2,3)))))) == 7 end @testset "KeepIndex" begin From 4beefeca23fe375d59961f28c1ebf33dda453b1b Mon Sep 17 00:00:00 2001 From: Daniel Ingraham Date: Fri, 31 Jan 2025 09:53:39 -0500 Subject: [PATCH 3/5] Remove whitespace... --- Project.toml | 2 +- test/runtests.jl | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index cab778b8..6e663390 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ComponentArrays" uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"] -version = "0.15.22" +version = "0.15.23" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/test/runtests.jl b/test/runtests.jl index 0fa7449f..249365e1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -417,8 +417,6 @@ end end end - - @testset "Similar" begin @test similar(ca) isa typeof(ca) @test similar(ca2) isa typeof(ca2) From 724e5479c98297507571aaa255d03ffae1aad3cc Mon Sep 17 00:00:00 2001 From: Daniel Ingraham Date: Fri, 7 Feb 2025 10:37:50 -0500 Subject: [PATCH 4/5] Fix for indexing `ViewAxis` --- src/axis.jl | 2 ++ test/runtests.jl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/axis.jl b/src/axis.jl index 11733531..7ab9eff9 100644 --- a/src/axis.jl +++ b/src/axis.jl @@ -125,6 +125,8 @@ ViewAxis(Inds, IdxMap) = ViewAxis(Inds, Axis(IdxMap)) ViewAxis(Inds) = Inds Base.length(ax::ViewAxis{Inds}) where Inds = length(Inds) +# Fix https://github.com/Deltares/Ribasim/issues/2028 +Base.getindex(::ViewAxis{Inds, IdxMap, <:ComponentArrays.Shaped1DAxis}, idx::Integer) where {Inds,IdxMap} = Inds[idx] const View = ViewAxis const NullOrFlatView{Inds,IdxMap} = ViewAxis{Inds,IdxMap,<:NullorFlatAxis} diff --git a/test/runtests.jl b/test/runtests.jl index 249365e1..25500824 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -851,6 +851,36 @@ end @test all(Xstack4_dcolon[:a, :, :] .== Xstack4_noca_dcolon[1, :, :]) @test all(Xstack4_dcolon[:b, :, :] .== Xstack4_noca_dcolon[2:3, :, :]) end + + # Test fix https://github.com/Deltares/Ribasim/issues/2028 + a = range(0.0, 1.0, length=0) |> collect + b = range(0.0, 1.0; length=2) |> collect + c = range(0.0, 1.0, length=3) |> collect + d = range(0.0, 1.0; length=0) |> collect + u = ComponentVector(a=a, b=b, c=c, d=d) + + function get_state_index( + idx::Int, + ::ComponentVector{A, B, <:Tuple{<:Axis{NT}}}, + component_name::Symbol + ) where {A, B, NT} + for (comp, range) in pairs(NT) + if comp == component_name + return range[idx] + end + end + return nothing + end + + @test_throws BoundsError get_state_index(1, u, :a) + @test_throws BoundsError get_state_index(2, u, :a) + @test get_state_index(1, u, :b) == 1 + @test get_state_index(2, u, :b) == 2 + @test get_state_index(1, u, :c) == 3 + @test get_state_index(2, u, :c) == 4 + @test get_state_index(3, u, :c) == 5 + @test_throws BoundsError get_state_index(1, u, :d) + @test_throws BoundsError get_state_index(2, u, :d) end @testset "axpy! / axpby!" begin From a29f1effe4cd2b6f38ffff39798f191ecac2f952 Mon Sep 17 00:00:00 2001 From: Daniel Ingraham Date: Fri, 7 Feb 2025 10:58:35 -0500 Subject: [PATCH 5/5] Fix for `Base.iterate(ViewAxis{UnitRange, Shaped1DAxis})` --- src/axis.jl | 2 ++ test/runtests.jl | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/axis.jl b/src/axis.jl index 7ab9eff9..1119a3fe 100644 --- a/src/axis.jl +++ b/src/axis.jl @@ -127,6 +127,8 @@ ViewAxis(Inds) = Inds Base.length(ax::ViewAxis{Inds}) where Inds = length(Inds) # Fix https://github.com/Deltares/Ribasim/issues/2028 Base.getindex(::ViewAxis{Inds, IdxMap, <:ComponentArrays.Shaped1DAxis}, idx::Integer) where {Inds,IdxMap} = Inds[idx] +Base.iterate(::ViewAxis{Inds, IdxMap, <:ComponentArrays.Shaped1DAxis}) where {Inds,IdxMap} = iterate(Inds) +Base.iterate(::ViewAxis{Inds, IdxMap, <:ComponentArrays.Shaped1DAxis}, idx) where {Inds,IdxMap} = iterate(Inds, idx) const View = ViewAxis const NullOrFlatView{Inds,IdxMap} = ViewAxis{Inds,IdxMap,<:NullorFlatAxis} diff --git a/test/runtests.jl b/test/runtests.jl index 25500824..f281f611 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -881,6 +881,19 @@ end @test get_state_index(3, u, :c) == 5 @test_throws BoundsError get_state_index(1, u, :d) @test_throws BoundsError get_state_index(2, u, :d) + + # Must be a better way to make sure we can `Base.iterate` the `ViewAxis{UnitRange, Shaped1DAxis}`. + nt = ComponentArrays.indexmap(getaxes(u)[1]) + for (i, idx) in enumerate(nt.a) + end + for (i, idx) in enumerate(nt.b) + @test idx == i + end + for (i, idx) in enumerate(nt.c) + @test idx == i + 2 + end + for (i, idx) in enumerate(nt.d) + end end @testset "axpy! / axpby!" begin