Skip to content

Commit 7b6d9c9

Browse files
authored
Merge pull request #74 from JuliaArrays/teh/rangeindexing
Preserve axes when indexing ranges with IdentityUnitRange
2 parents 6fa3b00 + 9b726d7 commit 7b6d9c9

File tree

4 files changed

+499
-385
lines changed

4 files changed

+499
-385
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ os:
77
julia:
88
- 0.7
99
- 1.0
10+
- 1.1
1011
- nightly
1112

1213
notifications:

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ environment:
22
matrix:
33
- julia_version: 0.7
44
- julia_version: 1.0
5+
- julia_version: 1.1
56
- julia_version: nightly
67

78
platform:

src/OffsetArrays.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,35 @@ end
160160
### Special handling for AbstractRange
161161

162162
const OffsetRange{T} = OffsetArray{T,1,<:AbstractRange{T}}
163+
const IIUR = IdentityUnitRange{S} where S<:AbstractUnitRange{T} where T<:Integer
163164

164165
Base.step(a::OffsetRange) = step(parent(a))
165166

166167
Base.getindex(a::OffsetRange, r::OffsetRange) = OffsetArray(a[parent(r)], r.offsets)
167168
Base.getindex(a::OffsetRange, r::AbstractRange) = a.parent[r .- a.offsets[1]]
168169
Base.getindex(a::AbstractRange, r::OffsetRange) = OffsetArray(a[parent(r)], r.offsets)
169170

171+
@inline @propagate_inbounds Base.getindex(r::UnitRange, s::IIUR) =
172+
OffsetArray(r[s.indices], s)
173+
174+
@inline @propagate_inbounds Base.getindex(r::StepRange, s::IIUR) =
175+
OffsetArray(r[s.indices], s)
176+
177+
@inline @propagate_inbounds Base.getindex(r::StepRangeLen{T,<:Base.TwicePrecision,<:Base.TwicePrecision}, s::IIUR) where T =
178+
OffsetArray(r[s.indices], s)
179+
@inline @propagate_inbounds Base.getindex(r::StepRangeLen{T}, s::IIUR) where {T} =
180+
OffsetArray(r[s.indices], s)
181+
182+
@inline @propagate_inbounds Base.getindex(r::LinRange, s::IIUR) =
183+
OffsetArray(r[s.indices], s)
184+
185+
function Base.show(io::IO, r::OffsetRange)
186+
show(io, r.parent)
187+
o = r.offsets[1]
188+
print(io, " with indices ", o+1:o+length(r))
189+
end
190+
Base.show(io::IO, ::MIME"text/plain", r::OffsetRange) = show(io, r)
191+
170192
### Convenience functions ###
171193

172194
Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =

0 commit comments

Comments
 (0)