From bcd9e4a9754b226715cd1acf79d42f8af58ad0fc Mon Sep 17 00:00:00 2001 From: Jeff Fessler Date: Sat, 11 May 2024 10:12:33 -0400 Subject: [PATCH] Support Char ranges --- src/ndgrid-avect.jl | 2 +- src/ndgrid-range.jl | 13 +++++++------ test/ndgrid-avect.jl | 2 +- test/ndgrid-range.jl | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ndgrid-avect.jl b/src/ndgrid-avect.jl index 53ae286..104cc2b 100644 --- a/src/ndgrid-avect.jl +++ b/src/ndgrid-avect.jl @@ -42,7 +42,7 @@ _grid(d::Int, dims::Dims, v::AbstractVector) = GridAV(dims, v, d) _grid_type(d::Int, D::Int, ::Base.OneTo{T}) where T = GridOT{T, d, D} _grid_type(d::Int, D::Int, ::UnitRange{T}) where T = GridUR{T, d, D} _grid_type(d::Int, D::Int, ::StepRangeLen{T,R,S}) where {T,R,S} = GridSL{T, d, D, R, S} -_grid_type(d::Int, D::Int, ::AbstractRange{T}) where T = GridAR{T, d, D} +_grid_type(d::Int, D::Int, v::AbstractRange{T}) where T = GridAR{T, d, D, typeof(step(v))} _grid_type(d::Int, D::Int, ::AbstractVector{T}) where T = GridAV{T, d, D} diff --git a/src/ndgrid-range.jl b/src/ndgrid-range.jl index a9f89f5..22d20aa 100644 --- a/src/ndgrid-range.jl +++ b/src/ndgrid-range.jl @@ -5,26 +5,27 @@ ndgrid type for an AbstractRange input. """ - GridAR{T,d,D} <: AbstractGrid{T,d,D} + GridAR{T,d,D,S} <: AbstractGrid{T,d,D} The `d`th component of `D`-dimensional `ndgrid(v₁, v₂, ...)` where `1 ≤ d ≤ D` and `v_d` is an `AbstractRange`. """ -struct GridAR{T,d,D} <: AbstractGrid{T,d,D} +struct GridAR{T,d,D,S} <: AbstractGrid{T,d,D} dims::Dims{D} first0::T # first - step - step::T + step::S function GridAR(dims::Dims{D}, v::AbstractRange{T}, d::Int) where {D, T} 1 ≤ d ≤ D || throw(ArgumentError("$d for $dims")) - new{T,d,D}(dims, first(v) - step(v), step(v)) + S = typeof(step(v)) + new{T,d,D,S}(dims, first(v) - step(v), step(v)) end end @inline Base.@propagate_inbounds function Base.getindex( - a::GridAR{T,d,D}, + a::GridAR{T,d,D,S}, i::Vararg{Int,D}, -) where {T,d,D} +) where {T,d,D,S} @boundscheck checkbounds(a, i...) return a.first0 + (@inbounds i[d]) * a.step end diff --git a/test/ndgrid-avect.jl b/test/ndgrid-avect.jl index 4112196..1d5807b 100644 --- a/test/ndgrid-avect.jl +++ b/test/ndgrid-avect.jl @@ -1,4 +1,4 @@ -# ndgrid-avect.jl +# ndgrid-avect.jl test AbstractVector types using LazyGrids: ndgrid, ndgrid_array, GridAV, GridUR using Test: @test, @testset, @test_throws, @inferred diff --git a/test/ndgrid-range.jl b/test/ndgrid-range.jl index 3ce821b..94c7e71 100644 --- a/test/ndgrid-range.jl +++ b/test/ndgrid-range.jl @@ -4,6 +4,21 @@ using LazyGrids: ndgrid, ndgrid_array, GridAR, GridSL, GridUR using Test: @test, @testset, @test_throws, @inferred +@testset "cvect" begin + x = 'a':9:'z' # character range + y = 1:2 + xx, yy = @inferred ndgrid(x, y) + @test eltype(xx) === eltype(x) + @test eltype(yy) === eltype(y) + @test size(xx) === (length(x), length(y)) + @test size(yy) === (length(x), length(y)) + @test xx isa GridAR + @test yy isa GridUR + @test xx[:,1] == x + @test yy[1,:] == y +end + + @testset "StepRangeLen" begin (x, y) = (1:3, (-3:4)/11) (xa, ya) = @inferred ndgrid_array(x, y)