Skip to content

Commit 982b4a1

Browse files
authored
Fix views with static boolean vector indexing (#1025)
* fix views with static boolean vector indexing * type stability of views * bump version
1 parent f395f35 commit 982b4a1

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.4.6"
3+
version = "1.4.7"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/MArray.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,21 @@ function promote_rule(::Type{<:MArray{S,T,N,L}}, ::Type{<:MArray{S,U,N,L}}) wher
106106
MArray{S,promote_type(T,U),N,L}
107107
end
108108

109+
@generated function _indices_have_bools(indices::Tuple)
110+
return any(index -> index <: StaticVector{<:Any,Bool}, indices.parameters)
111+
end
112+
109113
function Base.view(
110114
a::MArray{S},
111115
indices::Union{Integer, Colon, StaticVector, Base.Slice, SOneTo}...,
112116
) where {S}
113-
new_size = new_out_size(S, indices...)
114117
view_from_invoke = invoke(view, Tuple{AbstractArray, typeof(indices).parameters...}, a, indices...)
115-
return SizedArray{new_size}(view_from_invoke)
118+
if _indices_have_bools(indices)
119+
return view_from_invoke
120+
else
121+
new_size = new_out_size(S, indices...)
122+
return SizedArray{new_size}(view_from_invoke)
123+
end
116124
end
117125

118126
Base.elsize(::Type{<:MArray{<:Any, T}}) where T = Base.elsize(Vector{T})

src/SizedArray.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,13 @@ function Base.view(
193193
a::SizedArray{S},
194194
indices::Union{Integer, Colon, StaticVector, Base.Slice, SOneTo}...,
195195
) where {S}
196-
new_size = new_out_size(S, indices...)
197-
return SizedArray{new_size}(view(a.data, indices...))
196+
view_of_wrapped = view(a.data, indices...)
197+
if _indices_have_bools(indices)
198+
return view_of_wrapped
199+
else
200+
new_size = new_out_size(S, indices...)
201+
return SizedArray{new_size}(view_of_wrapped)
202+
end
198203
end
199204

200205
function Base.vec(a::SizedArray{S}) where {S}

test/MArray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@
221221
@test v[] == 2
222222
end
223223

224+
@testset "boolean indexing" begin
225+
v = @MArray [1,2,3]
226+
b = view(v, SA[true, false, true])
227+
@test b == [1,3]
228+
end
229+
224230
@testset "non-power-of-2 element size" begin
225231
primitive type Test24 24 end
226232
Test24(n) = Base.trunc_int(Test24, n)

test/SizedArray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@
225225
x6 = view(x, StaticArrays.SUnitRange(2, 3), :, 2)
226226
@test isa(x6, SizedArray{Tuple{2,3},Float64,2,2,<:SubArray{Float64,2}})
227227
@test x6 == view(x.data, StaticArrays.SUnitRange(2, 3), :, 2)
228+
229+
x7 = view(x, :, SA[true, false, true], 1)
230+
@test x7 == view(x.data, :, SA[true, false, true], 1)
228231
end
229232

230233
@testset "views of MArray" begin

0 commit comments

Comments
 (0)