Skip to content

Commit bec1ae2

Browse files
authored
Fix IdOffsetRange kwargs constructor given offset ranges (#303)
* 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
1 parent 0154f2c commit bec1ae2

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.3"
3+
version = "1.12.4"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/axes.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,15 @@ end
128128
IdOffsetRange(r::IdOffsetRange) = r
129129

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

137142
# Conversions to an AbstractUnitRange{Int} (and to an OrdinalRange{Int,Int} on Julia v"1.6") are necessary

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ end
138138
@test_throws MethodError IdOffsetRange(7:9, indices=-1:1)
139139
@test_throws MethodError IdOffsetRange(-1:1, values=7:9)
140140

141+
p = IdOffsetRange(1:3, 2)
142+
q = IdOffsetRange(values = p .- 2, indices = p)
143+
@test same_value(q, 1:3)
144+
check_indexed_by(q, p)
145+
146+
@testset for indices in Any[Base.OneTo(3), IdentityUnitRange(Base.OneTo(3))]
147+
p = IdOffsetRange(values = IdOffsetRange(1:3, 2), indices = indices)
148+
@test same_value(p, 3:5)
149+
check_indexed_by(p, 1:3)
150+
q = IdOffsetRange(values = Base.OneTo(3), indices = indices)
151+
@test same_value(q, 1:3)
152+
@test q isa IdOffsetRange{Int, Base.OneTo{Int}}
153+
end
154+
141155
# conversion preserves both the values and the axes, throwing an error if this is not possible
142156
@test @inferred(oftype(ro, ro)) === ro
143157
@test @inferred(convert(OffsetArrays.IdOffsetRange{Int}, ro)) === ro

0 commit comments

Comments
 (0)