Skip to content

Commit

Permalink
Generalize get to AbstractArrays
Browse files Browse the repository at this point in the history
This also renames the pre-allocated version get!
  • Loading branch information
timholy committed Aug 16, 2013
1 parent f40a63f commit 5994263
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
28 changes: 28 additions & 0 deletions base/abstractarray.jl
Expand Up @@ -573,6 +573,34 @@ function setindex!(A::AbstractMatrix, x, i1,i2,i3,i4...)
A[i1,i2] = x
end

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

typealias RangeVecIntList{A<:AbstractVector{Int}} Union((Union(Ranges, AbstractVector{Int})...), AbstractVector{Range1{Int}}, AbstractVector{Range{Int}}, AbstractVector{A})

get(A::AbstractArray, i::Integer, default) = in_bounds(length(A), i) ? A[i] : default
get(A::AbstractArray, I::(), default) = similar(A, typeof(default), 0)
get(A::AbstractArray, I::Dims, default) = in_bounds(size(A), I...) ? A[I...] : default

function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::Union(Ranges, AbstractVector{Int}), default::T)
ind = findin(I, 1:length(A))
X[ind] = A[I[ind]]
X[1:first(ind)-1] = default
X[last(ind)+1:length(X)] = default
X
end

get(A::AbstractArray, I::Ranges, default) = get!(similar(A, typeof(default), length(I)), A, I, default)

function get!{T}(X::AbstractArray{T}, A::AbstractArray, I::RangeVecIntList, default::T)
fill!(X, default)
dst, src = indcopy(size(A), I)
X[dst...] = A[src...]
X
end

get(A::AbstractArray, I::RangeVecIntList, default) = get!(similar(A, typeof(default), map(length, I)...), A, I, default)


## Concatenation ##

#TODO: ERROR CHECK
Expand Down
25 changes: 0 additions & 25 deletions base/array.jl
Expand Up @@ -651,31 +651,6 @@ setindex!(A::Array, x, I::AbstractVector{Bool}, J::AbstractVector{Bool}) = setin
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 (getindex with a default value)

get(A::Array, i::Integer, default) = in_bounds(length(A), i) ? A[i] : default
get(A::Array, I::(), default) = Array(typeof(default), 0)
get(A::Array, I::Dims, default) = in_bounds(size(A), I...) ? A[I...] : default

function get{T}(X::Array{T}, A::Array, I::Union(Ranges, Vector{Int}), default::T)
ind = findin(I, 1:length(A))
X[ind] = A[I[ind]]
X[1:first(ind)-1] = default
X[last(ind)+1:length(X)] = default
X
end
get(A::Array, I::Ranges, default) = get(Array(typeof(default), length(I)), A, I, default)

typealias RangeVecIntList Union((Union(Ranges, Vector{Int})...), Vector{Range1{Int}}, Vector{Range{Int}}, Vector{Vector{Int}})

function get{T}(X::Array{T}, A::Array, I::RangeVecIntList, default::T)
fill!(X, default)
dst, src = indcopy(size(A), I)
X[dst...] = A[src...]
X
end
get(A::Array, I::RangeVecIntList, default) = get(Array(typeof(default), map(length, I)...), A, I, default)

## Dequeue functionality ##

function push!{T}(a::Array{T,1}, item)
Expand Down
1 change: 1 addition & 0 deletions base/deprecated.jl
Expand Up @@ -225,6 +225,7 @@ export PipeString
@deprecate add(s::Set, x) push!(s,x)
@deprecate add!(s::Set, x) push!(s,x)
@deprecate delete!(d::Dict, key, default) pop!(d, key, default)
@deprecate get(A::Array, B::Array, I, default) get!(A, B, I, default)

deprecated_ls() = run(`ls -l`)
deprecated_ls(args::Cmd) = run(`ls -l $args`)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Expand Up @@ -693,6 +693,7 @@ export
filter!,
filter,
get,
get!,
getindex,
getkey,
haskey,
Expand Down

0 comments on commit 5994263

Please sign in to comment.