Skip to content

Commit ac9b71c

Browse files
committed
Better support for slicing with Colon
1 parent 9d4edc1 commit ac9b71c

File tree

5 files changed

+24
-1
lines changed

5 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.22"
4+
version = "0.7.23"
55

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

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ const GenericBlockIndexVectorSlices = BlockIndices{
185185
<:BlockVector{<:GenericBlockIndex{1},<:Vector{<:BlockIndexVector}}
186186
}
187187
const SubBlockSliceCollection = Union{
188+
Base.Slice,
188189
BlockIndexRangeSlice,
189190
BlockIndexRangeSlices,
190191
BlockIndexVectorSlices,

src/BlockArraysExtensions/blockedunitrange.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,15 @@ BlockArrays.Block(b::BlockIndexVector) = b.block
349349

350350
Base.copy(a::BlockIndexVector) = BlockIndexVector(a.block, copy.(a.indices))
351351

352+
# Copied from BlockArrays.BlockIndexRange.
353+
function Base.show(io::IO, B::BlockIndexVector)
354+
show(io, Block(B))
355+
print(io, "[")
356+
print_tuple_elements(io, B.indices)
357+
print(io, "]")
358+
end
359+
Base.show(io::IO, ::MIME"text/plain", B::BlockIndexVector) = show(io, B)
360+
352361
function Base.getindex(b::AbstractBlockedUnitRange, Kkr::BlockIndexVector{1})
353362
return b[block(Kkr)][Kkr.indices...]
354363
end

src/BlockArraysExtensions/blockrange.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ function Base.getindex(r::BlockUnitRange, I::Block{1})
1616
return eachblockaxis(r)[Int(I)] .+ (first(r.r[I]) - 1)
1717
end
1818

19+
const BlockOneTo{T<:Integer,B,CS,R<:BlockedOneTo{T,CS}} = BlockUnitRange{T,B,CS,R}
20+
Base.axes(S::Base.Slice{<:BlockOneTo}) = (S.indices,)
21+
Base.axes1(S::Base.Slice{<:BlockOneTo}) = S.indices
22+
Base.unsafe_indices(S::Base.Slice{<:BlockOneTo}) = (S.indices,)
23+
1924
function BlockArrays.combine_blockaxes(r1::BlockUnitRange, r2::BlockUnitRange)
2025
if eachblockaxis(r1) eachblockaxis(r2)
2126
return throw(ArgumentError("BlockUnitRanges must have the same block axes"))

src/abstractblocksparsearray/views.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ end
302302

303303
blockedslice_blocks(x::BlockSlice) = x.block
304304
blockedslice_blocks(x::BlockIndices) = x.blocks
305+
# Reinterpret the slice blockwise.
306+
function blockedslice_blocks(x::Base.Slice)
307+
return mortar(
308+
map(BlockRange(x.indices)) do b
309+
return BlockIndexRange(b, Base.Slice(Base.axes1(x.indices[b])))
310+
end,
311+
)
312+
end
305313

306314
# TODO: Define `@interface interface(a) viewblock`.
307315
function BlockArrays.viewblock(

0 commit comments

Comments
 (0)