Skip to content

Commit 138b1cc

Browse files
authored
Consistency in traits (mostly docs) (#191)
* Clean up documentation and trait consistency Code changes here are mostly shifting the fall back definitions to be called on the type (is_trait(::Type{T})=false) instead of the instance (is_trait(x)=false). Other places I've improved consistency in doc strings and corrected how optionally static ranges print. * Missed this commit * Update range doc string
1 parent b572dc1 commit 138b1cc

File tree

5 files changed

+53
-62
lines changed

5 files changed

+53
-62
lines changed

src/ArrayInterface.jl

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,12 @@ can_change_size(::Type{<:Base.ImmutableDict}) = false
119119
function ismutable end
120120

121121
"""
122-
ismutable(x::DataType) -> Bool
122+
ismutable(::Type{T}) -> Bool
123123
124-
Query whether a type is mutable or not, see
124+
Query whether instances of type `T` are mutable or not, see
125125
https://github.com/JuliaDiffEq/RecursiveArrayTools.jl/issues/19.
126126
"""
127127
ismutable(x) = ismutable(typeof(x))
128-
129128
function ismutable(::Type{T}) where {T<:AbstractArray}
130129
if parent_type(T) <: T
131130
return true
@@ -168,12 +167,12 @@ function Base.setindex(x::AbstractMatrix, v, i::Int, j::Int)
168167
end
169168

170169
"""
171-
can_setindex(x::DataType) -> Bool
170+
can_setindex(::Type{T}) -> Bool
172171
173172
Query whether a type can use `setindex!`.
174173
"""
175-
can_setindex(x) = true
176-
can_setindex(x::AbstractArray) = can_setindex(typeof(x))
174+
can_setindex(x) = can_setindex(typeof(x))
175+
can_setindex(::Type) = true
177176
can_setindex(::Type{<:AbstractRange}) = false
178177

179178
"""
@@ -188,8 +187,8 @@ aos_to_soa(x) = x
188187
189188
Query whether an array type has fast scalar indexing.
190189
"""
191-
fast_scalar_indexing(x) = true
192-
fast_scalar_indexing(x::AbstractArray) = fast_scalar_indexing(typeof(x))
190+
fast_scalar_indexing(x) = fast_scalar_indexing(typeof(x))
191+
fast_scalar_indexing(::Type) = true
193192
fast_scalar_indexing(::Type{<:LinearAlgebra.AbstractQ}) = false
194193
fast_scalar_indexing(::Type{<:LinearAlgebra.LQPackedQ}) = false
195194

@@ -208,34 +207,34 @@ A scalar `setindex!` which is always allowed.
208207
allowed_setindex!(x, v, i...) = Base.setindex!(x, v, i...)
209208

210209
"""
211-
isstructured(x::DataType) -> Bool
210+
isstructured(::Type{T}) -> Bool
212211
213212
Query whether a type is a representation of a structured matrix.
214213
"""
215-
isstructured(x) = false
216-
isstructured(x::AbstractArray) = isstructured(typeof(x))
217-
isstructured(::Symmetric) = true
218-
isstructured(::Hermitian) = true
219-
isstructured(::UpperTriangular) = true
220-
isstructured(::LowerTriangular) = true
221-
isstructured(::Tridiagonal) = true
222-
isstructured(::SymTridiagonal) = true
223-
isstructured(::Bidiagonal) = true
224-
isstructured(::Diagonal) = true
214+
isstructured(x) = isstructured(typeof(x))
215+
isstructured(::Type) = false
216+
isstructured(::Type{<:Symmetric}) = true
217+
isstructured(::Type{<:Hermitian}) = true
218+
isstructured(::Type{<:UpperTriangular}) = true
219+
isstructured(::Type{<:LowerTriangular}) = true
220+
isstructured(::Type{<:Tridiagonal}) = true
221+
isstructured(::Type{<:SymTridiagonal}) = true
222+
isstructured(::Type{<:Bidiagonal}) = true
223+
isstructured(::Type{<:Diagonal}) = true
225224

226225
"""
227226
has_sparsestruct(x::AbstractArray) -> Bool
228227
229228
Determine whether `findstructralnz` accepts the parameter `x`.
230229
"""
231-
has_sparsestruct(x) = false
232-
has_sparsestruct(x::AbstractArray) = has_sparsestruct(typeof(x))
233-
has_sparsestruct(x::Type{<:AbstractArray}) = false
234-
has_sparsestruct(x::Type{<:SparseMatrixCSC}) = true
235-
has_sparsestruct(x::Type{<:Diagonal}) = true
236-
has_sparsestruct(x::Type{<:Bidiagonal}) = true
237-
has_sparsestruct(x::Type{<:Tridiagonal}) = true
238-
has_sparsestruct(x::Type{<:SymTridiagonal}) = true
230+
has_sparsestruct(x) = has_sparsestruct(typeof(x))
231+
has_sparsestruct(::Type) = false
232+
has_sparsestruct(::Type{<:AbstractArray}) = false
233+
has_sparsestruct(::Type{<:SparseMatrixCSC}) = true
234+
has_sparsestruct(::Type{<:Diagonal}) = true
235+
has_sparsestruct(::Type{<:Bidiagonal}) = true
236+
has_sparsestruct(::Type{<:Tridiagonal}) = true
237+
has_sparsestruct(::Type{<:SymTridiagonal}) = true
239238

240239
"""
241240
issingular(A::AbstractMatrix) -> Bool

src/dimensions.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ ndims_index(::Type{I}) where {N,I<:Tuple{Vararg{Any,N}}} = eachop(_ndims_index,
3838

3939
"""
4040
from_parent_dims(::Type{T}) -> Tuple{Vararg{Union{Int,StaticInt}}}
41+
from_parent_dims(::Type{T}, dim) -> Union{Int,StaticInt}
4142
4243
Returns the mapping from parent dimensions to child dimensions.
4344
"""
@@ -71,11 +72,6 @@ function from_parent_dims(::Type{R}) where {T,N,S,A,R<:ReinterpretArray{T,N,S,A}
7172
end
7273
end
7374

74-
"""
75-
from_parent_dims(::Type{T}, dim) -> Union{Int,StaticInt}
76-
77-
Returns the mapping from child dimensions to parent dimensions.
78-
"""
7975
from_parent_dims(x, dim) = from_parent_dims(typeof(x), dim)
8076
@aggressive_constprop function from_parent_dims(::Type{T}, dim::Int)::Int where {T}
8177
if dim > ndims(T)
@@ -99,6 +95,7 @@ end
9995

10096
"""
10197
to_parent_dims(::Type{T}) -> Tuple{Vararg{Union{Int,StaticInt}}}
98+
to_parent_dims(::Type{T}, dim) -> Union{Int,StaticInt}
10299
103100
Returns the mapping from child dimensions to parent dimensions.
104101
"""
@@ -129,11 +126,6 @@ function to_parent_dims(::Type{R}) where {T,N,S,A,R<:ReinterpretArray{T,N,S,A}}
129126
end
130127
end
131128

132-
"""
133-
to_parent_dims(::Type{T}, dim) -> Union{Int,StaticInt}
134-
135-
Returns the mapping from child dimensions to parent dimensions.
136-
"""
137129
to_parent_dims(x, dim) = to_parent_dims(typeof(x), dim)
138130
@aggressive_constprop function to_parent_dims(::Type{T}, dim::Int)::Int where {T}
139131
if dim > ndims(T)

src/ranges.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
7575
"""
7676
OptionallyStaticUnitRange(start, stop) <: AbstractUnitRange{Int}
7777
78-
This range permits diverse representations of arrays to communicate common information
79-
about their indices. Each field may be an integer or `Val(<:Integer)` if it is known
80-
at compile time. An `OptionallyStaticUnitRange` is intended to be constructed internally
81-
from other valid indices. Therefore, users should not expect the same checks are used
82-
to ensure construction of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
78+
Similar to `UnitRange` except each field may be an `Int` or `StaticInt`. An
79+
`OptionallyStaticUnitRange` is intended to be constructed internally from other valid
80+
indices. Therefore, users should not expect the same checks are used to ensure construction
81+
of a valid `OptionallyStaticUnitRange` as a `UnitRange`.
8382
"""
8483
struct OptionallyStaticUnitRange{F<:CanonicalInt,L<:CanonicalInt} <: AbstractUnitRange{Int}
8584
start::F
@@ -140,13 +139,14 @@ the range operator (i.e., `:`).
140139
```julia
141140
julia> using ArrayInterface
142141
143-
julia> x = ArrayInterface.StaticInt(2);
142+
julia> x = ArrayInterface.static(2);
144143
145144
julia> x:x:10
146-
ArrayInterface.StaticInt{2}():ArrayInterface.StaticInt{2}():10
145+
static(2):static(2):10
147146
148147
julia> ArrayInterface.OptionallyStaticStepRange(x, x, 10)
149-
ArrayInterface.StaticInt{2}():ArrayInterface.StaticInt{2}():10
148+
static(2):static(2):10
149+
150150
```
151151
"""
152152
struct OptionallyStaticStepRange{F<:CanonicalInt,S<:CanonicalInt,L<:CanonicalInt} <: OrdinalRange{Int,Int}
@@ -422,16 +422,16 @@ function Base.reverse(r::OptionallyStaticStepRange)
422422
return OptionallyStaticStepRange(static_last(r), -static_step(r), static_first(r))
423423
end
424424

425-
function Base.show(io::IO, r::OptionallyStaticRange)
426-
print(io, first(r))
425+
function Base.show(io::IO, ::MIME"text/plain", r::OptionallyStaticRange)
426+
print(io, static_first(r))
427427
if known_step(r) === 1
428428
print(io, ":")
429429
else
430430
print(io, ":")
431-
print(io, step(r))
431+
print(io, static_step(r))
432432
print(io, ":")
433433
end
434-
print(io, last(r))
434+
print(io, static_last(r))
435435
end
436436

437437
"""
@@ -501,40 +501,40 @@ n = 16
501501
502502
More importantly, `reduce_tup(_pick_range, inds)` often performs better than `reduce(_pick_range, inds)`.
503503
```julia
504-
julia> using ArrayInterface, BenchmarkTools
504+
julia> using ArrayInterface, BenchmarkTools, Static
505505
506-
julia> inds = (Base.OneTo(100), 1:100, 1:ArrayInterface.StaticInt(100))
507-
(Base.OneTo(100), 1:100, 1:Static(100))
506+
julia> inds = (Base.OneTo(100), 1:100, 1:static(100))
507+
(Base.OneTo(100), 1:100, 1:static(100))
508508
509509
julia> @btime reduce(ArrayInterface._pick_range, \$(Ref(inds))[])
510510
6.405 ns (0 allocations: 0 bytes)
511-
Base.Slice(Static(1):Static(100))
511+
Base.Slice(static(1):static(100))
512512
513513
julia> @btime ArrayInterface.reduce_tup(ArrayInterface._pick_range, \$(Ref(inds))[])
514514
2.570 ns (0 allocations: 0 bytes)
515-
Base.Slice(Static(1):Static(100))
515+
Base.Slice(static(1):static(100))
516516
517517
julia> inds = (Base.OneTo(100), 1:100, 1:UInt(100))
518518
(Base.OneTo(100), 1:100, 0x0000000000000001:0x0000000000000064)
519519
520520
julia> @btime reduce(ArrayInterface._pick_range, \$(Ref(inds))[])
521521
6.411 ns (0 allocations: 0 bytes)
522-
Base.Slice(Static(1):100)
522+
Base.Slice(static(1):100)
523523
524524
julia> @btime ArrayInterface.reduce_tup(ArrayInterface._pick_range, \$(Ref(inds))[])
525525
2.592 ns (0 allocations: 0 bytes)
526-
Base.Slice(Static(1):100)
526+
Base.Slice(static(1):100)
527527
528528
julia> inds = (Base.OneTo(100), 1:100, 1:UInt(100), Int32(1):Int32(100))
529529
(Base.OneTo(100), 1:100, 0x0000000000000001:0x0000000000000064, 1:100)
530530
531531
julia> @btime reduce(ArrayInterface._pick_range, \$(Ref(inds))[])
532532
9.048 ns (0 allocations: 0 bytes)
533-
Base.Slice(Static(1):100)
533+
Base.Slice(static(1):100)
534534
535535
julia> @btime ArrayInterface.reduce_tup(ArrayInterface._pick_range, \$(Ref(inds))[])
536536
2.569 ns (0 allocations: 0 bytes)
537-
Base.Slice(Static(1):100)
537+
Base.Slice(static(1):100)
538538
```
539539
"""
540540
@generated function reduce_tup(f::F, inds::Tuple{Vararg{Any,N}}) where {F,N}

src/size.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ julia> using StaticArrays, ArrayInterface
1212
julia> A = @SMatrix rand(3,4);
1313
1414
julia> ArrayInterface.size(A)
15-
(StaticInt{3}(), StaticInt{4}())
15+
(static(3), static(4))
1616
```
1717
"""
1818
function size(a::A) where {A}
@@ -22,7 +22,6 @@ function size(a::A) where {A}
2222
return size(parent(a))
2323
end
2424
end
25-
#size(a::AbstractVector) = (size(a, One()),)
2625

2726
size(x::SubArray) = eachop(_sub_size, to_parent_dims(x), x.indices)
2827
_sub_size(x::Tuple, ::StaticInt{dim}) where {dim} = static_length(getfield(x, dim))

src/stridelayout.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function _offsets(x::X, dim::StaticInt{D}) where {X,D}
6666
end
6767

6868
"""
69-
known_offset1(x) -> Union{Int,Nothing}
69+
known_offset1(::Type{T}) -> Union{Int,Nothing}
7070
7171
Returns the linear offset of array `x` if known at compile time.
7272
"""
@@ -474,7 +474,8 @@ function known_strides(::Type{T}) where {T}
474474
end
475475

476476
"""
477-
strides(A) -> Tuple
477+
strides(A) -> Tuple{Vararg{Union{Int,StaticInt}}}
478+
strides(A, dim) -> Union{Int,StaticInt}
478479
479480
Returns the strides of array `A`. If any strides are known at compile time,
480481
these should be returned as `Static` numbers. For example:

0 commit comments

Comments
 (0)