|
| 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} |
| 9 | + |
| 10 | +""" |
| 11 | + Size(dims::Int...) |
| 12 | +
|
| 13 | +`Size` is used extensively throughout the `StaticArrays` API to describe _compile-time_ |
| 14 | +knowledge of the size of an array. The dimensions are stored as a type parameter and are |
| 15 | +statically propagated by the compiler, resulting in efficient, type-inferrable code. For |
| 16 | +example, to create a static matrix of zeros, use `A = zeros(SMatrix{3,3})`. The static |
| 17 | +size of `A` can be obtained by `Size(A)`. (rather than `size(zeros(3,3))`, which returns |
| 18 | +`Base.Tuple{2,Int}`). |
| 19 | +
|
| 20 | +Note that if dimensions are not known statically (e.g., for standard `Array`s), |
| 21 | +[`Dynamic()`](@ref) should be used instead of an `Int`. |
| 22 | +
|
| 23 | + Size(a::AbstractArray) |
| 24 | + Size(::Type{T<:AbstractArray}) |
| 25 | +
|
| 26 | +The `Size` constructor can be used to extract static dimension information from a given |
| 27 | +array. For example: |
| 28 | +
|
| 29 | +```julia-repl |
| 30 | +julia> Size(zeros(SMatrix{3, 4})) |
| 31 | +Size(3, 4) |
| 32 | +
|
| 33 | +julia> Size(zeros(3, 4)) |
| 34 | +Size(StaticArrays.Dynamic(), StaticArrays.Dynamic()) |
| 35 | +``` |
| 36 | +
|
| 37 | +This has multiple uses, including "trait"-based dispatch on the size of a statically-sized |
| 38 | +array. For example: |
| 39 | +
|
| 40 | +```julia |
| 41 | +det(x::StaticMatrix) = _det(Size(x), x) |
| 42 | +_det(::Size{(1,1)}, x::StaticMatrix) = x[1,1] |
| 43 | +_det(::Size{(2,2)}, x::StaticMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1] |
| 44 | +# and other definitions as necessary |
| 45 | +``` |
| 46 | +
|
| 47 | +""" |
| 48 | +struct Size{S} |
| 49 | + Size{S}() where {S} = new{S::Tuple{Vararg{StaticDimension}}}() |
| 50 | +end |
0 commit comments