Skip to content

Commit 3d8f17e

Browse files
authored
Block to Tuple conversion (#318)
* Block to Tuple conversion * Slightly better tests
1 parent 4c466fc commit 3d8f17e

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

src/BlockArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Base: @propagate_inbounds, Array, to_indices, to_index,
2525
step,
2626
broadcast, eltype, convert, similar,
2727
tail, reindex,
28-
RangeIndex, Int, Integer, Number,
28+
RangeIndex, Int, Integer, Number, Tuple,
2929
+, -, *, /, \, min, max, isless, in, copy, copyto!, axes, @deprecate,
3030
BroadcastStyle, checkbounds, throw_boundserror,
3131
oneunit, ones, zeros, intersect, Slice, resize!

src/blockindices.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))
9696

9797
# conversions
9898
convert(::Type{T}, index::Block{1}) where {T<:Number} = convert(T, index.n[1])
99-
convert(::Type{T}, index::Block) where {T<:Tuple} = convert(T, index.n)
99+
convert(::Type{T}, index::Block) where {T<:Tuple} = convert(T, Block.(index.n))
100100

101101
Int(index::Block{1}) = Int(index.n[1])
102102
Integer(index::Block{1}) = index.n[1]
103103
Number(index::Block{1}) = index.n[1]
104+
Tuple(index::Block) = Block.(index.n)
104105

105106
# print
106107
Base.show(io::IO, B::Block{0,Int}) = print(io, "Block()")

test/test_blockindices.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
44

55
@testset "Blocks" begin
66
@test Int(Block(2)) === Integer(Block(2)) === Number(Block(2)) === 2
7+
@test Tuple(Block(2,3)) === (Block(2),Block(3))
78
@test Block((Block(3), Block(4))) === Block(3,4)
89
@test Block() === Block(()) === Block{0}() === Block{0}(())
910
@test Block(1) === Block((1,)) === Block{1}(1) === Block{1}((1,))
@@ -70,6 +71,8 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
7071

7172
@test convert(Int, Block(2)) == 2
7273
@test convert(Float64, Block(2)) == 2.0
74+
@test convert(Tuple, Block(2,3)) == (Block(2),Block(3))
75+
@test convert(Tuple{Vararg{Int}}, Block(2,3)) == (2,3)
7376

7477
@test_throws MethodError convert(Int, Block(2,1))
7578
@test convert(Tuple{Int,Int}, Block(2,1)) == (2,1)

0 commit comments

Comments
 (0)