Skip to content

Commit 9b018f0

Browse files
authored
Forward reduced_indices to parent (#297)
* forward reduced_indices to parent * Add test for standard arrays * remove explicit test against StaticArrays
1 parent bec1ae2 commit 9b018f0

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.12.4"
3+
version = "1.12.5"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -12,7 +12,7 @@ CatIndices = "0.2"
1212
DistributedArrays = "0.6"
1313
Documenter = "0.27"
1414
EllipsisNotation = "1"
15-
FillArrays = "0.11"
15+
FillArrays = "0.11, 0.13"
1616
StaticArrays = "1"
1717
julia = "0.7, 1"
1818

src/axes.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange =
175175
@inline Base.unsafe_indices(r::IdOffsetRange) = (Base.axes1(r),)
176176
@inline Base.length(r::IdOffsetRange) = length(r.parent)
177177
@inline Base.isempty(r::IdOffsetRange) = isempty(r.parent)
178+
#= We specialize on reduced_indices to work around cases where the parent axis type doesn't
179+
support reduced_index, but the axes do support reduced_indices
180+
The difference is that reduced_index expects the axis type to remain unchanged,
181+
which may not always be possible, eg. for statically sized axes
182+
See https://github.com/JuliaArrays/OffsetArrays.jl/issues/204
183+
=#
184+
function Base.reduced_indices(inds::Tuple{IdOffsetRange, Vararg{IdOffsetRange}}, d::Int)
185+
parents_reduced = Base.reduced_indices(map(parent, inds), d)
186+
ntuple(i -> IdOffsetRange(parents_reduced[i], inds[i].offset), Val(length(inds)))
187+
end
178188
Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i))
179189
# Workaround for #92 on Julia < 1.4
180190
Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i))

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ CatIndices = "0.2"
1818
DistributedArrays = "0.6"
1919
Documenter = "0.27"
2020
EllipsisNotation = "1"
21-
FillArrays = "0.11"
21+
FillArrays = "0.11, 0.13"
2222
StaticArrays = "1"

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ end
234234
@test length(rred) == 1
235235
@test first(rred) == first(r)
236236

237+
@testset "reduced_indices" begin
238+
a = reshape(1:24, 2, 3, 4)
239+
sa = OffsetArray(a, (2, 3, 4));
240+
@testset for dim in 1:ndims(sa)
241+
sasum = sum(sa, dims = dim)
242+
@test parent(sasum) == sum(a, dims = dim)
243+
find = firstindex(sa, dim)
244+
@test axes(sasum, dim) == find:find
245+
end
246+
end
247+
237248
@testset "conversion to AbstractUnitRange" begin
238249
r = IdOffsetRange(1:2)
239250
@test AbstractUnitRange{Int}(r) === r

0 commit comments

Comments
 (0)