Skip to content

Commit

Permalink
Allow passing a type as first argument to similar
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Jun 29, 2016
1 parent 3c60d3e commit 00b960c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
9 changes: 9 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ checkbounds(A::AbstractArray) = checkbounds(A, 1) # 0-d case
typealias SimIdx Union{Integer,UnitRangeInteger}
"""
similar(array, [element_type=eltype(array)], [dims=size(array)])
similar(array_type, [element_type=eltype(array)], [dims=size(array)])
Create an uninitialized mutable array with the given element type and size, based upon the
given source array. The second and third arguments are both optional, defaulting to the
Expand Down Expand Up @@ -362,6 +363,14 @@ similar( a::AbstractArray, T::Type, dims::Dims) = Array(T, dims)
_similar(::IndicesStartAt1, a::AbstractArray, T::Type) = similar(a, T, size(a))
_similar(::IndicesBehavior, a::AbstractArray, T::Type) = similar(a, T, indices(a))

# similar creates an Array by default
similar{CT<:AbstractArray}(::Type{CT}) = similar(CT())
similar{CT<:AbstractArray}(::Type{CT}, dims::Tuple) = similar(CT(), dims)
similar{CT<:AbstractArray}(::Type{CT}, dims::SimIdx...) = similar(CT(), dims...)
similar{CT<:AbstractArray}(::Type{CT}, U::Type) = similar(CT(), U)
similar{CT<:AbstractArray}(::Type{CT}, U::Type, dims::Tuple) = similar(CT(), U, dims)
similar{CT<:AbstractArray}(::Type{CT}, U::Type, dims::SimIdx...) = similar(CT(), U, dims...)

"""
allocate_for(storagetype, referencearray, [shape])
Expand Down
6 changes: 6 additions & 0 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ similar(B::BitArray, T::Type{Bool}, dims::Dims) = BitArray(dims)
# (this triggers conversions like float(bitvector) etc.)
similar(B::BitArray, T::Type, dims::Dims) = Array{T}(dims)

similar(::Type{BitArray}) = BitArray(())
similar(::Type{BitArray}, dims::Int...) = BitArray(dims)
similar(::Type{BitArray}, dims::Dims) = BitArray(dims...)
similar(::Type{BitArray}, T::Type{Bool}, dims::Dims) = BitArray(dims)
similar(::Type{BitArray}, T::Type, dims::Dims) = Array{T}(dims)

function fill!(B::BitArray, x)
y = convert(Bool, x)
isempty(B) && return B
Expand Down
9 changes: 6 additions & 3 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ type ObjectIdDict <: Associative{Any,Any}
ObjectIdDict(o::ObjectIdDict) = new(copy(o.ht))
end

similar(d::ObjectIdDict) = ObjectIdDict()
similar(::ObjectIdDict) = ObjectIdDict()
similar(::Type{ObjectIdDict}) = ObjectIdDict()

function setindex!(t::ObjectIdDict, v::ANY, k::ANY)
t.ht = ccall(:jl_eqtable_put, Array{Any,1}, (Any, Any, Any), t.ht, k, v)
Expand Down Expand Up @@ -497,8 +498,10 @@ function grow_to!{K,V}(dest::Associative{K,V}, itr, st = start(itr))
return dest
end

similar{K,V}(d::Dict{K,V}) = Dict{K,V}()
similar{K,V}(d::Dict, ::Type{Pair{K,V}}) = Dict{K,V}()
similar{K,V}(::Dict{K,V}) = Dict{K,V}()
similar{K,V}(::Dict, ::Type{Pair{K,V}}) = Dict{K,V}()
similar{K,V}(::Type{Dict{K,V}}) = Dict{K,V}()
similar{K,V}(::Type{Dict}, ::Type{Pair{K,V}}) = Dict{K,V}()

# conversion between Dict types
function convert{K,V}(::Type{Dict{K,V}},d::Associative)
Expand Down
3 changes: 2 additions & 1 deletion base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ end
IntSet(itr) = (s=IntSet(); for a in itr; push!(s,a); end; s)

eltype(::Type{IntSet}) = Int64
similar(s::IntSet) = IntSet()
similar(::IntSet) = IntSet()
similar(::Type{IntSet}) = IntSet()

function show(io::IO, s::IntSet)
print(io, "IntSet([")
Expand Down
3 changes: 3 additions & 0 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ size(A::ReshapedArray) = A.dims
size(A::ReshapedArray, d) = d <= ndims(A) ? A.dims[d] : 1
similar(A::ReshapedArray, eltype::Type) = similar(parent(A), eltype, size(A))
similar(A::ReshapedArray, eltype::Type, dims::Dims) = similar(parent(A), eltype, dims)
similar{CT<:ReshapedArray}(::Type{CT}, eltype::Type) = similar(parenttype(CT), eltype, ())
similar{CT<:ReshapedArray}(::Type{CT}, eltype::Type, dims::Dims) = similar(parenttype(CT), eltype, dims)
linearindexing{R<:ReshapedArrayLF}(::Type{R}) = LinearFast()
parent(A::ReshapedArray) = A.parent
parenttype{T,N,P,MI}(::Type{ReshapedArray{T,N,P,MI}}) = P
parentindexes(A::ReshapedArray) = map(s->1:s, size(parent(A)))
reinterpret{T}(::Type{T}, A::ReshapedArray, dims::Dims) = reinterpret(T, parent(A), dims)

Expand Down
6 changes: 4 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ Set() = Set{Any}()
Set(itr) = Set{eltype(itr)}(itr)

eltype{T}(::Type{Set{T}}) = T
similar{T}(s::Set{T}) = Set{T}()
similar(s::Set, T::Type) = Set{T}()
similar{T}(::Set{T}) = Set{T}()
similar(::Set, T::Type) = Set{T}()
similar{T}(::Type{Set{T}}) = Set{T}()
similar(::Type{Set}, T::Type) = Set{T}()

function show(io::IO, s::Set)
print(io,"Set")
Expand Down
5 changes: 5 additions & 0 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ similar(S::SharedArray, T::Type) = similar(S.s, T, size(S))
similar(S::SharedArray, dims::Dims) = similar(S.s, eltype(S), dims)
similar(S::SharedArray) = similar(S.s, eltype(S), size(S))

similar{T,N}(::Type{SharedArray{T,N}}, U::Type, dims::Dims) = similar(Array{T,N}, U, dims)
similar{T,N}(::Type{SharedArray{T,N}}, U::Type) = similar(Array{T,N}, U, ())
similar{T,N}(::Type{SharedArray{T,N}}, dims::Dims) = similar(Array{T,N}, dims)
similar{T,N}(::Type{SharedArray{T,N}}) = similar(Array{T,N}, ())

reduce(f, S::SharedArray) =
mapreduce(fetch, f,
Any[ @spawnat p reduce(f, S.loc_subarr_1d) for p in procs(S) ])
Expand Down
1 change: 1 addition & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ size(V::SubArray) = V.dims
length(V::SubArray) = prod(V.dims)

similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
similar{T,N,P,I,L}(::Type{SubArray{T,N,P,I,L}}, U::Type, dims::Dims) = similar(P, T, dims)

parent(V::SubArray) = V.parent
parentindexes(V::SubArray) = V.indexes
Expand Down

0 comments on commit 00b960c

Please sign in to comment.