Skip to content
Merged
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
30 changes: 12 additions & 18 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ typealias AbstractVector{T} AbstractArray{T,1}
typealias AbstractMatrix{T} AbstractArray{T,2}
typealias AbstractVecOrMat{T} Union{AbstractVector{T}, AbstractMatrix{T}}
typealias RangeIndex Union{Int, Range{Int}, AbstractUnitRange{Int}, Colon}
typealias Indices{N} NTuple{N,AbstractUnitRange}
typealias IndicesOne{N} NTuple{N,OneTo}
typealias DimOrInd Union{Integer, AbstractUnitRange}
typealias DimsOrInds{N} NTuple{N,DimOrInd}

Expand All @@ -29,6 +27,7 @@ end

size{T,N}(t::AbstractArray{T,N}, d) = d <= N ? size(t)[d] : 1
size{N}(x, d1::Integer, d2::Integer, dx::Vararg{Integer, N}) = (size(x, d1), size(x, d2), ntuple(k->size(x, dx[k]), Val{N})...)

"""
indices(A, d)

Expand Down Expand Up @@ -66,15 +65,16 @@ is `indices(A, 1)`.
Calling this function is the "safe" way to write algorithms that
exploit linear indexing.
"""
linearindices(A) = (@_inline_meta; 1:length(A))
linearindices(A) = (@_inline_meta; OneTo(_length(A)))
linearindices(A::AbstractVector) = (@_inline_meta; indices1(A))
eltype{T}(::Type{AbstractArray{T}}) = T
eltype{T,N}(::Type{AbstractArray{T,N}}) = T
elsize{T}(::AbstractArray{T}) = sizeof(T)
ndims{T,N}(::AbstractArray{T,N}) = N
ndims{T,N}(::Type{AbstractArray{T,N}}) = N
ndims{T<:AbstractArray}(::Type{T}) = ndims(supertype(T))
length(t::AbstractArray) = prod(size(t))::Int
length(t::AbstractArray) = prod(size(t))
_length(A::AbstractArray) = prod(map(unsafe_length, indices(A))) # circumvent missing size
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[first(eachindex(a))]

Expand Down Expand Up @@ -132,6 +132,10 @@ function trailingsize(A, n)
end
return s
end
function trailingsize(inds::Indices)
@_inline_meta
prod(map(unsafe_length, inds))
end

## Traits for array types ##

Expand Down Expand Up @@ -230,7 +234,7 @@ function checkbounds_indices(::Type{Bool}, IA::Tuple{Any}, I::Tuple{Any})
end
function checkbounds_indices(::Type{Bool}, IA::Tuple, I::Tuple{Any})
@_inline_meta
checkindex(Bool, 1:prod(map(dimlength, IA)), I[1]) # linear indexing
checkindex(Bool, OneTo(trailingsize(IA)), I[1]) # linear indexing
end

"""
Expand Down Expand Up @@ -264,16 +268,6 @@ end

throw_boundserror(A, I) = (@_noinline_meta; throw(BoundsError(A, I)))

@generated function trailingsize{T,N,n}(A::AbstractArray{T,N}, ::Type{Val{n}})
(isa(n, Int) && isa(N, Int)) || error("Must have concrete type")
n > N && return 1
ex = :(size(A, $n))
for m = n+1:N
ex = :($ex * size(A, $m))
end
Expr(:block, Expr(:meta, :inline), ex)
end

# check along a single dimension
"""
checkindex(Bool, inds::AbstractUnitRange, index)
Expand Down Expand Up @@ -357,7 +351,7 @@ to_shape(dims::DimsOrInds) = map(to_shape, dims)
to_shape(i::Int) = i
to_shape(i::Integer) = Int(i)
to_shape(r::OneTo) = Int(last(r))
to_shape(r::UnitRange) = convert(UnitRange{Int}, r)
to_shape(r::AbstractUnitRange) = r

"""
similar(storagetype, indices)
Expand Down Expand Up @@ -492,7 +486,7 @@ function copy!(::LinearIndexing, dest::AbstractArray, ::LinearSlow, src::Abstrac
end

function copy!(dest::AbstractArray, dstart::Integer, src::AbstractArray)
copy!(dest, dstart, src, first(linearindices(src)), length(src))
copy!(dest, dstart, src, first(linearindices(src)), _length(src))
end

function copy!(dest::AbstractArray, dstart::Integer, src::AbstractArray, sstart::Integer)
Expand Down Expand Up @@ -618,7 +612,7 @@ function _maxlength(A, B, C...)
max(length(A), _maxlength(B, C...))
end

isempty(a::AbstractArray) = (length(a) == 0)
isempty(a::AbstractArray) = (_length(a) == 0)

## Conversions ##

Expand Down
13 changes: 7 additions & 6 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ transpose(a::AbstractArray) = error("transpose not implemented for $(typeof(a)).

## Constructors ##

vec(a::AbstractArray) = reshape(a,length(a))
vec(a::AbstractArray) = reshape(a,_length(a))
vec(a::AbstractVector) = a

_sub(::Tuple{}, ::Tuple{}) = ()
Expand Down Expand Up @@ -74,22 +74,23 @@ function flipdim(A::AbstractArray, d::Integer)
if d > nd || isempty(A)
return copy(A)
end
inds = indices(A)
B = similar(A)
nnd = 0
for i = 1:nd
nnd += Int(size(A,i)==1 || i==d)
nnd += Int(length(inds[i])==1 || i==d)
end
inds = indices(A, d)
sd = first(inds)+last(inds)
indsd = inds[d]
sd = first(indsd)+last(indsd)
if nnd==nd
# flip along the only non-singleton dimension
for i in inds
for i in indsd
B[i] = A[sd-i]
end
return B
end
alli = [ indices(B,n) for n in 1:nd ]
for i in inds
for i in indsd
B[[ n==d ? sd-i : alli[n] for n in 1:nd ]...] = slicedim(A, d, i)
end
return B
Expand Down
11 changes: 6 additions & 5 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,20 @@ end

const transposebaselength=64
function transpose_f!(f,B::AbstractMatrix,A::AbstractMatrix)
indices(B,1) == indices(A,2) && indices(B,2) == indices(A,1) || throw(DimensionMismatch(string(f)))
inds = indices(A)
indices(B,1) == inds[2] && indices(B,2) == inds[1] || throw(DimensionMismatch(string(f)))

m, n = size(A)
m, n = length(inds[1]), length(inds[2])
if m*n<=4*transposebaselength
@inbounds begin
for j = indices(A,2)
for i = indices(A,1)
for j = inds[2]
for i = inds[1]
B[j,i] = f(A[i,j])
end
end
end
else
transposeblock!(f,B,A,m,n,first(indices(A,1))-1,first(indices(A,2))-1)
transposeblock!(f,B,A,m,n,first(inds[1])-1,first(inds[2])-1)
end
return B
end
Expand Down
3 changes: 1 addition & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ type BitArray{N} <: DenseArray{Bool, N}
chunks::Vector{UInt64}
len::Int
dims::NTuple{N,Int}
function BitArray(dims::Int...)
length(dims) == N || throw(ArgumentError("number of dimensions must be $N, got $(length(dims))"))
function BitArray(dims::Vararg{Int,N})
n = 1
i = 1
for d in dims
Expand Down
Loading