Skip to content

Commit a45c582

Browse files
committed
[WIP] Remove BlockIndices
1 parent 3ba5eca commit a45c582

File tree

4 files changed

+3
-218
lines changed

4 files changed

+3
-218
lines changed

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -56,104 +56,6 @@ BlockArrays.blocks(a::NonBlockedArray) = SingleBlockView(parent(a))
5656
const NonBlockedVector{T,Array} = NonBlockedArray{T,1,Array}
5757
NonBlockedVector(array::AbstractVector) = NonBlockedArray(array)
5858

59-
# BlockIndices works around an issue that the indices of BlockSlice
60-
# are restricted to AbstractUnitRange{Int}.
61-
struct BlockIndices{B,T<:Integer,I<:AbstractVector{T}} <: AbstractVector{T}
62-
blocks::B
63-
indices::I
64-
end
65-
for f in (:axes, :unsafe_indices, :axes1, :first, :last, :size, :length, :unsafe_length)
66-
@eval Base.$f(S::BlockIndices) = Base.$f(S.indices)
67-
end
68-
Base.getindex(S::BlockIndices, i::Integer) = getindex(S.indices, i)
69-
function Base.getindex(S::BlockIndices, i::BlockSlice{<:Block{1}})
70-
# TODO: Check that `i.indices` is consistent with `S.indices`.
71-
# It seems like this isn't handling the case where `i` is a
72-
# subslice of a block correctly (i.e. it ignores `i.indices`).
73-
@assert length(S.indices[Block(i)]) == length(i.indices)
74-
return BlockSlice(S.blocks[Int(Block(i))], S.indices[Block(i)])
75-
end
76-
77-
# This is used in slicing like:
78-
# a = BlockSparseArray{Float64}([2, 2, 2, 2], [2, 2, 2, 2])
79-
# I = BlockedVector([Block(4), Block(3), Block(2), Block(1)], [2, 2])
80-
# a[I, I]
81-
function Base.getindex(
82-
S::BlockIndices{<:AbstractBlockVector{<:Block{1}}}, i::BlockSlice{<:Block{1}}
83-
)
84-
# TODO: Check for conistency of indices.
85-
# Wrapping the indices in `NonBlockedVector` reinterprets the blocked indices
86-
# as a single block, since the result shouldn't be blocked.
87-
return NonBlockedVector(BlockIndices(S.blocks[Block(i)], S.indices[Block(i)]))
88-
end
89-
function Base.getindex(
90-
S::BlockIndices{<:BlockedVector{<:Block{1},<:BlockRange{1}}}, i::BlockSlice{<:Block{1}}
91-
)
92-
return i
93-
end
94-
# Views of `BlockIndices` are eager.
95-
# This fixes an issue in Julia 1.11 where reindexing defaults to using views.
96-
Base.view(S::BlockIndices, i) = S[i]
97-
98-
# Used in indexing such as:
99-
# ```julia
100-
# a = BlockSparseArray{Float64}([2, 2, 2, 2], [2, 2, 2, 2])
101-
# I = BlockedVector([Block(4), Block(3), Block(2), Block(1)], [2, 2])
102-
# b = @view a[I, I]
103-
# @view b[Block(1, 1)[1:2, 2:2]]
104-
# ```
105-
# This is similar to the definition:
106-
# @interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})
107-
function Base.getindex(
108-
a::NonBlockedVector{<:Integer,<:BlockIndices}, I::UnitRange{<:Integer}
109-
)
110-
ax = only(axes(parent(a).indices))
111-
brs = to_blockindices(ax, I)
112-
inds = blockedunitrange_getindices(ax, I)
113-
return NonBlockedVector(parent(a)[BlockSlice(brs, inds)])
114-
end
115-
116-
function Base.getindex(S::BlockIndices, i::BlockSlice{<:BlockRange{1}})
117-
# TODO: Check that `i.indices` is consistent with `S.indices`.
118-
# TODO: Turn this into a `blockedunitrange_getindices` definition.
119-
subblocks = S.blocks[Int.(i.block)]
120-
subindices = mortar(
121-
map(1:length(i.block)) do I
122-
r = blocks(i.indices)[I]
123-
return S.indices[first(r)]:S.indices[last(r)]
124-
end,
125-
)
126-
return BlockIndices(subblocks, subindices)
127-
end
128-
129-
# Used when performing slices like:
130-
# @views a[[Block(2), Block(1)]][2:4, 2:4]
131-
function Base.getindex(S::BlockIndices, i::BlockSlice{<:BlockVector{<:BlockIndex{1}}})
132-
subblocks = mortar(
133-
map(blocks(i.block)) do br
134-
return S.blocks[Int(Block(br))][only(br.indices)]
135-
end,
136-
)
137-
subindices = mortar(
138-
map(blocks(i.block)) do br
139-
S.indices[br]
140-
end,
141-
)
142-
return BlockIndices(subblocks, subindices)
143-
end
144-
145-
# Similar to the definition of `BlockArrays.BlockSlices`:
146-
# ```julia
147-
# const BlockSlices = Union{Base.Slice,BlockSlice{<:BlockRange{1}}}
148-
# ```
149-
# but includes `BlockIndices`, where the blocks aren't contiguous.
150-
const BlockSliceCollection = Union{
151-
Base.Slice,BlockSlice{<:BlockRange{1}},BlockIndices{<:Vector{<:Block{1}}}
152-
}
153-
const SubBlockSliceCollection = BlockIndices{
154-
<:BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}}
155-
}
156-
15759
# TODO: This is type piracy. This is used in `reindex` when making
15860
# views of blocks of sliced block arrays, for example:
15961
# ```julia
@@ -326,10 +228,6 @@ function blockrange(axis::AbstractUnitRange, r::BlockSlice)
326228
return blockrange(axis, r.block)
327229
end
328230

329-
function blockrange(a::AbstractUnitRange, r::BlockIndices)
330-
return blockrange(a, r.blocks)
331-
end
332-
333231
function blockrange(axis::AbstractUnitRange, r::Block{1})
334232
return r:r
335233
end
@@ -397,10 +295,6 @@ function blockindices(
397295
return Base.OneTo(length(axis[block]))
398296
end
399297

400-
function blockindices(a::AbstractUnitRange, b::Block, r::BlockIndices)
401-
return blockindices(a, b, r.blocks)
402-
end
403-
404298
function blockindices(
405299
a::AbstractUnitRange,
406300
b::Block,

src/abstractblocksparsearray/map.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ function reblock(
2222
return @view parent(a)[map(I -> I.array, parentindices(a))...]
2323
end
2424

25-
# TODO: Make this more general, independent of `AbstractBlockSparseArray`.
26-
function reblock(
27-
a::SubArray{
28-
<:Any,
29-
<:Any,
30-
<:AbstractBlockSparseArray,
31-
<:Tuple{Vararg{BlockIndices{<:AbstractBlockVector{<:Block{1}}}}},
32-
},
33-
)
34-
# Remove the blocking.
35-
return @view parent(a)[map(I -> Vector(I.blocks), parentindices(a))...]
36-
end
37-
3825
function Base.map!(f, a_dest::AbstractArray, a_srcs::AnyAbstractBlockSparseArray...)
3926
@interface interface(a_dest, a_srcs...) map!(f, a_dest, a_srcs...)
4027
return a_dest

src/abstractblocksparsearray/views.jl

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,6 @@ function BlockArrays.viewblock(
119119
return Base.invoke(view, Tuple{AbstractArray,Vararg{Any}}, a, I...)
120120
end
121121

122-
function Base.view(
123-
a::SubArray{
124-
T,
125-
N,
126-
<:AbstractBlockSparseArray{T,N},
127-
<:Tuple{Vararg{Union{BlockSliceCollection,SubBlockSliceCollection},N}},
128-
},
129-
block::Union{Block{N},BlockIndexRange{N}},
130-
) where {T,N}
131-
return viewblock(a, block)
132-
end
133-
134122
# Fix ambiguity error with BlockArrays.jl for slices like
135123
# `a = BlockSparseArray{Float64}(undef, [2, 2], [2, 2]); @view a[:, :]`.
136124
function Base.view(
@@ -145,29 +133,6 @@ function Base.view(
145133
return viewblock(a, block)
146134
end
147135

148-
function Base.view(
149-
a::SubArray{
150-
T,
151-
N,
152-
<:AbstractBlockSparseArray{T,N},
153-
<:Tuple{Vararg{Union{BlockSliceCollection,SubBlockSliceCollection},N}},
154-
},
155-
block::Vararg{Union{Block{1},BlockIndexRange{1}},N},
156-
) where {T,N}
157-
return viewblock(a, block...)
158-
end
159-
function BlockArrays.viewblock(
160-
a::SubArray{
161-
T,
162-
N,
163-
<:AbstractBlockSparseArray{T,N},
164-
<:Tuple{Vararg{Union{BlockSliceCollection,SubBlockSliceCollection},N}},
165-
},
166-
block::Union{Block{N},BlockIndexRange{N}},
167-
) where {T,N}
168-
return viewblock(a, to_tuple(block)...)
169-
end
170-
171136
# Fixes ambiguity error with `AnyAbstractBlockSparseArray` definition.
172137
function Base.view(
173138
a::SubArray{
@@ -187,64 +152,13 @@ function Base.view(
187152
return viewblock(a, block...)
188153
end
189154

190-
# XXX: TODO: Distinguish if a sub-view of the block needs to be taken!
191-
# Define a new `SubBlockSlice` which is used in:
192-
# `@interface BlockSparseArrayInterface() to_indices(a, inds, I::Tuple{UnitRange{<:Integer},Vararg{Any}})`
193-
# in `blocksparsearrayinterface/blocksparsearrayinterface.jl`.
194-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
195-
function BlockArrays.viewblock(
196-
a::SubArray{T,N,<:AbstractBlockSparseArray{T,N},<:Tuple{Vararg{BlockSliceCollection,N}}},
197-
block::Vararg{Block{1},N},
198-
) where {T,N}
199-
I = CartesianIndex(Int.(block))
200-
# TODO: Use `eachblockstoredindex`.
201-
if I eachstoredindex(blocks(a))
202-
return blocks(a)[I]
203-
end
204-
return BlockView(parent(a), Block.(Base.reindex(parentindices(blocks(a)), Tuple(I))))
205-
end
206-
207-
function to_blockindexrange(
208-
a::BlockIndices{<:BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}}},
209-
I::Block{1},
210-
)
211-
# TODO: Ideally we would just use `a.blocks[I]` but that doesn't
212-
# work right now.
213-
return blocks(a.blocks)[Int(I)]
214-
end
215155
function to_blockindexrange(
216156
a::Base.Slice{<:AbstractBlockedUnitRange{<:Integer}}, I::Block{1}
217157
)
218158
@assert I in only(blockaxes(a.indices))
219159
return I
220160
end
221161

222-
function BlockArrays.viewblock(
223-
a::SubArray{
224-
T,
225-
N,
226-
<:AbstractBlockSparseArray{T,N},
227-
<:Tuple{Vararg{Union{BlockSliceCollection,SubBlockSliceCollection},N}},
228-
},
229-
block::Vararg{Block{1},N},
230-
) where {T,N}
231-
brs = ntuple(dim -> to_blockindexrange(parentindices(a)[dim], block[dim]), ndims(a))
232-
return @view parent(a)[brs...]
233-
end
234-
235-
# TODO: Define `@interface BlockSparseArrayInterface() viewblock`.
236-
function BlockArrays.viewblock(
237-
a::SubArray{
238-
T,
239-
N,
240-
<:AbstractBlockSparseArray{T,N},
241-
<:Tuple{Vararg{Union{BlockSliceCollection,SubBlockSliceCollection},N}},
242-
},
243-
block::Vararg{BlockIndexRange{1},N},
244-
) where {T,N}
245-
return view(viewblock(a, Block.(block)...), map(b -> only(b.indices), block)...)
246-
end
247-
248162
# Block slice of the result of slicing `@view a[2:5, 2:5]`.
249163
# TODO: Move this to `BlockArraysExtensions`.
250164
const BlockedSlice = BlockSlice{

src/blocksparsearrayinterface/blocksparsearrayinterface.jl

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,15 @@ end
149149
@interface ::AbstractBlockSparseArrayInterface function Base.to_indices(
150150
a, inds, I::Tuple{Vector{<:Block{1}},Vararg{Any}}
151151
)
152-
I1 = BlockIndices(I[1], blockedunitrange_getindices(inds[1], I[1]))
153-
return (I1, to_indices(a, Base.tail(inds), Base.tail(I))...)
152+
return error("Not implemented.")
154153
end
155154

156155
# a[mortar([Block(1)[1:2], Block(2)[1:3]]), mortar([Block(1)[1:2], Block(2)[1:3]])]
157156
# a[[Block(1)[1:2], Block(2)[1:3]], [Block(1)[1:2], Block(2)[1:3]]]
158157
@interface ::AbstractBlockSparseArrayInterface function Base.to_indices(
159158
a, inds, I::Tuple{BlockVector{<:BlockIndex{1},<:Vector{<:BlockIndexRange{1}}},Vararg{Any}}
160159
)
161-
I1 = BlockIndices(I[1], blockedunitrange_getindices(inds[1], I[1]))
162-
return (I1, to_indices(a, Base.tail(inds), Base.tail(I))...)
160+
return error("Not implemented.")
163161
end
164162

165163
# a[BlockVector([Block(2), Block(1)], [2]), BlockVector([Block(2), Block(1)], [2])]
@@ -168,8 +166,7 @@ end
168166
@interface ::AbstractBlockSparseArrayInterface function Base.to_indices(
169167
a, inds, I::Tuple{AbstractBlockVector{<:Block{1}},Vararg{Any}}
170168
)
171-
I1 = BlockIndices(I[1], blockedunitrange_getindices(inds[1], I[1]))
172-
return (I1, to_indices(a, Base.tail(inds), Base.tail(I))...)
169+
return error("Not implemented.")
173170
end
174171

175172
# TODO: Need to implement this!
@@ -424,14 +421,7 @@ function SparseArraysBase.getunstoredindex(
424421
end
425422

426423
to_blocks_indices(I::BlockSlice{<:BlockRange{1}}) = Int.(I.block)
427-
to_blocks_indices(I::BlockIndices{<:Vector{<:Block{1}}}) = Int.(I.blocks)
428424
to_blocks_indices(I::Base.Slice{<:BlockedOneTo}) = Base.OneTo(blocklength(I.indices))
429425

430-
@interface ::AbstractBlockSparseArrayInterface function BlockArrays.blocks(
431-
a::SubArray{<:Any,<:Any,<:Any,<:Tuple{Vararg{BlockSliceCollection}}}
432-
)
433-
return @view blocks(parent(a))[map(to_blocks_indices, parentindices(a))...]
434-
end
435-
436426
using BlockArrays: BlocksView
437427
SparseArraysBase.storedlength(a::BlocksView) = length(a)

0 commit comments

Comments
 (0)