33
44using BlockArrays: Block, BlockArrays, BlockIndexRange, BlockRange, blockedrange
55
6+ using TypeParameterAccessors: unspecify_type_parameters
7+
68#
79# ================================== AbstractBlockTuple ==================================
810#
@@ -18,7 +20,12 @@ Base.firstindex(::AbstractBlockTuple) = 1
1820Base. getindex (bt:: AbstractBlockTuple , i:: Integer ) = Tuple (bt)[i]
1921Base. getindex (bt:: AbstractBlockTuple , r:: AbstractUnitRange ) = Tuple (bt)[r]
2022Base. getindex (bt:: AbstractBlockTuple , b:: Block{1} ) = blocks (bt)[Int (b)]
21- Base. getindex (bt:: AbstractBlockTuple , br:: BlockRange{1} ) = blocks (bt)[Int .(br)]
23+ function Base. getindex (bt:: AbstractBlockTuple , br:: BlockRange{1} )
24+ r = Int .(br)
25+ T = unspecify_type_parameters (typeof (bt))
26+ flat = Tuple (bt)[blockfirsts (bt)[first (r)]: blocklasts (bt)[last (r)]]
27+ return T {blocklengths(bt)[r]} (flat)
28+ end
2229function Base. getindex (bt:: AbstractBlockTuple , bi:: BlockIndexRange{1} )
2330 return bt[Block (bi)][only (bi. indices)]
2431end
@@ -32,19 +39,21 @@ Base.lastindex(bt::AbstractBlockTuple) = length(bt)
3239
3340# Broadcast interface
3441Base. broadcastable (bt:: AbstractBlockTuple ) = bt
35- struct BlockedTupleBroadcastStyle {BlockLengths} <: Broadcast.BroadcastStyle end
36- function Base. BroadcastStyle (type :: Type{<:AbstractBlockTuple} )
37- return BlockedTupleBroadcastStyle {blocklengths(type )} ()
42+ struct AbstractBlockTupleBroadcastStyle {BlockLengths,BT } <: Broadcast.BroadcastStyle end
43+ function Base. BroadcastStyle (T :: Type{<:AbstractBlockTuple} )
44+ return AbstractBlockTupleBroadcastStyle {blocklengths(T),unspecify_type_parameters(T )} ()
3845end
3946
4047# BroadcastStyle is not called for two identical styles
41- function Base. BroadcastStyle (:: BlockedTupleBroadcastStyle , :: BlockedTupleBroadcastStyle )
48+ function Base. BroadcastStyle (
49+ :: AbstractBlockTupleBroadcastStyle , :: AbstractBlockTupleBroadcastStyle
50+ )
4251 throw (DimensionMismatch (" Incompatible blocks" ))
4352end
4453function Base. copy (
45- bc:: Broadcast.Broadcasted{BlockedTupleBroadcastStyle {BlockLengths}}
46- ) where {BlockLengths}
47- return BlockedTuple {BlockLengths} (bc. f .((Tuple .(bc. args)). .. ))
54+ bc:: Broadcast.Broadcasted{AbstractBlockTupleBroadcastStyle {BlockLengths,BT }}
55+ ) where {BlockLengths,BT }
56+ return BT {BlockLengths} (bc. f .((Tuple .(bc. args)). .. ))
4857end
4958
5059# BlockArrays interface
0 commit comments