Skip to content

Commit

Permalink
rename ref to getindex, assign to setindex!
Browse files Browse the repository at this point in the history
closes #1484, closes #907
there are aliases in deprecated.jl to ease the transition
  • Loading branch information
JeffBezanson committed Mar 10, 2013
1 parent 0781ef3 commit f0e6204
Show file tree
Hide file tree
Showing 29 changed files with 384 additions and 377 deletions.
12 changes: 6 additions & 6 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ end



## Indexing: ref ##
## Indexing: getindex ##

ref(t::AbstractArray, i::Real) = error("indexing not defined for ", typeof(t))
getindex(t::AbstractArray, i::Real) = error("indexing not defined for ", typeof(t))

# index A[:,:,...,i,:,:,...] where "i" is in dimension "d"
# TODO: more optimized special cases
Expand Down Expand Up @@ -506,12 +506,12 @@ function circshift(a, shiftamts)
a[I...]::typeof(a)
end

## Indexing: assign ##
## Indexing: setindex! ##

# 1-d indexing is assumed defined on subtypes
assign(t::AbstractArray, x, i::Real) =
error("assign not defined for ",typeof(t))
assign(t::AbstractArray, x) = throw(MethodError(assign, (t, x)))
setindex!(t::AbstractArray, x, i::Real) =
error("setindex! not defined for ",typeof(t))
setindex!(t::AbstractArray, x) = throw(MethodError(setindex!, (t, x)))

## Concatenation ##

Expand Down
140 changes: 70 additions & 70 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ similar{T}(a::Array{T,2}, m::Int) = Array(T, m)
similar{T}(a::Array{T,2}, S) = Array(S, size(a,1), size(a,2))

# T[x...] constructs Array{T,1}
function ref{T}(::Type{T}, vals...)
function getindex{T}(::Type{T}, vals...)
a = Array(T,length(vals))
for i = 1:length(vals)
a[i] = vals[i]
Expand All @@ -158,7 +158,7 @@ function ref{T}(::Type{T}, vals...)
end

# T[a:b] and T[a:s:b] also contruct typed ranges
function ref{T<:Number}(::Type{T}, r::Ranges)
function getindex{T<:Number}(::Type{T}, r::Ranges)
a = Array(T,length(r))
i = 1
for x in r
Expand Down Expand Up @@ -244,47 +244,47 @@ convert{T,n,S}(::Type{Array{T,n}}, x::Array{S,n}) = copy!(similar(x,T), x)

collect(itr) = [x for x in itr]

## Indexing: ref ##
## Indexing: getindex ##

ref(a::Array) = arrayref(a,1)
getindex(a::Array) = arrayref(a,1)

ref(A::Array, i0::Real) = arrayref(A,to_index(i0))
ref(A::Array, i0::Real, i1::Real) = arrayref(A,to_index(i0),to_index(i1))
ref(A::Array, i0::Real, i1::Real, i2::Real) =
getindex(A::Array, i0::Real) = arrayref(A,to_index(i0))
getindex(A::Array, i0::Real, i1::Real) = arrayref(A,to_index(i0),to_index(i1))
getindex(A::Array, i0::Real, i1::Real, i2::Real) =
arrayref(A,to_index(i0),to_index(i1),to_index(i2))
ref(A::Array, i0::Real, i1::Real, i2::Real, i3::Real) =
getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real) =
arrayref(A,to_index(i0),to_index(i1),to_index(i2),to_index(i3))
ref(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real) =
getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real) =
arrayref(A,to_index(i0),to_index(i1),to_index(i2),to_index(i3),to_index(i4))
ref(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real) =
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))

ref(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, I::Int...) =
getindex(A::Array, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, I::Int...) =
arrayref(A,to_index(i0),to_index(i1),to_index(i2),to_index(i3),to_index(i4),to_index(i5),I...)

# Fast copy using copy! for Range1
function ref(A::Array, I::Range1{Int})
function getindex(A::Array, I::Range1{Int})
X = similar(A, length(I))
copy!(X, 1, A, first(I), length(I))
return X
end

# note: this is also useful for Ranges
function ref{T<:Real}(A::AbstractArray, I::AbstractVector{T})
function getindex{T<:Real}(A::AbstractArray, I::AbstractVector{T})
return [ A[i] for i in indices(I) ]
end

# 2d indexing
function ref(A::Array, I::Range1{Int}, j::Real)
function getindex(A::Array, I::Range1{Int}, j::Real)
j = to_index(j)
check_bounds(A, I, j)
X = similar(A,length(I))
copy_unsafe!(X, 1, A, (j-1)*size(A,1) + first(I), length(I))
return X
end
function ref(A::Array, I::Range1{Int}, J::Range1{Int})
function getindex(A::Array, I::Range1{Int}, J::Range1{Int})
check_bounds(A, I, J)
X = similar(A, ref_shape(I, J))
X = similar(A, index_shape(I, J))
if length(I) == size(A,1)
copy_unsafe!(X, 1, A, (first(J)-1)*size(A,1) + 1, size(A,1)*length(J))
else
Expand All @@ -296,9 +296,9 @@ function ref(A::Array, I::Range1{Int}, J::Range1{Int})
end
return X
end
function ref(A::Array, I::Range1{Int}, J::AbstractVector{Int})
function getindex(A::Array, I::Range1{Int}, J::AbstractVector{Int})
check_bounds(A, I, J)
X = similar(A, ref_shape(I, J))
X = similar(A, index_shape(I, J))
storeoffset = 1
for j = J
copy_unsafe!(X, storeoffset, A, (j-1)*size(A,1) + first(I), length(I))
Expand All @@ -307,15 +307,15 @@ function ref(A::Array, I::Range1{Int}, J::AbstractVector{Int})
return X
end

ref{T<:Real}(A::Array, I::AbstractVector{T}, j::Real) = [ A[i,j] for i=indices(I) ]
ref{T<:Real}(A::Array, I::Real, J::AbstractVector{T}) = [ A[i,j] for i=I,j=indices(J) ]
getindex{T<:Real}(A::Array, I::AbstractVector{T}, j::Real) = [ A[i,j] for i=indices(I) ]
getindex{T<:Real}(A::Array, I::Real, J::AbstractVector{T}) = [ A[i,j] for i=I,j=indices(J) ]

# This next is a 2d specialization of the algorithm used for general
# multidimensional indexing
function ref{T<:Real}(A::Array, I::AbstractVector{T}, J::AbstractVector{T})
function getindex{T<:Real}(A::Array, I::AbstractVector{T}, J::AbstractVector{T})
check_bounds(A, I, J)
I = indices(I); J = indices(J)
X = similar(A, ref_shape(I, J))
X = similar(A, index_shape(I, J))
storeind = 1
for j = J
offset = (j-1)*size(A,1)
Expand All @@ -327,17 +327,17 @@ function ref{T<:Real}(A::Array, I::AbstractVector{T}, J::AbstractVector{T})
return X
end
# Multidimensional indexing
let ref_cache = nothing
global ref
function ref(A::Array, I::Union(Real,AbstractArray)...)
let getindex_cache = nothing
global getindex
function getindex(A::Array, I::Union(Real,AbstractArray)...)
check_bounds(A, I...)
I = indices(I)
X = similar(A, ref_shape(I...))
X = similar(A, index_shape(I...))

if is(ref_cache,nothing)
ref_cache = Dict()
if is(getindex_cache,nothing)
getindex_cache = Dict()
end
gen_array_index_map(ref_cache, refind -> quote
gen_array_index_map(getindex_cache, refind -> quote
X[storeind] = A[$refind]
storeind += 1
end, I, (:A, :X, :storeind), A, X, 1)
Expand All @@ -348,7 +348,7 @@ end

# logical indexing

function ref_bool_1d(A::Array, I::AbstractArray{Bool})
function getindex_bool_1d(A::Array, I::AbstractArray{Bool})
check_bounds(A, I)
n = sum(I)
out = similar(A, n)
Expand All @@ -362,52 +362,52 @@ function ref_bool_1d(A::Array, I::AbstractArray{Bool})
out
end

ref(A::Vector, I::AbstractVector{Bool}) = ref_bool_1d(A, I)
ref(A::Vector, I::AbstractArray{Bool}) = ref_bool_1d(A, I)
ref(A::Array, I::AbstractVector{Bool}) = ref_bool_1d(A, I)
ref(A::Array, I::AbstractArray{Bool}) = ref_bool_1d(A, I)
getindex(A::Vector, I::AbstractVector{Bool}) = getindex_bool_1d(A, I)
getindex(A::Vector, I::AbstractArray{Bool}) = getindex_bool_1d(A, I)
getindex(A::Array, I::AbstractVector{Bool}) = getindex_bool_1d(A, I)
getindex(A::Array, I::AbstractArray{Bool}) = getindex_bool_1d(A, I)

# @Jeff: more efficient is to check the bool vector, and then do
# indexing without checking. Turn off checking for the second stage?
ref(A::Matrix, I::Real, J::AbstractVector{Bool}) = A[I,find(J)]
ref(A::Matrix, I::AbstractVector{Bool}, J::Real) = A[find(I),J]
ref(A::Matrix, I::AbstractVector{Bool}, J::AbstractVector{Bool}) = A[find(I),find(J)]
ref{T<:Real}(A::Matrix, I::AbstractVector{T}, J::AbstractVector{Bool}) = A[I,find(J)]
ref{T<:Real}(A::Matrix, I::AbstractVector{Bool}, J::AbstractVector{T}) = A[find(I),J]
getindex(A::Matrix, I::Real, J::AbstractVector{Bool}) = A[I,find(J)]
getindex(A::Matrix, I::AbstractVector{Bool}, J::Real) = A[find(I),J]
getindex(A::Matrix, I::AbstractVector{Bool}, J::AbstractVector{Bool}) = A[find(I),find(J)]
getindex{T<:Real}(A::Matrix, I::AbstractVector{T}, J::AbstractVector{Bool}) = A[I,find(J)]
getindex{T<:Real}(A::Matrix, I::AbstractVector{Bool}, J::AbstractVector{T}) = A[find(I),J]

## Indexing: assign ##
assign{T}(A::Array{T,0}, x) = arrayset(A, convert(T,x), 1)
## Indexing: setindex! ##
setindex!{T}(A::Array{T,0}, x) = arrayset(A, convert(T,x), 1)

assign(A::Array{Any}, x::ANY, i::Real) = arrayset(A, x, to_index(i))
setindex!(A::Array{Any}, x::ANY, i::Real) = arrayset(A, x, to_index(i))

assign{T}(A::Array{T}, x, i0::Real) = arrayset(A, convert(T,x), to_index(i0))
assign{T}(A::Array{T}, x, i0::Real, i1::Real) =
setindex!{T}(A::Array{T}, x, i0::Real) = arrayset(A, convert(T,x), to_index(i0))
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1))
assign{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real) =
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1), to_index(i2))
assign{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real) =
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1), to_index(i2), to_index(i3))
assign{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real) =
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1), to_index(i2), to_index(i3), to_index(i4))
assign{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real) =
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1), to_index(i2), to_index(i3), to_index(i4), to_index(i5))
assign{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, I::Int...) =
setindex!{T}(A::Array{T}, x, i0::Real, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, I::Int...) =
arrayset(A, convert(T,x), to_index(i0), to_index(i1), to_index(i2), to_index(i3), to_index(i4), to_index(i5), I...)

function assign{T<:Real}(A::Array, x, I::AbstractVector{T})
function setindex!{T<:Real}(A::Array, x, I::AbstractVector{T})
for i in I
A[i] = x
end
return A
end

function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int})
function setindex!{T}(A::Array{T}, X::Array{T}, I::Range1{Int})
if length(X) != length(I); error("argument dimensions must match"); end
copy!(A, first(I), X, 1, length(I))
return A
end

function assign{T<:Real}(A::Array, X::AbstractArray, I::AbstractVector{T})
function setindex!{T<:Real}(A::Array, X::AbstractArray, I::AbstractVector{T})
if length(X) != length(I); error("argument dimensions must match"); end
count = 1
for i in I
Expand All @@ -417,7 +417,7 @@ function assign{T<:Real}(A::Array, X::AbstractArray, I::AbstractVector{T})
return A
end

function assign{T<:Real}(A::Array, x, i::Real, J::AbstractVector{T})
function setindex!{T<:Real}(A::Array, x, i::Real, J::AbstractVector{T})
i = to_index(i)
check_bounds(A, i, J)
m = size(A, 1)
Expand All @@ -437,7 +437,7 @@ function assign{T<:Real}(A::Array, x, i::Real, J::AbstractVector{T})
return A
end

function assign{T<:Real}(A::Array, x, I::AbstractVector{T}, j::Real)
function setindex!{T<:Real}(A::Array, x, I::AbstractVector{T}, j::Real)
j = to_index(j)
check_bounds(A, I, j)
m = size(A, 1)
Expand All @@ -459,15 +459,15 @@ function assign{T<:Real}(A::Array, x, I::AbstractVector{T}, j::Real)
return A
end

function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, j::Real)
function setindex!{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, j::Real)
j = to_index(j)
check_bounds(A, I, j)
if length(X) != length(I); error("argument dimensions must match"); end
copy_unsafe!(A, first(I) + (j-1)*size(A,1), X, 1, length(I))
return A
end

function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::Range1{Int})
function setindex!{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::Range1{Int})
check_bounds(A, I, J)
nel = length(I)*length(J)
if length(X) != nel ||
Expand All @@ -486,7 +486,7 @@ function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::Range1{Int})
return A
end

function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::AbstractVector{Int})
function setindex!{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::AbstractVector{Int})
check_bounds(A, I, J)
nel = length(I)*length(J)
if length(X) != nel ||
Expand All @@ -501,7 +501,7 @@ function assign{T}(A::Array{T}, X::Array{T}, I::Range1{Int}, J::AbstractVector{I
return A
end

function assign{T<:Real}(A::Array, x, I::AbstractVector{T}, J::AbstractVector{T})
function setindex!{T<:Real}(A::Array, x, I::AbstractVector{T}, J::AbstractVector{T})
check_bounds(A, I, J)
m = size(A, 1)
if !isa(x,AbstractArray)
Expand Down Expand Up @@ -531,8 +531,8 @@ function assign{T<:Real}(A::Array, x, I::AbstractVector{T}, J::AbstractVector{T}
end

let assign_cache = nothing, assign_scalar_cache = nothing
global assign
function assign(A::Array, x, I::Union(Real,AbstractArray)...)
global setindex!
function setindex!(A::Array, x, I::Union(Real,AbstractArray)...)
check_bounds(A, I...)
I = indices(I)
if !isa(x,AbstractArray)
Expand Down Expand Up @@ -603,18 +603,18 @@ function assign_bool_vector_1d(A::Array, X::AbstractArray, I::AbstractArray{Bool
A
end

assign(A::Array, X::AbstractArray, I::AbstractVector{Bool}) = assign_bool_vector_1d(A, X, I)
assign(A::Array, X::AbstractArray, I::AbstractArray{Bool}) = assign_bool_vector_1d(A, X, I)
assign(A::Array, x, I::AbstractVector{Bool}) = assign_bool_scalar_1d(A, x, I)
assign(A::Array, x, I::AbstractArray{Bool}) = assign_bool_scalar_1d(A, x, I)
setindex!(A::Array, X::AbstractArray, I::AbstractVector{Bool}) = assign_bool_vector_1d(A, X, I)
setindex!(A::Array, X::AbstractArray, I::AbstractArray{Bool}) = assign_bool_vector_1d(A, X, I)
setindex!(A::Array, x, I::AbstractVector{Bool}) = assign_bool_scalar_1d(A, x, I)
setindex!(A::Array, x, I::AbstractArray{Bool}) = assign_bool_scalar_1d(A, x, I)

assign(A::Array, x, I::Real, J::AbstractVector{Bool}) = assign(A, x, I,find(J))
assign(A::Array, x, I::AbstractVector{Bool}, J::Real) = assign(A,x,find(I),J)
assign(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{Bool}) = assign(A, x, find(I),find(J))
assign{T<:Real}(A::Array, x, I::AbstractVector{T}, J::AbstractVector{Bool}) = assign(A, x, I,find(J))
assign{T<:Real}(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{T}) = assign(A, x, find(I),J)
setindex!(A::Array, x, I::Real, J::AbstractVector{Bool}) = setindex!(A, x, I,find(J))
setindex!(A::Array, x, I::AbstractVector{Bool}, J::Real) = setindex!(A,x,find(I),J)
setindex!(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{Bool}) = setindex!(A, x, find(I),find(J))
setindex!{T<:Real}(A::Array, x, I::AbstractVector{T}, J::AbstractVector{Bool}) = setindex!(A, x, I,find(J))
setindex!{T<:Real}(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{T}) = setindex!(A, x, find(I),J)

# get (ref with a default value)
# get (getindex with a default value)

get{T}(A::Array, i::Integer, default::T) = in_bounds(length(A), i) ? A[i] : default
get{T}(A::Array, I::(), default::T) = Array(T, 0)
Expand Down
8 changes: 4 additions & 4 deletions base/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
## required core functionality ##

endof(s::ASCIIString) = length(s.data)
ref(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')
getindex(s::ASCIIString, i::Int) = (x=s.data[i]; x < 0x80 ? char(x) : '\ufffd')

## overload methods for efficiency ##

ref(s::ASCIIString, r::Vector) = ASCIIString(ref(s.data,r))
ref(s::ASCIIString, r::Range1{Int}) = ASCIIString(ref(s.data,r))
ref(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx])
getindex(s::ASCIIString, r::Vector) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, r::Range1{Int}) = ASCIIString(getindex(s.data,r))
getindex(s::ASCIIString, indx::AbstractVector{Int}) = ASCIIString(s.data[indx])
search(s::ASCIIString, c::Char, i::Integer) = c < 0x80 ? search(s.data,c,i) : 0
string(a::ASCIIString, b::ASCIIString, c::ASCIIString...) =
ASCIIString([a.data,b.data,map(s->s.data,c)...])
Expand Down
Loading

3 comments on commit f0e6204

@ViralBShah
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getind and setind would be shorter.

@JeffBezanson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal, because these are almost always used through syntax, not typed out. I'm also skeptical of the excess brevity that gives rise to O_CREAT :)

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that getind and setind! are distinctly worse than getindex and setindex!.

Please sign in to comment.