|
1 | 1 | """
|
2 |
| - SizeOf(static_array) |
3 |
| -
|
4 |
| -Returns the size of a static array, wrapped in a `Size` trait such as |
5 |
| -`Size{(2,3)}` for a 2×3 matrix. |
| 2 | + Size(static_array) |
| 3 | + Size(StaticArrayType) |
| 4 | + Size(dims...) |
6 | 5 |
|
7 |
| -`SizeOf` implements the "traitor" paradigm for traits, whereby an abstract trait |
8 |
| -class `SizeOf` returns a direct subtype, in this case `Size`. |
| 6 | +A trait type allowing convenient trait-based dispatch on the size of a statically |
| 7 | +sized array. The dimensions are stored as a type parameter and are statically |
| 8 | +propagated by the compiler. |
9 | 9 |
|
10 | 10 | For example,
|
11 | 11 | ```
|
12 |
| -det(x::StaticMatrix) = det(SizeOf(x), x) |
13 |
| -det(::Type{Size{1,1}}, x::AbstractMatrix) = x[1,1] |
14 |
| -det(::Type{Size{2,2}}, x::AbstractMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1] |
| 12 | +det(x::StaticMatrix) = _det(Size(x), x) |
| 13 | +_det(::Size{(1,1)}, x::StaticMatrix) = x[1,1] |
| 14 | +_det(::Size{(2,2)}, x::StaticMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1] |
15 | 15 | # and other definitions as necessary
|
16 | 16 | ```
|
17 | 17 | """
|
18 |
| -abstract SizeOf |
| 18 | +immutable Size{S} |
| 19 | + function Size() |
| 20 | + check_size(S) |
| 21 | + new() |
| 22 | + end |
| 23 | +end |
19 | 24 |
|
| 25 | +@pure check_size(S::Tuple{Vararg{Int}}) = nothing |
| 26 | +check_size(S) = error("Size was expected to be a tuple of `Int`s") |
20 | 27 |
|
21 |
| -""" |
22 |
| - Size{(dims...)} <: SizeOf |
23 |
| -
|
24 |
| -A trait type allowing convenient trait-based dispatch on the size of a statically |
25 |
| -sized array. |
| 28 | +@pure Size{SA<:StaticArray}(::Type{SA}) = Size{size(SA)}() |
| 29 | +@inline Size(a::StaticArray) = Size(typeof(a)) |
26 | 30 |
|
27 |
| -`Size` implements the "traitor" paradigm for traits, whereby an abstract trait |
28 |
| -class `SizeOf` returns a direct subtype, in this case `Size`. |
| 31 | +@pure Size(s::Tuple{Vararg{Int}}) = Size{s}() |
| 32 | +@pure Size(s::Int...) = Size{s}() |
29 | 33 |
|
30 |
| -For example, |
31 |
| -``` |
32 |
| -det(x::StaticMatrix) = det(SizeOf(x), x) |
33 |
| -det(::Type{Size{1,1}}, x::AbstractMatrix) = x[1,1] |
34 |
| -det(::Type{Size{2,2}}, x::AbstractMatrix) = x[1,1]*x[2,2] - x[1,2]*x[2,1] |
35 |
| -# and other definitions as necessary |
36 |
| -``` |
37 |
| -""" |
38 |
| -immutable Size{S} <: SizeOf |
39 |
| -end |
| 34 | +Base.show{S}(io::IO, ::Size{S}) = print(io, "Size", S) |
40 | 35 |
|
41 |
| -@pure SizeOf{SA<:StaticArray}(::Type{SA}) = Size{size(SA)} |
42 |
| -@inline SizeOf(a::StaticArray) = Size(typeof(a)) |
43 | 36 |
|
44 |
| -# Also define these, since may be more convenient than SizeOf |
45 |
| -""" |
46 |
| - Size(static_array) |
47 |
| - Size(StaticArrayType) |
| 37 | +# Some @pure convenience functions. |
48 | 38 |
|
49 |
| -Convenience constructor for the `Size` of a static array. See also `SizeOf`. |
50 |
| -""" |
51 |
| -@pure Size{SA<:StaticArray}(::Type{SA}) = Size{size(SA)} |
52 |
| -@inline Size(a::StaticArray) = Size(typeof(a)) |
| 39 | +# (This type could *probably* be returned from the `size()` function. |
| 40 | +# This might enable some generic programming, e.g. with `similar(A, size(A))`.) |
53 | 41 |
|
54 |
| -""" |
55 |
| - Size((dims...)) |
56 |
| - Size(dims...) |
| 42 | +@pure getindex{S}(::Size{S}, i::Int) = S[i] |
57 | 43 |
|
58 |
| -Pure function that constructs a compile-time constant `Size` of an array. This |
59 |
| -allows for dispatch of standard (dynamically-sized) arrays to faster, specialized |
60 |
| -*StaticArrays* library methods when the programmer knows or can reasonably infer |
61 |
| -the size. For example, |
62 |
| -``` |
63 |
| -mat = [1.0 2.0; 3.0 4.0] |
64 |
| -det(Size(2,2), mat) # Faster than det(mat) |
65 |
| -``` |
66 |
| -""" |
67 |
| -@pure Size(s::Tuple{Vararg{Int}}) = Size{s} |
68 |
| -@pure Size(s::Int...) = Size{s} |
| 44 | +@pure Base.:(==){S}(::Size{S}, s::Tuple{Vararg{Int}}) = S == s |
| 45 | +@pure Base.:(==){S}(s::Tuple{Vararg{Int}}, ::Size{S}) = s == S |
69 | 46 |
|
70 |
| -@pure getindex{S}(::Type{Size{S}}, i::Int) = S[i] |
| 47 | +@pure Base.:(!=){S}(::Size{S}, s::Tuple{Vararg{Int}}) = S != s |
| 48 | +@pure Base.:(!=){S}(s::Tuple{Vararg{Int}}, ::Size{S}) = s != S |
0 commit comments