Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Return views from UnitRange indexing of Arrays #9150

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ function edit_move_up(s)
end

function edit_move_down(buf::IOBuffer)
npos = rsearch(buf.data[1:buf.size], '\n', position(buf))
npos = rsearch(copy(buf.data[1:buf.size]), '\n', position(buf))
# We're interested in character count, not byte count
offset = length(bytestring(buf.data[(npos+1):(position(buf))]))
npos2 = search(buf.data[1:buf.size], '\n', position(buf)+1)
npos2 = search(copy(buf.data[1:buf.size]), '\n', position(buf)+1)
if npos2 == 0 #we're in the last line
return false
end
Expand Down
7 changes: 4 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ 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 @@ -88,17 +89,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,AbstractArray), J::Union(Real,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,AbstractArray), J::Union(Real,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,AbstractArray)...)
function checkbounds(A::AbstractArray, I::Union(Real,AbstractArray,Colon)...)
n = length(I)
if n > 0
for dim = 1:(n-1)
Expand Down
20 changes: 11 additions & 9 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@ getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real,
arrayref(A,to_index(i0),to_index(i1),to_index(i2),to_index(i3),to_index(i4),to_index(i5),to_index(I)...)

# Fast copy using copy! for UnitRange
function getindex(A::Array, I::UnitRange{Int})
lI = length(I)
X = similar(A, lI)
if lI > 0
copy!(X, 1, A, first(I), lI)
end
return X
end
getindex(A::Array, I::UnitRange{Int}) = slice(A, I)
# function getindex(A::Array, I::UnitRange{Int})
# lI = length(I)
# X = similar(A, lI)
# if lI > 0
# copy!(X, 1, A, first(I), lI)
# end
# return X
# end

function getindex{T<:Real}(A::Array, I::AbstractVector{T})
return [ A[i] for i in to_index(I) ]
Expand Down Expand Up @@ -361,6 +362,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

Expand Down Expand Up @@ -608,7 +610,7 @@ function splice!(a::Vector, i::Integer, ins=_default_splice)
end

function splice!{T<:Integer}(a::Vector, r::UnitRange{T}, ins=_default_splice)
v = a[r]
v = copy(a[r])
m = length(ins)
if m == 0
deleteat!(a, r)
Expand Down
6 changes: 3 additions & 3 deletions base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ getindex(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')

sizeof(s::ASCIIString) = sizeof(s.data)

getindex(s::ASCIIString, r::Vector) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, r::UnitRange{Int}) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx])
getindex(s::ASCIIString, r::Vector) = ASCIIString([s.data[i] for i = r])
getindex(s::ASCIIString, r::UnitRange{Int}) = ASCIIString([s.data[i] for i = r])
getindex(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString([s.data[i] for i = indx])
search(s::ASCIIString, c::Char, i::Integer) = c < char(0x80) ? search(s.data,uint8(c),i) : 0
rsearch(s::ASCIIString, c::Char, i::Integer) = c < char(0x80) ? rsearch(s.data,uint8(c),i) : 0

Expand Down
2 changes: 1 addition & 1 deletion base/darray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function chunk_idxs(dims, chunks)
idxs, cuts
end

function localpartindex(pmap::Vector{Int})
function localpartindex(pmap::StridedVector{Int})
mi = myid()
for i = 1:length(pmap)
if pmap[i] == mi
Expand Down
2 changes: 1 addition & 1 deletion base/help.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function print_help_entries(io::IO, entries)
end
end

func_expr_from_symbols(s::Vector{Symbol}) = length(s) == 1 ? s[1] : Expr(:., func_expr_from_symbols(s[1:end-1]), Expr(:quote, s[end]))
func_expr_from_symbols(s::Vector{Symbol}) = length(s) == 1 ? s[1] : Expr(:., func_expr_from_symbols(copy(s[1:end-1])), Expr(:quote, s[end]))

function help(io::IO, fname::AbstractString, obj=0)
init_help()
Expand Down
16 changes: 8 additions & 8 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ function abstract_eval_arg(a::ANY, vtypes::ANY, sv::StaticVarInfo)
end

function abstract_eval_call(e, vtypes, sv::StaticVarInfo)
fargs = e.args[2:end]
fargs = [e.args[i] for i = 2:length(e.args)]
argtypes = tuple([abstract_eval_arg(a, vtypes, sv) for a in fargs]...)
if any(x->is(x,Bottom), argtypes)
return Bottom
Expand Down Expand Up @@ -1673,7 +1673,7 @@ function eval_annotate(e::ANY, vtypes::ANY, sv::StaticVarInfo, decls, clo)
end
if (head === :call || head === :call1) && isa(e.args[1],LambdaStaticData)
called = e.args[1]
fargs = e.args[2:end]
fargs = [e.args[i] for i = 2:length(e.args)]
argtypes = tuple([abstract_eval_arg(a, vtypes, sv) for a in fargs]...)
# recur inside inner functions once we have all types
tr,ty = typeinf(called, argtypes, called.sparams, called, false)
Expand Down Expand Up @@ -2029,7 +2029,7 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
if !(isa(f,Function) || isa(f,IntrinsicFunction))
return NF
end
argexprs = e.args[2:end]
argexprs = [e.args[i] for i = 2:length(e.args)]

if is(f, typeassert) && length(atypes)==2
# typeassert(x::S, T) => x, when S<:T
Expand Down Expand Up @@ -2214,7 +2214,7 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
needcopy = false
end
replace_tupleref!(ast, body, vaname, newnames, sv, 1)
args = vcat(args[1:na-1], newnames)
args = vcat([args[i] for i = 1:na-1], newnames)
na = length(args)

islocal = false # if the argument name is also used as a local variable,
Expand All @@ -2233,8 +2233,8 @@ function inlineable(f, e::Expr, atypes, sv, enclosing_ast)
end
else
# construct tuple-forming expression for argument tail
vararg = mk_tuplecall(argexprs[na:end])
argexprs = Any[argexprs[1:(na-1)]..., vararg]
vararg = mk_tuplecall([argexprs[i] for i = na:length(argexprs)])
argexprs = Any[[argexprs[i] for i = 1:(na-1)]..., vararg]
isva = true
end
elseif na != length(argexprs)
Expand Down Expand Up @@ -2663,7 +2663,7 @@ function inlining_pass(e::Expr, sv, ast)
end

for ninline = 1:100
atypes = tuple(Any[exprtype(x) for x in e.args[2:end]]...)
atypes = tuple(Any[exprtype(x) for x in [e.args[ea] for ea = 2:length(e.args)]]...)
if length(atypes) > MAX_TUPLETYPE_LEN
atypes = limit_tuple_type(atypes)
end
Expand Down Expand Up @@ -2694,7 +2694,7 @@ function inlining_pass(e::Expr, sv, ast)
t = to_tuple_of_Types(exprtype(aarg))
if isa(aarg,Expr) && is_known_call(aarg, tuple, sv)
# apply(f,tuple(x,y,...)) => f(x,y,...)
newargs[i-3] = aarg.args[2:end]
newargs[i-3] = [aarg.args[j] for j = 2:length(aarg.args)]
elseif isa(aarg, Tuple)
newargs[i-3] = Any[ QuoteNode(x) for x in aarg ]
elseif isa(t,Tuple) && !isvatuple(t) && effect_free(aarg,sv,true)
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const chm_l_com = fill(0xff, chm_com_sz)
## chm_com and chm_l_com must be initialized at runtime because they contain pointers
## to functions in libc.so, whose addresses can change
function cmn(::Type{Int32})
if isnan(reinterpret(Float64,chm_com[1:8])[1])
if isnan(reinterpret(Float64, copy(chm_com[1:8]))[1])
@isok ccall((:cholmod_start, :libcholmod), Cint, (Ptr{UInt8},), chm_com)
end
chm_com
end
function cmn(::Type{Int64})
if isnan(reinterpret(Float64,chm_l_com[1:8])[1])
if isnan(reinterpret(Float64, copy(chm_l_com[1:8]))[1])
@isok ccall((:cholmod_l_start, :libcholmod), Cint, (Ptr{UInt8},), chm_l_com)
end
chm_l_com
Expand Down Expand Up @@ -839,7 +839,7 @@ end

A_ldiv_B!(L::CholmodFactor, B) = L\B # Revisit this to see if allocation can be avoided. It should be possible at least for the right hand side.
(\){T<:CHMVTypes}(L::CholmodFactor{T}, B::CholmodDense{T}) = solve(L, B, CHOLMOD_A)
(\){T<:CHMVTypes}(L::CholmodFactor{T}, b::Vector{T}) = reshape(solve(L, CholmodDense!(b), CHOLMOD_A).mat, length(b))
(\){T<:CHMVTypes}(L::CholmodFactor{T}, b::StridedVector{T}) = reshape(solve(L, CholmodDense!(convert(Vector{T}, b)), CHOLMOD_A).mat, length(b))
(\){T<:CHMVTypes}(L::CholmodFactor{T}, B::Matrix{T}) = solve(L, CholmodDense!(B),CHOLMOD_A).mat
function (\){Tv<:CHMVTypes,Ti<:CHMITypes}(L::CholmodFactor{Tv,Ti},B::CholmodSparse{Tv,Ti})
solve(L,B,CHOLMOD_A)
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function inv{S}(A::StridedMatrix{S})
return convert(typeof(Ac), Ai)
end

function factorize{T}(A::Matrix{T})
function factorize{T}(A::StridedMatrix{T})
m, n = size(A)
if m == n
if m == 1 return A[1] end
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ convert{T}(::Type{Factorization{T}}, A::QRPivoted) = convert(QRPivoted{T}, A)

function getindex(A::QR, d::Symbol)
m, n = size(A)
d == :R && return triu!(A.factors[1:min(m,n), 1:n])
d == :R && return triu(A.factors[1:min(m,n), 1:n])
d == :Q && return QRPackedQ(A.factors,A.τ)
throw(KeyError(d))
end
function getindex(A::QRCompactWY, d::Symbol)
m, n = size(A)
d == :R && return triu!(A.factors[1:min(m,n), 1:n])
d == :R && return triu(A.factors[1:min(m,n), 1:n])
d == :Q && return QRCompactWYQ(A.factors,A.T)
throw(KeyError(d))
end
function getindex{T}(A::QRPivoted{T}, d::Symbol)
m, n = size(A)
d == :R && return triu!(A.factors[1:min(m,n), 1:n])
d == :R && return triu(A.factors[1:min(m,n), 1:n])
d == :Q && return QRPackedQ(A.factors,A.τ)
d == :p && return A.jpvt
if d == :P
Expand Down
Loading