Skip to content

Commit

Permalink
fix deleteat
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Jul 24, 2018
1 parent 5eb5f34 commit cda9e30
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/TupleTools.jl
Expand Up @@ -82,8 +82,8 @@ Delete the element at location `i` in `t`; if a list `I` of indices is specified
"""
deleteat(t::Tuple, I::Tuple{Int}) = deleteat(t, I[1])
function deleteat(t::Tuple, I::Tuple{Int, Int, Vararg{Int}})
any(i->(1 <= i <= length(t)), I) && throw(BoundsError(t, I))
_deleteat(_deleteat(t, I[1]), ishift(tail(I), I[1], -1))
any(i->!(1 <= i <= length(t)), I) && throw(BoundsError(t, I))
_deleteat(t, sort(I, rev = true))
end
deleteat(t::Tuple, i::Int) = 1 <= i <= length(t) ? _deleteat(t, i) : throw(BoundsError(t, i))
@inline _deleteat(t::Tuple, i::Int) = i == 1 ? tail(t) : (t[1], _deleteat(tail(t), i-1)...)
Expand All @@ -92,8 +92,6 @@ deleteat(t::Tuple, i::Int) = 1 <= i <= length(t) ? _deleteat(t, i) : throw(Bound
@inline _deleteat(t::Tuple, I::Tuple{Int}) = _deleteat(t, I[1])
@inline _deleteat(t::Tuple, I::Tuple{Int,Int,Vararg{Int}}) = _deleteat(_deleteat(t, I[1]), tail(I)) # assumes sorted from big to small

ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)

"""
insertat(t::Tuple, i::Int, t2::Tuple) -> ::Tuple
Expand Down Expand Up @@ -280,11 +278,13 @@ Computes a tuple that contains the permutation required to sort `t`.
end
end
r = _sortperm(_deleteat(t, i), lt, by, rev)
return (i, ishift(r, i, +1)...)
return (i, _ishift(r, i, +1)...)
end
@inline _sortperm(t::Tuple{Any}, lt=isless, by=identity, rev::Bool=false) = (1,)
@inline _sortperm(t::Tuple{}, lt=isless, by=identity, rev::Bool=false) = ()

_ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)

"""
getindices(t::Tuple, I::Tuple{Vararg{Int}}) -> ::Tuple
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -33,6 +33,7 @@ for i = 1:n
@test @inferred(TupleTools.deleteat(t, i)) == (deleteat!(copy(p), i)...,)
@test @inferred(TupleTools.insertat(t, i, (1,2,3))) == (vcat(p[1:i-1], [1,2,3], p[i+1:n])...,)
end
@test @inferred(TupleTools.deleteat((1,2,3,4,5,6), (3,1,5))) == (2,4,6)
for i = 0:n
@test @inferred(TupleTools.insertafter(t, i, (1,2,3))) == (vcat(p[1:i], [1,2,3], p[i+1:n])...,)
end
Expand Down

0 comments on commit cda9e30

Please sign in to comment.