Skip to content

Commit cda9e30

Browse files
committed
fix deleteat
1 parent 5eb5f34 commit cda9e30

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/TupleTools.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ Delete the element at location `i` in `t`; if a list `I` of indices is specified
8282
"""
8383
deleteat(t::Tuple, I::Tuple{Int}) = deleteat(t, I[1])
8484
function deleteat(t::Tuple, I::Tuple{Int, Int, Vararg{Int}})
85-
any(i->(1 <= i <= length(t)), I) && throw(BoundsError(t, I))
86-
_deleteat(_deleteat(t, I[1]), ishift(tail(I), I[1], -1))
85+
any(i->!(1 <= i <= length(t)), I) && throw(BoundsError(t, I))
86+
_deleteat(t, sort(I, rev = true))
8787
end
8888
deleteat(t::Tuple, i::Int) = 1 <= i <= length(t) ? _deleteat(t, i) : throw(BoundsError(t, i))
8989
@inline _deleteat(t::Tuple, i::Int) = i == 1 ? tail(t) : (t[1], _deleteat(tail(t), i-1)...)
@@ -92,8 +92,6 @@ deleteat(t::Tuple, i::Int) = 1 <= i <= length(t) ? _deleteat(t, i) : throw(Bound
9292
@inline _deleteat(t::Tuple, I::Tuple{Int}) = _deleteat(t, I[1])
9393
@inline _deleteat(t::Tuple, I::Tuple{Int,Int,Vararg{Int}}) = _deleteat(_deleteat(t, I[1]), tail(I)) # assumes sorted from big to small
9494

95-
ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)
96-
9795
"""
9896
insertat(t::Tuple, i::Int, t2::Tuple) -> ::Tuple
9997
@@ -280,11 +278,13 @@ Computes a tuple that contains the permutation required to sort `t`.
280278
end
281279
end
282280
r = _sortperm(_deleteat(t, i), lt, by, rev)
283-
return (i, ishift(r, i, +1)...)
281+
return (i, _ishift(r, i, +1)...)
284282
end
285283
@inline _sortperm(t::Tuple{Any}, lt=isless, by=identity, rev::Bool=false) = (1,)
286284
@inline _sortperm(t::Tuple{}, lt=isless, by=identity, rev::Bool=false) = ()
287285

286+
_ishift(t::Tuple{Vararg{Int}}, i::Int, s::Int) = map(n->(n < i ? n : n+s), t)
287+
288288
"""
289289
getindices(t::Tuple, I::Tuple{Vararg{Int}}) -> ::Tuple
290290

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ for i = 1:n
3333
@test @inferred(TupleTools.deleteat(t, i)) == (deleteat!(copy(p), i)...,)
3434
@test @inferred(TupleTools.insertat(t, i, (1,2,3))) == (vcat(p[1:i-1], [1,2,3], p[i+1:n])...,)
3535
end
36+
@test @inferred(TupleTools.deleteat((1,2,3,4,5,6), (3,1,5))) == (2,4,6)
3637
for i = 0:n
3738
@test @inferred(TupleTools.insertafter(t, i, (1,2,3))) == (vcat(p[1:i], [1,2,3], p[i+1:n])...,)
3839
end

0 commit comments

Comments
 (0)