Skip to content

Commit 435ed6e

Browse files
authored
Merge pull request #3 from JuliaArrays/mb/inbounds
Update for 0.5 inbounds
2 parents b0ee8f1 + 0c57a71 commit 435ed6e

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.4
87
- 0.5
98
- nightly
109
notifications:

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
julia 0.4
1+
julia 0.5
22
Compat 0.19

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
environment:
22
matrix:
3-
- JULIAVERSION: "julialang/bin/winnt/x86/0.4/julia-0.4-latest-win32.exe"
4-
- JULIAVERSION: "julialang/bin/winnt/x64/0.4/julia-0.4-latest-win64.exe"
53
- JULIAVERSION: "julialang/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
64
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
75
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"

src/RangeArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
__precompile__()
12
module RangeArrays
23

34
using Compat

src/matrix.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,27 @@ Base.size(R::RangeMatrix) = R.dims
2727
@compat Base.IndexStyle(::Type{<:RangeMatrix}) = IndexCartesian()
2828

2929
# Scalar indexing
30-
Base.getindex(R::RangeMatrix, i::Int, j::Int) = (checkbounds(R, i, j); Base.unsafe_getindex(R, i, j))
31-
Base.unsafe_getindex(R::RangeMatrix, i::Int, j::Int) = @inbounds return R.rs[j][i]
30+
@inline function Base.getindex(R::RangeMatrix, i::Int, j::Int)
31+
@boundscheck checkbounds(R, i, j);
32+
@inbounds return R.rs[j][i]
33+
end
3234

3335
# For non-scalar indexing, only specialize with inner Ranges and Colons to
3436
# return Ranges or RangeMatrixes. For everything else, we can use the fallbacks.
35-
Base.getindex(R::RangeMatrix, I::Union{Range, Colon}, J) = (checkbounds(R, I, J); Base.unsafe_getindex(R, I, J))
36-
Base.unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, j::Real) = @inbounds return R.rs[j][I]
37-
Base.unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, ::Colon) = @inbounds return RangeMatrix([R.rs[j][I] for j=1:length(R.rs)])
38-
Base.unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, J) = @inbounds return RangeMatrix([R.rs[j][I] for j in J])
37+
@inline function Base.getindex(R::RangeMatrix, I::Union{Range, Colon}, J)
38+
@boundscheck checkbounds(R, I, J)
39+
unsafe_getindex(R, I, J)
40+
end
41+
@inline unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, j::Real) = @inbounds return R.rs[j][I]
42+
@inline unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, ::Colon) = @inbounds return RangeMatrix([R.rs[j][I] for j=1:length(R.rs)])
43+
@inline unsafe_getindex(R::RangeMatrix, I::Union{Range, Colon}, J) = @inbounds return RangeMatrix([R.rs[j][I] for j in J])
44+
45+
# We can also optimize bounds checks to only look at each range's endpoints
46+
function Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, R::RangeMatrix)
47+
b = true
48+
@inbounds for r in R.rs
49+
b &= checkindex(Bool, inds, r[1])
50+
b &= checkindex(Bool, inds, r[end])
51+
end
52+
b
53+
end

src/repeatedrange.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,28 @@ Base.size(R::RepeatedRangeMatrix) = (length(R.r), length(R.at))
1515
@compat Base.IndexStyle(::Type{<:RepeatedRangeMatrix}) = IndexCartesian()
1616

1717
# Scalar indexing
18-
Base.getindex(R::RepeatedRangeMatrix, i::Int, j::Int) = (checkbounds(R, i, j); Base.unsafe_getindex(R, i, j))
19-
Base.unsafe_getindex(R::RepeatedRangeMatrix, i::Int, j::Int) = @inbounds return R.r[i] + R.at[j]
18+
@inline function Base.getindex(R::RepeatedRangeMatrix, i::Int, j::Int)
19+
@boundscheck checkbounds(R, i, j)
20+
@inbounds return R.r[i] + R.at[j]
21+
end
2022

2123
# For non-scalar indexing, only specialize with inner Ranges and Colons to
2224
# return Ranges or RangeMatrixes. For everything else, we can use the fallbacks.
23-
Base.getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, J) = (checkbounds(R, I, J); Base.unsafe_getindex(R, I, J))
24-
Base.unsafe_getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, j::Real) = @inbounds return R.r[I] + R.at[j]
25-
Base.unsafe_getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, ::Colon) = @inbounds return RepeatedRangeMatrix(R.r[I], R.at[:])
26-
Base.unsafe_getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, J) = @inbounds return RepeatedRangeMatrix(R.r[I], R.at[J])
25+
@inline function Base.getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, j::Real)
26+
@boundscheck checkbounds(R, I, j)
27+
@inbounds return R.r[I] + R.at[j]
28+
end
29+
@inline function Base.getindex(R::RepeatedRangeMatrix, I::Union{Range, Colon}, J)
30+
@boundscheck checkbounds(R, I, J)
31+
@inbounds return RepeatedRangeMatrix(R.r[I], R.at[J])
32+
end
33+
34+
# We can also optimize bounds checks to only look at the range's endpoints
35+
function Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, R::RepeatedRangeMatrix)
36+
b = true
37+
@inbounds for a in R.at
38+
b &= checkindex(Bool, inds, R.r[1] + a)
39+
b &= checkindex(Bool, inds, R.r[end] + a)
40+
end
41+
b
42+
end

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,18 @@ end
5959
@test R[:, 1:2] == RangeMatrix(1:10, 11:20)
6060
@test R[:, 1:4] == R[:,:] == RangeMatrix(1:10, 11:20, 21:30, 31:40)
6161
@test R[:, 3:4] == RangeMatrix(21:30, 31:40)
62+
63+
A = 1:100
64+
@test_throws BoundsError A[RangeMatrix(0:9, 20:29)]
65+
@test_throws BoundsError A[RangeMatrix(20:29, 0:9)]
66+
@test_throws BoundsError A[RangeMatrix(20:29, 92:101)]
67+
@test_throws BoundsError A[RangeMatrix(92:101, 20:29)]
68+
@test_throws BoundsError A[RangeMatrix(-100:100)]
69+
@test A[RangeMatrix(1:10, 91:100)] == [1:10 91:100]
70+
71+
@test_throws BoundsError A[RepeatedRangeMatrix(1:10, [-1,33])]
72+
@test_throws BoundsError A[RepeatedRangeMatrix(1:10, [33,-1])]
73+
@test_throws BoundsError A[RepeatedRangeMatrix(1:10, [91,33])]
74+
@test_throws BoundsError A[RepeatedRangeMatrix(1:10, [33,91])]
75+
@test_throws BoundsError A[RepeatedRangeMatrix(-100:100, [0])]
76+
@test A[RepeatedRangeMatrix(1:10, [0,90])] == [1:10 91:100]

0 commit comments

Comments
 (0)