Skip to content

Commit abcde2f

Browse files
authored
Dl/blockaxisfixes (#99)
* More PSeudoBlockArray constructors, remove unused copyto! * Update Project.toml * fix #98 * increase coverage * fix tests
1 parent 3addf8f commit abcde2f

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.11"
3+
version = "0.11.1"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/blockarray.jl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -417,20 +417,6 @@ Base.dataids(arr::BlockArray) = (dataids(arr.blocks)..., dataids(arr.axes)...)
417417
end
418418
end
419419

420-
@generated function copyto!(block_array::BlockArray{T, N, R}, arr::R) where {T,N,R <: AbstractArray}
421-
return quote
422-
block_sizes = blocksizes(block_array)
423-
424-
@nloops $N i i->(1:nblocks(block_sizes, i)) begin
425-
block_index = @ntuple $N i
426-
indices = globalrange(block_sizes, block_index)
427-
copyto!(getblock(block_array, block_index...), arr[indices...])
428-
end
429-
430-
return block_array
431-
end
432-
end
433-
434420
function Base.fill!(block_array::BlockArray, v)
435421
for block in block_array.blocks
436422
fill!(block, v)

src/blockindices.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ BlockIndexRange(block::Block{N}, inds::Vararg{AbstractUnitRange{Int},N}) where {
175175

176176
getindex(B::Block{N}, inds::Vararg{Int,N}) where N = BlockIndex(B,inds)
177177
getindex(B::Block{N}, inds::Vararg{AbstractUnitRange{Int},N}) where N = BlockIndexRange(B,inds)
178+
getindex(B::Block{1}, inds::Colon) = B
179+
getindex(B::Block{1}, inds::Base.Slice) = B
180+
181+
getindex(B::BlockIndexRange{1}, kr::AbstractUnitRange{Int}) = BlockIndexRange(B.block, B.indices[1][kr])
178182

179183
eltype(R::BlockIndexRange) = eltype(typeof(R))
180184
eltype(::Type{BlockIndexRange{N}}) where {N} = BlockIndex{N}
@@ -250,6 +254,8 @@ for f in (:axes, :unsafe_indices, :axes1, :first, :last, :size, :length,
250254
end
251255

252256
getindex(S::BlockSlice, i::Integer) = getindex(S.indices, i)
257+
getindex(S::BlockSlice{<:Block}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
258+
getindex(S::BlockSlice{<:BlockIndexRange}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
253259
show(io::IO, r::BlockSlice) = print(io, "BlockSlice(", r.block, ",", r.indices, ")")
254260
next(S::BlockSlice, s) = next(S.indices, s)
255261
done(S::BlockSlice, s) = done(S.indices, s)
@@ -285,8 +291,8 @@ BlockRange(inds::Vararg{AbstractUnitRange{Int},N}) where {N} =
285291
(:)(start::Block{1}, stop::Block{1}) = BlockRange((first(start.n):first(stop.n),))
286292
(:)(start::Block, stop::Block) = throw(ArgumentError("Use `BlockRange` to construct a cartesian range of blocks"))
287293
Base.BroadcastStyle(::Type{<:BlockRange{1}}) = DefaultArrayStyle{1}()
288-
broadcasted(::DefaultArrayStyle{1}, ::typeof(Block), r::AbstractUnitRange) = Block(first(r)):Block(last(r))
289-
broadcasted(::DefaultArrayStyle{1}, ::typeof(Int), block_range::BlockRange{1}) = first(block_range.indices)
294+
broadcasted(::DefaultArrayStyle{1}, ::Type{Block}, r::AbstractUnitRange) = Block(first(r)):Block(last(r))
295+
broadcasted(::DefaultArrayStyle{1}, ::Type{Int}, block_range::BlockRange{1}) = first(block_range.indices)
290296

291297

292298
# AbstractArray implementation

src/pseudo_blockarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const PseudoBlockVecOrMat{T} = Union{PseudoBlockMatrix{T}, PseudoBlockVector{T}}
5555
@inline PseudoBlockArray(blocks::R, baxes::BS) where {T,N,R<:AbstractArray{T,N},BS<:NTuple{N,AbstractUnitRange{Int}}} =
5656
PseudoBlockArray{T, N, R,BS}(blocks, baxes)
5757

58+
@inline PseudoBlockArray(blocks::PseudoBlockArray, baxes::BS) where {N,BS<:NTuple{N,AbstractUnitRange{Int}}} =
59+
PseudoBlockArray(blocks.blocks, baxes)
60+
5861
PseudoBlockArray(blocks::AbstractArray{T, N}, block_sizes::Vararg{AbstractVector{Int}, N}) where {T, N} =
5962
PseudoBlockArray(blocks, map(blockedrange,block_sizes))
6063

@@ -92,6 +95,10 @@ convert(::Type{PseudoBlockArray{T,N}}, A::PseudoBlockArray{T,N}) where {T,N} = A
9295
convert(::Type{PseudoBlockArray{T}}, A::PseudoBlockArray{T}) where {T} = A
9396
convert(::Type{PseudoBlockArray}, A::PseudoBlockArray) = A
9497

98+
convert(::Type{PseudoBlockArray{T,N,R,BS}}, A::PseudoBlockArray) where {T,N,R,BS} =
99+
PseudoBlockArray{T,N,R,BS}(convert(R, A.blocks), convert(BS, A.axes))
100+
101+
95102
PseudoBlockArray{T, N}(A::AbstractArray{T2, N}) where {T,T2,N} =
96103
PseudoBlockArray(Array{T, N}(A), axes(A))
97104
PseudoBlockArray{T1}(A::AbstractArray{T2, N}) where {T1,T2,N} = PseudoBlockArray{T1, N}(A)

test/test_blockarrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,12 @@ end
301301
@test C A BlockArray(A)
302302
@test eltype(C) == Float32
303303

304+
= PseudoBlockArray(rand(2,3), Fill(1,2), [2,1])
305+
@test convert(typeof(A), Ã) ==
306+
307+
@test PseudoBlockArray(A, axes(Ã)) isa typeof(Ã)
308+
@test PseudoBlockArray(A, axes(Ã)) == A
309+
304310

305311
A = BlockArray(rand(2,3), [1,1], [2,1])
306312
C = convert(PseudoBlockArray, A)

test/test_blockindices.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using BlockArrays, FillArrays, OffsetArrays, Test, Base64
2-
import BlockArrays: BlockIndex, BlockIndexRange
2+
import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
33

44
@testset "Blocks" begin
55
@test Int(Block(2)) === Integer(Block(2)) === Number(Block(2)) === 2
@@ -55,19 +55,25 @@ import BlockArrays: BlockIndex, BlockIndexRange
5555
@test_throws MethodError convert(Int, Block(2,1))
5656
@test convert(Tuple{Int,Int}, Block(2,1)) == (2,1)
5757
@test convert(Tuple{Float64,Int}, Block(2,1)) == (2.0,1)
58+
59+
@test Block(1)[:] Block(1)[Base.Slice(1:2)] Block(1)
5860
end
5961

6062
@testset "BlockIndex" begin
6163
@test Block(1)[1] == BlockIndex((1,),(1,))
6264
@test Block(1)[1:2] == BlockIndexRange(Block(1),(1:2,))
6365
@test Block(1,1)[1,1] == BlockIndex((1,1),(1,1))
6466
@test Block(1,1)[1:2,1:2] == BlockIndexRange(Block(1,1),(1:2,1:2))
67+
@test Block(1)[1:3][1:2] == BlockIndexRange(Block(1),1:2)
6568
end
6669

6770
@testset "BlockRange" begin
6871
@test Block.(2:5) isa BlockRange
6972
@test Block.(Base.OneTo(5)) isa BlockRange
7073
@test Block.(2:5) == [Block(2),Block(3),Block(4),Block(5)]
74+
b = Block.(2:5)
75+
@test Int.(b) === 2:5
76+
@test Base.OneTo.(1:5) isa Vector{Base.OneTo{Int}} #98
7177
end
7278
end
7379

@@ -229,6 +235,12 @@ end
229235
end
230236
end
231237

238+
@testset "BlockSlice" begin
239+
b = BlockSlice(Block(5),1:3)
240+
@test b[Base.Slice(1:3)] b
241+
@test b[1:2] b[1:2][1:2] BlockSlice(Block(5)[1:2],1:2)
242+
end
243+
232244
#=
233245
[1,1 1,2] | [1,3 1,4 1,5]
234246
--------------------------

0 commit comments

Comments
 (0)