Skip to content

Commit 546c5a7

Browse files
authored
Merge pull request #259 from nchisholm/master
Rename static_length to length and document
2 parents 4ebd62a + 50e75a3 commit 546c5a7

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

src/ArrayInterface.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,28 @@ const MatAdjTrans{T,M<:AbstractMatrix{T}} = Union{Transpose{T,M},Adjoint{T,M}}
3636
const UpTri{T,M} = Union{UpperTriangular{T,M},UnitUpperTriangular{T,M}}
3737
const LoTri{T,M} = Union{LowerTriangular{T,M},UnitLowerTriangular{T,M}}
3838

39-
@inline static_length(a::UnitRange{T}) where {T} = last(a) - first(a) + oneunit(T)
40-
@inline static_length(x) = Static.maybe_static(known_length, length, x)
39+
"""
40+
length(A) -> Union{Int,StaticInt}
41+
42+
Returns the length of `A`. If the length is known at compile time, it is
43+
returned as `Static` number. Otherwise, `ArrayInterface.length(A)` is identical
44+
to `Base.length(A)`.
45+
46+
```julia
47+
julia> using StaticArrays, ArrayInterface
48+
49+
julia> A = @SMatrix rand(3,4);
50+
51+
julia> ArrayInterface.length(A)
52+
static(12)
53+
```
54+
"""
55+
@inline length(a::UnitRange{T}) where {T} = last(a) - first(a) + oneunit(T)
56+
@inline length(x) = Static.maybe_static(known_length, Base.length, x)
57+
58+
# Alias to to-be-depreciated internal function
59+
const static_length = length
60+
4161
@inline static_first(x) = Static.maybe_static(known_first, first, x)
4262
@inline static_last(x) = Static.maybe_static(known_last, last, x)
4363
@inline static_step(x) = Static.maybe_static(known_step, step, x)

src/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ broadcast_axis(x, y) = broadcast_axis(BroadcastAxis(x), x, y)
3030
# stagger default broadcasting in case y has something other than default
3131
broadcast_axis(::BroadcastAxisDefault, x, y) = _broadcast_axis(BroadcastAxis(y), x, y)
3232
function _broadcast_axis(::BroadcastAxisDefault, x, y)
33-
return One():_combine_length(static_length(x), static_length(y))
33+
return One():_combine_length(length(x), length(y))
3434
end
3535
_broadcast_axis(s::BroadcastAxis, x, y) = broadcast_axis(s, x, y)
3636

src/indexing.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ indices calling [`to_axis`](@ref).
243243
@inline function to_axes(A, inds::Tuple)
244244
if ndims(A) === 1
245245
return (to_axis(axes(A, 1), first(inds)),)
246-
elseif length(inds) === 1
246+
elseif Base.length(inds) === 1
247247
return (to_axis(eachindex(IndexLinear(), A), first(inds)),)
248248
else
249249
return to_axes(A, axes(A), inds)
@@ -292,7 +292,7 @@ end
292292
return axis
293293
end
294294
end
295-
to_axis(S::IndexLinear, axis, inds) = StaticInt(1):static_length(inds)
295+
to_axis(S::IndexLinear, axis, inds) = StaticInt(1):length(inds)
296296

297297
"""
298298
ArrayInterface.getindex(A, args...)
@@ -378,14 +378,14 @@ end
378378
_ints2range(x::Integer) = x:x
379379
_ints2range(x::AbstractRange) = x
380380
@inline function unsafe_get_collection(A::CartesianIndices{N}, inds) where {N}
381-
if (length(inds) === 1 && N > 1) || stride_preserving_index(typeof(inds)) === False()
381+
if (Base.length(inds) === 1 && N > 1) || stride_preserving_index(typeof(inds)) === False()
382382
return Base._getindex(IndexStyle(A), A, inds...)
383383
else
384384
return CartesianIndices(to_axes(A, _ints2range.(inds)))
385385
end
386386
end
387387
@inline function unsafe_get_collection(A::LinearIndices{N}, inds) where {N}
388-
if length(inds) === 1 && isone(_ndims_index(typeof(inds), static(1)))
388+
if Base.length(inds) === 1 && isone(_ndims_index(typeof(inds), static(1)))
389389
return @inbounds(eachindex(A)[first(inds)])
390390
elseif stride_preserving_index(typeof(inds)) === True()
391391
return LinearIndices(to_axes(A, _ints2range.(inds)))

src/ranges.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function Base.AbstractUnitRange{T}(r::OptionallyStaticUnitRange) where {T}
356356
end
357357
end
358358

359-
Base.eachindex(r::OptionallyStaticRange) = One():static_length(r)
359+
Base.eachindex(r::OptionallyStaticRange) = One():length(r)
360360
@inline function Base.iterate(r::OptionallyStaticRange)
361361
isempty(r) && return nothing
362362
fi = Int(first(r));

src/size.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ julia> ArrayInterface.size(A)
1717
```
1818
"""
1919
size(a::A) where {A} = _maybe_size(Base.IteratorSize(A), a)
20-
size(a::Base.Broadcast.Broadcasted) = map(static_length, axes(a))
20+
size(a::Base.Broadcast.Broadcasted) = map(length, axes(a))
2121

22-
_maybe_size(::Base.HasShape{N}, a::A) where {N,A} = map(static_length, axes(a))
23-
_maybe_size(::Base.HasLength, a::A) where {A} = (static_length(a),)
22+
_maybe_size(::Base.HasShape{N}, a::A) where {N,A} = map(length, axes(a))
23+
_maybe_size(::Base.HasLength, a::A) where {A} = (length(a),)
2424
size(x::SubArray) = eachop(_sub_size, to_parent_dims(x), x.indices)
25-
_sub_size(x::Tuple, ::StaticInt{dim}) where {dim} = static_length(getfield(x, dim))
25+
_sub_size(x::Tuple, ::StaticInt{dim}) where {dim} = length(getfield(x, dim))
2626
@inline size(B::VecAdjTrans) = (One(), length(parent(B)))
2727
@inline size(B::MatAdjTrans) = permute(size(parent(B)), to_parent_dims(B))
2828
@inline function size(B::PermutedDimsArray{T,N,I1}) where {T,N,I1}
@@ -43,7 +43,7 @@ function size(a::ReinterpretArray{T,N,S,A}) where {T,N,S,A}
4343
end
4444
end
4545
size(A::ReshapedArray) = Base.size(A)
46-
size(A::AbstractRange) = (static_length(A),)
46+
size(A::AbstractRange) = (length(A),)
4747
size(x::Base.Generator) = size(getfield(x, :iter))
4848
size(x::Iterators.Reverse) = size(getfield(x, :itr))
4949
size(x::Iterators.Enumerate) = size(getfield(x, :itr))
@@ -72,7 +72,7 @@ function size(A::SubArray, dim::Integer)
7272
if pdim > ndims(parent_type(A))
7373
return size(parent(A), pdim)
7474
else
75-
return static_length(A.indices[pdim])
75+
return length(A.indices[pdim])
7676
end
7777
end
7878
size(x::Iterators.Zip) = Static.reduce_tup(promote_shape, map(size, getfield(x, :is)))

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,9 @@ end
790790
v = @SVector rand(8);
791791
A = @MMatrix rand(7,6);
792792
T = SizedArray{Tuple{5,4,3}}(zeros(5,4,3));
793-
@test @inferred(ArrayInterface.static_length(v)) === StaticInt(8)
794-
@test @inferred(ArrayInterface.static_length(A)) === StaticInt(42)
795-
@test @inferred(ArrayInterface.static_length(T)) === StaticInt(60)
793+
@test @inferred(ArrayInterface.length(v)) === StaticInt(8)
794+
@test @inferred(ArrayInterface.length(A)) === StaticInt(42)
795+
@test @inferred(ArrayInterface.length(T)) === StaticInt(60)
796796
end
797797

798798
@testset "indices" begin

0 commit comments

Comments
 (0)