• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37505

pending completion
#37505

push

local

web-flow
Rename `ipython_mode` to `numbered_prompt` (#49314)


Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com>

1 of 1 new or added line in 1 file covered. (100.0%)

70164 of 83158 relevant lines covered (84.37%)

31979364.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.7
/base/subarray.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
abstract type AbstractCartesianIndex{N} end # This is a hacky forward declaration for CartesianIndex
4
const ViewIndex = Union{Real, AbstractArray}
5
const ScalarIndex = Real
6

7
"""
8
    SubArray{T,N,P,I,L} <: AbstractArray{T,N}
9

10
`N`-dimensional view into a parent array (of type `P`) with an element type `T`, restricted by a tuple of indices (of type `I`). `L` is true for types that support fast linear indexing, and `false` otherwise.
11

12
Construct `SubArray`s using the [`view`](@ref) function.
13
"""
14
struct SubArray{T,N,P,I,L} <: AbstractArray{T,N}
15
    parent::P
16
    indices::I
17
    offset1::Int       # for linear indexing and pointer, only valid when L==true
18
    stride1::Int       # used only for linear indexing
19
    function SubArray{T,N,P,I,L}(parent, indices, offset1, stride1) where {T,N,P,I,L}
525,751✔
20
        @inline
525,751✔
21
        check_parent_index_match(parent, indices)
525,751✔
22
        new(parent, indices, offset1, stride1)
4,076,793✔
23
    end
24
end
25
# Compute the linear indexability of the indices, and combine it with the linear indexing of the parent
26
function SubArray(parent::AbstractArray, indices::Tuple)
1,107,802✔
27
    @inline
1,107,802✔
28
    SubArray(IndexStyle(viewindexing(indices), IndexStyle(parent)), parent, ensure_indexable(indices), index_dimsum(indices...))
4,076,231✔
29
end
30
function SubArray(::IndexCartesian, parent::P, indices::I, ::NTuple{N,Any}) where {P,I,N}
237,140✔
31
    @inline
237,133✔
32
    SubArray{eltype(P), N, P, I, false}(parent, indices, 0, 0)
237,197✔
33
end
34
function SubArray(::IndexLinear, parent::P, indices::I, ::NTuple{N,Any}) where {P,I,N}
870,662✔
35
    @inline
870,662✔
36
    # Compute the stride and offset
37
    stride1 = compute_stride1(parent, indices)
870,758✔
38
    SubArray{eltype(P), N, P, I, true}(parent, indices, compute_offset1(parent, stride1, indices), stride1)
3,839,034✔
39
end
40

41
check_parent_index_match(parent, indices) = check_parent_index_match(parent, index_ndims(indices...))
525,750✔
42
check_parent_index_match(parent::AbstractArray{T,N}, ::NTuple{N, Bool}) where {T,N} = nothing
453,522✔
43
check_parent_index_match(parent, ::NTuple{N, Bool}) where {N} =
×
44
    throw(ArgumentError("number of indices ($N) must match the parent dimensionality ($(ndims(parent)))"))
45

46
# This computes the linear indexing compatibility for a given tuple of indices
47
viewindexing(I::Tuple{}) = IndexLinear()
50✔
48
# Leading scalar indices simply increase the stride
49
viewindexing(I::Tuple{ScalarIndex, Vararg{Any}}) = (@inline; viewindexing(tail(I)))
7,362✔
50
# Slices may begin a section which may be followed by any number of Slices
51
viewindexing(I::Tuple{Slice, Slice, Vararg{Any}}) = (@inline; viewindexing(tail(I)))
2,114✔
52
# A UnitRange can follow Slices, but only if all other indices are scalar
53
viewindexing(I::Tuple{Slice, AbstractUnitRange, Vararg{ScalarIndex}}) = IndexLinear()
432✔
54
viewindexing(I::Tuple{Slice, Slice, Vararg{ScalarIndex}}) = IndexLinear() # disambiguate
1,360✔
55
# In general, ranges are only fast if all other indices are scalar
56
viewindexing(I::Tuple{AbstractRange, Vararg{ScalarIndex}}) = IndexLinear()
156,517✔
57
# All other index combinations are slow
58
viewindexing(I::Tuple{Vararg{Any}}) = IndexCartesian()
×
59
# Of course, all other array types are slow
60
viewindexing(I::Tuple{AbstractArray, Vararg{Any}}) = IndexCartesian()
19,601✔
61

62
# Simple utilities
63
size(V::SubArray) = (@inline; map(length, axes(V)))
14,367,571✔
64

65
similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
7,686✔
66

67
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
2✔
68
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)
308,241✔
69

70
function Base.copy(V::SubArray)
1,173✔
71
    v = V.parent[V.indices...]
1,653✔
72
    ndims(V) == 0 || return v
1,173✔
73
    x = similar(V) # ensure proper type of x
×
74
    x[] = v
×
75
    return x
×
76
end
77

78
parent(V::SubArray) = V.parent
98,570✔
79
parentindices(V::SubArray) = V.indices
97,865✔
80

81
"""
82
    parentindices(A)
83

84
Return the indices in the [`parent`](@ref) which correspond to the array view `A`.
85

86
# Examples
87
```jldoctest
88
julia> A = [1 2; 3 4];
89

90
julia> V = view(A, 1, :)
91
2-element view(::Matrix{Int64}, 1, :) with eltype Int64:
92
 1
93
 2
94

95
julia> parentindices(V)
96
(1, Base.Slice(Base.OneTo(2)))
97
```
98
"""
99
parentindices(a::AbstractArray) = map(oneto, size(a))
×
100

101
## Aliasing detection
102
dataids(A::SubArray) = (dataids(A.parent)..., _splatmap(dataids, A.indices)...)
2,892,743✔
103
_splatmap(f, ::Tuple{}) = ()
×
104
_splatmap(f, t::Tuple) = (f(t[1])..., _splatmap(f, tail(t))...)
17,476✔
105
unaliascopy(A::SubArray) = typeof(A)(unaliascopy(A.parent), map(unaliascopy, A.indices), A.offset1, A.stride1)
4✔
106

107
# When the parent is an Array we can trim the size down a bit. In the future this
108
# could possibly be extended to any mutable array.
109
function unaliascopy(V::SubArray{T,N,A,I,LD}) where {T,N,A<:Array,I<:Tuple{Vararg{Union{Real,AbstractRange,Array}}},LD}
558✔
110
    dest = Array{T}(undef, index_lengths(V.indices...))
558✔
111
    copyto!(dest, V)
558✔
112
    SubArray{T,N,A,I,LD}(dest, map(_trimmedindex, V.indices), 0, Int(LD))
558✔
113
end
114
# Transform indices to be "dense"
115
_trimmedindex(i::Real) = oftype(i, 1)
540✔
116
_trimmedindex(i::AbstractUnitRange) = oftype(i, oneto(length(i)))
580✔
117
_trimmedindex(i::AbstractArray) = oftype(i, reshape(eachindex(IndexLinear(), i), axes(i)))
×
118

119
## SubArray creation
120
# We always assume that the dimensionality of the parent matches the number of
121
# indices that end up getting passed to it, so we store the parent as a
122
# ReshapedArray view if necessary. The trouble is that arrays of `CartesianIndex`
123
# can make the number of effective indices not equal to length(I).
124
_maybe_reshape_parent(A::AbstractArray, ::NTuple{1, Bool}) = reshape(A, Val(1))
1,114✔
125
_maybe_reshape_parent(A::AbstractArray{<:Any,1}, ::NTuple{1, Bool}) = reshape(A, Val(1))
263,181✔
126
_maybe_reshape_parent(A::AbstractArray{<:Any,N}, ::NTuple{N, Bool}) where {N} = A
179,251✔
127
_maybe_reshape_parent(A::AbstractArray, ::NTuple{N, Bool}) where {N} = reshape(A, Val(N))
336✔
128
"""
129
    view(A, inds...)
130

131
Like [`getindex`](@ref), but returns a lightweight array that lazily references
132
(or is effectively a _view_ into) the parent array `A` at the given index or indices
133
`inds` instead of eagerly extracting elements or constructing a copied subset.
134
Calling [`getindex`](@ref) or [`setindex!`](@ref) on the returned value
135
(often a [`SubArray`](@ref)) computes the indices to access or modify the
136
parent array on the fly.  The behavior is undefined if the shape of the parent array is
137
changed after `view` is called because there is no bound check for the parent array; e.g.,
138
it may cause a segmentation fault.
139

140
Some immutable parent arrays (like ranges) may choose to simply
141
recompute a new array in some circumstances instead of returning
142
a `SubArray` if doing so is efficient and provides compatible semantics.
143

144
!!! compat "Julia 1.6"
145
    In Julia 1.6 or later, `view` can be called on an `AbstractString`, returning a
146
    `SubString`.
147

148
# Examples
149
```jldoctest
150
julia> A = [1 2; 3 4]
151
2×2 Matrix{Int64}:
152
 1  2
153
 3  4
154

155
julia> b = view(A, :, 1)
156
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
157
 1
158
 3
159

160
julia> fill!(b, 0)
161
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
162
 0
163
 0
164

165
julia> A # Note A has changed even though we modified b
166
2×2 Matrix{Int64}:
167
 0  2
168
 0  4
169

170
julia> view(2:5, 2:3) # returns a range as type is immutable
171
3:4
172
```
173
"""
174
function view(A::AbstractArray{<:Any,N}, I::Vararg{Any,M}) where {N,M}
3,589,102✔
175
    @inline
1,107,967✔
176
    J = map(i->unalias(A,i), to_indices(A, I))
1,834,787✔
177
    @boundscheck checkbounds(A, J...)
4,078,057✔
178
    if length(J) > ndims(A) && J[N+1:end] isa Tuple{Vararg{Int}}
1,107,949✔
179
        # view([1,2,3], :, 1) does not need to reshape
180
        return unsafe_view(A, J[1:N]...)
9,350✔
181
    end
182
    unsafe_view(_maybe_reshape_parent(A, index_ndims(J...)), J...)
4,068,703✔
183
end
184

185
# Ranges implement getindex to return recomputed ranges; use that for views, too (when possible)
186
function view(r1::OneTo, r2::OneTo)
1✔
187
    @_propagate_inbounds_meta
1✔
188
    getindex(r1, r2)
1✔
189
end
190
function view(r1::AbstractUnitRange, r2::AbstractUnitRange{<:Integer})
284✔
191
    @_propagate_inbounds_meta
284✔
192
    getindex(r1, r2)
284✔
193
end
194
function view(r1::AbstractUnitRange, r2::StepRange{<:Integer})
1✔
195
    @_propagate_inbounds_meta
1✔
196
    getindex(r1, r2)
1✔
197
end
198
function view(r1::StepRange, r2::AbstractRange{<:Integer})
1✔
199
    @_propagate_inbounds_meta
1✔
200
    getindex(r1, r2)
1✔
201
end
202
function view(r1::StepRangeLen, r2::OrdinalRange{<:Integer})
×
203
    @_propagate_inbounds_meta
×
204
    getindex(r1, r2)
×
205
end
206
function view(r1::LinRange, r2::OrdinalRange{<:Integer})
×
207
    @_propagate_inbounds_meta
×
208
    getindex(r1, r2)
×
209
end
210

211
# getindex(r::AbstractRange, ::Colon) returns a copy of the range, and we may do the same for a view
212
function view(r1::AbstractRange, c::Colon)
2✔
213
    @_propagate_inbounds_meta
2✔
214
    getindex(r1, c)
2✔
215
end
216

217
function unsafe_view(A::AbstractArray, I::Vararg{ViewIndex,N}) where {N}
979,504✔
218
    @inline
979,355✔
219
    SubArray(A, I)
3,930,216✔
220
end
221
# When we take the view of a view, it's often possible to "reindex" the parent
222
# view's indices such that we can "pop" the parent view and keep just one layer
223
# of indirection. But we can't always do this because arrays of `CartesianIndex`
224
# might span multiple parent indices, making the reindex calculation very hard.
225
# So we use _maybe_reindex to figure out if there are any arrays of
226
# `CartesianIndex`, and if so, we punt and keep two layers of indirection.
227
unsafe_view(V::SubArray, I::Vararg{ViewIndex,N}) where {N} =
128,446✔
228
    (@inline; _maybe_reindex(V, I))
146,076✔
229
_maybe_reindex(V, I) = (@inline; _maybe_reindex(V, I, I))
146,076✔
230
_maybe_reindex(V, I, ::Tuple{AbstractArray{<:AbstractCartesianIndex}, Vararg{Any}}) =
17✔
231
    (@inline; SubArray(V, I))
17✔
232
# But allow arrays of CartesianIndex{1}; they behave just like arrays of Ints
233
_maybe_reindex(V, I, A::Tuple{AbstractArray{<:AbstractCartesianIndex{1}}, Vararg{Any}}) =
×
234
    (@inline; _maybe_reindex(V, I, tail(A)))
×
235
_maybe_reindex(V, I, A::Tuple{Any, Vararg{Any}}) = (@inline; _maybe_reindex(V, I, tail(A)))
152,319✔
236
function _maybe_reindex(V, I, ::Tuple{})
128,429✔
237
    @inline
128,429✔
238
    @inbounds idxs = to_indices(V.parent, reindex(V.indices, I))
146,059✔
239
    SubArray(V.parent, idxs)
145,997✔
240
end
241

242
## Re-indexing is the heart of a view, transforming A[i, j][x, y] to A[i[x], j[y]]
243
#
244
# Recursively look through the heads of the parent- and sub-indices, considering
245
# the following cases:
246
# * Parent index is array  -> re-index that with one or more sub-indices (one per dimension)
247
# * Parent index is Colon  -> just use the sub-index as provided
248
# * Parent index is scalar -> that dimension was dropped, so skip the sub-index and use the index as is
249

250
AbstractZeroDimArray{T} = AbstractArray{T, 0}
251

252
reindex(::Tuple{}, ::Tuple{}) = ()
×
253

254
# Skip dropped scalars, so simply peel them off the parent indices and continue
255
reindex(idxs::Tuple{ScalarIndex, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) =
2,273,948✔
256
    (@_propagate_inbounds_meta; (idxs[1], reindex(tail(idxs), subidxs)...))
2,273,948✔
257

258
# Slices simply pass their subindices straight through
259
reindex(idxs::Tuple{Slice, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =
6,355,034✔
260
    (@_propagate_inbounds_meta; (subidxs[1], reindex(tail(idxs), tail(subidxs))...))
6,355,048✔
261

262
# Re-index into parent vectors with one subindex
263
reindex(idxs::Tuple{AbstractVector, Vararg{Any}}, subidxs::Tuple{Any, Vararg{Any}}) =
15,594,872✔
264
    (@_propagate_inbounds_meta; (idxs[1][subidxs[1]], reindex(tail(idxs), tail(subidxs))...))
15,613,203✔
265

266
# Parent matrices are re-indexed with two sub-indices
267
reindex(idxs::Tuple{AbstractMatrix, Vararg{Any}}, subidxs::Tuple{Any, Any, Vararg{Any}}) =
4,588✔
268
    (@_propagate_inbounds_meta; (idxs[1][subidxs[1], subidxs[2]], reindex(tail(idxs), tail(tail(subidxs)))...))
4,594✔
269

270
# In general, we index N-dimensional parent arrays with N indices
271
@generated function reindex(idxs::Tuple{AbstractArray{T,N}, Vararg{Any}}, subidxs::Tuple{Vararg{Any}}) where {T,N}
×
272
    if length(subidxs.parameters) >= N
×
273
        subs = [:(subidxs[$d]) for d in 1:N]
×
274
        tail = [:(subidxs[$d]) for d in N+1:length(subidxs.parameters)]
×
275
        :(@_propagate_inbounds_meta; (idxs[1][$(subs...)], reindex(tail(idxs), ($(tail...),))...))
×
276
    else
277
        :(throw(ArgumentError("cannot re-index SubArray with fewer indices than dimensions\nThis should not occur; please submit a bug report.")))
×
278
    end
279
end
280

281
# In general, we simply re-index the parent indices by the provided ones
282
SlowSubArray{T,N,P,I} = SubArray{T,N,P,I,false}
283
function getindex(V::SubArray{T,N}, I::Vararg{Int,N}) where {T,N}
4,397,223✔
284
    @inline
4,397,223✔
285
    @boundscheck checkbounds(V, I...)
4,397,232✔
286
    @inbounds r = V.parent[reindex(V.indices, I)...]
4,714,027✔
287
    r
4,397,198✔
288
end
289

290
# But SubArrays with fast linear indexing pre-compute a stride and offset
291
FastSubArray{T,N,P,I} = SubArray{T,N,P,I,true}
292
function getindex(V::FastSubArray, i::Int)
352✔
293
    @inline
352✔
294
    @boundscheck checkbounds(V, i)
352✔
295
    @inbounds r = V.parent[V.offset1 + V.stride1*i]
352✔
296
    r
352✔
297
end
298
# We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange,
299
# or if all the indices are scalars, i.e. the view is for a single value only
300
FastContiguousSubArray{T,N,P,I<:Union{Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}},
301
                                      Tuple{Vararg{ScalarIndex}}}} = SubArray{T,N,P,I,true}
302
function getindex(V::FastContiguousSubArray, i::Int)
3,872✔
303
    @inline
3,872✔
304
    @boundscheck checkbounds(V, i)
3,874✔
305
    @inbounds r = V.parent[V.offset1 + i]
3,870✔
306
    r
3,870✔
307
end
308
# For vector views with linear indexing, we disambiguate to favor the stride/offset
309
# computation as that'll generally be faster than (or just as fast as) re-indexing into a range.
310
function getindex(V::FastSubArray{<:Any, 1}, i::Int)
210,511✔
311
    @inline
206,964✔
312
    @boundscheck checkbounds(V, i)
236,308✔
313
    @inbounds r = V.parent[V.offset1 + V.stride1*i]
236,302✔
314
    r
236,302✔
315
end
316
function getindex(V::FastContiguousSubArray{<:Any, 1}, i::Int)
35,972,205✔
317
    @inline
20,747,002✔
318
    @boundscheck checkbounds(V, i)
102,693,604✔
319
    @inbounds r = V.parent[V.offset1 + i]
102,693,598✔
320
    r
102,693,598✔
321
end
322

323
# Indexed assignment follows the same pattern as `getindex` above
324
function setindex!(V::SubArray{T,N}, x, I::Vararg{Int,N}) where {T,N}
8,405,073✔
325
    @inline
8,404,369✔
326
    @boundscheck checkbounds(V, I...)
8,405,083✔
327
    @inbounds V.parent[reindex(V.indices, I)...] = x
8,405,185✔
328
    V
8,405,069✔
329
end
330
function setindex!(V::FastSubArray, x, i::Int)
1,000,628✔
331
    @inline
1,000,628✔
332
    @boundscheck checkbounds(V, i)
1,000,628✔
333
    @inbounds V.parent[V.offset1 + V.stride1*i] = x
1,000,628✔
334
    V
1,000,628✔
335
end
336
function setindex!(V::FastContiguousSubArray, x, i::Int)
21,477✔
337
    @inline
21,477✔
338
    @boundscheck checkbounds(V, i)
21,477✔
339
    @inbounds V.parent[V.offset1 + i] = x
21,477✔
340
    V
21,477✔
341
end
342
function setindex!(V::FastSubArray{<:Any, 1}, x, i::Int)
30,380✔
343
    @inline
30,291✔
344
    @boundscheck checkbounds(V, i)
31,956✔
345
    @inbounds V.parent[V.offset1 + V.stride1*i] = x
31,956✔
346
    V
31,956✔
347
end
348
function setindex!(V::FastContiguousSubArray{<:Any, 1}, x, i::Int)
54,435,757✔
349
    @inline
54,435,295✔
350
    @boundscheck checkbounds(V, i)
85,678,759✔
351
    @inbounds V.parent[V.offset1 + i] = x
85,678,759✔
352
    V
85,678,759✔
353
end
354

355
IndexStyle(::Type{<:FastSubArray}) = IndexLinear()
36,340✔
356
IndexStyle(::Type{<:SubArray}) = IndexCartesian()
8,664,632✔
357

358
# Strides are the distance in memory between adjacent elements in a given dimension
359
# which we determine from the strides of the parent
360
strides(V::SubArray) = substrides(strides(V.parent), V.indices)
55,766✔
361

362
substrides(strds::Tuple{}, ::Tuple{}) = ()
×
363
substrides(strds::NTuple{N,Int}, I::Tuple{ScalarIndex, Vararg{Any}}) where N = (substrides(tail(strds), tail(I))...,)
2,068✔
364
substrides(strds::NTuple{N,Int}, I::Tuple{Slice, Vararg{Any}}) where N = (first(strds), substrides(tail(strds), tail(I))...)
5,832✔
365
substrides(strds::NTuple{N,Int}, I::Tuple{AbstractRange, Vararg{Any}}) where N = (first(strds)*step(I[1]), substrides(tail(strds), tail(I))...)
125,059✔
366
substrides(strds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("strides is invalid for SubArrays with indices of type $(typeof(I[1]))"))
×
367

368
stride(V::SubArray, d::Integer) = d <= ndims(V) ? strides(V)[d] : strides(V)[end] * size(V)[end]
225,778✔
369

370
compute_stride1(parent::AbstractArray, I::NTuple{N,Any}) where {N} =
870,662✔
371
    (@inline; compute_stride1(1, fill_to_length(axes(parent), OneTo(1), Val(N)), I))
288,185✔
372
compute_stride1(s, inds, I::Tuple{}) = s
1✔
373
compute_stride1(s, inds, I::Tuple{Vararg{ScalarIndex}}) = s
49✔
374
compute_stride1(s, inds, I::Tuple{ScalarIndex, Vararg{Any}}) =
5,039✔
375
    (@inline; compute_stride1(s*length(inds[1]), tail(inds), tail(I)))
5,039✔
376
compute_stride1(s, inds, I::Tuple{AbstractRange, Vararg{Any}}) = s*step(I[1])
221,883✔
377
compute_stride1(s, inds, I::Tuple{Slice, Vararg{Any}}) = s
55,650✔
378
compute_stride1(s, inds, I::Tuple{Any, Vararg{Any}}) = throw(ArgumentError("invalid strided index type $(typeof(I[1]))"))
×
379

380
elsize(::Type{<:SubArray{<:Any,<:Any,P}}) where {P} = elsize(P)
27,622✔
381

382
iscontiguous(A::SubArray) = iscontiguous(typeof(A))
×
383
iscontiguous(::Type{<:SubArray}) = false
×
384
iscontiguous(::Type{<:FastContiguousSubArray}) = true
×
385

386
first_index(V::FastSubArray) = V.offset1 + V.stride1 # cached for fast linear SubArrays
×
387
function first_index(V::SubArray)
×
388
    P, I = parent(V), V.indices
×
389
    s1 = compute_stride1(P, I)
×
390
    s1 + compute_offset1(P, s1, I)
×
391
end
392

393
# Computing the first index simply steps through the indices, accumulating the
394
# sum of index each multiplied by the parent's stride.
395
# The running sum is `f`; the cumulative stride product is `s`.
396
# If the parent is a vector, then we offset the parent's own indices with parameters of I
397
compute_offset1(parent::AbstractVector, stride1::Integer, I::Tuple{AbstractRange}) =
723,861✔
398
    (@inline; first(I[1]) - stride1*first(axes1(I[1])))
2,767,949✔
399
# If the result is one-dimensional and it's a Colon, then linear
400
# indexing uses the indices along the given dimension.
401
# If the result is one-dimensional and it's a range, then linear
402
# indexing might be offset if the index itself is offset
403
# Otherwise linear indexing always matches the parent.
404
compute_offset1(parent, stride1::Integer, I::Tuple) =
146,801✔
405
    (@inline; compute_offset1(parent, stride1, find_extended_dims(1, I...), find_extended_inds(I...), I))
146,801✔
406
compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{Slice}, I::Tuple) =
55,068✔
407
    (@inline; compute_linindex(parent, I) - stride1*first(axes(parent, dims[1])))  # index-preserving case
55,068✔
408
compute_offset1(parent, stride1::Integer, dims, inds::Tuple{AbstractRange}, I::Tuple) =
89,945✔
409
    (@inline; compute_linindex(parent, I) - stride1*first(axes1(inds[1]))) # potentially index-offsetting case
89,945✔
410
compute_offset1(parent, stride1::Integer, dims, inds, I::Tuple) =
1,788✔
411
    (@inline; compute_linindex(parent, I) - stride1)
1,788✔
412
function compute_linindex(parent, I::NTuple{N,Any}) where N
146,801✔
413
    @inline
146,801✔
414
    IP = fill_to_length(axes(parent), OneTo(1), Val(N))
146,801✔
415
    compute_linindex(first(LinearIndices(parent)), 1, IP, I)
146,801✔
416
end
417
function compute_linindex(f, s, IP::Tuple, I::Tuple{ScalarIndex, Vararg{Any}})
150,027✔
418
    @inline
150,027✔
419
    Δi = I[1]-first(IP[1])
150,027✔
420
    compute_linindex(f + Δi*s, s*length(IP[1]), tail(IP), tail(I))
150,027✔
421
end
422
function compute_linindex(f, s, IP::Tuple, I::Tuple{Any, Vararg{Any}})
149,570✔
423
    @inline
149,570✔
424
    Δi = first(I[1])-first(IP[1])
149,570✔
425
    compute_linindex(f + Δi*s, s*length(IP[1]), tail(IP), tail(I))
149,570✔
426
end
427
compute_linindex(f, s, IP::Tuple, I::Tuple{}) = f
146,801✔
428

429
find_extended_dims(dim, ::ScalarIndex, I...) = (@inline; find_extended_dims(dim + 1, I...))
150,027✔
430
find_extended_dims(dim, i1, I...) = (@inline; (dim, find_extended_dims(dim + 1, I...)...))
149,570✔
431
find_extended_dims(dim) = ()
146,801✔
432
find_extended_inds(::ScalarIndex, I...) = (@inline; find_extended_inds(I...))
150,027✔
433
find_extended_inds(i1, I...) = (@inline; (i1, find_extended_inds(I...)...))
149,570✔
434
find_extended_inds() = ()
146,801✔
435

436
function unsafe_convert(::Type{Ptr{T}}, V::SubArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) where {T,N,P}
152,638✔
437
    return unsafe_convert(Ptr{T}, V.parent) + _memory_offset(V.parent, map(first, V.indices)...)
354,963✔
438
end
439

440
pointer(V::FastSubArray, i::Int) = pointer(V.parent, V.offset1 + V.stride1*i)
×
441
pointer(V::FastContiguousSubArray, i::Int) = pointer(V.parent, V.offset1 + i)
575,079✔
442

443
function pointer(V::SubArray{<:Any,<:Any,<:Array,<:Tuple{Vararg{RangeIndex}}}, is::AbstractCartesianIndex{N}) where {N}
×
444
    index = first_index(V)
×
445
    strds = strides(V)
×
446
    for d = 1:N
×
447
        index += (is[d]-1)*strds[d]
×
448
    end
×
449
    return pointer(V.parent, index)
×
450
end
451

452
# indices are taken from the range/vector
453
# Since bounds-checking is performance-critical and uses
454
# indices, it's worth optimizing these implementations thoroughly
455
axes(S::SubArray) = (@inline; _indices_sub(S.indices...))
108,260,111✔
456
_indices_sub(::Real, I...) = (@inline; _indices_sub(I...))
10,258,445✔
457
_indices_sub() = ()
×
458
function _indices_sub(i1::AbstractArray, I...)
41,106,034✔
459
    @inline
19,597,588✔
460
    (axes(i1)..., _indices_sub(I...)...)
119,615,518✔
461
end
462

463
has_offset_axes(S::SubArray) = has_offset_axes(S.indices...)
155,812✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc