Skip to content

Commit

Permalink
miscellaneous array method cleanup (JuliaLang#30904)
Browse files Browse the repository at this point in the history
- manually specializing `all` for `isinteger` is not necessary
- `BitVector` `append!` and `prepend!` can support any iterator
- move `stride` and `strides` to Base, since Base exports them
- remove some redundant methods
  • Loading branch information
JeffBezanson committed Feb 1, 2019
1 parent 585feb0 commit 4844a4b
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 43 deletions.
1 change: 0 additions & 1 deletion base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
isreal(x::AbstractArray) = all(isreal,x)
iszero(x::AbstractArray) = all(iszero,x)
isreal(x::AbstractArray{<:Real}) = true
all(::typeof(isinteger), ::AbstractArray{<:Integer}) = true

## Constructors ##

Expand Down
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ Use [`push!`](@ref) to add individual items to `collection` which are not alread
themselves in another collection. The result of the preceding example is equivalent to
`push!([1, 2, 3], 4, 5, 6)`.
"""
function append!(a::Array{<:Any,1}, items::AbstractVector)
function append!(a::Vector, items::AbstractVector)
itemindices = eachindex(items)
n = length(itemindices)
_growend!(a, n)
Expand Down Expand Up @@ -932,7 +932,7 @@ julia> prepend!([3],[1,2])
"""
function prepend! end

function prepend!(a::Array{<:Any,1}, items::AbstractVector)
function prepend!(a::Vector, items::AbstractVector)
itemindices = eachindex(items)
n = length(itemindices)
_growbeg!(a, n)
Expand Down
4 changes: 2 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ function append!(B::BitVector, items::BitVector)
return B
end

append!(B::BitVector, items::AbstractVector{Bool}) = append!(B, BitArray(items))
append!(B::BitVector, items) = append!(B, BitArray(items))
append!(A::Vector{Bool}, items::BitVector) = append!(A, Array(items))

function prepend!(B::BitVector, items::BitVector)
Expand All @@ -761,7 +761,7 @@ function prepend!(B::BitVector, items::BitVector)
return B
end

prepend!(B::BitVector, items::AbstractVector{Bool}) = prepend!(B, BitArray(items))
prepend!(B::BitVector, items) = prepend!(B, BitArray(items))
prepend!(A::Vector{Bool}, items::BitVector) = prepend!(A, Array(items))

function sizehint!(B::BitVector, sz::Integer)
Expand Down
17 changes: 17 additions & 0 deletions base/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ StridedVector{T} = Union{DenseArray{T,1}, StridedSubArray{T,1}, StridedReshapedA
StridedMatrix{T} = Union{DenseArray{T,2}, StridedSubArray{T,2}, StridedReshapedArray{T,2}, StridedReinterpretArray{T,2}}
StridedVecOrMat{T} = Union{StridedVector{T}, StridedMatrix{T}}

# the definition of strides for Array{T,N} is tuple() if N = 0, otherwise it is
# a tuple containing 1 and a cumulative product of the first N-1 sizes
# this definition is also used for StridedReshapedArray and StridedReinterpretedArray
# which have the same memory storage as Array
function stride(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, i::Int)
if i > ndims(a)
return length(a)
end
s = 1
for n = 1:(i-1)
s *= size(a, n)
end
return s
end

strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)

function check_readable(a::ReinterpretArray{T, N, S} where N) where {T,S}
# See comment in check_writable
if !a.readable && !array_subpadding(T, S)
Expand Down
1 change: 0 additions & 1 deletion base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,6 @@ end
length(s::CodeUnits) = ncodeunits(s.s)
sizeof(s::CodeUnits{T}) where {T} = ncodeunits(s.s) * sizeof(T)
size(s::CodeUnits) = (length(s),)
strides(s::CodeUnits) = (1,)
elsize(s::CodeUnits{T}) where {T} = sizeof(T)
@propagate_inbounds getindex(s::CodeUnits, i::Int) = codeunit(s.s, i)
IndexStyle(::Type{<:CodeUnits}) = IndexLinear()
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
cosh, cot, coth, csc, csch, eltype, exp, fill!, floor, getindex, hcat,
getproperty, imag, inv, isapprox, isone, iszero, IndexStyle, kron, length, log, map, ndims,
oneunit, parent, power_by_squaring, print_matrix, promote_rule, real, round, sec, sech,
setindex!, show, similar, sin, sincos, sinh, size, size_to_strides, sqrt, StridedReinterpretArray,
StridedReshapedArray, strides, stride, tan, tanh, transpose, trunc, typed_hcat, vec
setindex!, show, similar, sin, sincos, sinh, size, sqrt,
strides, stride, tan, tanh, transpose, trunc, typed_hcat, vec
using Base: hvcat_fill, IndexLinear, promote_op, promote_typeof,
@propagate_inbounds, @pure, reduce, typed_vcat, require_one_based_indexing
using Base.Broadcast: Broadcasted
Expand Down
16 changes: 0 additions & 16 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,6 @@ isposdef(A::AbstractMatrix) =
ishermitian(A) && isposdef(cholesky(Hermitian(A); check = false))
isposdef(x::Number) = imag(x)==0 && real(x) > 0

# the definition of strides for Array{T,N} is tuple() if N = 0, otherwise it is
# a tuple containing 1 and a cumulative product of the first N-1 sizes
# this definition is also used for StridedReshapedArray and StridedReinterpretedArray
# which have the same memory storage as Array
function stride(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, i::Int)
if i > ndims(a)
return length(a)
end
s = 1
for n = 1:(i-1)
s *= size(a, n)
end
return s
end
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)

function norm(x::StridedVector{T}, rx::Union{UnitRange{TI},AbstractRange{TI}}) where {T<:BlasFloat,TI<:Integer}
if minimum(rx) < 1 || maximum(rx) > length(x)
throw(BoundsError(x, rx))
Expand Down
17 changes: 0 additions & 17 deletions stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,25 +856,8 @@ end
@testset "strides" begin
a = rand(10)
b = view(a,2:2:10)
A = rand(10,10)
B = view(A, 2:2:10, 2:2:10)

@test LinearAlgebra.stride1(a) == 1
@test LinearAlgebra.stride1(b) == 2

@test strides(a) == (1,)
@test strides(b) == (2,)
@test strides(A) == (1,10)
@test strides(B) == (2,20)

for M in (a, b, A, B)
@inferred strides(M)
strides_M = strides(M)

for (i, _stride) in enumerate(collect(strides_M))
@test _stride == stride(M, i)
end
end
end

@testset "inverse of Adjoint" begin
Expand Down
2 changes: 0 additions & 2 deletions stdlib/SharedArrays/src/SharedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ const SharedMatrix{T} = SharedArray{T,2}
SharedVector(A::Vector) = SharedArray(A)
SharedMatrix(A::Matrix) = SharedArray(A)

length(S::SharedArray) = prod(S.dims)
size(S::SharedArray) = S.dims
ndims(S::SharedArray) = length(S.dims)
IndexStyle(::Type{<:SharedArray}) = IndexLinear()

function reshape(a::SharedArray{T}, dims::NTuple{N,Int}) where {T,N}
Expand Down
21 changes: 21 additions & 0 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ let A = collect(reshape(1:20, 5, 4))
@test reshape(R, :) isa StridedArray
end

@testset "strides" begin
a = rand(10)
b = view(a,2:2:10)
A = rand(10,10)
B = view(A, 2:2:10, 2:2:10)

@test strides(a) == (1,)
@test strides(b) == (2,)
@test strides(A) == (1,10)
@test strides(B) == (2,20)

for M in (a, b, A, B)
@inferred strides(M)
strides_M = strides(M)

for (i, _stride) in enumerate(collect(strides_M))
@test _stride == stride(M, i)
end
end
end

# IndexStyle
let a = fill(1.0, 5, 3)
r = reinterpret(Int64, a)
Expand Down

0 comments on commit 4844a4b

Please sign in to comment.