Skip to content

Commit 247cacc

Browse files
committed
[WIP] Generalize blockwise slicing
1 parent 4ee09d2 commit 247cacc

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.7.20"
4+
version = "0.7.21"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,13 @@ function blockrange(
392392
return map(Block, blocks(r))
393393
end
394394

395+
function blockrange(
396+
axis::AbstractUnitRange,
397+
r::BlockVector{<:GenericBlockIndex{1},<:AbstractVector{<:BlockIndexVector}},
398+
)
399+
return map(Block, blocks(r))
400+
end
401+
395402
function blockrange(axis::AbstractUnitRange, r)
396403
return error("Slicing not implemented for range of type `$(typeof(r))`.")
397404
end

src/BlockArraysExtensions/blockedunitrange.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ BlockArrays.blockindex(b::GenericBlockIndex{1}) = b.α[1]
244244
function GenericBlockIndex(indcs::Tuple{Vararg{GenericBlockIndex{1},N}}) where {N}
245245
GenericBlockIndex(block.(indcs), blockindex.(indcs))
246246
end
247+
248+
function Base.checkindex(
249+
::Type{Bool}, axis::AbstractBlockedUnitRange, ind::GenericBlockIndex{1}
250+
)
251+
return checkindex(Bool, axis, block(ind)) &&
252+
checkbounds(Bool, axis[block(ind)], blockindex(ind))
253+
end
254+
Base.to_index(i::GenericBlockIndex) = i
255+
247256
function print_tuple_elements(io::IO, @nospecialize(t))
248257
if !isempty(t)
249258
print(io, t[1])
@@ -261,6 +270,13 @@ function Base.show(io::IO, B::GenericBlockIndex)
261270
return nothing
262271
end
263272

273+
# https://github.com/JuliaArrays/BlockArrays.jl/blob/v1.6.3/src/views.jl#L31-L32
274+
_maybetail(::Tuple{}) = ()
275+
_maybetail(t::Tuple) = Base.tail(t)
276+
@inline function Base.to_indices(A, inds, I::Tuple{GenericBlockIndex{1},Vararg{Any}})
277+
return (inds[1][I[1]], to_indices(A, _maybetail(inds), Base.tail(I))...)
278+
end
279+
264280
using Base: @propagate_inbounds
265281
@propagate_inbounds function Base.getindex(b::AbstractVector, K::GenericBlockIndex{1})
266282
return b[Block(K.I[1])][K.α[1]]

0 commit comments

Comments
 (0)