Skip to content

Commit 8100a84

Browse files
committed
Fix insert and deleteat boundscheck for tuples. Improve unsafe_deleteat performance for single- and two-element tuples.
1 parent 8a1ec72 commit 8100a84

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/ArrayInterface.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,8 @@ Base.@propagate_inbounds function insert(collection, index, item)
617617
return ret
618618
end
619619

620-
function insert(x::Tuple, index::Integer, item)
621-
@boundscheck if !checkindex(Bool, static_first(x):static_last(x), index)
620+
function insert(x::Tuple{Vararg{Any,N}}, index::Integer, item) where {N}
621+
@boundscheck if !checkindex(Bool, StaticInt{1}():StaticInt{N}(), index)
622622
throw(BoundsError(x, index))
623623
end
624624
return unsafe_insert(x, Int(index), item)
@@ -643,8 +643,8 @@ Return a new instance of `collection` with the item at the given `index` removed
643643
end
644644
return unsafe_deleteat(collection, index)
645645
end
646-
@propagate_inbounds function deleteat(collection::Tuple, index)
647-
@boundscheck if !checkindex(Bool, static_first(collection):static_last(collection), index)
646+
@propagate_inbounds function deleteat(collection::Tuple{Vararg{Any,N}}, index) where {N}
647+
@boundscheck if !checkindex(Bool, StaticInt{1}():StaticInt{N}(), index)
648648
throw(BoundsError(collection, index))
649649
end
650650
return unsafe_deleteat(collection, index)
@@ -686,6 +686,8 @@ end
686686
return Tuple(dst)
687687
end
688688

689+
@inline unsafe_deleteat(x::Tuple{T}, i::Integer) where {T} = ()
690+
@inline unsafe_deleteat(x::Tuple{T1,T2}, i::Integer) where {T1,T2} = isone(i) ? (x[2],) : (x[1],)
689691
@inline function unsafe_deleteat(x::Tuple, i::Integer)
690692
if i === one(i)
691693
return Base.tail(x)

test/runtests.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,13 @@ end
463463
@test @inferred(ArrayInterface.deleteat([1, 2, 3], [2, 3])) == [1]
464464

465465

466-
@test @inferred(ArrayInterface.insert((1,2,3), 1, -2)) == (-2, 1, 2, 3)
467-
@test @inferred(ArrayInterface.insert((1,2,3), 2, -2)) == (1, -2, 2, 3)
468-
@test @inferred(ArrayInterface.insert((1,2,3), 3, -2)) == (1, 2, -2, 3)
466+
@test @inferred(ArrayInterface.insert((2,3,4), 1, -2)) == (-2, 2, 3, 4)
467+
@test @inferred(ArrayInterface.insert((2,3,4), 2, -2)) == (2, -2, 3, 4)
468+
@test @inferred(ArrayInterface.insert((2,3,4), 3, -2)) == (2, 3, -2, 4)
469469

470-
@test @inferred(ArrayInterface.deleteat((1, 2, 3), 1)) == (2, 3)
471-
@test @inferred(ArrayInterface.deleteat((1, 2, 3), 2)) == (1, 3)
472-
@test @inferred(ArrayInterface.deleteat((1, 2, 3), 3)) == (1, 2)
470+
@test @inferred(ArrayInterface.deleteat((2, 3, 4), 1)) == (3, 4)
471+
@test @inferred(ArrayInterface.deleteat((2, 3, 4), 2)) == (2, 4)
472+
@test @inferred(ArrayInterface.deleteat((2, 3, 4), 3)) == (2, 3)
473473
@test ArrayInterface.deleteat((1, 2, 3), [1, 2]) == (3,)
474474
end
475475

0 commit comments

Comments
 (0)