Skip to content

Views that leave dynamic axes should return HybridArray #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HybridArrays"
uuid = "1baab800-613f-4b0a-84e4-9cd3431bfbb9"
authors = ["Mateusz Baran <[email protected]>"]
version = "0.4.2"
version = "0.4.3"

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

[compat]
Requires = "1"
StaticArrays = "=1.0.1"
StaticArrays = "=1.0.1,=1.0.2"
julia = "1"

[extras]
Expand Down
14 changes: 10 additions & 4 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function new_out_size_nongen(::Type{Size}, inds...) where Size
map(Size.parameters, inds) do s, i
if i == Int
elseif i <: StaticVector
push!(os, i.parameters[1].parameters[1])
push!(os, length(i))
elseif i == Colon
push!(os, s)
else
Expand Down Expand Up @@ -142,12 +142,17 @@ function new_out_size(S::Type{Size}, inds::StaticArrays.StaticIndexing...) where
return new_out_size(S, map(StaticArrays.unwrap, inds)...)
end


# _get_static_vector_length is used in a generated function so using a generic function
# may not be a good idea
_get_static_vector_length(::Type{<:StaticVector{N}}) where {N} = N

@generated function new_out_size(::Type{Size}, inds...) where Size
os = []
map(Size.parameters, inds) do s, i
if i == Int
elseif i <: StaticVector
push!(os, i.parameters[1].parameters[1])
push!(os, _get_static_vector_length(i))
elseif i == Colon || i <: Base.Slice
push!(os, s)
elseif i <: SOneTo
Expand Down Expand Up @@ -285,8 +290,9 @@ end
return SizedArray{new_size}(inner_view)
end

@inline function _view_hybrid(a::HybridArray, ::Val{:dynamic_fixed_false}, inner_view, indices...)
return inner_view
@inline function _view_hybrid(a::HybridArray{S}, ::Val{:dynamic_fixed_false}, inner_view, indices...) where {S}
new_size = new_out_size(S, indices...)
return HybridArray{new_size}(inner_view)
end

@inline function Base.view(
Expand Down
6 changes: 6 additions & 0 deletions test/ssubarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ using Test, Random, HybridArrays, StaticArrays
@test (@inferred Av[:,:,1]) === SA[0.0 0.0; 0.0 0.0]
@test (@inferred Av[:,SOneTo(2),1]) === SA[0.0 0.0; 0.0 0.0]
@test StaticArrays.similar_type(Av) === SArray{Tuple{2,2,3},Float64,3,12}

# views that leave dynamically sized axes should return HybridArray (see issue #31)
B = HybridArray{Tuple{2,5,StaticArrays.Dynamic()}}(randn(2, 5, 3))
@test isa((@inferred view(B, :, StaticArrays.SUnitRange(2, 4), :)), HybridArray{Tuple{2,3,StaticArrays.Dynamic()},Float64,3,3,<:SubArray})
Bv = view(B, :, StaticArrays.SUnitRange(2, 4), :)
@test Bv == B[:, StaticArrays.SUnitRange(2, 4), :]
end