Skip to content

Commit

Permalink
Deprecate _length in favor of length
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 11, 2018
1 parent 8f7f963 commit e99191a
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 80 deletions.
8 changes: 3 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ julia> length([1 2; 3 4])
```
"""
length(t::AbstractArray) = (@_inline_meta; prod(size(t)))
_length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, axes(A)))) # circumvent missing size
_length(A) = (@_inline_meta; length(A))

# `eachindex` is mostly an optimization of `keys`
eachindex(itrs...) = keys(itrs...)
Expand Down Expand Up @@ -220,7 +218,7 @@ function eachindex(A::AbstractArray, B::AbstractArray...)
@_inline_meta
eachindex(IndexStyle(A,B...), A, B...)
end
eachindex(::IndexLinear, A::AbstractArray) = (@_inline_meta; OneTo(_length(A)))
eachindex(::IndexLinear, A::AbstractArray) = (@_inline_meta; OneTo(length(A)))
eachindex(::IndexLinear, A::AbstractVector) = (@_inline_meta; axes1(A))
function eachindex(::IndexLinear, A::AbstractArray, B::AbstractArray...)
@_inline_meta
Expand Down Expand Up @@ -755,7 +753,7 @@ function copyto!(::IndexStyle, dest::AbstractArray, ::IndexCartesian, src::Abstr
end

function copyto!(dest::AbstractArray, dstart::Integer, src::AbstractArray)
copyto!(dest, dstart, src, first(LinearIndices(src)), _length(src))
copyto!(dest, dstart, src, first(LinearIndices(src)), length(src))
end

function copyto!(dest::AbstractArray, dstart::Integer, src::AbstractArray, sstart::Integer)
Expand Down Expand Up @@ -848,7 +846,7 @@ function iterate(A::AbstractArray, state=(eachindex(A),))
A[y[1]], (state[1], tail(y)...)
end

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


## range conversions ##
Expand Down
6 changes: 3 additions & 3 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ julia> vec(1:3)
See also [`reshape`](@ref).
"""
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 @@ -72,7 +72,7 @@ squeeze(A; dims) = _squeeze(A, dims)
function _squeeze(A::AbstractArray, dims::Dims)
for i in 1:length(dims)
1 <= dims[i] <= ndims(A) || throw(ArgumentError("squeezed dims must be in range 1:ndims(A)"))
_length(axes(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
length(axes(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
for j = 1:i-1
dims[j] == dims[i] && throw(ArgumentError("squeezed dims must be unique"))
end
Expand Down Expand Up @@ -158,7 +158,7 @@ function reverse(A::AbstractArray; dims::Integer)
B = similar(A)
nnd = 0
for i = 1:nd
nnd += Int(_length(inds[i])==1 || i==d)
nnd += Int(length(inds[i])==1 || i==d)
end
indsd = inds[d]
sd = first(indsd)+last(indsd)
Expand Down
8 changes: 4 additions & 4 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,9 @@ end

_collect_indices(::Tuple{}, A) = copyto!(Array{eltype(A),0}(undef), A)
_collect_indices(indsA::Tuple{Vararg{OneTo}}, A) =
copyto!(Array{eltype(A)}(undef, _length.(indsA)), A)
copyto!(Array{eltype(A)}(undef, length.(indsA)), A)
function _collect_indices(indsA, A)
B = Array{eltype(A)}(undef, _length.(indsA))
B = Array{eltype(A)}(undef, length.(indsA))
copyto!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA))
end

Expand Down Expand Up @@ -868,7 +868,7 @@ themselves in another collection. The result is of the preceding example is equi
"""
function append!(a::Array{<:Any,1}, items::AbstractVector)
itemindices = eachindex(items)
n = _length(itemindices)
n = length(itemindices)
_growend!(a, n)
copyto!(a, length(a)-n+1, items, first(itemindices), n)
return a
Expand Down Expand Up @@ -912,7 +912,7 @@ function prepend! end

function prepend!(a::Array{<:Any,1}, items::AbstractVector)
itemindices = eachindex(items)
n = _length(itemindices)
n = length(itemindices)
_growbeg!(a, n)
if a === items
copyto!(a, 1, items, n+1, n)
Expand Down
14 changes: 7 additions & 7 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function alignment(io::IO, X::AbstractVecOrMat,
break
end
end
if 1 < length(a) < _length(axes(X,2))
if 1 < length(a) < length(axes(X,2))
while sum(map(sum,a)) + sep*length(a) >= cols_otherwise
pop!(a)
end
Expand Down Expand Up @@ -168,7 +168,7 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
@assert textwidth(hdots) == textwidth(ddots)
sepsize = length(sep)
rowsA, colsA = axes(X,1), axes(X,2)
m, n = _length(rowsA), _length(colsA)
m, n = length(rowsA), length(colsA)
# To figure out alignments, only need to look at as many rows as could
# fit down screen. If screen has at least as many rows as A, look at A.
# If not, then we only need to look at the first and last chunks of A,
Expand Down Expand Up @@ -266,10 +266,10 @@ function show_nd(io::IO, a::AbstractArray, print_matrix::Function, label_slices:
for i = 1:nd
ii = idxs[i]
ind = tailinds[i]
if _length(ind) > 10
if length(ind) > 10
if ii == ind[firstindex(ind)+3] && all(d->idxs[d]==first(tailinds[d]),1:i-1)
for j=i+1:nd
szj = _length(axes(a, j+2))
szj = length(axes(a, j+2))
indj = tailinds[j]
if szj>10 && first(indj)+2 < idxs[j] <= last(indj)-3
@goto skip
Expand Down Expand Up @@ -313,7 +313,7 @@ print_array(io::IO, X::AbstractArray) = show_nd(io, X, print_matrix, true)
# implements: show(io::IO, ::MIME"text/plain", X::AbstractArray)
function show(io::IO, ::MIME"text/plain", X::AbstractArray)
# 0) compute new IOContext
if !haskey(io, :compact) && _length(axes(X, 2)) > 1
if !haskey(io, :compact) && length(axes(X, 2)) > 1
io = IOContext(io, :compact => true)
end
if get(io, :limit, false) && eltype(X) === Method
Expand Down Expand Up @@ -359,7 +359,7 @@ function _show_nonempty(io::IO, X::AbstractMatrix, prefix::String)
@assert !isempty(X)
limit = get(io, :limit, false)::Bool
indr, indc = axes(X,1), axes(X,2)
nr, nc = _length(indr), _length(indc)
nr, nc = length(indr), length(indc)
rdots, cdots = false, false
rr1, rr2 = UnitRange{Int}(indr), 1:0
cr1, cr2 = UnitRange{Int}(indc), 1:0
Expand Down Expand Up @@ -432,7 +432,7 @@ function show_vector(io::IO, v, opn='[', cls=']')
# directly or indirectly, the context now knows about eltype(v)
io = IOContext(io, :typeinfo => eltype(v), :compact => get(io, :compact, true))
limited = get(io, :limit, false)
if limited && _length(v) > 20
if limited && length(v) > 20
inds = axes1(v)
show_delim_array(io, v, opn, ",", "", false, inds[1], inds[1]+9)
print(io, "")
Expand Down
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()
@inline function _newindexer(indsA::Tuple)
ind1 = indsA[1]
keep, Idefault = _newindexer(tail(indsA))
(Base._length(ind1)!=1, keep...), (first(ind1), Idefault...)
(Base.length(ind1)!=1, keep...), (first(ind1), Idefault...)
end

@inline function Base.getindex(bc::Broadcasted, I::Union{Integer,CartesianIndex})
Expand Down
1 change: 1 addition & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,7 @@ function print(io::IO, ::Nothing)
end

@deprecate indices1 axes1
@deprecate _length length

# END 0.7 deprecations

Expand Down
9 changes: 4 additions & 5 deletions base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,16 @@ function setindex_shape_check(X::AbstractArray, I::Integer...)
end

setindex_shape_check(X::AbstractArray) =
(_length(X)==1 || throw_setindex_mismatch(X,()))
(length(X)==1 || throw_setindex_mismatch(X,()))

setindex_shape_check(X::AbstractArray, i::Integer) =
(_length(X)==i || throw_setindex_mismatch(X, (i,)))
(length(X)==i || throw_setindex_mismatch(X, (i,)))

setindex_shape_check(X::AbstractArray{<:Any,1}, i::Integer) =
(_length(X)==i || throw_setindex_mismatch(X, (i,)))
(length(X)==i || throw_setindex_mismatch(X, (i,)))

setindex_shape_check(X::AbstractArray{<:Any,1}, i::Integer, j::Integer) =
(_length(X)==i*j || throw_setindex_mismatch(X, (i,j)))
(length(X)==i*j || throw_setindex_mismatch(X, (i,j)))

function setindex_shape_check(X::AbstractArray{<:Any,2}, i::Integer, j::Integer)
if length(X) != i*j
Expand Down Expand Up @@ -291,7 +291,6 @@ first(S::Slice) = first(S.indices)
last(S::Slice) = last(S.indices)
size(S::Slice) = (length(S.indices),)
length(S::Slice) = length(S.indices)
_length(S::Slice) = length(S.indices)
unsafe_length(S::Slice) = unsafe_length(S.indices)
getindex(S::Slice, i::Int) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
getindex(S::Slice, i::AbstractUnitRange{<:Integer}) = (@_inline_meta; @boundscheck checkbounds(S, i); i)
Expand Down
1 change: 0 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,6 @@ _prod_axes1(a, A) =

ndims(p::ProductIterator) = length(axes(p))
length(P::ProductIterator) = prod(size(P))
_length(p::ProductIterator) = prod(map(unsafe_length, axes(p)))

IteratorEltype(::Type{ProductIterator{Tuple{}}}) = HasEltype()
IteratorEltype(::Type{ProductIterator{Tuple{I}}}) where {I} = IteratorEltype(I)
Expand Down
10 changes: 5 additions & 5 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ module IteratorsMD
end

simd_inner_length(iter::CartesianIndices{0}, ::CartesianIndex) = 1
simd_inner_length(iter::CartesianIndices, I::CartesianIndex) = Base._length(iter.indices[1])
simd_inner_length(iter::CartesianIndices, I::CartesianIndex) = Base.length(iter.indices[1])

simd_index(iter::CartesianIndices{0}, ::CartesianIndex, I1::Int) = first(iter)
@inline function simd_index(iter::CartesianIndices, Ilast::CartesianIndex, I1::Int)
Expand Down Expand Up @@ -463,7 +463,7 @@ index_dimsum() = ()
index_lengths() = ()
@inline index_lengths(::Real, rest...) = (1, index_lengths(rest...)...)
@inline index_lengths(A::AbstractArray, rest...) = (length(A), index_lengths(rest...)...)
@inline index_lengths(A::Slice, rest...) = (_length(axes1(A)), index_lengths(rest...)...)
@inline index_lengths(A::Slice, rest...) = (length(axes1(A)), index_lengths(rest...)...)

# shape of array to create for getindex() with indices I, dropping scalars
# returns a Tuple{Vararg{AbstractUnitRange}} of indices
Expand Down Expand Up @@ -898,7 +898,7 @@ circshift!(dest::AbstractArray, src, shiftamt) = circshift!(dest, src, (shiftamt
inds::Tuple{AbstractUnitRange,Vararg{Any}},
shiftamt::Tuple{Integer,Vararg{Any}})
ind1, d = inds[1], shiftamt[1]
s = mod(d, _length(ind1))
s = mod(d, length(ind1))
sf, sl = first(ind1)+s, last(ind1)-s
r1, r2 = first(ind1):sf-1, sf:last(ind1)
r3, r4 = first(ind1):sl, sl+1:last(ind1)
Expand Down Expand Up @@ -946,7 +946,7 @@ true
function circcopy!(dest, src)
dest === src && throw(ArgumentError("dest and src must be separate arrays"))
indssrc, indsdest = axes(src), axes(dest)
if (szsrc = map(_length, indssrc)) != (szdest = map(_length, indsdest))
if (szsrc = map(length, indssrc)) != (szdest = map(length, indsdest))
throw(DimensionMismatch("src and dest must have the same sizes (got $szsrc and $szdest)"))
end
shift = map((isrc, idest)->first(isrc)-first(idest), indssrc, indsdest)
Expand All @@ -958,7 +958,7 @@ end
@inline function _circcopy!(dest, rdest, indsdest::Tuple{AbstractUnitRange,Vararg{Any}},
src, rsrc, indssrc::Tuple{AbstractUnitRange,Vararg{Any}})
indd1, inds1 = indsdest[1], indssrc[1]
l = _length(indd1)
l = length(indd1)
s = mod(first(inds1)-first(indd1), l)
sdf = first(indd1)+s
rd1, rd2 = first(indd1):sdf-1, sdf:last(indd1)
Expand Down
6 changes: 3 additions & 3 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function check_reducedims(R, A)
had_nonreduc = false
for i = 1:ndims(A)
Ri, Ai = axes(R, i), axes(A, i)
sRi, sAi = _length(Ri), _length(Ai)
sRi, sAi = length(Ri), length(Ai)
if sRi == 1
if sAi > 1
if had_nonreduc
Expand Down Expand Up @@ -209,7 +209,7 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray)

if has_fast_linear_indexing(A) && lsiz > 16
# use mapreduce_impl, which is probably better tuned to achieve higher performance
nslices = div(_length(A), lsiz)
nslices = div(length(A), lsiz)
ibase = first(LinearIndices(A))-1
for i = 1:nslices
@inbounds R[i] = op(R[i], mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
Expand Down Expand Up @@ -795,7 +795,7 @@ function _findmax(A, region)
end
end

reducedim1(R, A) = _length(axes1(R)) == 1
reducedim1(R, A) = length(axes1(R)) == 1

"""
argmin(A; dims) -> indices
Expand Down
14 changes: 7 additions & 7 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = _reshape
@inline function _reshape_uncolon(A, dims)
@noinline throw1(dims) = throw(DimensionMismatch(string("new dimensions $(dims) ",
"may have at most one omitted dimension specified by `Colon()`")))
@noinline throw2(A, dims) = throw(DimensionMismatch(string("array size $(_length(A)) ",
@noinline throw2(A, dims) = throw(DimensionMismatch(string("array size $(length(A)) ",
"must be divisible by the product of the new dimensions $dims")))
pre = _before_colon(dims...)
post = _after_colon(dims...)
any(d -> d isa Colon, post) && throw1(dims)
sz, remainder = divrem(_length(A), prod(pre)*prod(post))
sz, remainder = divrem(length(A), prod(pre)*prod(post))
remainder == 0 || throw2(A, dims)
(pre..., Int(sz), post...)
end
Expand All @@ -124,8 +124,8 @@ end
# Move elements from inds to out until out reaches the desired
# dimensionality N, either filling with OneTo(1) or collapsing the
# product of trailing dims into the last element
rdims_trailing(l, inds...) = _length(l) * rdims_trailing(inds...)
rdims_trailing(l) = _length(l)
rdims_trailing(l, inds...) = length(l) * rdims_trailing(inds...)
rdims_trailing(l) = length(l)
rdims(out::Val{N}, inds::Tuple) where {N} = rdims(ntuple(i -> OneTo(1), Val(N)), inds)
rdims(out::Tuple{}, inds::Tuple{}) = () # N == 0, M == 0
rdims(out::Tuple{}, inds::Tuple{Any}) = ()
Expand All @@ -147,12 +147,12 @@ _reshape(parent::Array, dims::Dims) = reshape(parent, dims)
function _reshape(v::AbstractVector, dims::Dims{1})
@assert !has_offset_axes(v)
len = dims[1]
len == length(v) || _throw_dmrs(_length(v), "length", len)
len == length(v) || _throw_dmrs(length(v), "length", len)
v
end
# General reshape
function _reshape(parent::AbstractArray, dims::Dims)
n = _length(parent)
n = length(parent)
prod(dims) == n || _throw_dmrs(n, "size", dims)
__reshape((parent, IndexStyle(parent)), dims)
end
Expand All @@ -167,7 +167,7 @@ _reshape(R::ReshapedArray, dims::Dims) = _reshape(R.parent, dims)

function __reshape(p::Tuple{AbstractArray,IndexCartesian}, dims::Dims)
parent = p[1]
strds = front(size_to_strides(map(_length, axes(parent))..., 1))
strds = front(size_to_strides(map(length, axes(parent))..., 1))
strds1 = map(s->max(1,Int(s)), strds) # for resizing empty arrays
mi = map(SignedMultiplicativeInverse, strds1)
ReshapedArray(parent, dims, reverse(mi))
Expand Down
2 changes: 1 addition & 1 deletion base/simdloop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ check_body!(x) = true
simd_outer_range(r) = 0:0

# Get trip count for inner loop.
@inline simd_inner_length(r,j::Int) = Base._length(r)
@inline simd_inner_length(r,j::Int) = Base.length(r)

# Construct user-level element from original range, outer loop index j, and inner loop index i.
@inline simd_index(r,j::Int,i) = (@inbounds ret = r[i+firstindex(r)]; ret)
Expand Down
8 changes: 4 additions & 4 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Sort
import ..@__MODULE__, ..parentmodule
const Base = parentmodule(@__MODULE__)
using .Base.Order
using .Base: copymutable, LinearIndices, _length, (:),
using .Base: copymutable, LinearIndices, length, (:),
eachindex, axes, first, last, similar, start, next, done, zip, OrdinalRange,
AbstractVector, @inbounds, AbstractRange, @eval, @inline, Vector, @noinline,
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !,
Expand Down Expand Up @@ -628,7 +628,7 @@ function sort!(v::AbstractVector;
order::Ordering=Forward)
ordr = ord(lt,by,rev,order)
if ordr === Forward && isa(v,Vector) && eltype(v)<:Integer
n = _length(v)
n = length(v)
if n > 1
min, max = extrema(v)
(diff, o1) = sub_with_overflow(max, min)
Expand Down Expand Up @@ -786,7 +786,7 @@ function sortperm(v::AbstractVector;
order::Ordering=Forward)
ordr = ord(lt,by,rev,order)
if ordr === Forward && isa(v,Vector) && eltype(v)<:Integer
n = _length(v)
n = length(v)
if n > 1
min, max = extrema(v)
(diff, o1) = sub_with_overflow(max, min)
Expand Down Expand Up @@ -911,7 +911,7 @@ function sort(A::AbstractArray;
Base.depwarn("`initialized` keyword argument is deprecated", :sort)
end
order = ord(lt,by,rev,order)
n = _length(axes(A, dim))
n = length(axes(A, dim))
if dim != 1
pdims = (dim, setdiff(1:ndims(A), dim)...) # put the selected dimension first
Ap = permutedims(A, pdims)
Expand Down
6 changes: 3 additions & 3 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ function length(s::String, i::Int, j::Int)
j < i && return 0
@inbounds i, k = thisind(s, i), i
c = j - i + (i == k)
_length(s, i, j, c)
length(s, i, j, c)
end

length(s::String) = _length(s, 1, ncodeunits(s), ncodeunits(s))
length(s::String) = length(s, 1, ncodeunits(s), ncodeunits(s))

@inline function _length(s::String, i::Int, n::Int, c::Int)
@inline function length(s::String, i::Int, n::Int, c::Int)
i < n || return c
@inbounds b = codeunit(s, i)
@inbounds while true
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
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
using Base: hvcat_fill, iszero, IndexLinear, _length, promote_op, promote_typeof,
using Base: hvcat_fill, iszero, IndexLinear, promote_op, promote_typeof,
@propagate_inbounds, @pure, reduce, typed_vcat, has_offset_axes
using Base.Broadcast: Broadcasted

Expand Down
Loading

0 comments on commit e99191a

Please sign in to comment.