Skip to content

Commit 35f456d

Browse files
authored
Support views with BlockedIndexRange (#250)
* Support views with BlockedIndexRange * 0.16.15 * add tests * Update test_blockindices.jl
1 parent 49efe5b commit 35f456d

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.16.24"
3+
version = "0.16.25"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/blockaxis.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ function getindex(b::BlockedUnitRange, KR::BlockRange{1,Tuple{Base.OneTo{Int}}})
232232
_BlockedUnitRange(first(b),cs[Base.OneTo(j)])
233233
end
234234

235+
getindex(b::BlockedUnitRange, KR::BlockSlice) = b[KR.block]
236+
235237
_searchsortedfirst(a::AbstractVector, k) = searchsortedfirst(a, k)
236238
function _searchsortedfirst(a::Tuple, k)
237239
k first(a) && return 1

src/blocklinalg.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ sublayout(BL::BlockLayout{MLAY,BLAY}, ::Type{<:Tuple{<:BlockSlice{BlockRange{1,T
6565
BlockLayout{typeof(sublayout(MLAY(),Tuple{II,Sl})), BLAY}()
6666
sublayout(BL::BlockLayout{MLAY,BLAY}, ::Type{<:Tuple{Sl1,Sl2}}) where {MLAY,BLAY,Sl1<:Slice,Sl2<:Slice} =
6767
BlockLayout{typeof(sublayout(MLAY(),Tuple{Sl1,Sl2})), BLAY}()
68+
sublayout(BL::BlockLayout{MLAY,BLAY}, ::Type{<:NTuple{N,<:BlockedUnitRange}}) where {N,MLAY,BLAY} =
69+
BlockLayout{typeof(sublayout(MLAY(),NTuple{N,Base.OneTo{Int}})), BLAY}()
6870

6971
# materialize views, used for `getindex`
7072
sub_materialize(::AbstractBlockLayout, V, _) = BlockArray(V)

src/views.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ reindex(idxs::Tuple{BlockSlice{<:BlockRange}, Vararg{Any}},
5656
idxs[1].indices[subidxs[1].indices]),
5757
reindex(tail(idxs), tail(subidxs))...))
5858

59-
59+
reindex(idxs::Tuple{BlockedUnitRange, Vararg{Any}},
60+
subidxs::Tuple{BlockSlice{<:Block}, Vararg{Any}}) =
61+
(@_propagate_inbounds_meta; (BlockSlice(subidxs[1].block,
62+
idxs[1][subidxs[1].block]),
63+
reindex(tail(idxs), tail(subidxs))...))
6064
# _splatmap taken from Base:
6165
_splatmap(f, ::Tuple{}) = ()
6266
_splatmap(f, t::Tuple) = (f(t[1])..., _splatmap(f, tail(t))...)

test/test_blockindices.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ end
331331
@test S[Block(2)] == 2:3
332332
@test S[Block.(1:2)] == 1:3
333333
@test axes(S) == axes(b)
334+
335+
336+
bs = BlockSlice(Block.(1:3), 1:6)
337+
@test b[bs] == b
334338
end
335339

336340
@testset "StaticArrays" begin

test/test_blockviews.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BlockArrays, Test, Base64
1+
using BlockArrays, ArrayLayouts, Test, Base64
22

33
# useds to force SubArray return
44
bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
@@ -322,4 +322,11 @@ bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
322322
v = BlockArray(randn(3),1:2)
323323
@test_skip @test_throws BlockBoundsError v[Block(1,2)]
324324
end
325+
326+
@testset "BlockedUnitRange views" begin
327+
a = BlockArray(randn(3),1:2)
328+
v = view(a, axes(a,1))
329+
@test MemoryLayout(v) == MemoryLayout(a)
330+
@test v[Block(1)] == a[Block(1)]
331+
end
325332
end

0 commit comments

Comments
 (0)