diff --git a/Project.toml b/Project.toml index bea3a262..7c60d66b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OffsetArrays" uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.12.4" +version = "1.12.5" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" @@ -12,7 +12,7 @@ CatIndices = "0.2" DistributedArrays = "0.6" Documenter = "0.27" EllipsisNotation = "1" -FillArrays = "0.11" +FillArrays = "0.11, 0.13" StaticArrays = "1" julia = "0.7, 1" diff --git a/src/axes.jl b/src/axes.jl index 7b977ac3..bf93a1f2 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -175,6 +175,16 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange = @inline Base.unsafe_indices(r::IdOffsetRange) = (Base.axes1(r),) @inline Base.length(r::IdOffsetRange) = length(r.parent) @inline Base.isempty(r::IdOffsetRange) = isempty(r.parent) +#= We specialize on reduced_indices to work around cases where the parent axis type doesn't +support reduced_index, but the axes do support reduced_indices +The difference is that reduced_index expects the axis type to remain unchanged, +which may not always be possible, eg. for statically sized axes +See https://github.com/JuliaArrays/OffsetArrays.jl/issues/204 +=# +function Base.reduced_indices(inds::Tuple{IdOffsetRange, Vararg{IdOffsetRange}}, d::Int) + parents_reduced = Base.reduced_indices(map(parent, inds), d) + ntuple(i -> IdOffsetRange(parents_reduced[i], inds[i].offset), Val(length(inds))) +end Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i)) # Workaround for #92 on Julia < 1.4 Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i)) diff --git a/test/Project.toml b/test/Project.toml index 4b0051ec..096d6bf5 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -18,5 +18,5 @@ CatIndices = "0.2" DistributedArrays = "0.6" Documenter = "0.27" EllipsisNotation = "1" -FillArrays = "0.11" +FillArrays = "0.11, 0.13" StaticArrays = "1" diff --git a/test/runtests.jl b/test/runtests.jl index aa78dad7..5b495ffb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -234,6 +234,17 @@ end @test length(rred) == 1 @test first(rred) == first(r) + @testset "reduced_indices" begin + a = reshape(1:24, 2, 3, 4) + sa = OffsetArray(a, (2, 3, 4)); + @testset for dim in 1:ndims(sa) + sasum = sum(sa, dims = dim) + @test parent(sasum) == sum(a, dims = dim) + find = firstindex(sa, dim) + @test axes(sasum, dim) == find:find + end + end + @testset "conversion to AbstractUnitRange" begin r = IdOffsetRange(1:2) @test AbstractUnitRange{Int}(r) === r