Skip to content

Commit 8a95783

Browse files
committed
Improve orginization
Moved some methods to common files in ArrayInterfaceCore, but mostly just organized imports into ArrayInterface so it's easier to keep track of
1 parent 74c992a commit 8a95783

File tree

4 files changed

+98
-81
lines changed

4 files changed

+98
-81
lines changed

ArrayInterfaceCore/src/ArrayInterfaceCore.jl

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,6 @@ parameterless_type(x::Type) = __parameterless_type(x)
3535
const VecAdjTrans{T,V<:AbstractVector{T}} = Union{Transpose{T,V},Adjoint{T,V}}
3636
const MatAdjTrans{T,M<:AbstractMatrix{T}} = Union{Transpose{T,M},Adjoint{T,M}}
3737

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

104-
"""
105-
known_length(::Type{T}) -> Union{Int,Nothing}
106-
107-
If `length` of an instance of type `T` is known at compile time, return it.
108-
Otherwise, return `nothing`.
109-
"""
110-
known_length(x) = known_length(typeof(x))
111-
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
112-
known_length(::Type{T}) where {T<:Slice} = known_length(parent_type(T))
113-
known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
114-
known_length(::Type{<:Number}) = 1
115-
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
116-
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
117-
118-
@generated function _prod_or_nothing(x::Tuple)
119-
p = 1
120-
for i in eachindex(x.parameters)
121-
x.parameters[i] === Nothing && return nothing
122-
p *= x.parameters[i].parameters[1]
123-
end
124-
StaticInt(p)
125-
end
126-
127-
function _maybe_known_length(::Base.HasShape, ::Type{T}) where {T}
128-
t = map(_static_or_nothing, known_size(T))
129-
_int_or_nothing(_prod_or_nothing(t))
130-
end
131-
132-
_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
133-
_static_or_nothing(::Nothing) = nothing
134-
@inline _static_or_nothing(x::Int) = StaticInt{x}()
135-
_int_or_nothing(::StaticInt{N}) where {N} = N
136-
_int_or_nothing(::Nothing) = nothing
137-
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
138-
_int_or_nothing(_prod_or_nothing((_static_or_nothing(known_length(I)),_static_or_nothing(known_length(eltype(I))))))
139-
end
140-
14182
"""
14283
can_change_size(::Type{T}) -> Bool
14384
@@ -478,22 +419,6 @@ _not_pointer(x) = x
478419
_device(::False, ::Type{T}) where {T<:DenseArray} = CPUPointer()
479420
_device(::False, ::Type{T}) where {T} = CPUIndex()
480421

481-
"""
482-
defines_strides(::Type{T}) -> Bool
483-
484-
Is strides(::T) defined? It is assumed that types returning `true` also return a valid
485-
pointer on `pointer(::T)`.
486-
"""
487-
defines_strides(x) = defines_strides(typeof(x))
488-
_defines_strides(::Type{T}, ::Type{T}) where {T} = false
489-
_defines_strides(::Type{P}, ::Type{T}) where {P,T} = defines_strides(P)
490-
defines_strides(::Type{T}) where {T} = _defines_strides(parent_type(T), T)
491-
defines_strides(::Type{<:StridedArray}) = true
492-
function defines_strides(::Type{<:SubArray{T,N,P,I}}) where {T,N,P,I}
493-
return stride_preserving_index(I) === True()
494-
end
495-
defines_strides(::Type{<:BitArray}) = true
496-
497422
"""
498423
can_avx(f) -> Bool
499424

ArrayInterfaceCore/src/size.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,62 @@ _known_size(::Type{T}, dim::StaticInt) where {T} = known_length(field_type(T, di
125125
end
126126
end
127127

128+
"""
129+
length(A) -> Union{Int,StaticInt}
130+
131+
Returns the length of `A`. If the length is known at compile time, it is
132+
returned as `Static` number. Otherwise, `ArrayInterfaceCore.length(A)` is identical
133+
to `Base.length(A)`.
134+
135+
```julia
136+
julia> using StaticArrays, ArrayInterface
137+
138+
julia> A = @SMatrix rand(3,4);
139+
140+
julia> ArrayInterfaceCore.length(A)
141+
static(12)
142+
```
143+
"""
144+
@inline length(a::UnitRange{T}) where {T} = last(a) - first(a) + oneunit(T)
145+
@inline length(x) = Static.maybe_static(known_length, Base.length, x)
146+
147+
# Alias to to-be-depreciated internal function
148+
const static_length = length
149+
150+
"""
151+
known_length(::Type{T}) -> Union{Int,Nothing}
152+
153+
If `length` of an instance of type `T` is known at compile time, return it.
154+
Otherwise, return `nothing`.
155+
"""
156+
known_length(x) = known_length(typeof(x))
157+
known_length(::Type{<:NamedTuple{L}}) where {L} = length(L)
158+
known_length(::Type{T}) where {T<:Slice} = known_length(parent_type(T))
159+
known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
160+
known_length(::Type{<:Number}) = 1
161+
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
162+
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
163+
164+
@generated function _prod_or_nothing(x::Tuple)
165+
p = 1
166+
for i in eachindex(x.parameters)
167+
x.parameters[i] === Nothing && return nothing
168+
p *= x.parameters[i].parameters[1]
169+
end
170+
StaticInt(p)
171+
end
172+
173+
function _maybe_known_length(::Base.HasShape, ::Type{T}) where {T}
174+
t = map(_static_or_nothing, known_size(T))
175+
_int_or_nothing(_prod_or_nothing(t))
176+
end
177+
178+
_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
179+
_static_or_nothing(::Nothing) = nothing
180+
@inline _static_or_nothing(x::Int) = StaticInt{x}()
181+
_int_or_nothing(::StaticInt{N}) where {N} = N
182+
_int_or_nothing(::Nothing) = nothing
183+
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
184+
_int_or_nothing(_prod_or_nothing((_static_or_nothing(known_length(I)),_static_or_nothing(known_length(eltype(I))))))
185+
end
186+

ArrayInterfaceCore/src/stridelayout.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11

2+
"""
3+
defines_strides(::Type{T}) -> Bool
4+
5+
Is strides(::T) defined? It is assumed that types returning `true` also return a valid
6+
pointer on `pointer(::T)`.
7+
"""
8+
defines_strides(x) = defines_strides(typeof(x))
9+
_defines_strides(::Type{T}, ::Type{T}) where {T} = false
10+
_defines_strides(::Type{P}, ::Type{T}) where {P,T} = defines_strides(P)
11+
defines_strides(::Type{T}) where {T} = _defines_strides(parent_type(T), T)
12+
defines_strides(::Type{<:StridedArray}) = true
13+
function defines_strides(::Type{<:SubArray{T,N,P,I}}) where {T,N,P,I}
14+
stride_preserving_index(I) === True()
15+
end
16+
defines_strides(::Type{<:BitArray}) = true
17+
218
#=
319
stride_preserving_index(::Type{T}) -> StaticBool
420

src/ArrayInterface.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
module ArrayInterface
22

33
using ArrayInterfaceCore
4-
import ArrayInterfaceCore: axes, axes_types, buffer, can_setindex, contiguous_axis, contiguous_batch_size,
5-
defines_strides, dense_dims, dimnames, fast_matrix_colors, fast_scalar_indexing, findstructralnz,
6-
is_lazy_conjugate, length,
7-
has_sparsestruct, isstructured, lu_instance, matrix_colors, ismutable, restructure, known_first,
8-
known_last, known_length, known_step, known_size, known_strides, known_offsets, offsets,
9-
parent_type, size, strides, stride_rank, to_dims, to_indices, to_index, zeromatrix
4+
import ArrayInterfaceCore: allowed_getindex, allowed_setindex!, aos_to_soa, buffer, can_change_size, can_setindex,
5+
deleteat, fast_matrix_colors, fast_scalar_indexing, findstructralnz, has_parent,
6+
has_sparsestruct, is_column_major, issingular, is_splat_index, is_lazy_conjugate, insert,
7+
isstructured, matrix_colors, ismutable, restructure, lu_instance, parent_type, safevec,
8+
unsafe_reconstruct, zeromatrix
9+
10+
# ArrayIndex subtypes and methods
1011
import ArrayInterfaceCore: ArrayIndex, MatrixIndex, VectorIndex, BidiagonalIndex, TridiagonalIndex, StrideIndex
12+
# device types and methods
1113
import ArrayInterfaceCore: AbstractDevice, AbstractCPU, CPUTuple, CPUPointer, GPU, CPUIndex, CheckParent, device
14+
# range types and methods
15+
import ArrayInterfaceCore: OptionallyStaticStepRange, OptionallyStaticUnitRange, SOneTo,
16+
SUnitRange, indices, known_first, known_last, known_step, static_first, static_last, static_step
17+
# dimension methods
18+
import ArrayInterfaceCore: dimnames, known_dimnames, has_dimnames, from_parent_dims, to_dims, to_parent_dims
19+
# indexing methods
20+
import ArrayInterfaceCore: to_axes, to_axis, to_indices, to_index, getindex, setindex!, ndims_index
21+
# stride layout methods
22+
import ArrayInterfaceCore: strides, stride_rank, contiguous_axis_indicator, contiguous_batch_size,
23+
known_strides, known_offsets,offsets, offset1, known_offset1, contiguous_axis, dense_dims, defines_strides
24+
# axes types and methods
25+
import ArrayInterfaceCore: axes, axes_types, lazy_axes, LazyAxis
26+
# static sizing
27+
import ArrayInterfaceCore: size, known_size, known_length, static_length
28+
1229
using LinearAlgebra
1330
using Requires
1431
using Static

0 commit comments

Comments
 (0)