Skip to content

Commit

Permalink
Inferrably construct leaf types
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed May 24, 2017
1 parent 16f6c08 commit 8eaccf5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/SArray.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
SArray{S, T, L}(x::NTuple{L, T})
SArray{S, T, L}(x1, x2, x3, ...)
sa = SArray{S, T, L}(x::NTuple{L, T})
sa = SArray{S, T, L}(x1, x2, x3, ...)
Construct a statically-sized array `SArray`. Since this type is immutable, the data must be
provided upon construction and cannot be mutated later. The `S` parameter is a Tuple-type
Expand All @@ -9,7 +9,7 @@ array. The `L` parameter is the `length` of the array and is always equal to `pr
Constructors may drop the `L` and `T` parameters if they are inferrable from the input
(e.g. `L` is always inferrable from `S`).
SArray{S}(a::Array)
sa = SArray{S}(a::Array)
Construct a statically-sized array of dimensions `S` (expressed as a `Tuple{...}`) using
the data from `a`. The `S` parameter is mandatory since the size of `a` is unknown to the
Expand Down Expand Up @@ -52,6 +52,22 @@ end

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


"""
SA = SArray(s::Size, ::Type{T})
Inferrably construct a concrete (leaf) `SArray` type with the given
[Size](@ref) `s` and element type `T`.
# Example
```julia
julia> SArray(Size(3,2,5), Float64)
StaticArrays.SArray{Tuple{3,2,5},Float64,3,30}
```
"""
SArray{S,T}(s::Size{S}, ::Type{T}) = SArray{Tuple{S...}, T, length(s), prod(s)}

# Simplified show for the type
show(io::IO, ::Type{SArray{S, T, N}}) where {S, T, N} = print(io, "SArray{$S,$T,$N}")

Expand Down
12 changes: 12 additions & 0 deletions test/SArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
@test @SArray([1 for x = SVector(1,2), y = SVector(1,2,3)]) == ones(2,3)
end

@testset "Leaftypes" begin
leaf1{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N), Float64)}(0)
leaf2{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N,N), Float32)}(0)
leaf3{N}(::NTuple{N,Bool}) = Vector{SArray(Size(N,N,N), Int)}(0)
@test isa(@inferred(leaf1((true, true))), Vector{SArray{Tuple{2}, Float64, 1, 2}})
@test isa(@inferred(leaf1((true, true, true))), Vector{SArray{Tuple{3}, Float64, 1, 3}})
@test isa(@inferred(leaf2((true, true))), Vector{SArray{Tuple{2,2}, Float32, 2, 4}})
@test isa(@inferred(leaf2((true, true, true))), Vector{SArray{Tuple{3,3}, Float32, 2, 9}})
@test isa(@inferred(leaf3((true, true))), Vector{SArray{Tuple{2,2,2}, Int, 3, 8}})
@test isa(@inferred(leaf3((true, true, true))), Vector{SArray{Tuple{3,3,3}, Int, 3, 27}})
end

@testset "Methods" begin
m = @SArray [11 13; 12 14]

Expand Down

0 comments on commit 8eaccf5

Please sign in to comment.