Skip to content

Commit 8eaccf5

Browse files
committed
Inferrably construct leaf types
1 parent 16f6c08 commit 8eaccf5

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/SArray.jl

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
SArray{S, T, L}(x::NTuple{L, T})
3-
SArray{S, T, L}(x1, x2, x3, ...)
2+
sa = SArray{S, T, L}(x::NTuple{L, T})
3+
sa = SArray{S, T, L}(x1, x2, x3, ...)
44
55
Construct a statically-sized array `SArray`. Since this type is immutable, the data must be
66
provided upon construction and cannot be mutated later. The `S` parameter is a Tuple-type
@@ -9,7 +9,7 @@ array. The `L` parameter is the `length` of the array and is always equal to `pr
99
Constructors may drop the `L` and `T` parameters if they are inferrable from the input
1010
(e.g. `L` is always inferrable from `S`).
1111
12-
SArray{S}(a::Array)
12+
sa = SArray{S}(a::Array)
1313
1414
Construct a statically-sized array of dimensions `S` (expressed as a `Tuple{...}`) using
1515
the data from `a`. The `S` parameter is mandatory since the size of `a` is unknown to the
@@ -52,6 +52,22 @@ end
5252

5353
@inline SArray(a::StaticArray) = SArray{size_tuple(a)}(Tuple(a)) # TODO fixme
5454

55+
56+
"""
57+
SA = SArray(s::Size, ::Type{T})
58+
59+
Inferrably construct a concrete (leaf) `SArray` type with the given
60+
[Size](@ref) `s` and element type `T`.
61+
62+
# Example
63+
64+
```julia
65+
julia> SArray(Size(3,2,5), Float64)
66+
StaticArrays.SArray{Tuple{3,2,5},Float64,3,30}
67+
```
68+
"""
69+
SArray{S,T}(s::Size{S}, ::Type{T}) = SArray{Tuple{S...}, T, length(s), prod(s)}
70+
5571
# Simplified show for the type
5672
show(io::IO, ::Type{SArray{S, T, N}}) where {S, T, N} = print(io, "SArray{$S,$T,$N}")
5773

test/SArray.jl

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@
6161
@test @SArray([1 for x = SVector(1,2), y = SVector(1,2,3)]) == ones(2,3)
6262
end
6363

64+
@testset "Leaftypes" begin
65+
leaf1{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N), Float64)}(0)
66+
leaf2{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N,N), Float32)}(0)
67+
leaf3{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N,N,N), Int)}(0)
68+
@test isa(@inferred(leaf1((true, true))), Vector{SArray{Tuple{2}, Float64, 1, 2}})
69+
@test isa(@inferred(leaf1((true, true, true))), Vector{SArray{Tuple{3}, Float64, 1, 3}})
70+
@test isa(@inferred(leaf2((true, true))), Vector{SArray{Tuple{2,2}, Float32, 2, 4}})
71+
@test isa(@inferred(leaf2((true, true, true))), Vector{SArray{Tuple{3,3}, Float32, 2, 9}})
72+
@test isa(@inferred(leaf3((true, true))), Vector{SArray{Tuple{2,2,2}, Int, 3, 8}})
73+
@test isa(@inferred(leaf3((true, true, true))), Vector{SArray{Tuple{3,3,3}, Int, 3, 27}})
74+
end
75+
6476
@testset "Methods" begin
6577
m = @SArray [11 13; 12 14]
6678

0 commit comments

Comments
 (0)