@@ -11,10 +11,10 @@ i.e., it's the "identity," which is the origin of the "Id" in `IdOffsetRange`.
11
11
The most common case is shifting a range that starts at 1 (either `1:n` or `Base.OneTo(n)`):
12
12
```jldoctest; setup=:(import OffsetArrays)
13
13
julia> ro = OffsetArrays.IdOffsetRange(1:3, -2)
14
- OffsetArrays.IdOffsetRange(-1:1)
14
+ OffsetArrays.IdOffsetRange(values=-1:1, indices= -1:1)
15
15
16
16
julia> axes(ro, 1)
17
- OffsetArrays.IdOffsetRange(-1:1)
17
+ OffsetArrays.IdOffsetRange(values=-1:1, indices= -1:1)
18
18
19
19
julia> ro[-1]
20
20
-1
@@ -26,10 +26,10 @@ ERROR: BoundsError: attempt to access 3-element UnitRange{$Int} at index [5]
26
26
If the range doesn't start at 1, the values may be different from the indices:
27
27
```jldoctest; setup=:(import OffsetArrays)
28
28
julia> ro = OffsetArrays.IdOffsetRange(11:13, -2)
29
- OffsetArrays.IdOffsetRange(9:11)
29
+ OffsetArrays.IdOffsetRange(values= 9:11, indices=-1:1 )
30
30
31
31
julia> 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)
33
33
34
34
julia> ro[-1]
35
35
9
@@ -41,7 +41,7 @@ ERROR: BoundsError: attempt to access 3-element UnitRange{$Int} at index [5]
41
41
# Extended help
42
42
43
43
Construction/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,
45
45
46
46
r = OffsetArrays.IdOffsetRange{Int,UnitRange{Int}}(3:4)
47
47
@@ -78,10 +78,10 @@ struct IdOffsetRange{T<:Integer,I<:AbstractUnitRange{T}} <: AbstractUnitRange{T}
78
78
offset:: T
79
79
80
80
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.
85
85
=#
86
86
function IdOffsetRange {T,IdOffsetRange{T,I}} (r:: IdOffsetRange{T,I} , offset:: T ) where {T<: Integer ,I<: AbstractUnitRange{T} }
87
87
new {T,IdOffsetRange{T,I}} (r, offset)
@@ -111,8 +111,15 @@ function IdOffsetRange{T}(r::IdOffsetRange, offset::Integer = 0) where T<:Intege
111
111
end
112
112
IdOffsetRange (r:: IdOffsetRange ) = r
113
113
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
+
114
121
# 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
116
123
# # is not possible.
117
124
# Base.convert(::Type{IdOffsetRange{T,I}}, r::IdOffsetRange{T,I}) where {T<:Integer,I<:AbstractUnitRange{T}} = r
118
125
# 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
175
182
Broadcast. broadcasted (:: Base.Broadcast.DefaultArrayStyle{1} , :: typeof (+ ), x:: Integer , r:: IdOffsetRange{T} ) where T =
176
183
IdOffsetRange {T} (x .+ r. parent, r. offset)
177
184
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)), " )" )
179
186
180
187
# Optimizations
181
188
@inline Base. checkindex (:: Type{Bool} , inds:: IdOffsetRange , i:: Real ) = Base. checkindex (Bool, inds. parent, i - inds. offset)
0 commit comments