changeset 1092:c4ea28d904f5 refactor/grids

Fix grid indexing with CartesianIndex
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 27 Apr 2022 10:25:53 +0200
parents 9b40aeac4269
children 703eaa3e50c4 9275d95e2d90
files src/Grids/EquidistantGrid.jl test/Grids/EquidistantGrid_test.jl
diffstat 2 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/EquidistantGrid.jl	Tue Apr 26 14:28:42 2022 +0200
+++ b/src/Grids/EquidistantGrid.jl	Wed Apr 27 10:25:53 2022 +0200
@@ -44,6 +44,7 @@
 function EquidistantGrid(size, limit_lower, limit_upper)
     return EquidistantGrid{length(size), eltype(limit_lower)}(size, limit_lower, limit_upper)
 end
+# TBD: Should it be an AbstractArray?
 
 """
     EquidistantGrid{T}()
@@ -67,11 +68,14 @@
 
 Base.size(g::EquidistantGrid) = g.size
 
-function Base.getindex(g::EquidistantGrid, I...)
+function Base.getindex(g::EquidistantGrid, I::Vararg{Int})
     h = spacing(g)
     return g.limit_lower .+ (I.-1).*h
 end
 
+Base.getindex(g::EquidistantGrid, I::CartesianIndex) = g[Tuple(I)...]
+# TBD: Can this method be removed if `EquidistantGrid` is an AbstractArray?
+
 """
     dimension(grid::EquidistantGrid)
 
--- a/test/Grids/EquidistantGrid_test.jl	Tue Apr 26 14:28:42 2022 +0200
+++ b/test/Grids/EquidistantGrid_test.jl	Wed Apr 27 10:25:53 2022 +0200
@@ -52,6 +52,8 @@
         @test g[5,3] == (0.0,7.11)
 
         @test g[4,2] == (-0.25,7.11/2)
+
+        @test g[CartesianIndex(1,3)] == (-1.0,7.11)
     end
 
     # restrict