Yes indeed, in fact all of them have big trouble dealing with arrays with more than 1000 elements.
The only faster implementations are @nnnnnnnn version:
Iterative copying
extension Collection {
func removingIndices<S: Sequence>(_ indicesToRemove: S) -> [Element]
where S.Element == Index
{
var result: [Element] = []
var low = startIndex
for high in indicesToRemove {
result.append(contentsOf: self[low..<high])
low = index(after: high)
}
result.append(contentsOf: self[low...])
return result
}
}
and @vadian version:
It's all about partitions
extension RangeReplaceableCollection where Self: MutableCollection, Index == Int {
mutating func remove(at indexes : IndexSet) {
guard var i = indexes.first, i < count else { return }
var j = index(after: i)
var k = indexes.integerGreaterThan(i) ?? endIndex
while j != endIndex {
if k != j { swapAt(i, j); formIndex(after: &i) }
else { k = indexes.integerGreaterThan(k) ?? endIndex }
formIndex(after: &j)
}
removeSubrange(i...)
}
}
@dabrahams the "Iterative copying" version is not O(n²), isn't it? If it is, then I don't understand how it is much faster than the other (bad) implementations.
Thank you for your amazing WWDC presentation