Skip to content

Commit

Permalink
Support Char ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFessler committed May 11, 2024
1 parent 118d4f2 commit bcd9e4a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ndgrid-avect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down
13 changes: 7 additions & 6 deletions src/ndgrid-range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/ndgrid-avect.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/ndgrid-range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bcd9e4a

Please sign in to comment.