Skip to content

Commit c5d58dd

Browse files
committed
Use view instead of getindex; set up for constant prop type stability
1 parent b5cbcab commit c5d58dd

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,9 @@ Deprecated or removed
646646
* Using Bool values directly as indices is now deprecated and will be an error in the future. Convert
647647
them to `Int` before indexing if you intend to access index `1` for `true` and `0` for `false`.
648648

649-
* `slicedim` has been deprecated in favor of `selectdim` ([#26009]).
649+
* `slicedim(A, d, i)` has been deprecated in favor of `copy(selectdim(A, d, i)`. The new
650+
`selectdim` function now always returns a view into `A`; in many cases the `copy` is
651+
not necessary ([#26009]).
650652

651653
* `whos` has been renamed `varinfo`, and now returns a markdown table instead of printing
652654
output ([#12131]).

base/abstractarraymath.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ imag(x::AbstractArray{<:Real}) = zero(x)
100100
"""
101101
selectdim(A, d::Integer, i)
102102
103-
Return all the data of `A` where the index for dimension `d` equals `i`. Equivalent to
104-
`A[:,:,...,i,:,:,...]` where `i` is in position `d`.
103+
Return a view of all the data of `A` where the index for dimension `d` equals `i`.
104+
105+
Equivalent to `view(A,:,:,...,i,:,:,...)` where `i` is in position `d`.
105106
106107
# Examples
107108
```jldoctest
@@ -110,17 +111,18 @@ julia> A = [1 2 3 4; 5 6 7 8]
110111
1 2 3 4
111112
5 6 7 8
112113
113-
julia> selectdim(A,2,3)
114-
2-element Array{Int64,1}:
114+
julia> selectdim(A, 2, 3)
115+
2-element view(::Array{Int64,2}, Base.OneTo(2), 3) with eltype Int64:
115116
3
116117
7
117118
```
118119
"""
119-
function selectdim(A::AbstractArray, d::Integer, i)
120+
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, setindex(axes(A), i, d))
121+
@noinline function _selectdim(A, d, i, idxs)
120122
d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
121123
nd = ndims(A)
122-
d > nd && (i == 1 || throw_boundserror(A, (ntuple(k->Colon(),nd)..., ntuple(k->1,d-1-nd)..., i)))
123-
A[setindex(axes(A), i, d)...]
124+
d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i))))
125+
return view(A, idxs...)
124126
end
125127

126128
"""

base/deprecated.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,18 @@ end
13601360
# issue #25928
13611361
@deprecate wait(t::Task) fetch(t)
13621362

1363-
# PR #26008
1364-
@deprecate slicedim(A::AbstractArray, d::Integer, i) selectdim(A, d, i)
1363+
# issue #18326
1364+
@deprecate slicedim(A::AbstractArray, d::Integer, i) copy(selectdim(A, d, i))
1365+
function slicedim(A::AbstractVector, d::Integer, i::Number)
1366+
if d == 1
1367+
# slicedim would have returned a scalar value, selectdim always returns views
1368+
depwarn("`slicedim(A::AbstractVector, d::Integer, i)` is deprecated, use `selectdim(A, d, i)[]` instead.", :slicedim)
1369+
selectdim(A, d, i)[]
1370+
else
1371+
depwarn("`slicedim(A::AbstractArray, d::Integer, i)` is deprecated, use `copy(selectdim(A, d, i))` instead.", :slicedim)
1372+
copy(selectdim(A, d, i))
1373+
end
1374+
end
13651375

13661376
# PR 25062
13671377
@deprecate(link_pipe(pipe; julia_only_read = true, julia_only_write = true),

0 commit comments

Comments
 (0)