Skip to content

Commit 89faa55

Browse files
authored
Fix zero-dim view! (#149)
1 parent 1ee8b12 commit 89faa55

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
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.13"
4+
version = "0.7.14"
55

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

src/BlockArraysExtensions/BlockArraysExtensions.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ function view!(a::AbstractArray{<:Any,N}, index::Vararg{Block{1},N}) where {N}
561561
blocks(a)[Int.(index)...] = blocks(a)[Int.(index)...]
562562
return blocks(a)[Int.(index)...]
563563
end
564+
# Fix ambiguity error.
565+
function view!(a::AbstractArray{<:Any,0})
566+
blocks(a)[] = blocks(a)[]
567+
return blocks(a)[]
568+
end
564569

565570
function view!(a::AbstractArray{<:Any,N}, index::BlockIndexRange{N}) where {N}
566571
# TODO: Is there a better code pattern for this?

test/test_basics.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ using BlockSparseArrays:
3939
using GPUArraysCore: @allowscalar
4040
using JLArrays: JLArray, JLMatrix
4141
using LinearAlgebra: Adjoint, Transpose, dot, norm, tr
42-
using SparseArraysBase: SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, storedlength
42+
using SparseArraysBase:
43+
SparseArrayDOK, SparseMatrixDOK, SparseVectorDOK, isstored, storedlength
4344
using Test: @test, @test_broken, @test_throws, @testset, @inferred
4445
using TestExtras: @constinferred
4546
using TypeParameterAccessors: TypeParameterAccessors, Position
@@ -1159,6 +1160,24 @@ arrayts = (Array, JLArray)
11591160
@test view!(a, blk...) == x
11601161
@test @view!(a[blk...]) == x
11611162
end
1163+
# 0-dim case
1164+
# Regression test for https://github.com/ITensor/BlockSparseArrays.jl/issues/148
1165+
for I in ((), (Block(),))
1166+
a = dev(BlockSparseArray{elt}(undef))
1167+
@test !isstored(a)
1168+
@test iszero(blockstoredlength(a))
1169+
@test isempty(eachblockstoredindex(a))
1170+
@test iszero(a)
1171+
b = @view! a[I...]
1172+
@test isstored(a)
1173+
@test isone(blockstoredlength(a))
1174+
@test issetequal(eachblockstoredindex(a), [Block()])
1175+
@test iszero(adapt(Array)(a))
1176+
@test b isa arrayt{elt,0}
1177+
@test size(b) == ()
1178+
# Converting to `Array` works around a bug in `iszero(JLArray{Float64}(undef))`.
1179+
@test iszero(adapt(Array)(b))
1180+
end
11621181
end
11631182
@testset "LinearAlgebra" begin
11641183
a1 = dev(BlockSparseArray{elt}(undef, [2, 3], [2, 3]))

0 commit comments

Comments
 (0)