@@ -11,10 +11,10 @@ i.e., it's the "identity," which is the origin of the "Id" in `IdOffsetRange`.
1111The most common case is shifting a range that starts at 1 (either `1:n` or `Base.OneTo(n)`):
1212```jldoctest; setup=:(import OffsetArrays)
1313julia> ro = OffsetArrays.IdOffsetRange(1:3, -2)
14- OffsetArrays.IdOffsetRange(-1:1)
14+ OffsetArrays.IdOffsetRange(values=-1:1, indices= -1:1)
1515
1616julia> axes(ro, 1)
17- OffsetArrays.IdOffsetRange(-1:1)
17+ OffsetArrays.IdOffsetRange(values=-1:1, indices= -1:1)
1818
1919julia> ro[-1]
2020-1
@@ -26,10 +26,10 @@ ERROR: BoundsError: attempt to access 3-element UnitRange{$Int} at index [5]
2626If the range doesn't start at 1, the values may be different from the indices:
2727```jldoctest; setup=:(import OffsetArrays)
2828julia> ro = OffsetArrays.IdOffsetRange(11:13, -2)
29- OffsetArrays.IdOffsetRange(9:11)
29+ OffsetArrays.IdOffsetRange(values= 9:11, indices=-1:1 )
3030
3131julia> axes(ro, 1) # 11:13 is indexed by 1:3, and the offset is also applied to the axes
32- OffsetArrays.IdOffsetRange(-1:1)
32+ OffsetArrays.IdOffsetRange(values=-1:1, indices= -1:1)
3333
3434julia> ro[-1]
35359
@@ -41,7 +41,7 @@ ERROR: BoundsError: attempt to access 3-element UnitRange{$Int} at index [5]
4141# Extended help
4242
4343Construction/coercion preserves the (shifted) values of the input range, but may modify
44- the indexes if required by the specified types. For example,
44+ the indices if required by the specified types. For example,
4545
4646 r = OffsetArrays.IdOffsetRange{Int,UnitRange{Int}}(3:4)
4747
@@ -78,10 +78,10 @@ struct IdOffsetRange{T<:Integer,I<:AbstractUnitRange{T}} <: AbstractUnitRange{T}
7878 offset:: T
7979
8080 IdOffsetRange {T,I} (r:: I , offset:: T ) where {T<: Integer ,I<: AbstractUnitRange{T} } = new {T,I} (r, offset)
81-
82- #= This method is necessary to avoid a StackOverflowError in IdOffsetRange{T,I}(r::IdOffsetRange, offset::Integer).
83- The type signature in that method is more specific than IdOffsetRange{T,I}(r::I, offset::T),
84- so it ends up calling itself if I <: IdOffsetRange.
81+
82+ #= This method is necessary to avoid a StackOverflowError in IdOffsetRange{T,I}(r::IdOffsetRange, offset::Integer).
83+ The type signature in that method is more specific than IdOffsetRange{T,I}(r::I, offset::T),
84+ so it ends up calling itself if I <: IdOffsetRange.
8585 =#
8686 function IdOffsetRange {T,IdOffsetRange{T,I}} (r:: IdOffsetRange{T,I} , offset:: T ) where {T<: Integer ,I<: AbstractUnitRange{T} }
8787 new {T,IdOffsetRange{T,I}} (r, offset)
@@ -111,8 +111,15 @@ function IdOffsetRange{T}(r::IdOffsetRange, offset::Integer = 0) where T<:Intege
111111end
112112IdOffsetRange (r:: IdOffsetRange ) = r
113113
114+ # Constructor to make `show` round-trippable
115+ function IdOffsetRange (; values:: AbstractUnitRange{<:Integer} , indices:: AbstractUnitRange{<:Integer} )
116+ length (values) == length (indices) || throw (ArgumentError (" values and indices must have the same length" ))
117+ offset = first (indices) - 1
118+ return IdOffsetRange (values .- offset, offset)
119+ end
120+
114121# TODO : uncomment these when Julia is ready
115- # # Conversion preserves both the values and the indexes , throwing an InexactError if this
122+ # # Conversion preserves both the values and the indices , throwing an InexactError if this
116123# # is not possible.
117124# Base.convert(::Type{IdOffsetRange{T,I}}, r::IdOffsetRange{T,I}) where {T<:Integer,I<:AbstractUnitRange{T}} = r
118125# Base.convert(::Type{IdOffsetRange{T,I}}, r::IdOffsetRange) where {T<:Integer,I<:AbstractUnitRange{T}} =
@@ -175,7 +182,7 @@ Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdO
175182Broadcast. broadcasted (:: Base.Broadcast.DefaultArrayStyle{1} , :: typeof (+ ), x:: Integer , r:: IdOffsetRange{T} ) where T =
176183 IdOffsetRange {T} (x .+ r. parent, r. offset)
177184
178- Base. show (io:: IO , r:: IdOffsetRange ) = print (io, " OffsetArrays. IdOffsetRange( " ,first (r), ' :' , last (r)," )" )
185+ Base. show (io:: IO , r:: IdOffsetRange ) = print (io, IdOffsetRange, " (values= " ,first (r), ' :' , last (r)," , indices= " , first ( eachindex (r)), ' : ' , last ( eachindex (r)), " )" )
179186
180187# Optimizations
181188@inline Base. checkindex (:: Type{Bool} , inds:: IdOffsetRange , i:: Real ) = Base. checkindex (Bool, inds. parent, i - inds. offset)
0 commit comments