Skip to content

Commit 66e63c4

Browse files
authored
Don't use generated in initialized_blocks_BlockArray (#361)
* Don't use generated in initialized_blocks_BlockArray * Add test
1 parent 1ac6aef commit 66e63c4

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/blockarray.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,12 @@ See also [`undef_blocks`](@ref), [`UndefBlocksInitializer`](@ref)
183183
@inline BlockArray{T,N,R}(::UndefBlocksInitializer, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {T, N, R<:AbstractArray{<:AbstractArray{T,N},N}} =
184184
undef_blocks_BlockArray(R, block_sizes...)
185185

186-
@generated function initialized_blocks_BlockArray(::Type{R}, baxes::NTuple{N,AbstractUnitRange{Int}}) where R<:AbstractArray{V,N} where {T,N,V<:AbstractArray{T,N}}
187-
return quote
188-
block_arr = _BlockArray(R, baxes)
189-
@nloops $N i i->blockaxes(baxes[i],1) begin
190-
block_index = @ntuple $N i
191-
block_arr[block_index...] = similar(V, length.(getindex.(baxes, block_index)))
192-
end
193-
194-
return block_arr
186+
function initialized_blocks_BlockArray(::Type{R}, baxes::NTuple{N,AbstractUnitRange{Int}}) where R<:AbstractArray{V,N} where {T,N,V<:AbstractArray{T,N}}
187+
blocks = map(Iterators.product(map(x -> blockaxes(x,1), baxes)...)) do block_index
188+
indices = map((x,y) -> x[y], baxes, block_index)
189+
similar(V, map(length, indices))
195190
end
191+
return _BlockArray(convert(R, blocks), baxes)
196192
end
197193

198194

test/test_blockarrays.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SparseArrays, BlockArrays, FillArrays, LinearAlgebra, Test
1+
using SparseArrays, BlockArrays, FillArrays, LinearAlgebra, Test, OffsetArrays
22
import BlockArrays: _BlockArray
33

44
function test_error_message(f, needle, expected = Exception)
@@ -27,6 +27,19 @@ end
2727
fill!(ret, 0)
2828
@test Array(ret) == zeros(6)
2929

30+
ret = BlockArray{Float64,1,Vector{OffsetVector{Float64,Vector{Float64}}}}(undef, 1:3)
31+
fill!(ret, 0)
32+
@test blocks(ret) isa Vector{OffsetVector{Float64,Vector{Float64}}}
33+
@test Array(ret) == zeros(6)
34+
35+
ret = BlockArray{Float64,1,
36+
OffsetVector{OffsetVector{Float64,Vector{Float64}},
37+
Vector{OffsetVector{Float64,Vector{Float64}}}}}(undef, 1:3)
38+
fill!(ret, 0)
39+
@test blocks(ret) isa OffsetVector{OffsetVector{Float64,Vector{Float64}},
40+
Vector{OffsetVector{Float64,Vector{Float64}}}}
41+
@test Array(ret) == zeros(6)
42+
3043
ret = BlockArray{Float64}(undef, (blockedrange(1:3),))
3144
fill!(ret, 0)
3245
@test Array(ret) == zeros(6)

0 commit comments

Comments
 (0)