From 36227a26412244fa979073a196557c983940c46a Mon Sep 17 00:00:00 2001 From: Andreas Noack Date: Sat, 22 Nov 2014 16:53:36 -0500 Subject: [PATCH] Lower [:] as Colon instead of a range 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 --- base/abstractarray.jl | 8 ++++---- base/array.jl | 1 + base/multidimensional.jl | 6 +++--- base/range.jl | 2 ++ src/julia-syntax.scm | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 4645082550f4b..f98fbde9d51b0 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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 @@ -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) diff --git a/base/array.jl b/base/array.jl index 94c0938b1dff9..052cd69012540 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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 diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 00fdc8983291e..a9fe71a8df2b7 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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 @@ -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] @@ -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 diff --git a/base/range.jl b/base/range.jl index 4b39a9b6d9fc0..cb67fb1ed640f 100644 --- a/base/range.jl +++ b/base/range.jl @@ -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) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index c95a3bfeb1ac8..8277fd61bb7f9 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -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)