Skip to content

Commit 5f7debb

Browse files
authored
Move Size to StaticArraysCore.jl (#1094)
1 parent 3ce3b20 commit 5f7debb

File tree

3 files changed

+4
-83
lines changed

3 files changed

+4
-83
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.5.7"
3+
version = "1.5.8"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -10,7 +10,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1010

1111
[compat]
1212
julia = "1.6"
13-
StaticArraysCore = "~1.3.0"
13+
StaticArraysCore = "~1.4.0"
1414

1515
[extras]
1616
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"

src/StaticArrays.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ using StaticArraysCore: StaticArraysCore, StaticArray, StaticScalar, StaticVecto
2929
tuple_minimum, size_to_tuple, require_one_based_indexing
3030
using StaticArraysCore: FieldArray, FieldMatrix, FieldVector
3131
using StaticArraysCore: StaticArrayStyle
32+
using StaticArraysCore: Dynamic, StaticDimension
3233
import StaticArraysCore: SArray, SVector, SMatrix
3334
import StaticArraysCore: MArray, MVector, MMatrix
3435
import StaticArraysCore: SizedArray, SizedVector, SizedMatrix
3536
import StaticArraysCore: check_array_parameters, convert_ntuple
36-
import StaticArraysCore: similar_type
37+
import StaticArraysCore: similar_type, Size
3738

3839
# end of StaticArraysCore imports
3940
# StaticArraysCore exports

src/traits.jl

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
"""
2-
Dynamic()
3-
4-
Used to signify that a dimension of an array is not known statically.
5-
"""
6-
struct Dynamic end
7-
8-
const StaticDimension = Union{Int, Dynamic}
91

102
"""
113
dimmatch(x::StaticDimension, y::StaticDimension)
@@ -19,85 +11,13 @@ function dimmatch end
1911
@inline dimmatch(x::Int, y::Int) = x === y
2012
@inline dimmatch(x::StaticDimension, y::StaticDimension) = true
2113

22-
"""
23-
Size(dims::Int...)
24-
25-
`Size` is used extensively throughout the `StaticArrays` API to describe _compile-time_
26-
knowledge of the size of an array. The dimensions are stored as a type parameter and are
27-
statically propagated by the compiler, resulting in efficient, type-inferrable code. For
28-
example, to create a static matrix of zeros, use `A = zeros(SMatrix{3,3})`. The static
29-
size of `A` can be obtained by `Size(A)`. (rather than `size(zeros(3,3))`, which returns
30-
`Base.Tuple{2,Int}`).
31-
32-
Note that if dimensions are not known statically (e.g., for standard `Array`s),
33-
[`Dynamic()`](@ref) should be used instead of an `Int`.
34-
35-
Size(a::AbstractArray)
36-
Size(::Type{T<:AbstractArray})
37-
38-
The `Size` constructor can be used to extract static dimension information from a given
39-
array. For example:
40-
41-
```julia-repl
42-
julia> Size(zeros(SMatrix{3, 4}))
43-
Size(3, 4)
44-
45-
julia> Size(zeros(3, 4))
46-
Size(StaticArrays.Dynamic(), StaticArrays.Dynamic())
47-
```
48-
49-
This has multiple uses, including "trait"-based dispatch on the size of a statically-sized
50-
array. For example:
51-
52-
```julia
53-
det(x::StaticMatrix) = _det(Size(x), x)
54-
_det(::Size{(1,1)}, x::StaticMatrix) = x[1,1]
55-
_det(::Size{(2,2)}, x::StaticMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1]
56-
# and other definitions as necessary
57-
```
58-
59-
"""
60-
struct Size{S}
61-
function Size{S}() where {S}
62-
new{S::Tuple{Vararg{StaticDimension}}}()
63-
end
64-
end
65-
66-
@pure Size(s::Tuple{Vararg{StaticDimension}}) = Size{s}()
67-
@pure Size(s::StaticDimension...) = Size{s}()
68-
@pure Size(s::Type{<:Tuple}) = Size{tuple(s.parameters...)}()
69-
70-
Base.show(io::IO, ::Size{S}) where {S} = print(io, "Size", S)
71-
72-
function missing_size_error(::Type{SA}) where SA
73-
error("""
74-
The size of type `$SA` is not known.
75-
76-
If you were trying to construct (or `convert` to) a `StaticArray` you
77-
may need to add the size explicitly as a type parameter so its size is
78-
inferrable to the Julia compiler (or performance would be terrible). For
79-
example, you might try
80-
81-
m = zeros(3,3)
82-
SMatrix(m) # this error
83-
SMatrix{3,3}(m) # correct - size is inferrable
84-
SArray{Tuple{3,3}}(m) # correct, note Tuple{3,3}
85-
""")
86-
end
87-
88-
Size(a::T) where {T<:AbstractArray} = Size(T)
89-
Size(::Type{SA}) where {SA <: StaticArray} = missing_size_error(SA)
90-
Size(::Type{SA}) where {SA <: StaticArray{S}} where {S<:Tuple} = @isdefined(S) ? Size(S) : missing_size_error(SA)
91-
9214
Size(::Type{Adjoint{T, A}}) where {T, A <: AbstractVecOrMat{T}} = Size(Size(A)[2], Size(A)[1])
9315
Size(::Type{Transpose{T, A}}) where {T, A <: AbstractVecOrMat{T}} = Size(Size(A)[2], Size(A)[1])
9416
Size(::Type{Symmetric{T, A}}) where {T, A <: AbstractMatrix{T}} = Size(A)
9517
Size(::Type{Hermitian{T, A}}) where {T, A <: AbstractMatrix{T}} = Size(A)
9618
Size(::Type{Diagonal{T, A}}) where {T, A <: AbstractVector{T}} = Size(Size(A)[1], Size(A)[1])
9719
Size(::Type{<:LinearAlgebra.AbstractTriangular{T, A}}) where {T,A} = Size(A)
9820

99-
@pure Size(::Type{<:AbstractArray{<:Any, N}}) where {N} = Size(ntuple(_ -> Dynamic(), N))
100-
10121
struct Length{L}
10222
function Length{L}() where L
10323
check_length(L)

0 commit comments

Comments
 (0)