Skip to content

Commit

Permalink
docstring setindex (#32738)
Browse files Browse the repository at this point in the history
* docstring setindex

* add bound check

* fix selectdim implementation
  • Loading branch information
matbesancon authored and mbauman committed Oct 2, 2019
1 parent 32e3c9e commit b6991f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ julia> selectdim(A, 2, 3)
7
```
"""
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, setindex(map(Slice, axes(A)), i, d))
@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, _setindex(i, d, map(Slice, axes(A))...))
@noinline function _selectdim(A, d, i, idxs)
d >= 1 || throw(ArgumentError("dimension must be ≥ 1, got $d"))
nd = ndims(A)
Expand Down
20 changes: 19 additions & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,25 @@ getindex(t::Tuple, b::AbstractArray{Bool,1}) = length(b) == length(t) ? getindex
getindex(t::Tuple, c::Colon) = t

# returns new tuple; N.B.: becomes no-op if i is out-of-bounds
setindex(x::Tuple, v, i::Integer) = (@_inline_meta; _setindex(v, i, x...))

"""
setindex(c::Tuple, v, i::Integer)
Creates a new tuple similar to `x` with the value at index `i` set to `v`.
Throws a `BoundsError` when out of bounds.
# Examples
```jldoctest
julia> Base.setindex((1, 2, 6), 2, 3) == (1, 2, 2)
true
```
"""
function setindex(x::Tuple, v, i::Integer)
@boundscheck 1 <= i <= length(x) || throw(BoundsError(x, i))
@_inline_meta
_setindex(v, i, x...)
end

function _setindex(v, i::Integer, first, tail...)
@_inline_meta
return (ifelse(i == 1, v, first), _setindex(v, i - 1, tail...)...)
Expand Down

0 comments on commit b6991f2

Please sign in to comment.