Skip to content

Commit

Permalink
Use view instead of getindex; set up for constant prop type stability
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Feb 13, 2018
1 parent b5cbcab commit c5d58dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ Deprecated or removed
* Using Bool values directly as indices is now deprecated and will be an error in the future. Convert
them to `Int` before indexing if you intend to access index `1` for `true` and `0` for `false`.

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

* `whos` has been renamed `varinfo`, and now returns a markdown table instead of printing
output ([#12131]).
Expand Down
16 changes: 9 additions & 7 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ imag(x::AbstractArray{<:Real}) = zero(x)
"""
selectdim(A, d::Integer, i)
Return all the data of `A` where the index for dimension `d` equals `i`. Equivalent to
`A[:,:,...,i,:,:,...]` where `i` is in position `d`.
Return a view of all the data of `A` where the index for dimension `d` equals `i`.
Equivalent to `view(A,:,:,...,i,:,:,...)` where `i` is in position `d`.
# Examples
```jldoctest
Expand All @@ -110,17 +111,18 @@ julia> A = [1 2 3 4; 5 6 7 8]
1 2 3 4
5 6 7 8
julia> selectdim(A,2,3)
2-element Array{Int64,1}:
julia> selectdim(A, 2, 3)
2-element view(::Array{Int64,2}, Base.OneTo(2), 3) with eltype Int64:
3
7
```
"""
function selectdim(A::AbstractArray, d::Integer, i)
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, setindex(axes(A), i, d))
@noinline function _selectdim(A, d, i, idxs)
d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
nd = ndims(A)
d > nd && (i == 1 || throw_boundserror(A, (ntuple(k->Colon(),nd)..., ntuple(k->1,d-1-nd)..., i)))
A[setindex(axes(A), i, d)...]
d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i))))
return view(A, idxs...)
end

"""
Expand Down
14 changes: 12 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1360,8 +1360,18 @@ end
# issue #25928
@deprecate wait(t::Task) fetch(t)

# PR #26008
@deprecate slicedim(A::AbstractArray, d::Integer, i) selectdim(A, d, i)
# issue #18326
@deprecate slicedim(A::AbstractArray, d::Integer, i) copy(selectdim(A, d, i))
function slicedim(A::AbstractVector, d::Integer, i::Number)
if d == 1
# slicedim would have returned a scalar value, selectdim always returns views
depwarn("`slicedim(A::AbstractVector, d::Integer, i)` is deprecated, use `selectdim(A, d, i)[]` instead.", :slicedim)
selectdim(A, d, i)[]
else
depwarn("`slicedim(A::AbstractArray, d::Integer, i)` is deprecated, use `copy(selectdim(A, d, i))` instead.", :slicedim)
copy(selectdim(A, d, i))
end
end

# PR 25062
@deprecate(link_pipe(pipe; julia_only_read = true, julia_only_write = true),
Expand Down

0 comments on commit c5d58dd

Please sign in to comment.