Skip to content

Commit

Permalink
add docs, compat, move to abstractarray.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Apr 22, 2021
1 parent 792182b commit e49e536
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
41 changes: 41 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2549,3 +2549,44 @@ function rest(a::AbstractArray{T}, state...) where {T}
sizehint!(v, length(a))
return foldl(push!, Iterators.rest(a, state...), init=v)
end


## keepat! ##

"""
keepat!(a::AbstractVector, inds)
Remove the items at all the indices which are not given by `inds`,
and return the modified `a`.
Items which are kept are shifted to fill the resulting gaps.
`inds` can be either an iterator or a collection of sorted integer indices.
See also [`deleteat!`](@ref).
!!! compat "Julia 1.7"
This function is available as of Julia 1.7.
# Examples
```jldoctest
julia> keepat!([6, 5, 4, 3, 2, 1], 1:2:5)
3-element Array{Int64,1}:
6
4
2
```
"""
function keepat!(a::AbstractVector, inds)
isempty(inds) && return a
local prev
i = firstindex(a)
for k in inds
@isdefined(prev) && (prev <= k || throw(ArgumentError("indices must be sorted")))
if i != k
a[i] = a[k]
end
prev = k
i = nextind(a, i)
end
deleteat!(a, i:lastindex(a))
return a
end
34 changes: 0 additions & 34 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1367,40 +1367,6 @@ function insert!(a::Array{T,1}, i::Integer, item) where T
return a
end

"""
keepat!(a::Vector, inds)
Remove the items at all the indices which are not given by `inds`, and return the modified `a`.
Items which are kept are shifted to fill the resulting gaps.
`inds` can be either an iterator or a collection of sorted and unique integer indices.
See also [`deleteat!`](@ref).
# Examples
```jldoctest
julia> keepat!([6, 5, 4, 3, 2, 1], 1:2:5)
3-element Array{Int64,1}:
6
4
2
```
"""
function keepat!(a::AbstractVector, inds)
isempty(inds) && return a
local prev
i = firstindex(a)
for k in inds
@isdefined(prev) && (prev <= k || throw(ArgumentError("indices must be sorted")))
if i != k
a[i] = a[k]
end
prev = k
i = nextind(a, i)
end
deleteat!(a, i:lastindex(a))
return a
end

"""
deleteat!(a::Vector, i::Integer)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ Base.pushfirst!
Base.popfirst!
Base.insert!
Base.deleteat!
Base.keepat!
Base.splice!
Base.resize!
Base.append!
Expand Down

0 comments on commit e49e536

Please sign in to comment.