Skip to content

Commit 56760f9

Browse files
authored
Views that leave dynamic axes should return HybridArray (#32)
* views that leave dynamic axes should return HybridArray * better handling of subtypes of StaticVector
1 parent 8943ca8 commit 56760f9

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "HybridArrays"
22
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
33
authors = ["Mateusz Baran <[email protected]>"]
4-
version = "0.4.2"
4+
version = "0.4.3"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -10,7 +10,7 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1010

1111
[compat]
1212
Requires = "1"
13-
StaticArrays = "=1.0.1"
13+
StaticArrays = "=1.0.1,=1.0.2"
1414
julia = "1"
1515

1616
[extras]

src/indexing.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function new_out_size_nongen(::Type{Size}, inds...) where Size
4141
map(Size.parameters, inds) do s, i
4242
if i == Int
4343
elseif i <: StaticVector
44-
push!(os, i.parameters[1].parameters[1])
44+
push!(os, length(i))
4545
elseif i == Colon
4646
push!(os, s)
4747
else
@@ -142,12 +142,17 @@ function new_out_size(S::Type{Size}, inds::StaticArrays.StaticIndexing...) where
142142
return new_out_size(S, map(StaticArrays.unwrap, inds)...)
143143
end
144144

145+
146+
# _get_static_vector_length is used in a generated function so using a generic function
147+
# may not be a good idea
148+
_get_static_vector_length(::Type{<:StaticVector{N}}) where {N} = N
149+
145150
@generated function new_out_size(::Type{Size}, inds...) where Size
146151
os = []
147152
map(Size.parameters, inds) do s, i
148153
if i == Int
149154
elseif i <: StaticVector
150-
push!(os, i.parameters[1].parameters[1])
155+
push!(os, _get_static_vector_length(i))
151156
elseif i == Colon || i <: Base.Slice
152157
push!(os, s)
153158
elseif i <: SOneTo
@@ -285,8 +290,9 @@ end
285290
return SizedArray{new_size}(inner_view)
286291
end
287292

288-
@inline function _view_hybrid(a::HybridArray, ::Val{:dynamic_fixed_false}, inner_view, indices...)
289-
return inner_view
293+
@inline function _view_hybrid(a::HybridArray{S}, ::Val{:dynamic_fixed_false}, inner_view, indices...) where {S}
294+
new_size = new_out_size(S, indices...)
295+
return HybridArray{new_size}(inner_view)
290296
end
291297

292298
@inline function Base.view(

test/ssubarray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ using Test, Random, HybridArrays, StaticArrays
1010
@test (@inferred Av[:,:,1]) === SA[0.0 0.0; 0.0 0.0]
1111
@test (@inferred Av[:,SOneTo(2),1]) === SA[0.0 0.0; 0.0 0.0]
1212
@test StaticArrays.similar_type(Av) === SArray{Tuple{2,2,3},Float64,3,12}
13+
14+
# views that leave dynamically sized axes should return HybridArray (see issue #31)
15+
B = HybridArray{Tuple{2,5,StaticArrays.Dynamic()}}(randn(2, 5, 3))
16+
@test isa((@inferred view(B, :, StaticArrays.SUnitRange(2, 4), :)), HybridArray{Tuple{2,3,StaticArrays.Dynamic()},Float64,3,3,<:SubArray})
17+
Bv = view(B, :, StaticArrays.SUnitRange(2, 4), :)
18+
@test Bv == B[:, StaticArrays.SUnitRange(2, 4), :]
1319
end

0 commit comments

Comments
 (0)