Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DimIndices constructors #910

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/dimindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ end

abstract type AbstractDimIndices{T,N,D} <: AbstractDimArrayGenerator{T,N,D} end

(::Type{T})(::Nothing; kw...) where T<:AbstractDimIndices = throw(ArgumentError("Object has no `dims` method"))
(::Type{T})(x; kw...) where T<:AbstractDimIndices = T(dims(x); kw...)
(::Type{T})(dim::Dimension; kw...) where T<:AbstractDimIndices = T((dim,); kw...)

"""
DimIndices <: AbstractArray

Expand Down Expand Up @@ -121,6 +117,9 @@ function DimIndices(dims::D) where {D<:Tuple{Vararg{Dimension}}}
dims = N > 0 ? _dimindices_format(dims) : dims
DimIndices{T,N,typeof(dims)}(dims)
end
DimIndices(x) = DimIndices(dims(x))
DimIndices(dim::Dimension) = DimIndices((dim,))
DimIndices(::Nothing) = throw(ArgumentError("Object has no `dims` method"))

# Forces multiple indices not linear
function Base.getindex(di::DimIndices, i1::Integer, i2::Integer, I::Integer...)
Expand All @@ -146,6 +145,11 @@ _dimindices_axis(x::Lookup) = axes(x, 1)
_dimindices_axis(x) =
throw(ArgumentError("`$x` is not a valid input for `DimIndices`. Use `Dimension`s wrapping `Integer`, `AbstractArange{<:Integer}`, or a `Lookup` (the `axes` will be used)"))

abstract type AbstractDimVals{T,N,D} <: AbstractDimIndices{T,N,D} end

(::Type{T})(::Nothing; kw...) where T<:AbstractDimVals = throw(ArgumentError("Object has no `dims` method"))
(::Type{T})(x; kw...) where T<:AbstractDimVals = T(dims(x); kw...)
(::Type{T})(dim::Dimension; kw...) where T<:AbstractDimVals = T((dim,); kw...)

"""
DimPoints <: AbstractArray
Expand All @@ -165,7 +169,7 @@ that defines a `dims` method can be passed in.

- `order`: determines the order of the points, the same as the order of `dims` by default.
"""
struct DimPoints{T,N,D<:Tuple{Vararg{Dimension}},O} <: AbstractDimIndices{T,N,D}
struct DimPoints{T,N,D<:Tuple{Vararg{Dimension}},O} <: AbstractDimVals{T,N,D}
dims::D
order::O
end
Expand Down Expand Up @@ -241,7 +245,7 @@ Using `At` would make sure we only use exact interpolation,
while `Contains` with sampling of `Intervals` would make sure that
each values is taken only from an Interval that is present in the lookups.
"""
struct DimSelectors{T,N,D<:Tuple{Vararg{Dimension}},S<:Tuple} <: AbstractDimIndices{T,N,D}
struct DimSelectors{T,N,D<:Tuple{Vararg{Dimension}},S<:Tuple} <: AbstractDimVals{T,N,D}
dims::D
selectors::S
end
Expand Down
3 changes: 3 additions & 0 deletions test/dimindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ A = zeros(X(4.0:7.0), Y(10.0:12.0))
@test dims(di0) == ()
@test size(di0) == ()
end
@testset "keywords error" begin
@test_throws MethodError DimIndices(A; order=ForwardOrdered())
end
end

@testset "DimPoints" begin
Expand Down
Loading