Skip to content

Commit 1eb848f

Browse files
authored
propagate inbounds in getindex (#289)
* propagate inbounds in getindex * missed methods
1 parent 87263ce commit 1eb848f

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

src/abstractblockarray.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,16 @@ function eachblock(A::AbstractBlockArray)
216216
end
217217

218218
# Use memory layout for sub-blocks
219-
@inline Base.getindex(A::AbstractMatrix, kr::Colon, jr::Block{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
220-
@inline Base.getindex(A::AbstractMatrix, kr::Block{1}, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
221-
@inline Base.getindex(A::AbstractMatrix, kr::Block{1}, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
222-
@inline Base.getindex(A::AbstractArray{T,N}, kr::Block{1}, jrs...) where {T,N} = ArrayLayouts.layout_getindex(A, kr, jrs...)
223-
@inline Base.getindex(A::AbstractArray{T,N}, block::Block{N}) where {T,N} = ArrayLayouts.layout_getindex(A, block)
224-
@inline Base.getindex(A::AbstractMatrix, kr::AbstractVector, jr::Block) = ArrayLayouts.layout_getindex(A, kr, jr)
225-
@inline Base.getindex(A::AbstractMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
226-
@inline Base.getindex(A::LayoutMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
219+
@inline getindex(A::AbstractMatrix, kr::Colon, jr::Block{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
220+
@inline getindex(A::AbstractMatrix, kr::Block{1}, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
221+
@inline getindex(A::AbstractMatrix, kr::Block{1}, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
222+
@inline getindex(A::AbstractArray{T,N}, kr::Block{1}, jrs...) where {T,N} = ArrayLayouts.layout_getindex(A, kr, jrs...)
223+
@inline getindex(A::AbstractArray{T,N}, block::Block{N}) where {T,N} = ArrayLayouts.layout_getindex(A, block)
224+
@inline getindex(A::AbstractMatrix, kr::AbstractVector, jr::Block) = ArrayLayouts.layout_getindex(A, kr, jr)
225+
@inline getindex(A::AbstractMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
226+
@inline getindex(A::LayoutMatrix, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
227227
for Typ in (:AbstractTriangular, :Adjoint, :Transpose, :Symmetric, :Hermitian)
228-
@eval @inline Base.getindex(A::$Typ{<:Any,<:LayoutMatrix}, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
228+
@eval @inline getindex(A::$Typ{<:Any,<:LayoutMatrix}, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
229229
end
230230

231231
###

src/blockarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ copy(A::BlockArray) = _BlockArray(map(copy,A.blocks), A.axes)
330330
################################
331331
@inline axes(block_array::BlockArray) = block_array.axes
332332

333-
function viewblock(block_arr::BlockArray, block)
333+
@propagate_inbounds function viewblock(block_arr::BlockArray, block)
334334
blks = block.n
335335
@boundscheck blockcheckbounds(block_arr, blks...)
336336
block_arr.blocks[blks...]
@@ -345,9 +345,9 @@ end
345345
return v
346346
end
347347

348-
@inline Base.getindex(block_arr::BlockArray{T,N}, blockindex::BlockIndex{N}) where {T,N} =
348+
@propagate_inbounds getindex(block_arr::BlockArray{T,N}, blockindex::BlockIndex{N}) where {T,N} =
349349
_blockindex_getindex(block_arr, blockindex)
350-
@inline Base.getindex(block_arr::BlockVector{T}, blockindex::BlockIndex{1}) where {T} =
350+
@propagate_inbounds getindex(block_arr::BlockVector{T}, blockindex::BlockIndex{1}) where {T} =
351351
_blockindex_getindex(block_arr, blockindex)
352352

353353
###########################
@@ -381,7 +381,7 @@ const OffsetAxis = Union{Integer, UnitRange, Base.OneTo, Base.IdentityUnitRange}
381381
@inline Base.similar(block_array::BlockArray, ::Type{T}, axes::Tuple{Base.OneTo{Int},Vararg{Base.OneTo{Int}}}) where T =
382382
BlockArray{T}(undef, map(to_axes,axes))
383383

384-
@inline function Base.getindex(block_arr::BlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
384+
@inline function getindex(block_arr::BlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
385385
@boundscheck checkbounds(block_arr, i...)
386386
@inbounds v = block_arr[findblockindex.(axes(block_arr), i)...]
387387
return v

src/blockaxis.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
# interface
33

4-
@inline getindex(b::AbstractVector, K::BlockIndex{1}) = b[Block(K.I[1])][K.α[1]]
5-
@inline getindex(b::AbstractArray{T,N}, K::BlockIndex{N}) where {T,N} =
4+
@propagate_inbounds getindex(b::AbstractVector, K::BlockIndex{1}) = b[Block(K.I[1])][K.α[1]]
5+
@propagate_inbounds getindex(b::AbstractArray{T,N}, K::BlockIndex{N}) where {T,N} =
66
b[block(K)][K.α...]
7-
@inline getindex(b::AbstractArray, K::BlockIndex{1}, J::BlockIndex{1}...) =
7+
@propagate_inbounds getindex(b::AbstractArray, K::BlockIndex{1}, J::BlockIndex{1}...) =
88
b[BlockIndex(tuple(K, J...))]
99

10-
@inline getindex(b::AbstractArray{T,N}, K::BlockIndexRange{N}) where {T,N} = b[block(K)][K.indices...]
11-
@inline getindex(b::LayoutArray{T,N}, K::BlockIndexRange{N}) where {T,N} = b[block(K)][K.indices...]
12-
@inline getindex(b::LayoutArray{T,1}, K::BlockIndexRange{1}) where {T} = b[block(K)][K.indices...]
10+
@propagate_inbounds getindex(b::AbstractArray{T,N}, K::BlockIndexRange{N}) where {T,N} = b[block(K)][K.indices...]
11+
@propagate_inbounds getindex(b::LayoutArray{T,N}, K::BlockIndexRange{N}) where {T,N} = b[block(K)][K.indices...]
12+
@propagate_inbounds getindex(b::LayoutArray{T,1}, K::BlockIndexRange{1}) where {T} = b[block(K)][K.indices...]
1313

1414
function findblockindex(b::AbstractVector, k::Integer)
1515
@boundscheck k in b || throw(BoundsError())
@@ -252,7 +252,7 @@ first(b::BlockedUnitRange) = b.first
252252
# ::Integer works around case where blocklasts might return different type
253253
last(b::BlockedUnitRange)::Integer = isempty(blocklasts(b)) ? first(b)-1 : last(blocklasts(b))
254254

255-
function getindex(b::BlockedUnitRange, K::Block{1})
255+
@propagate_inbounds function getindex(b::BlockedUnitRange, K::Block{1})
256256
k = Integer(K)
257257
bax = blockaxes(b,1)
258258
cs = blocklasts(b)
@@ -262,7 +262,7 @@ function getindex(b::BlockedUnitRange, K::Block{1})
262262
return cs[k-1]+1:cs[k]
263263
end
264264

265-
function getindex(b::BlockedUnitRange, KR::BlockRange{1})
265+
@propagate_inbounds function getindex(b::BlockedUnitRange, KR::BlockRange{1})
266266
cs = blocklasts(b)
267267
isempty(KR) && return _BlockedUnitRange(1,cs[1:0])
268268
K,J = first(KR),last(KR)
@@ -274,7 +274,7 @@ function getindex(b::BlockedUnitRange, KR::BlockRange{1})
274274
_BlockedUnitRange(cs[k-1]+1,cs[k:j])
275275
end
276276

277-
function getindex(b::BlockedUnitRange, KR::BlockRange{1,Tuple{Base.OneTo{Int}}})
277+
@propagate_inbounds function getindex(b::BlockedUnitRange, KR::BlockRange{1,Tuple{Base.OneTo{Int}}})
278278
cs = blocklasts(b)
279279
isempty(KR) && return _BlockedUnitRange(1,cs[Base.OneTo(0)])
280280
J = last(KR)
@@ -284,7 +284,7 @@ function getindex(b::BlockedUnitRange, KR::BlockRange{1,Tuple{Base.OneTo{Int}}})
284284
_BlockedUnitRange(first(b),cs[Base.OneTo(j)])
285285
end
286286

287-
getindex(b::BlockedUnitRange, KR::BlockSlice) = b[KR.block]
287+
@propagate_inbounds getindex(b::BlockedUnitRange, KR::BlockSlice) = b[KR.block]
288288

289289
_searchsortedfirst(a::AbstractVector, k) = searchsortedfirst(a, k)
290290
function _searchsortedfirst(a::Tuple, k)
@@ -314,12 +314,12 @@ function Base.checkindex(::Type{Bool}, axis::BlockedUnitRange, ind::BlockIndex{1
314314
checkindex(Bool, axis, block(ind)) && checkbounds(Bool, axis[block(ind)], blockindex(ind))
315315
end
316316

317-
function getindex(b::AbstractUnitRange{Int}, K::Block{1})
317+
@propagate_inbounds function getindex(b::AbstractUnitRange{Int}, K::Block{1})
318318
@boundscheck K == Block(1) || throw(BlockBoundsError(b, K))
319319
b
320320
end
321321

322-
function getindex(b::AbstractUnitRange{Int}, K::BlockRange)
322+
@propagate_inbounds function getindex(b::AbstractUnitRange{Int}, K::BlockRange)
323323
@boundscheck K == Block.(1:1) || throw(BlockBoundsError(b, K))
324324
b
325325
end
@@ -422,8 +422,8 @@ Base.axes(S::Base.Slice{<:BlockedUnitRange}) = (S.indices,)
422422
Base.unsafe_indices(S::Base.Slice{<:BlockedUnitRange}) = (S.indices,)
423423
Base.axes1(S::Base.Slice{<:BlockedUnitRange}) = S.indices
424424
blockaxes(S::Base.Slice) = blockaxes(S.indices)
425-
getindex(S::Base.Slice, b::Block{1}) = S.indices[b]
426-
getindex(S::Base.Slice, b::BlockRange{1}) = S.indices[b]
425+
@propagate_inbounds getindex(S::Base.Slice, b::Block{1}) = S.indices[b]
426+
@propagate_inbounds getindex(S::Base.Slice, b::BlockRange{1}) = S.indices[b]
427427

428428

429429
# This supports broadcasting with infinite block arrays

src/blockindices.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ getindex(B::Block{N}, inds::Vararg{AbstractUnitRange{Int},N}) where N = BlockInd
216216
getindex(B::Block{1}, inds::Colon) = B
217217
getindex(B::Block{1}, inds::Base.Slice) = B
218218

219-
getindex(B::BlockIndexRange{1}, kr::AbstractUnitRange{Int}) = BlockIndexRange(B.block, B.indices[1][kr])
220-
getindex(B::BlockIndexRange{N}, inds::Vararg{Int,N}) where N = B.block[Base.reindex(B.indices, inds)...]
219+
@propagate_inbounds getindex(B::BlockIndexRange{1}, kr::AbstractUnitRange{Int}) = BlockIndexRange(B.block, B.indices[1][kr])
220+
@propagate_inbounds getindex(B::BlockIndexRange{N}, inds::Vararg{Int,N}) where N = B.block[Base.reindex(B.indices, inds)...]
221221

222222
eltype(R::BlockIndexRange) = eltype(typeof(R))
223223
eltype(::Type{BlockIndexRange{N}}) where {N} = BlockIndex{N}
@@ -292,9 +292,9 @@ for f in (:axes, :unsafe_indices, :axes1, :first, :last, :size, :length,
292292
@eval $f(S::BlockSlice) = $f(S.indices)
293293
end
294294

295-
getindex(S::BlockSlice, i::Integer) = getindex(S.indices, i)
296-
getindex(S::BlockSlice{<:Block}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
297-
getindex(S::BlockSlice{<:BlockIndexRange}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
295+
@propagate_inbounds getindex(S::BlockSlice, i::Integer) = getindex(S.indices, i)
296+
@propagate_inbounds getindex(S::BlockSlice{<:Block}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
297+
@propagate_inbounds getindex(S::BlockSlice{<:BlockIndexRange}, k::AbstractUnitRange{Int}) = BlockSlice(S.block[k],S.indices[k])
298298
show(io::IO, r::BlockSlice) = print(io, "BlockSlice(", r.block, ",", r.indices, ")")
299299

300300
Block(bs::BlockSlice{<:BlockIndexRange}) = Block(bs.block)
@@ -363,16 +363,16 @@ broadcasted(::DefaultArrayStyle{0}, ::Type{Int}, block::Block{1}) = Int(block)
363363

364364
# AbstractArray implementation
365365
axes(iter::BlockRange{N,R}) where {N,R} = map(axes1, iter.indices)
366-
@inline function Base.getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Int, N}) where {N}
366+
@inline function getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Int, N}) where {N}
367367
@boundscheck checkbounds(iter, I...)
368368
Block(I)
369369
end
370-
@propagate_inbounds function Base.getindex(iter::BlockRange{N}, I::Vararg{Int, N}) where {N}
370+
@propagate_inbounds function getindex(iter::BlockRange{N}, I::Vararg{Int, N}) where {N}
371371
@boundscheck checkbounds(iter, I...)
372372
Block(getindex.(iter.indices, I))
373373
end
374374

375-
@propagate_inbounds function Base.getindex(iter::BlockRange{N}, I::Vararg{AbstractUnitRange{<:Integer}, N}) where {N}
375+
@propagate_inbounds function getindex(iter::BlockRange{N}, I::Vararg{AbstractUnitRange{<:Integer}, N}) where {N}
376376
@boundscheck checkbounds(iter, I...)
377377
BlockRange(getindex.(iter.indices, I))
378378
end

src/blocks.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ Base.axes(a::BlocksView) = map(br -> only(br.indices), blockaxes(a.array))
8181
#=
8282
This is broken for now. See: https://github.com/JuliaArrays/BlockArrays.jl/issues/120
8383
# IndexLinear implementations
84-
@propagate_inbounds Base.getindex(a::BlocksView, i::Int) = view(a.array, Block(i))
84+
@propagate_inbounds getindex(a::BlocksView, i::Int) = view(a.array, Block(i))
8585
@propagate_inbounds Base.setindex!(a::BlocksView, b, i::Int) = copyto!(a[i], b)
8686
=#
8787

8888
# IndexCartesian implementations
89-
@propagate_inbounds Base.getindex(a::BlocksView{T,N}, i::Vararg{Int,N}) where {T,N} =
89+
@propagate_inbounds getindex(a::BlocksView{T,N}, i::Vararg{Int,N}) where {T,N} =
9090
view(a.array, Block.(i)...)
9191
@propagate_inbounds Base.setindex!(a::BlocksView{T,N}, b, i::Vararg{Int,N}) where {T,N} =
9292
copyto!(a[i...], b)

src/pseudo_blockarray.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ to_axes(n::Integer) = Base.oneto(n)
176176
@inline Base.similar(block_array::PseudoBlockArray, ::Type{T}, axes::Tuple{Union{Integer,AbstractUnitRange{Int}},BlockedUnitRange,Vararg{Union{Integer,AbstractUnitRange{Int}}}}) where T =
177177
PseudoBlockArray{T}(undef, map(to_axes,axes))
178178

179-
@inline @propagate_inbounds Base.getindex(block_arr::PseudoBlockArray{T, N}, i::Vararg{Integer, N}) where {T,N} = block_arr.blocks[i...]
180-
@inline @propagate_inbounds Base.setindex!(block_arr::PseudoBlockArray{T, N}, v, i::Vararg{Integer, N}) where {T,N} = setindex!(block_arr.blocks, v, i...)
179+
@propagate_inbounds getindex(block_arr::PseudoBlockArray{T, N}, i::Vararg{Integer, N}) where {T,N} = block_arr.blocks[i...]
180+
@propagate_inbounds Base.setindex!(block_arr::PseudoBlockArray{T, N}, v, i::Vararg{Integer, N}) where {T,N} = setindex!(block_arr.blocks, v, i...)
181181

182182
################################
183183
# AbstractBlockArray Interface #
@@ -193,16 +193,16 @@ to_axes(n::Integer) = Base.oneto(n)
193193
return view(block_arr.blocks, range...)
194194
end
195195

196-
@inline @propagate_inbounds function _pseudoblockindex_getindex(block_arr, blockindex)
196+
@propagate_inbounds function _pseudoblockindex_getindex(block_arr, blockindex)
197197
I = getindex.(axes(block_arr), getindex.(Block.(blockindex.I), blockindex.α))
198198
block_arr.blocks[I...]
199199
end
200200

201-
@inline Base.getindex(block_arr::PseudoBlockArray{T,N}, blockindex::BlockIndex{N}) where {T,N} =
201+
@propagate_inbounds getindex(block_arr::PseudoBlockArray{T,N}, blockindex::BlockIndex{N}) where {T,N} =
202202
_pseudoblockindex_getindex(block_arr, blockindex)
203203

204204

205-
@inline Base.getindex(block_arr::PseudoBlockVector{T}, blockindex::BlockIndex{1}) where T =
205+
@propagate_inbounds getindex(block_arr::PseudoBlockVector{T}, blockindex::BlockIndex{1}) where T =
206206
_pseudoblockindex_getindex(block_arr, blockindex)
207207

208208
########

0 commit comments

Comments
 (0)