Skip to content

Commit b95d07e

Browse files
mateuszbaranc42f
andauthored
Fixing constructors from 0-element arrays (JuliaArrays#831)
* fixing constructors from 0-element arrays * avoiding some Julia 1.0 bug * typo * Update src/SArray.jl Co-authored-by: Chris Foster <chris42f@gmail.com> * Update src/MArray.jl Co-authored-by: Chris Foster <chris42f@gmail.com>
1 parent 63bffb2 commit b95d07e

File tree

14 files changed

+44
-4
lines changed

14 files changed

+44
-4
lines changed

src/MArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ end
7171
end
7272
end
7373

74-
@inline MArray(a::StaticArray) = MArray{size_tuple(Size(a))}(Tuple(a))
74+
@inline MArray(a::StaticArray{S,T}) where {S<:Tuple,T} = MArray{S,T}(Tuple(a))
7575

7676
####################
7777
## MArray methods ##

src/MMatrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ end
5252
end
5353

5454
@inline convert(::Type{MMatrix{S1,S2}}, a::StaticArray{<:Tuple, T}) where {S1,S2,T} = MMatrix{S1,S2,T}(Tuple(a))
55-
@inline MMatrix(a::StaticMatrix) = MMatrix{size(typeof(a),1),size(typeof(a),2)}(Tuple(a))
55+
@inline MMatrix(a::StaticMatrix{N,M,T}) where {N,M,T} = MMatrix{N,M,T}(Tuple(a))
5656

5757
# Some more advanced constructor-like functions
5858
@inline one(::Type{MMatrix{N}}) where {N} = one(MMatrix{N,N})

src/MVector.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ compiler (the element type may optionally also be specified).
1616
"""
1717
const MVector{S, T} = MArray{Tuple{S}, T, 1, S}
1818

19+
@inline MVector(a::StaticVector{N,T}) where {N,T} = MVector{N,T}(a)
1920
@inline MVector(x::NTuple{S,Any}) where {S} = MVector{S}(x)
2021
@inline MVector{S}(x::NTuple{S,T}) where {S, T} = MVector{S, T}(x)
2122
@inline MVector{S}(x::NTuple{S,Any}) where {S} = MVector{S, promote_tuple_eltype(typeof(x))}(x)

src/SArray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ sacollect
106106
@inline (::Type{SA})(gen::Base.Generator) where {SA <: StaticArray} =
107107
sacollect(SA, gen)
108108

109-
@inline SArray(a::StaticArray) = SArray{size_tuple(Size(a))}(Tuple(a))
109+
@inline SArray(a::StaticArray{S,T}) where {S<:Tuple,T} = SArray{S,T}(Tuple(a))
110110

111111
####################
112112
## SArray methods ##

src/SMatrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ end
5858
sacollect(SMatrix{M, N}, gen)
5959

6060
@inline convert(::Type{SMatrix{S1,S2}}, a::StaticArray{<:Tuple, T}) where {S1,S2,T} = SMatrix{S1,S2,T}(Tuple(a))
61-
@inline SMatrix(a::StaticMatrix{S1, S2}) where {S1, S2} = SMatrix{S1, S2}(Tuple(a))
61+
@inline SMatrix(a::StaticMatrix{S1, S2, T}) where {S1, S2, T} = SMatrix{S1, S2, T}(Tuple(a))
6262

6363
# Some more advanced constructor-like functions
6464
@inline one(::Type{SMatrix{N}}) where {N} = one(SMatrix{N,N})

src/SVector.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ compiler (the element type may optionally also be specified).
1515
"""
1616
const SVector{S, T} = SArray{Tuple{S}, T, 1, S}
1717

18+
@inline SVector(a::StaticVector{N,T}) where {N,T} = SVector{N,T}(a)
1819
@inline SVector(x::NTuple{S,Any}) where {S} = SVector{S}(x)
1920
@inline SVector{S}(x::NTuple{S,T}) where {S, T} = SVector{S,T}(x)
2021
@inline SVector{S}(x::T) where {S, T <: Tuple} = SVector{S,promote_tuple_eltype(T)}(x)

src/SizedArray.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct SizedArray{S<:Tuple,T,N,M,TData<:AbstractArray{T,M}} <: StaticArray{S,T,N
2929
end
3030
end
3131

32+
# Julia v1.0 has some weird bug that prevents this from working
33+
@static if VERSION >= v"1.1"
34+
@inline SizedArray(a::StaticArray{S,T,N}) where {S<:Tuple,T,N} = SizedArray{S,T,N}(a)
35+
end
3236
@inline function SizedArray{S,T,N}(
3337
a::TData,
3438
) where {S,T,N,M,TData<:AbstractArray{T,M}}
@@ -123,6 +127,7 @@ Base.pointer(sa::SizedArray) = pointer(sa.data)
123127

124128
const SizedVector{S,T} = SizedArray{Tuple{S},T,1,1}
125129

130+
SizedVector(a::StaticVector{N,T}) where {N,T} = SizedVector{N,T}(a)
126131
@inline function SizedVector{S}(a::TData) where {S,T,TData<:AbstractVector{T}}
127132
return SizedArray{Tuple{S},T,1,1,TData}(a)
128133
end
@@ -142,6 +147,10 @@ end
142147

143148
const SizedMatrix{S1,S2,T} = SizedArray{Tuple{S1,S2},T,2}
144149

150+
# Julia v1.0 has some weird bug that prevents this from working
151+
@static if VERSION >= v"1.1"
152+
SizedMatrix(a::StaticMatrix{N,M,T}) where {N,M,T} = SizedMatrix{N,M,T}(a)
153+
end
145154
@inline function SizedMatrix{S1,S2}(
146155
a::TData,
147156
) where {S1,S2,T,M,TData<:AbstractArray{T,M}}

test/MArray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
v = MArray{Tuple{2}}(1,2)
3333
@test MArray(v) !== v && MArray(v) == v
3434

35+
# test for #557-like issues
36+
@test (@inferred MArray(SVector{0,Float64}()))::MVector{0,Float64} == MVector{0,Float64}()
37+
3538
@test MArray{Tuple{}}(i for i in 1:1).data === (1,)
3639
@test MArray{Tuple{3}}(i for i in 1:3).data === (1,2,3)
3740
@test MArray{Tuple{3}}(float(i) for i in 1:3).data === (1.0,2.0,3.0)

test/MMatrix.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
@test MMatrix{2,2}((1,2,3,4)).data === (1,2,3,4)
2727
@test MMatrix{2}((1,2,3,4)).data === (1,2,3,4)
2828

29+
# test for #557-like issues
30+
@test (@inferred MMatrix(SMatrix{0,0,Float64}()))::MMatrix{0,0,Float64} == MMatrix{0,0,Float64}()
31+
2932
@test ((@MMatrix [1.0])::MMatrix{1,1}).data === (1.0,)
3033
@test ((@MMatrix [1 2])::MMatrix{1,2}).data === (1, 2)
3134
@test ((@MMatrix [1 ; 2])::MMatrix{2,1}).data === (1, 2)

test/MVector.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
v = MVector(1,2)
2121
@test MVector(v) !== v && MVector(v) == v
2222

23+
# test for #557-like issues
24+
@test (@inferred MVector(SVector{0,Float64}()))::MVector{0,Float64} == MVector{0,Float64}()
25+
2326
@test ((@MVector [1.0])::MVector{1}).data === (1.0,)
2427
@test ((@MVector [1, 2, 3])::MVector{3}).data === (1, 2, 3)
2528
@test ((@MVector Float64[1,2,3])::MVector{3}).data === (1.0, 2.0, 3.0)

0 commit comments

Comments
 (0)