Skip to content

Commit bfbc49a

Browse files
committed
Add missing BlockUnitRange functionality
1 parent 5944fb6 commit bfbc49a

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

src/BlockArraysExtensions/blockrange.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ end
88
function blockrange(eachblockaxis)
99
return BlockUnitRange(blockedrange(length.(eachblockaxis)), eachblockaxis)
1010
end
11+
function blockrange(first::Integer, eachblockaxis)
12+
return BlockUnitRange(blockedrange(first, length.(eachblockaxis)), eachblockaxis)
13+
end
1114
Base.first(r::BlockUnitRange) = first(r.r)
1215
Base.last(r::BlockUnitRange) = last(r.r)
1316
BlockArrays.blocklasts(r::BlockUnitRange) = blocklasts(r.r)
1417
eachblockaxis(r::BlockUnitRange) = r.eachblockaxis
1518
function Base.getindex(r::BlockUnitRange, I::Block{1})
1619
return eachblockaxis(r)[Int(I)] .+ (first(r.r[I]) - 1)
1720
end
21+
function Base.getindex(r::BlockUnitRange, I::BlockRange{1})
22+
return blockrange(first(r), eachblockaxis(r)[Int.(I)])
23+
end
24+
Base.axes(r::BlockUnitRange) = (blockrange(eachblockaxis(r)),)
25+
Base.axes1(r::BlockUnitRange) = blockrange(eachblockaxis(r))
1826

1927
using BlockArrays: BlockedOneTo
2028
const BlockOneTo{T<:Integer,B,CS,R<:BlockedOneTo{T,CS}} = BlockUnitRange{T,B,CS,R}

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,18 @@ function Base.setindex!(
8787
),
8888
)
8989
end
90-
if !isconcretetype(blocktype(a))
90+
if isstored(a, I...)
91+
# This writes into existing blocks, or constructs blocks
92+
# using the axes.
93+
aI = @view! a[I...]
94+
aI .= value
95+
else
9196
# Custom `_convert` works around the issue that
9297
# `convert(::Type{<:Diagonal}, ::AbstractMatrix)` isnt' defined
9398
# in Julia v1.10 (https://github.com/JuliaLang/julia/pull/48895,
9499
# https://github.com/JuliaLang/julia/pull/52487).
95-
# TODO: Delete once we drop support for Julia v1.10.
100+
# TODO: Delete `_convert` once we drop support for Julia v1.10.
96101
blocks(a)[Int.(I)...] = _convert(blocktype(a), value)
97-
else
98-
# This writes into existing blocks, or constructs blocks
99-
# using the axes.
100-
aI = @view! a[I...]
101-
aI .= value
102102
end
103103
return a
104104
end

src/blocksparsearray/blocksparsearray.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,14 @@ function blocksparsezeros(::BlockType{A}, axes...) where {A<:AbstractArray}
262262
# to make a bit more generic.
263263
return BlockSparseArray{eltype(A),ndims(A),A}(undef, axes...)
264264
end
265-
function blocksparse(d::Dict{<:Block,<:AbstractArray}, ax::Tuple)
266-
blockaxtype = Tuple{map(eltype eachblockaxis, ax)...}
267-
# TODO: Catch if inference fails and use `valtype(d)` instead.
268-
blockt = Base.promote_op(similar, Type{valtype(d)}, blockaxtype)
269-
a = blocksparsezeros(BlockType(blockt), ax)
265+
function blocksparse(d::Dict, ax::Tuple)
266+
a = blocksparsezeros(BlockType(valtype(d)), ax...)
267+
268+
## blockaxtype = Tuple{map(eltype ∘ eachblockaxis, ax)...}
269+
## # TODO: Catch if inference fails and use `valtype(d)` instead.
270+
## blockt = Base.promote_op(similar, Type{valtype(d)}, blockaxtype)
271+
## a = blocksparsezeros(BlockType(blockt), ax)
272+
270273
for I in eachindex(d)
271274
a[I] = d[I]
272275
end

src/blocksparsearrayinterface/getunstoredblock.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@ function Base.AbstractArray{A}(a::ZeroBlocks{N}) where {N,A}
1818
end
1919

2020
@inline function Base.getindex(a::ZeroBlocks{N,A}, I::Vararg{Int,N}) where {N,A}
21+
22+
#foreach(display, a.parentaxes)
23+
#foreach(display ∘ eachblockaxis, a.parentaxes)
24+
2125
# TODO: Use `BlockArrays.eachblockaxes`.
2226
ax = ntuple(N) do d
2327
return eachblockaxis(a.parentaxes[d])[I[d]]
2428
end
29+
30+
#@show ax
31+
2532
!isconcretetype(A) && return zero!(similar(Array{eltype(A),N}, ax))
2633
return zero!(similar(A, ax))
2734
end

0 commit comments

Comments
 (0)