Skip to content

Commit

Permalink
Lower [:] as Colon instead of a range
Browse files Browse the repository at this point in the history
Cherry-picked from a squashed-and-split version of #9150 to exclude the bigger change of returning views from indexing with UnitRanges.

Conflicts:
	base/abstractarray.jl
	base/multidimensional.jl
	src/julia-syntax.scm
  • Loading branch information
andreasnoack authored and mbauman committed Feb 25, 2015
1 parent ee316f2 commit 36227a2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ linearindexing{A<:Array}(::Type{A}) = LinearFast()
linearindexing{A<:Range}(::Type{A}) = LinearFast()

## Bounds checking ##
checkbounds(sz::Int, ::Colon) = nothing
checkbounds(sz::Int, i::Int) = 1 <= i <= sz || throw(BoundsError())
checkbounds(sz::Int, i::Real) = checkbounds(sz, to_index(i))
checkbounds(sz::Int, I::AbstractVector{Bool}) = length(I) == sz || throw(BoundsError())
checkbounds(sz::Int, r::Range{Int}) = isempty(r) || (minimum(r) >= 1 && maximum(r) <= sz) || throw(BoundsError())
checkbounds{T<:Real}(sz::Int, r::Range{T}) = checkbounds(sz, to_index(r))
checkbounds(sc::Int, ::Colon) = true

function checkbounds{T <: Real}(sz::Int, I::AbstractArray{T})
for i in I
Expand All @@ -131,17 +131,17 @@ checkbounds(A::AbstractArray, I::AbstractArray{Bool}) = size(A) == size(I) || th

checkbounds(A::AbstractArray, I) = checkbounds(length(A), I)

function checkbounds(A::AbstractMatrix, I::Union(Real,Colon,AbstractArray), J::Union(Real,Colon,AbstractArray))
function checkbounds(A::AbstractMatrix, I::Union(Real,AbstractArray,Colon), J::Union(Real,AbstractArray,Colon))
checkbounds(size(A,1), I)
checkbounds(size(A,2), J)
end

function checkbounds(A::AbstractArray, I::Union(Real,Colon,AbstractArray), J::Union(Real,Colon,AbstractArray))
function checkbounds(A::AbstractArray, I::Union(Real,AbstractArray,Colon), J::Union(Real,AbstractArray,Colon))
checkbounds(size(A,1), I)
checkbounds(trailingsize(A,2), J)
end

function checkbounds(A::AbstractArray, I::Union(Real,Colon,AbstractArray)...)
function checkbounds(A::AbstractArray, I::Union(Real,AbstractArray,Colon)...)
n = length(I)
if n > 0
for dim = 1:(n-1)
Expand Down
1 change: 1 addition & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ function setindex!{T<:Real}(A::Array, X::AbstractArray, I::AbstractVector{T})
return A
end

setindex!(A::Array, x, I::Colon) = setindex!(A, x, 1:length(x))

# logical indexing
# (when the indexing is provided as an Array{Bool} or a BitArray we can be
Expand Down
6 changes: 3 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ _getindex(A, I::(Union(Int,AbstractVector)...)) =

# The stagedfunction here is just to work around the performance hit
# of splatting
stagedfunction getindex(A::Array, I::Union(Real,AbstractVector)...)
stagedfunction getindex(A::Array, I::Union(Real,AbstractVector,Colon)...)
N = length(I)
Isplat = Expr[:(I[$d]) for d = 1:N]
quote
Expand All @@ -246,7 +246,7 @@ stagedfunction getindex(A::Array, I::Union(Real,AbstractVector)...)
end

# Also a safe version of getindex!
stagedfunction getindex!(dest, src, I::Union(Real,AbstractVector)...)
stagedfunction getindex!(dest, src, I::Union(Real,AbstractVector,Colon)...)
N = length(I)
Isplat = Expr[:(I[$d]) for d = 1:N]
Jsplat = Expr[:(to_index(I[$d])) for d = 1:N]
Expand All @@ -257,7 +257,7 @@ stagedfunction getindex!(dest, src, I::Union(Real,AbstractVector)...)
end


stagedfunction setindex!(A::Array, x, J::Union(Real,AbstractArray)...)
stagedfunction setindex!(A::Array, x, J::Union(Real,AbstractArray,Colon)...)
N = length(J)
if x<:AbstractArray
ex=quote
Expand Down
2 changes: 2 additions & 0 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ immutable UnitRange{T<:Real} <: OrdinalRange{T,Int}
end
UnitRange{T<:Real}(start::T, stop::T) = UnitRange{T}(start, stop)

colon() = Colon()

colon(a::Real, b::Real) = colon(promote(a,b)...)

colon{T<:Real}(start::T, stop::T) = UnitRange{T}(start, stop)
Expand Down
2 changes: 1 addition & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@

; translate index x from colons to ranges
(define (expand-index-colon x)
(cond ((eq? x ':) `(call colon 1 end))
(cond ((eq? x ':) `(call colon))
((and (pair? x)
(eq? (car x) ':))
(cond ((length= x 3)
Expand Down

0 comments on commit 36227a2

Please sign in to comment.