Mercurial > repos > public > sbplib_julia
changeset 1390:47931bef8471 bugfix/grids/complete_interface_impl
Implement `firstindex` and `lastindex` for `TensorGrid`
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 15 Aug 2023 22:39:34 +0200 |
parents | d2219cc8316b |
children | 9da927271752 |
files | src/Grids/tensor_grid.jl test/Grids/tensor_grid_test.jl |
diffstat | 2 files changed, 16 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/tensor_grid.jl Tue Aug 15 22:38:48 2023 +0200 +++ b/src/Grids/tensor_grid.jl Tue Aug 15 22:39:34 2023 +0200 @@ -33,6 +33,16 @@ return CartesianIndices(szs) end +function Base.firstindex(g::TensorGrid, d) + i, ld = grid_and_local_dim_index(ndims.(g.grids), d) + return firstindex(g.grids[i], ld) +end + +function Base.lastindex(g::TensorGrid, d) + i, ld = grid_and_local_dim_index(ndims.(g.grids), d) + return lastindex(g.grids[i], ld) +end + # Iteration interface Base.iterate(g::TensorGrid) = iterate(Iterators.product(g.grids...)) |> _iterate_combine_coords Base.iterate(g::TensorGrid, state) = iterate(Iterators.product(g.grids...), state) |> _iterate_combine_coords
--- a/test/Grids/tensor_grid_test.jl Tue Aug 15 22:38:48 2023 +0200 +++ b/test/Grids/tensor_grid_test.jl Tue Aug 15 22:39:34 2023 +0200 @@ -61,15 +61,15 @@ end @testset "firstindex" begin - @test_broken firstindex(TensorGrid(g₁, g₂, g₃), 1) == 1 - @test_broken firstindex(TensorGrid(g₁, g₂, g₃), 2) == 1 - @test_broken firstindex(TensorGrid(g₁, g₂, g₃), 3) == 1 + @test firstindex(TensorGrid(g₁, g₂, g₃), 1) == 1 + @test firstindex(TensorGrid(g₁, g₂, g₃), 2) == 1 + @test firstindex(TensorGrid(g₁, g₂, g₃), 3) == 1 end @testset "lastindex" begin - @test_broken lastindex(TensorGrid(g₁, g₂, g₃), 1) == 11 - @test_broken lastindex(TensorGrid(g₁, g₂, g₃), 2) == 6 - @test_broken lastindex(TensorGrid(g₁, g₂, g₃), 3) == 10 + @test lastindex(TensorGrid(g₁, g₂, g₃), 1) == 11 + @test lastindex(TensorGrid(g₁, g₂, g₃), 2) == 6 + @test lastindex(TensorGrid(g₁, g₂, g₃), 3) == 10 end end