Skip to content

Commit 7b9e88d

Browse files
committed
subarrays of Block and BlockRange conform to block array interface
1 parent a111211 commit 7b9e88d

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/views.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,30 @@ function _unblock(cum_sizes, I::Tuple{BlockRange{1,R}, Vararg{Any}}) where {R}
4848
end
4949

5050

51-
@inline _cumul_sizes(A::AbstractArray, j) = cumulsizes(A,j)
51+
_sub_cumul_sizes(cs, inds) = _sub_cumul_sizes(cs, inds[1], tail(inds))
52+
_sub_cumul_sizes(::Tuple{}, ::Tuple{}) = ()
5253

53-
54-
# For a SubArray, we need to shift the block indices appropriately
55-
_cumul_sizes(V::SubArray, j) = _sub_cumul_sizes(_cumul_sizes(parent(V), j), parentindices(V)[j])
56-
57-
function _sub_cumul_sizes(cs, sl::BlockSlice{BlockRange{1,Tuple{UnitRange{Int}}}})
58-
ret = view(cs, sl.block.indices[1][1]:(sl.block.indices[1][end]+1))
59-
ret .- ret[1] .+ 1
54+
function _sub_cumul_sizes(cs, inds1::BlockSlice{Block{1,Int}}, inds)
55+
B = Int(inds1.block)
56+
ret = view(cs[1], B:B+1)
57+
(ret .- ret[1] .+ 1, _sub_cumul_sizes(tail(cs), inds)...)
6058
end
6159

62-
function _sub_cumul_sizes(cs, sl::BlockSlice{Block{1,Int}})
63-
b = Int(sl.block)
64-
ret = view(cs, b:(b+1))
65-
ret .- ret[1] .+ 1
60+
function _sub_cumul_sizes(cs, inds1::BlockSlice{BlockRange{1,Tuple{UnitRange{Int}}}}, inds)
61+
ret = view(cs[1], inds1.block.indices[1][1]:(inds1.block.indices[1][end]+1))
62+
(ret .- ret[1] .+ 1, _sub_cumul_sizes(tail(cs), inds)...)
6663
end
6764

65+
66+
blocksizes(V::SubArray) = BlockSizes(_sub_cumul_sizes(cumulsizes(parent(V)), parentindices(V)))
67+
68+
6869
"""
6970
unblock(block_sizes, inds, I)
7071
7172
Returns the indices associated with a block as a `BlockSlice`.
7273
"""
73-
unblock(A::AbstractArray{T,N}, inds, I) where {T, N} =
74-
_unblock(_cumul_sizes(A, N - length(inds) + 1), I)
74+
unblock(A::AbstractArray{T,N}, inds, I) where {T, N} = _unblock(cumulsizes(A, N - length(inds) + 1), I)
7575

7676

7777

test/test_blockviews.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

2-
3-
42
@testset "block slice" begin
53
A = BlockArray(1:6,1:3)
64
b = parentindices(view(A, Block(2)))[1] # A BlockSlice
@@ -120,3 +118,32 @@ end
120118
V = view(A, Block.(2:3))
121119
@test view(V, Block(2)[1:2]) == [4,5]
122120
end
121+
122+
123+
@testset "subarray implements block interface" begin
124+
A = PseudoBlockArray(reshape(Vector{Float64}(1:(6^2)),6,6), 1:3, 1:3)
125+
126+
V = view(A, Block(2,3))
127+
@test PseudoBlockArray(V) isa PseudoBlockArray
128+
@test BlockArray(V) isa BlockArray
129+
@test PseudoBlockArray(V) == BlockArray(V) == V
130+
@test blocksizes(V) == BlockSizes([2],[3])
131+
132+
V = view(A, Block(2), Block.(2:3))
133+
@test PseudoBlockArray(V) isa PseudoBlockArray
134+
@test BlockArray(V) isa BlockArray
135+
@test PseudoBlockArray(V) == BlockArray(V) == V
136+
@test blocksizes(V) == BlockSizes([2],[2,3])
137+
138+
V = view(A, Block.(2:3), Block(3))
139+
@test PseudoBlockArray(V) isa PseudoBlockArray
140+
@test BlockArray(V) isa BlockArray
141+
@test PseudoBlockArray(V) == BlockArray(V) == V
142+
@test blocksizes(V) == BlockSizes([2,3],[3])
143+
144+
V = view(A, Block.(2:3), Block.(1:2))
145+
@test PseudoBlockArray(V) isa PseudoBlockArray
146+
@test BlockArray(V) isa BlockArray
147+
@test PseudoBlockArray(V) == BlockArray(V) == V
148+
@test blocksizes(V) == BlockSizes([2,3],[1,2])
149+
end

0 commit comments

Comments
 (0)