@@ -49,6 +49,7 @@ blocks(a::AbstractArray) = BlocksView(a)
4949blocks (a:: BlockArray ) = a. blocks
5050blocks (A:: Adjoint ) = adjoint (blocks (parent (A)))
5151blocks (A:: Transpose ) = transpose (blocks (parent (A)))
52+ blocks (A:: StridedArray ) = BlockView (A)
5253
5354# convert a tuple of BlockRange to a tuple of `AbstractUnitRange{Int}`
5455_block2int (B:: Block{1} ) = Int (B): Int (B)
@@ -63,7 +64,7 @@ struct BlocksView{
6364 S, # eltype(eltype(BlocksView(...)))
6465 N, # ndims
6566 T<: AbstractArray{S,N} , # eltype(BlocksView(...)), i.e., block type
66- B<: AbstractArray{S,N} , # array to be wrapped
67+ B<: AbstractArray{S,N} , # array to be wrapped
6768} <: AbstractArray{T,N}
6869 array:: B
6970end
@@ -78,13 +79,6 @@ Base.IteratorEltype(::Type{<:BlocksView}) = Base.EltypeUnknown()
7879Base. size (a:: BlocksView ) = blocksize (a. array)
7980Base. axes (a:: BlocksView ) = map (br -> Int .(br), blockaxes (a. array))
8081
81- #=
82- This is broken for now. See: https://github.com/JuliaArrays/BlockArrays.jl/issues/120
83- # IndexLinear implementations
84- @propagate_inbounds getindex(a::BlocksView, i::Int) = view(a.array, Block(i))
85- @propagate_inbounds setindex!(a::BlocksView, b, i::Int) = copyto!(a[i], b)
86- =#
87-
8882# IndexCartesian implementations
8983@propagate_inbounds getindex (a:: BlocksView{T,N} , i:: Vararg{Int,N} ) where {T,N} =
9084 view (a. array, Block .(i)... )
@@ -93,6 +87,31 @@ This is broken for now. See: https://github.com/JuliaArrays/BlockArrays.jl/issue
9387 a
9488end
9589
90+ # Like `BlocksView` but specialized for a single block
91+ # in order to avoid unnecessary wrappers when accessing the block.
92+ # Note that it does not check the array being wrapped actually
93+ # only has a single block, and will interpret it as if it just has one block.
94+ # By default, this is what gets constructed when calling `blocks(::StridedArray)`.
95+ struct BlockView{
96+ S, # eltype(eltype(BlockView(...)))
97+ N, # ndims
98+ T<: AbstractArray{S,N} , # array to be wrapped
99+ } <: AbstractArray{T,N}
100+ array:: T
101+ end
102+
103+ Base. size (a:: BlockView ) = map (one, size (a. array))
104+
105+ # IndexCartesian implementations
106+ @propagate_inbounds function getindex (a:: BlockView{T,N} , i:: Vararg{Int,N} ) where {T,N}
107+ @boundscheck checkbounds (a, i... )
108+ a. array
109+ end
110+ @propagate_inbounds function setindex! (a:: BlockView{T,N} , b, i:: Vararg{Int,N} ) where {T,N}
111+ copyto! (a[i... ], b)
112+ a
113+ end
114+
96115# AbstractArray version of `Iterators.product`.
97116# https://en.wikipedia.org/wiki/Cartesian_product
98117# https://github.com/lazyLibraries/ProductArrays.jl
0 commit comments