Skip to content

Commit

Permalink
Block to Tuple conversion (#318)
Browse files Browse the repository at this point in the history
* Block to Tuple conversion

* Slightly better tests
  • Loading branch information
mtfishman authored Dec 1, 2023
1 parent 4c466fc commit 3d8f17e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BlockArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Base: @propagate_inbounds, Array, to_indices, to_index,
step,
broadcast, eltype, convert, similar,
tail, reindex,
RangeIndex, Int, Integer, Number,
RangeIndex, Int, Integer, Number, Tuple,
+, -, *, /, \, min, max, isless, in, copy, copyto!, axes, @deprecate,
BroadcastStyle, checkbounds, throw_boundserror,
oneunit, ones, zeros, intersect, Slice, resize!
Expand Down
3 changes: 2 additions & 1 deletion src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1))

# conversions
convert(::Type{T}, index::Block{1}) where {T<:Number} = convert(T, index.n[1])
convert(::Type{T}, index::Block) where {T<:Tuple} = convert(T, index.n)
convert(::Type{T}, index::Block) where {T<:Tuple} = convert(T, Block.(index.n))

Int(index::Block{1}) = Int(index.n[1])
Integer(index::Block{1}) = index.n[1]
Number(index::Block{1}) = index.n[1]
Tuple(index::Block) = Block.(index.n)

# print
Base.show(io::IO, B::Block{0,Int}) = print(io, "Block()")
Expand Down
3 changes: 3 additions & 0 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice

@testset "Blocks" begin
@test Int(Block(2)) === Integer(Block(2)) === Number(Block(2)) === 2
@test Tuple(Block(2,3)) === (Block(2),Block(3))
@test Block((Block(3), Block(4))) === Block(3,4)
@test Block() === Block(()) === Block{0}() === Block{0}(())
@test Block(1) === Block((1,)) === Block{1}(1) === Block{1}((1,))
Expand Down Expand Up @@ -70,6 +71,8 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice

@test convert(Int, Block(2)) == 2
@test convert(Float64, Block(2)) == 2.0
@test convert(Tuple, Block(2,3)) == (Block(2),Block(3))
@test convert(Tuple{Vararg{Int}}, Block(2,3)) == (2,3)

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

0 comments on commit 3d8f17e

Please sign in to comment.