Skip to content

Commit

Permalink
Fix IdOffsetRange kwargs constructor given offset ranges (#303)
Browse files Browse the repository at this point in the history
* fix IdOffsetRange kwargs constructor

* preserve types if indices are 1-based

* no_offset_view in values

* remove test for SOneTo (#301)

* Add tests

* bump version to v1.12.4
  • Loading branch information
jishnub authored Jun 7, 2022
1 parent 0154f2c commit bec1ae2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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.3"
version = "1.12.4"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
7 changes: 6 additions & 1 deletion src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,15 @@ end
IdOffsetRange(r::IdOffsetRange) = r

# Constructor to make `show` round-trippable
# try to preserve typeof(values) if the indices are known to be 1-based
_subtractindexoffset(values, indices::Union{Base.OneTo, IdentityUnitRange{<:Base.OneTo}}, offset) = values
_subtractindexoffset(values, indices, offset) = _subtractoffset(values, offset)
function IdOffsetRange(; values::AbstractUnitRange{<:Integer}, indices::AbstractUnitRange{<:Integer})
length(values) == length(indices) || throw(ArgumentError("values and indices must have the same length"))
values_nooffset = no_offset_view(values)
offset = first(indices) - 1
return IdOffsetRange(values .- offset, offset)
values_minus_offset = _subtractindexoffset(values_nooffset, indices, offset)
return IdOffsetRange(values_minus_offset, offset)
end

# Conversions to an AbstractUnitRange{Int} (and to an OrdinalRange{Int,Int} on Julia v"1.6") are necessary
Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ end
@test_throws MethodError IdOffsetRange(7:9, indices=-1:1)
@test_throws MethodError IdOffsetRange(-1:1, values=7:9)

p = IdOffsetRange(1:3, 2)
q = IdOffsetRange(values = p .- 2, indices = p)
@test same_value(q, 1:3)
check_indexed_by(q, p)

@testset for indices in Any[Base.OneTo(3), IdentityUnitRange(Base.OneTo(3))]
p = IdOffsetRange(values = IdOffsetRange(1:3, 2), indices = indices)
@test same_value(p, 3:5)
check_indexed_by(p, 1:3)
q = IdOffsetRange(values = Base.OneTo(3), indices = indices)
@test same_value(q, 1:3)
@test q isa IdOffsetRange{Int, Base.OneTo{Int}}
end

# conversion preserves both the values and the axes, throwing an error if this is not possible
@test @inferred(oftype(ro, ro)) === ro
@test @inferred(convert(OffsetArrays.IdOffsetRange{Int}, ro)) === ro
Expand Down

2 comments on commit bec1ae2

@jishnub
Copy link
Member Author

@jishnub jishnub commented on bec1ae2 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/61873

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.4 -m "<description of version>" bec1ae29a1f13f2ec9d871ebe3d1a9757cfb9648
git push origin v1.12.4

Please sign in to comment.