Skip to content

Commit

Permalink
Forward reduced_indices to parent (#297)
Browse files Browse the repository at this point in the history
* forward reduced_indices to parent

* Add test for standard arrays

* remove explicit test against StaticArrays
  • Loading branch information
jishnub authored Jun 7, 2022
1 parent bec1ae2 commit 9b018f0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"

Expand Down
10 changes: 10 additions & 0 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 9b018f0

@jishnub
Copy link
Member Author

@jishnub jishnub commented on 9b018f0 Jun 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/61876

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.12.5 -m "<description of version>" 9b018f0aae24db5d93a2315b5581ad8316cef18f
git push origin v1.12.5

Please sign in to comment.