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 7b42c00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 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
21 changes: 17 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro deprecate(old, new, ex=true)
newname = Expr(:quote, new)
Expr(:toplevel,
ex ? Expr(:export, esc(old)) : nothing,
:(function $(esc(old))(args...)
:(@__doc__ function $(esc(old))(args...)
$meta
depwarn($"`$old` is deprecated, use `$new` instead.", $oldmtname)
$(esc(new))(args...)
Expand All @@ -51,7 +51,7 @@ macro deprecate(old, new, ex=true)
end
Expr(:toplevel,
ex ? Expr(:export, esc(oldsym)) : nothing,
:($(esc(old)) = begin
:($(@__doc__ esc(old)) = begin
$meta
depwarn($"`$oldcall` is deprecated, use `$newcall` instead.", $oldmtname)
$(esc(new))
Expand Down Expand Up @@ -1360,8 +1360,21 @@ end
# issue #25928
@deprecate wait(t::Task) fetch(t)

# PR #26008
@deprecate slicedim(A::AbstractArray, d::Integer, i) selectdim(A, d, i)
"
`slicedim` is deprecated in favor of `selectdim`. Whereas `slicedim` previously
used `getindex` to select its data, `selectdim` now always uses `view`.
"
@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 7b42c00

Please sign in to comment.