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

JuliaLang / julia / #37798

05 Jun 2024 07:57AM UTC coverage: 83.152% (-3.8%) from 86.908%
#37798

push

local

web-flow
Use the public `wait()` in the `errormonitor()` docstring (#54650)

72847 of 87607 relevant lines covered (83.15%)

14515294.14 hits per line

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

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

3
using  Base.MultiplicativeInverses: SignedMultiplicativeInverse
4

5
struct ReshapedArray{T,N,P<:AbstractArray,MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int}}}} <: AbstractArray{T,N}
6
    parent::P
125,188✔
7
    dims::NTuple{N,Int}
8
    mi::MI
9
end
10
ReshapedArray(parent::AbstractArray{T}, dims::NTuple{N,Int}, mi) where {T,N} = ReshapedArray{T,N,typeof(parent),typeof(mi)}(parent, dims, mi)
125,184✔
11

12
# IndexLinear ReshapedArray
13
const ReshapedArrayLF{T,N,P<:AbstractArray} = ReshapedArray{T,N,P,Tuple{}}
14

15
# Fast iteration on ReshapedArrays: use the parent iterator
16
struct ReshapedArrayIterator{I,M}
17
    iter::I
18
    mi::NTuple{M,SignedMultiplicativeInverse{Int}}
19
end
20
ReshapedArrayIterator(A::ReshapedArray) = _rs_iterator(parent(A), A.mi)
×
21
function _rs_iterator(P, mi::NTuple{M}) where M
×
22
    iter = eachindex(P)
×
23
    ReshapedArrayIterator{typeof(iter),M}(iter, mi)
×
24
end
25

26
struct ReshapedIndex{T}
27
    parentindex::T
28
end
29

30
# eachindex(A::ReshapedArray) = ReshapedArrayIterator(A)  # TODO: uncomment this line
31
@inline function iterate(R::ReshapedArrayIterator, i...)
×
32
    item, inext = iterate(R.iter, i...)
×
33
    ReshapedIndex(item), inext
×
34
end
35
length(R::ReshapedArrayIterator) = length(R.iter)
×
36
eltype(::Type{<:ReshapedArrayIterator{I}}) where {I} = @isdefined(I) ? ReshapedIndex{eltype(I)} : Any
×
37

38
## reshape(::Array, ::Dims) returns an Array, except for isbitsunion eltypes (issue #28611)
39
# reshaping to same # of dimensions
40
@eval function reshape(a::Array{T,M}, dims::NTuple{N,Int}) where {T,N,M}
3,638✔
41
    throw_dmrsa(dims, len) =
36,470✔
42
        throw(DimensionMismatch("new dimensions $(dims) must be consistent with array length $len"))
43
    len = Core.checked_dims(dims...) # make sure prod(dims) doesn't overflow (and because of the comparison to length(a))
36,470✔
44
    if len != length(a)
36,470✔
45
        throw_dmrsa(dims, length(a))
×
46
    end
47
    isbitsunion(T) && return ReshapedArray(a, dims, ())
36,470✔
48
    if N == M && dims == size(a)
40,947✔
49
        return a
4,584✔
50
    end
51
    ref = a.ref
31,836✔
52
    if M == 1 && N !== 1
31,836✔
53
        mem = ref.mem::Memory{T}
5,573✔
54
        if !(ref === GenericMemoryRef(mem) && len === mem.length)
5,573✔
55
            mem = ccall(:jl_genericmemory_slice, Memory{T}, (Any, Ptr{Cvoid}, Int), mem, ref.ptr_or_offset, len)
8✔
56
            ref = GenericMemoryRef(mem)::typeof(ref)
8✔
57
        end
58
    end
59
    # or we could use `a = Array{T,N}(undef, ntuple(0, Val(N))); a.ref = ref; a.size = dims; return a` here
60
    return $(Expr(:new, :(Array{T,N}), :ref, :dims))
31,836✔
61
end
62

63

64
"""
65
    reshape(A, dims...) -> AbstractArray
66
    reshape(A, dims) -> AbstractArray
67

68
Return an array with the same data as `A`, but with different
69
dimension sizes or number of dimensions. The two arrays share the same
70
underlying data, so that the result is mutable if and only if `A` is
71
mutable, and setting elements of one alters the values of the other.
72

73
The new dimensions may be specified either as a list of arguments or
74
as a shape tuple. At most one dimension may be specified with a `:`,
75
in which case its length is computed such that its product with all
76
the specified dimensions is equal to the length of the original array
77
`A`. The total number of elements must not change.
78

79
# Examples
80
```jldoctest
81
julia> A = Vector(1:16)
82
16-element Vector{Int64}:
83
  1
84
  2
85
  3
86
  4
87
  5
88
  6
89
  7
90
  8
91
  9
92
 10
93
 11
94
 12
95
 13
96
 14
97
 15
98
 16
99

100
julia> reshape(A, (4, 4))
101
4×4 Matrix{Int64}:
102
 1  5   9  13
103
 2  6  10  14
104
 3  7  11  15
105
 4  8  12  16
106

107
julia> reshape(A, 2, :)
108
2×8 Matrix{Int64}:
109
 1  3  5  7   9  11  13  15
110
 2  4  6  8  10  12  14  16
111

112
julia> reshape(1:6, 2, 3)
113
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
114
 1  3  5
115
 2  4  6
116
```
117
"""
118
reshape
119

120
reshape(parent::AbstractArray, dims::IntOrInd...) = reshape(parent, dims)
1✔
121
reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, Vararg{Union{Integer,OneTo}}}) = reshape(parent, to_shape(shp))
22,043✔
122
reshape(parent::AbstractArray, dims::Dims)        = _reshape(parent, dims)
7,276✔
123

124
# Allow missing dimensions with Colon():
125
reshape(parent::AbstractVector, ::Colon) = parent
2✔
126
reshape(parent::AbstractVector, ::Tuple{Colon}) = parent
2✔
127
reshape(parent::AbstractArray, dims::Int...) = reshape(parent, dims)
14,973✔
128
reshape(parent::AbstractArray, dims::Union{Int,Colon}...) = reshape(parent, dims)
71✔
129
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = reshape(parent, _reshape_uncolon(parent, dims))
102✔
130
@inline function _reshape_uncolon(A, dims)
131
    @noinline throw1(dims) = throw(DimensionMismatch(string("new dimensions $(dims) ",
100✔
132
        "may have at most one omitted dimension specified by `Colon()`")))
133
    @noinline throw2(A, dims) = throw(DimensionMismatch(string("array size $(length(A)) ",
101✔
134
        "must be divisible by the product of the new dimensions $dims")))
135
    pre = _before_colon(dims...)::Tuple{Vararg{Int}}
100✔
136
    post = _after_colon(dims...)
100✔
137
    _any_colon(post...) && throw1(dims)
100✔
138
    post::Tuple{Vararg{Int}}
100✔
139
    len = length(A)
100✔
140
    sz, is_exact = if iszero(len)
100✔
141
        (0, true)
10✔
142
    else
143
        let pr = Core.checked_dims(pre..., post...)  # safe product
90✔
144
            if iszero(pr)
89✔
145
                throw2(A, dims)
1✔
146
            end
147
            (quo, rem) = divrem(len, pr)
88✔
148
            (Int(quo), iszero(rem))
185✔
149
        end
150
    end::Tuple{Int,Bool}
151
    is_exact || throw2(A, dims)
98✔
152
    (pre..., sz, post...)::Tuple{Int,Vararg{Int}}
98✔
153
end
154
@inline _any_colon() = false
×
155
@inline _any_colon(dim::Colon, tail...) = true
×
156
@inline _any_colon(dim::Any, tail...) = _any_colon(tail...)
49✔
157
@inline _before_colon(dim::Any, tail...) = (dim, _before_colon(tail...)...)
33✔
158
@inline _before_colon(dim::Colon, tail...) = ()
100✔
159
@inline _after_colon(dim::Any, tail...) =  _after_colon(tail...)
33✔
160
@inline _after_colon(dim::Colon, tail...) = tail
100✔
161

162
reshape(parent::AbstractArray{T,N}, ndims::Val{N}) where {T,N} = parent
406,864✔
163
function reshape(parent::AbstractArray, ndims::Val{N}) where N
19✔
164
    reshape(parent, rdims(Val(N), axes(parent)))
21,975✔
165
end
166

167
# Move elements from inds to out until out reaches the desired
168
# dimensionality N, either filling with OneTo(1) or collapsing the
169
# product of trailing dims into the last element
170
rdims_trailing(l, inds...) = length(l) * rdims_trailing(inds...)
21,583✔
171
rdims_trailing(l) = length(l)
21,469✔
172
rdims(out::Val{N}, inds::Tuple) where {N} = rdims(ntuple(Returns(OneTo(1)), Val(N)), inds)
21,975✔
173
rdims(out::Tuple{}, inds::Tuple{}) = () # N == 0, M == 0
×
174
rdims(out::Tuple{}, inds::Tuple{Any}) = ()
×
175
rdims(out::Tuple{}, inds::NTuple{M,Any}) where {M} = ()
×
176
rdims(out::Tuple{Any}, inds::Tuple{}) = out # N == 1, M == 0
498✔
177
rdims(out::NTuple{N,Any}, inds::Tuple{}) where {N} = out # N > 1, M == 0
8✔
178
rdims(out::Tuple{Any}, inds::Tuple{Any}) = inds # N == 1, M == 1
×
179
rdims(out::Tuple{Any}, inds::NTuple{M,Any}) where {M} = (oneto(rdims_trailing(inds...)),) # N == 1, M > 1
21,469✔
180
rdims(out::NTuple{N,Any}, inds::NTuple{N,Any}) where {N} = inds # N > 1, M == N
×
181
rdims(out::NTuple{N,Any}, inds::NTuple{M,Any}) where {N,M} = (first(inds), rdims(tail(out), tail(inds))...) # N > 1, M > 1, M != N
542✔
182

183

184
# _reshape on Array returns an Array
185
_reshape(parent::Vector, dims::Dims{1}) = parent
×
186
_reshape(parent::Array, dims::Dims{1}) = reshape(parent, dims)
×
187
_reshape(parent::Array, dims::Dims) = reshape(parent, dims)
×
188

189
# When reshaping Vector->Vector, don't wrap with a ReshapedArray
190
function _reshape(v::AbstractVector, dims::Dims{1})
191
    require_one_based_indexing(v)
35✔
192
    len = dims[1]
35✔
193
    len == length(v) || _throw_dmrs(length(v), "length", len)
35✔
194
    v
35✔
195
end
196
# General reshape
197
function _reshape(parent::AbstractArray, dims::Dims)
43✔
198
    n = length(parent)
7,236✔
199
    prod(dims) == n || _throw_dmrs(n, "size", dims)
7,247✔
200
    __reshape((parent, IndexStyle(parent)), dims)
7,225✔
201
end
202

203
@noinline function _throw_dmrs(n, str, dims)
11✔
204
    throw(DimensionMismatch("parent has $n elements, which is incompatible with $str $dims"))
11✔
205
end
206

207
# Reshaping a ReshapedArray
208
_reshape(v::ReshapedArray{<:Any,1}, dims::Dims{1}) = _reshape(v.parent, dims)
×
209
_reshape(R::ReshapedArray, dims::Dims) = _reshape(R.parent, dims)
30✔
210

211
function __reshape(p::Tuple{AbstractArray,IndexStyle}, dims::Dims)
31✔
212
    parent = p[1]
1,998✔
213
    strds = front(size_to_strides(map(length, axes(parent))..., 1))
1,998✔
214
    strds1 = map(s->max(1,Int(s)), strds)  # for resizing empty arrays
3,581✔
215
    mi = map(SignedMultiplicativeInverse, strds1)
1,998✔
216
    ReshapedArray(parent, dims, reverse(mi))
1,998✔
217
end
218

219
function __reshape(p::Tuple{AbstractArray{<:Any,0},IndexCartesian}, dims::Dims)
220
    parent = p[1]
1✔
221
    ReshapedArray(parent, dims, ())
1✔
222
end
223

224
function __reshape(p::Tuple{AbstractArray,IndexLinear}, dims::Dims)
225
    parent = p[1]
5,226✔
226
    ReshapedArray(parent, dims, ())
5,226✔
227
end
228

229
size(A::ReshapedArray) = A.dims
1,994,398✔
230
length(A::ReshapedArray) = length(parent(A))
8,081,137✔
231
similar(A::ReshapedArray, eltype::Type, dims::Dims) = similar(parent(A), eltype, dims)
1,392✔
232
IndexStyle(::Type{<:ReshapedArrayLF}) = IndexLinear()
23,400✔
233
parent(A::ReshapedArray) = A.parent
16,797,740✔
234
parentindices(A::ReshapedArray) = map(oneto, size(parent(A)))
×
235
reinterpret(::Type{T}, A::ReshapedArray, dims::Dims) where {T} = reinterpret(T, parent(A), dims)
×
236
elsize(::Type{<:ReshapedArray{<:Any,<:Any,P}}) where {P} = elsize(P)
6,836✔
237

238
unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi)
4✔
239
dataids(A::ReshapedArray) = dataids(A.parent)
5,150✔
240
# forward the aliasing check the parent in case there are specializations
241
mightalias(A::ReshapedArray, B::ReshapedArray) = mightalias(parent(A), parent(B))
49✔
242
# special handling for reshaped SubArrays that dispatches to the subarray aliasing check
243
mightalias(A::ReshapedArray, B::SubArray) = mightalias(parent(A), B)
23✔
244
mightalias(A::SubArray, B::ReshapedArray) = mightalias(A, parent(B))
3,102✔
245

246
@inline ind2sub_rs(ax, ::Tuple{}, i::Int) = (i,)
94,660✔
247
@inline ind2sub_rs(ax, strds, i) = _ind2sub_rs(ax, strds, i - 1)
696,133✔
248
@inline _ind2sub_rs(ax, ::Tuple{}, ind) = (ind + first(ax[end]),)
696,133✔
249
@inline function _ind2sub_rs(ax, strds, ind)
250
    d, r = divrem(ind, strds[1])
1,259,879✔
251
    (_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
1,259,879✔
252
end
253
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
68,062✔
254
offset_if_vec(i::Integer, axs::Tuple) = i
593,095✔
255

256
@inline function isassigned(A::ReshapedArrayLF, index::Int)
×
257
    @boundscheck checkbounds(Bool, A, index) || return false
×
258
    indexparent = index - firstindex(A) + firstindex(parent(A))
×
259
    @inbounds ret = isassigned(parent(A), indexparent)
×
260
    ret
×
261
end
262
@inline function isassigned(A::ReshapedArray{T,N}, indices::Vararg{Int, N}) where {T,N}
14,132✔
263
    @boundscheck checkbounds(Bool, A, indices...) || return false
14,132✔
264
    axp = axes(A.parent)
14,132✔
265
    i = offset_if_vec(_sub2ind(size(A), indices...), axp)
14,132✔
266
    I = ind2sub_rs(axp, A.mi, i)
14,132✔
267
    @inbounds isassigned(A.parent, I...)
14,132✔
268
end
269

270
@inline function getindex(A::ReshapedArrayLF, index::Int)
5✔
271
    @boundscheck checkbounds(A, index)
4,027,813✔
272
    indexparent = index - firstindex(A) + firstindex(parent(A))
4,027,813✔
273
    @inbounds ret = parent(A)[indexparent]
4,028,013✔
274
    ret
4,027,813✔
275
end
276
@inline function getindex(A::ReshapedArray{T,N}, indices::Vararg{Int,N}) where {T,N}
14,146✔
277
    @boundscheck checkbounds(A, indices...)
645,219✔
278
    _unsafe_getindex(A, indices...)
646,635✔
279
end
280
@inline function getindex(A::ReshapedArray, index::ReshapedIndex)
×
281
    @boundscheck checkbounds(parent(A), index.parentindex)
×
282
    @inbounds ret = parent(A)[index.parentindex]
×
283
    ret
×
284
end
285

286
@inline function _unsafe_getindex(A::ReshapedArray{T,N}, indices::Vararg{Int,N}) where {T,N}
16✔
287
    axp = axes(A.parent)
645,215✔
288
    i = offset_if_vec(_sub2ind(size(A), indices...), axp)
645,215✔
289
    I = ind2sub_rs(axp, A.mi, i)
645,215✔
290
    _unsafe_getindex_rs(parent(A), I)
646,635✔
291
end
292
@inline _unsafe_getindex_rs(A, i::Integer) = (@inbounds ret = A[i]; ret)
×
293
@inline _unsafe_getindex_rs(A, I) = (@inbounds ret = A[I...]; ret)
646,635✔
294

295
@inline function setindex!(A::ReshapedArrayLF, val, index::Int)
1✔
296
    @boundscheck checkbounds(A, index)
261✔
297
    indexparent = index - firstindex(A) + firstindex(parent(A))
261✔
298
    @inbounds parent(A)[indexparent] = val
261✔
299
    val
261✔
300
end
301
@inline function setindex!(A::ReshapedArray{T,N}, val, indices::Vararg{Int,N}) where {T,N}
1✔
302
    @boundscheck checkbounds(A, indices...)
1,810✔
303
    _unsafe_setindex!(A, val, indices...)
1,810✔
304
end
305
@inline function setindex!(A::ReshapedArray, val, index::ReshapedIndex)
×
306
    @boundscheck checkbounds(parent(A), index.parentindex)
×
307
    @inbounds parent(A)[index.parentindex] = val
×
308
    val
×
309
end
310

311
@inline function _unsafe_setindex!(A::ReshapedArray{T,N}, val, indices::Vararg{Int,N}) where {T,N}
312
    axp = axes(A.parent)
1,810✔
313
    i = offset_if_vec(_sub2ind(size(A), indices...), axp)
1,810✔
314
    @inbounds parent(A)[ind2sub_rs(axes(A.parent), A.mi, i)...] = val
1,810✔
315
    val
1,810✔
316
end
317

318
# helpful error message for a common failure case
319
const ReshapedRange{T,N,A<:AbstractRange} = ReshapedArray{T,N,A,Tuple{}}
320
setindex!(A::ReshapedRange, val, index::Int) = _rs_setindex!_err()
×
321
setindex!(A::ReshapedRange{T,N}, val, indices::Vararg{Int,N}) where {T,N} = _rs_setindex!_err()
×
322
setindex!(A::ReshapedRange, val, index::ReshapedIndex) = _rs_setindex!_err()
×
323

324
@noinline _rs_setindex!_err() = error("indexed assignment fails for a reshaped range; consider calling collect")
×
325

326
cconvert(::Type{Ptr{T}}, a::ReshapedArray{T}) where {T} = cconvert(Ptr{T}, parent(a))
6,776✔
327

328
# Add a few handy specializations to further speed up views of reshaped ranges
329
const ReshapedUnitRange{T,N,A<:AbstractUnitRange} = ReshapedArray{T,N,A,Tuple{}}
330
viewindexing(I::Tuple{Slice, ReshapedUnitRange, Vararg{ScalarIndex}}) = IndexLinear()
2✔
331
viewindexing(I::Tuple{ReshapedRange, Vararg{ScalarIndex}}) = IndexLinear()
5✔
332
compute_stride1(s, inds, I::Tuple{ReshapedRange, Vararg{Any}}) = s*step(I[1].parent)
5✔
333
compute_offset1(parent::AbstractVector, stride1::Integer, I::Tuple{ReshapedRange}) =
334
    (@inline; first(I[1]) - first(axes1(I[1]))*stride1)
1✔
335
substrides(strds::NTuple{N,Int}, I::Tuple{ReshapedUnitRange, Vararg{Any}}) where N =
197✔
336
    (size_to_strides(strds[1], size(I[1])...)..., substrides(tail(strds), tail(I))...)
337

338
# cconvert(::Type{<:Ptr}, V::SubArray{T,N,P,<:Tuple{Vararg{Union{RangeIndex,ReshapedUnitRange}}}}) where {T,N,P} = V
339
function unsafe_convert(::Type{Ptr{S}}, V::SubArray{T,N,P,<:Tuple{Vararg{Union{RangeIndex,ReshapedUnitRange}}}}) where {S,T,N,P}
340
    parent = V.parent
18,288,864✔
341
    p = cconvert(Ptr{T}, parent) # XXX: this should occur in cconvert, the result is not GC-rooted
19,136,140✔
342
    Δmem = if _checkcontiguous(Bool, parent)
18,285,831✔
343
        (first_index(V) - firstindex(parent)) * elsize(parent)
18,321,317✔
344
    else
345
        _memory_offset(parent, map(first, V.indices)...)
25,572✔
346
    end
347
    return Ptr{S}(unsafe_convert(Ptr{T}, p) + Δmem)
19,136,140✔
348
end
349

350
_checkcontiguous(::Type{Bool}, A::AbstractArray) = false
×
351
# `strides(A::DenseArray)` calls `size_to_strides` by default.
352
# Thus it's OK to assume all `DenseArray`s are contiguously stored.
353
_checkcontiguous(::Type{Bool}, A::DenseArray) = true
×
354
_checkcontiguous(::Type{Bool}, A::ReshapedArray) = _checkcontiguous(Bool, parent(A))
1,905✔
355
_checkcontiguous(::Type{Bool}, A::FastContiguousSubArray) = _checkcontiguous(Bool, parent(A))
17,736,758✔
356

357
function strides(a::ReshapedArray)
616✔
358
    _checkcontiguous(Bool, a) && return size_to_strides(1, size(a)...)
1,696✔
359
    apsz::Dims = size(a.parent)
1,674✔
360
    apst::Dims = strides(a.parent)
1,674✔
361
    msz, mst, n = merge_adjacent_dim(apsz, apst) # Try to perform "lazy" reshape
1,674✔
362
    n == ndims(a.parent) && return size_to_strides(mst, size(a)...) # Parent is stridevector like
1,674✔
363
    return _reshaped_strides(size(a), 1, msz, mst, n, apsz, apst)
581✔
364
end
365

366
function _reshaped_strides(::Dims{0}, reshaped::Int, msz::Int, ::Int, ::Int, ::Dims, ::Dims)
367
    reshaped == msz && return ()
581✔
368
    throw(ArgumentError("Input is not strided."))
5✔
369
end
370
function _reshaped_strides(sz::Dims, reshaped::Int, msz::Int, mst::Int, n::Int, apsz::Dims, apst::Dims)
976✔
371
    st = reshaped * mst
2,924✔
372
    reshaped = reshaped * sz[1]
2,924✔
373
    if length(sz) > 1 && reshaped == msz && sz[2] != 1
2,924✔
374
        msz, mst, n = merge_adjacent_dim(apsz, apst, n + 1)
576✔
375
        reshaped = 1
576✔
376
    end
377
    sts = _reshaped_strides(tail(sz), reshaped, msz, mst, n, apsz, apst)
2,937✔
378
    return (st, sts...)
2,908✔
379
end
380

381
merge_adjacent_dim(::Dims{0}, ::Dims{0}) = 1, 1, 0
1✔
382
merge_adjacent_dim(apsz::Dims{1}, apst::Dims{1}) = apsz[1], apst[1], 1
3,381✔
383
function merge_adjacent_dim(apsz::Dims{N}, apst::Dims{N}, n::Int = 1) where {N}
384
    sz, st = apsz[n], apst[n]
2,412✔
385
    while n < N
1,848✔
386
        szₙ, stₙ = apsz[n+1], apst[n+1]
942✔
387
        if sz == 1
942✔
388
            sz, st = szₙ, stₙ
3✔
389
        elseif stₙ == st * sz || szₙ == 1
1,544✔
390
            sz *= szₙ
351✔
391
        else
392
            break
588✔
393
        end
394
        n += 1
354✔
395
    end
354✔
396
    return sz, st, n
1,494✔
397
end
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