Skip to content

Commit

Permalink
eliminate StoredArray (fix JuliaLang#6212, JuliaLang#987); UniformSca…
Browse files Browse the repository at this point in the history
…ling is no longer an AbstractArray (JuliaLang#5810)
  • Loading branch information
stevengj committed Mar 31, 2014
1 parent c81664a commit 184fa09
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 119 deletions.
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ New language features
* Default "inner" constructors now accept any arguments. Constructors that
look like `MyType(a, b) = new(a, b)` can and should be removed ([#4026]).

* Expanded array type hierarchy, including ``StoredArray`` for all
container-like arrays, and ``DenseArray`` for in-memory arrays with
standard strided storage ([#987], [#2345]).
* Expanded array type hierarchy to include an abstract ``DenseArray`` for
in-memory arrays with standard strided storage ([#987], [#2345],
[#6212]).

* When reloading code, types whose definitions have not changed can be
ignored in some cases.
Expand Down
60 changes: 48 additions & 12 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function squeeze(A::AbstractArray, dims)
reshape(A, d)
end

function copy!(dest::StoredArray, src)
function copy!(dest::AbstractArray, src)
i = 1
for x in src
dest[i] = x
Expand All @@ -174,7 +174,7 @@ end

# copy with minimal requirements on src
# if src is not an AbstractArray, moving to the offset might be O(n)
function copy!(dest::StoredArray, doffs::Integer, src, soffs::Integer=1)
function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer=1)
st = start(src)
for j = 1:(soffs-1)
_, st = next(src, st)
Expand All @@ -191,13 +191,13 @@ end
# NOTE: this is to avoid ambiguity with the deprecation of
# copy!(dest::AbstractArray, src, doffs::Integer)
# Remove this when that deprecation is removed.
function copy!(dest::StoredArray, doffs::Integer, src::Integer)
function copy!(dest::AbstractArray, doffs::Integer, src::Integer)
dest[doffs] = src
return dest
end

# this method must be separate from the above since src might not have a length
function copy!(dest::StoredArray, doffs::Integer, src, soffs::Integer, n::Integer)
function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Integer)
n == 0 && return dest
st = start(src)
for j = 1:(soffs-1)
Expand All @@ -212,7 +212,7 @@ function copy!(dest::StoredArray, doffs::Integer, src, soffs::Integer, n::Intege
end

# if src is an AbstractArray and a source offset is passed, use indexing
function copy!(dest::StoredArray, doffs::Integer, src::AbstractArray, soffs::Integer, n::Integer=length(src))
function copy!(dest::AbstractArray, doffs::Integer, src::AbstractArray, soffs::Integer, n::Integer=length(src))
for i = 0:(n-1)
dest[doffs+i] = src[soffs+i]
end
Expand All @@ -222,6 +222,42 @@ end
copy(a::AbstractArray) = copy!(similar(a), a)
copy(a::AbstractArray{None}) = a # cannot be assigned into so is immutable

function copy!{R,S}(B::AbstractMatrix{R}, ir_dest::Ranges{Int}, jr_dest::Ranges{Int}, A::AbstractMatrix{S}, ir_src::Ranges{Int}, jr_src::Ranges{Int})
if length(ir_dest) != length(ir_src) || length(jr_dest) != length(jr_src)
error("source and destination must have same size")
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
jdest = first(jr_dest)
for jsrc in jr_src
idest = first(ir_dest)
for isrc in ir_src
B[idest,jdest] = A[isrc,jsrc]
idest += step(ir_dest)
end
jdest += step(jr_dest)
end
return B
end

function copy_transpose!{R,S}(B::AbstractMatrix{R}, ir_dest::Ranges{Int}, jr_dest::Ranges{Int}, A::AbstractVecOrMat{S}, ir_src::Ranges{Int}, jr_src::Ranges{Int})
if length(ir_dest) != length(jr_src) || length(jr_dest) != length(ir_src)
error("source and destination must have same size")
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
idest = first(ir_dest)
for jsrc in jr_src
jdest = first(jr_dest)
for isrc in ir_src
B[idest,jdest] = A[isrc,jsrc]
jdest += step(jr_dest)
end
idest += step(ir_dest)
end
return B
end

zero{T}(x::AbstractArray{T}) = fill!(similar(x), zero(T))

## iteration support for arrays as ranges ##
Expand All @@ -248,9 +284,9 @@ for (f,t) in ((:char, Char),
(:uint128,Uint128))
@eval begin
($f)(x::AbstractArray{$t}) = x
($f)(x::StoredArray{$t}) = x
($f)(x::AbstractArray{$t}) = x

function ($f)(x::StoredArray)
function ($f)(x::AbstractArray)
y = similar(x,$t)
i = 1
for e in x
Expand All @@ -266,9 +302,9 @@ for (f,t) in ((:integer, Integer),
(:unsigned, Unsigned))
@eval begin
($f){T<:$t}(x::AbstractArray{T}) = x
($f){T<:$t}(x::StoredArray{T}) = x
($f){T<:$t}(x::AbstractArray{T}) = x

function ($f)(x::StoredArray)
function ($f)(x::AbstractArray)
y = similar(x,typeof(($f)(one(eltype(x)))))
i = 1
for e in x
Expand Down Expand Up @@ -1228,7 +1264,7 @@ end


## 1 argument
function map_to!(f::Callable, first, dest::StoredArray, A::AbstractArray)
function map_to!(f::Callable, first, dest::AbstractArray, A::AbstractArray)
dest[1] = first
for i=2:length(A)
dest[i] = f(A[i])
Expand All @@ -1244,7 +1280,7 @@ function map(f::Callable, A::AbstractArray)
end

## 2 argument
function map_to!(f::Callable, first, dest::StoredArray, A::AbstractArray, B::AbstractArray)
function map_to!(f::Callable, first, dest::AbstractArray, A::AbstractArray, B::AbstractArray)
dest[1] = first
for i=2:length(A)
dest[i] = f(A[i], B[i])
Expand All @@ -1263,7 +1299,7 @@ function map(f::Callable, A::AbstractArray, B::AbstractArray)
end

## N argument
function map_to!(f::Callable, first, dest::StoredArray, As::AbstractArray...)
function map_to!(f::Callable, first, dest::AbstractArray, As::AbstractArray...)
n = length(As[1])
i = 1
ith = a->a[i]
Expand Down
58 changes: 5 additions & 53 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ typealias DenseVector{T} DenseArray{T,1}
typealias DenseMatrix{T} DenseArray{T,2}
typealias DenseVecOrMat{T} Union(DenseVector{T}, DenseMatrix{T})

typealias StoredVector{T} StoredArray{T,1}
typealias StridedArray{T,N,A<:DenseArray} Union(DenseArray{T,N}, SubArray{T,N,A})
typealias StridedVector{T,A<:DenseArray} Union(DenseArray{T,1}, SubArray{T,1,A})
typealias StridedMatrix{T,A<:DenseArray} Union(DenseArray{T,2}, SubArray{T,2,A})
Expand Down Expand Up @@ -58,53 +57,6 @@ end

copy!{T}(dest::Array{T}, src::Array{T}) = copy!(dest, 1, src, 1, length(src))

function copy!{R,S}(B::Matrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, A::StridedMatrix{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
if length(ir_dest) != length(ir_src) || length(jr_dest) != length(jr_src)
error("source and destination must have same size")
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
jdest = first(jr_dest)
Askip = size(A, 1)
Bskip = size(B, 1)
if stride(A, 1) == 1 && R == S
for jsrc in jr_src
copy!(B, (jdest-1)*Bskip+first(ir_dest), A, (jsrc-1)*Askip+first(ir_src), length(ir_src))
jdest += 1
end
else
for jsrc in jr_src
aoffset = (jsrc-1)*Askip
boffset = (jdest-1)*Bskip
idest = first(ir_dest)
for isrc in ir_src
B[boffset+idest] = A[aoffset+isrc]
idest += 1
end
jdest += 1
end
end
end

function copy_transpose!{R,S}(B::Matrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, A::StridedVecOrMat{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
if length(ir_dest) != length(jr_src) || length(jr_dest) != length(ir_src)
error("source and destination must have same size")
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
idest = first(ir_dest)
Askip = size(A, 1)
for jsrc in jr_src
offset = (jsrc-1)*Askip
jdest = first(jr_dest)
for isrc in ir_src
B[idest,jdest] = A[offset+isrc]
jdest += 1
end
idest += 1
end
end

function reinterpret{T,S}(::Type{T}, a::Array{S,1})
nel = int(div(length(a)*sizeof(S),sizeof(T)))
return reinterpret(T, a, (nel,))
Expand Down Expand Up @@ -689,7 +641,7 @@ end

## Unary operators ##

function conj!{T<:Number}(A::StoredArray{T})
function conj!{T<:Number}(A::AbstractArray{T})
for i=1:length(A)
A[i] = conj(A[i])
end
Expand Down Expand Up @@ -994,7 +946,7 @@ end
rotr90(A::AbstractMatrix, k::Integer) = rotl90(A,-k)
rot180(A::AbstractMatrix, k::Integer) = mod(k, 2) == 1 ? rot180(A) : copy(A)

# note: probably should be StridedVector or StoredVector
# note: probably should be StridedVector or AbstractVector
function reverse(A::AbstractVector, s=1, n=length(A))
B = similar(A)
for i = 1:s-1
Expand Down Expand Up @@ -1392,7 +1344,7 @@ _cumsum_type(v) = typeof(v[1]+v[1])
for (f, fp, op) = ((:cumsum, :cumsum_pairwise, :+),
(:cumprod, :cumprod_pairwise, :*) )
# in-place cumsum of c = s+v(i1:n), using pairwise summation as for sum
@eval function ($fp)(v::StoredVector, c::StoredVector, s, i1, n)
@eval function ($fp)(v::AbstractVector, c::AbstractVector, s, i1, n)
if n < 128
@inbounds c[i1] = ($op)(s, v[i1])
for i = i1+1:i1+n-1
Expand All @@ -1405,7 +1357,7 @@ for (f, fp, op) = ((:cumsum, :cumsum_pairwise, :+),
end
end

@eval function ($f)(v::StoredVector)
@eval function ($f)(v::AbstractVector)
n = length(v)
c = $(op===:+ ? (:(similar(v,_cumsum_type(v)))) :
(:(similar(v))))
Expand Down Expand Up @@ -1445,7 +1397,7 @@ for (f, fp, op) = ((:cumsum, :cumsum_pairwise, :+),
end

for (f, op) = ((:cummin, :min), (:cummax, :max))
@eval function ($f)(v::StoredVector)
@eval function ($f)(v::AbstractVector)
n = length(v)
cur_val = v[1]
res = similar(v, n)
Expand Down
5 changes: 2 additions & 3 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
#end

#abstract AbstractArray{T,N}
#abstract StoredArray{T,N} <: AbstractArray{T,N}
#abstract DenseArray{T,N} <: StoredArray{T,N}
#abstract DenseArray{T,N} <: AbstractArray{T,N}

#type Array{T,N} <: DenseArray{T,N}
#end
Expand Down Expand Up @@ -118,7 +117,7 @@ export
# key types
Any, DataType, Vararg, ANY, NTuple, None, Top,
Tuple, Type, TypeConstructor, TypeName, TypeVar, Union, UnionType, Void,
AbstractArray, StoredArray, DenseArray,
AbstractArray, DenseArray,
# special objects
Box, Function, IntrinsicFunction, LambdaStaticData, Method, MethodTable,
Module, Nothing, Symbol, Task, Array,
Expand Down
2 changes: 1 addition & 1 deletion base/darray.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type DArray{T,N,A} <: StoredArray{T,N}
type DArray{T,N,A} <: AbstractArray{T,N}
dims::NTuple{N,Int}

chunks::Array{RemoteRef,N}
Expand Down
7 changes: 7 additions & 0 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ size(D::Diagonal,d::Integer) = d<1 ? error("dimension out of range") : (d<=2 ? l
full(D::Diagonal) = diagm(D.diag)
getindex(D::Diagonal, i::Integer, j::Integer) = i == j ? D.diag[i] : zero(eltype(D.diag))

function getindex(D::Diagonal, i::Integer)
n = length(D.diag)
id = div(i-1, n)
id + id * n == i-1 && return D.diag[id+1]
zero(eltype(D.diag))
end

ishermitian(D::Diagonal) = true
issym(D::Diagonal) = true
isposdef(D::Diagonal) = all(D.diag .> 0)
Expand Down
26 changes: 13 additions & 13 deletions base/linalg/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ end
# blas.jl defines matmul for floats; other integer and mixed precision
# cases are handled here

lapack_size(t::Char, M::StridedVecOrMat) = (size(M, t=='N' ? 1:2), size(M, t=='N' ? 2:1))
lapack_size(t::Char, M::AbstractVecOrMat) = (size(M, t=='N' ? 1:2), size(M, t=='N' ? 2:1))

function copy!{R,S}(B::Matrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, tM::Char, M::StridedMatrix{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
function copy!{R,S}(B::AbstractMatrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, tM::Char, M::AbstractMatrix{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
if tM == 'N'
copy!(B, ir_dest, jr_dest, M, ir_src, jr_src)
else
Expand All @@ -250,7 +250,7 @@ function copy!{R,S}(B::Matrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, tM
end
end

function copy_transpose!{R,S}(B::Matrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, tM::Char, M::StridedVecOrMat{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
function copy_transpose!{R,S}(B::AbstractMatrix{R}, ir_dest::Range1{Int}, jr_dest::Range1{Int}, tM::Char, M::AbstractVecOrMat{S}, ir_src::Range1{Int}, jr_src::Range1{Int})
if tM == 'N'
Base.copy_transpose!(B, ir_dest, jr_dest, M, ir_src, jr_src)
else
Expand All @@ -264,17 +264,17 @@ end

# NOTE: the generic version is also called as fallback for
# strides != 1 cases in libalg_blas.jl
(*){T,S}(A::StridedMatrix{T}, B::StridedVector{S}) = generic_matvecmul('N', A, B)
(*){T,S}(A::AbstractMatrix{T}, B::AbstractVector{S}) = generic_matvecmul('N', A, B)

arithtype(T) = T
arithtype(::Type{Bool}) = Int

function generic_matvecmul{T,S}(tA::Char, A::StridedMatrix{T}, B::StridedVector{S})
function generic_matvecmul{T,S}(tA::Char, A::AbstractMatrix{T}, B::AbstractVector{S})
C = similar(B, promote_type(arithtype(T),arithtype(S)), size(A, tA=='N' ? 1 : 2))
generic_matvecmul(C, tA, A, B)
end

function generic_matvecmul{T,S,R}(C::StridedVector{R}, tA, A::StridedMatrix{T}, B::StridedVector{S})
function generic_matvecmul{T,S,R}(C::AbstractVector{R}, tA, A::AbstractMatrix{T}, B::AbstractVector{S})
mB = length(B)
mA, nA = lapack_size(tA, A)
mB==nA || throw(DimensionMismatch("*"))
Expand Down Expand Up @@ -318,9 +318,9 @@ end

# NOTE: the generic version is also called as fallback for strides != 1 cases
# in libalg_blas.jl
(*){T,S}(A::StridedVecOrMat{T}, B::StridedMatrix{S}) = generic_matmatmul('N', 'N', A, B)
(*){T,S}(A::AbstractVecOrMat{T}, B::AbstractMatrix{S}) = generic_matmatmul('N', 'N', A, B)

function generic_matmatmul{T,S}(tA, tB, A::StridedVecOrMat{T}, B::StridedMatrix{S})
function generic_matmatmul{T,S}(tA, tB, A::AbstractVecOrMat{T}, B::AbstractMatrix{S})
mA, nA = lapack_size(tA, A)
mB, nB = lapack_size(tB, B)
C = similar(B, promote_type(arithtype(T),arithtype(S)), mA, nB)
Expand All @@ -332,7 +332,7 @@ const Abuf = Array(Uint8, tilebufsize)
const Bbuf = Array(Uint8, tilebufsize)
const Cbuf = Array(Uint8, tilebufsize)

function generic_matmatmul{T,S,R}(C::StridedVecOrMat{R}, tA, tB, A::StridedVecOrMat{T}, B::StridedMatrix{S})
function generic_matmatmul{T,S,R}(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVecOrMat{T}, B::AbstractMatrix{S})
mA, nA = lapack_size(tA, A)
mB, nB = lapack_size(tB, B)
mB==nA || throw(DimensionMismatch("*"))
Expand Down Expand Up @@ -482,11 +482,11 @@ end


# multiply 2x2 matrices
function matmul2x2{T,S}(tA, tB, A::StridedMatrix{T}, B::StridedMatrix{S})
function matmul2x2{T,S}(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S})
matmul2x2(similar(B, promote_type(T,S), 2, 2), tA, tB, A, B)
end

function matmul2x2{T,S,R}(C::StridedMatrix{R}, tA, tB, A::StridedMatrix{T}, B::StridedMatrix{S})
function matmul2x2{T,S,R}(C::AbstractMatrix{R}, tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S})
if tA == 'T'
A11 = A[1,1]; A12 = A[2,1]; A21 = A[1,2]; A22 = A[2,2]
elseif tA == 'C'
Expand All @@ -509,11 +509,11 @@ function matmul2x2{T,S,R}(C::StridedMatrix{R}, tA, tB, A::StridedMatrix{T}, B::S
end

# Multiply 3x3 matrices
function matmul3x3{T,S}(tA, tB, A::StridedMatrix{T}, B::StridedMatrix{S})
function matmul3x3{T,S}(tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S})
matmul3x3(similar(B, promote_type(T,S), 3, 3), tA, tB, A, B)
end

function matmul3x3{T,S,R}(C::StridedMatrix{R}, tA, tB, A::StridedMatrix{T}, B::StridedMatrix{S})
function matmul3x3{T,S,R}(C::AbstractMatrix{R}, tA, tB, A::AbstractMatrix{T}, B::AbstractMatrix{S})
if tA == 'T'
A11 = A[1,1]; A12 = A[2,1]; A13 = A[3,1];
A21 = A[1,2]; A22 = A[2,2]; A23 = A[3,2];
Expand Down
Loading

0 comments on commit 184fa09

Please sign in to comment.