Skip to content

Commit 1a30ffd

Browse files
committed
Revert "[stdlib] Add custom implementations of removeLast and remove(at:) to Array (#14212)"
This reverts commit 5ed4afe. It doesn't appear to be correct when the client is compiled with optimizations (as in, there's a failing bot).
1 parent 9ff62ff commit 1a30ffd

File tree

1 file changed

+61
-74
lines changed

1 file changed

+61
-74
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,17 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
14931493
}
14941494

14951495
%if Self == 'ArraySlice':
1496+
/// Removes and returns the last element of the array.
1497+
///
1498+
/// The array must not be empty. This example removes the last number from an
1499+
/// array of `Double` values.
1500+
///
1501+
/// var measurements: [Double] = [1.1, 1.5, 2.9, 1.2, 1.5, 1.3, 1.2]
1502+
/// let removed = measurements.removeLast()
1503+
/// print(measurements)
1504+
/// // Prints "[1.1, 1.5, 2.9, 1.2, 1.5, 1.3]"
1505+
///
1506+
/// - Returns: The element that was removed.
14961507
@_inlineable
14971508
public mutating func _customRemoveLast() -> Element? {
14981509
_precondition(count > 0, "Can't removeLast from an empty ${Self}")
@@ -1508,54 +1519,33 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
15081519
self.replaceSubrange((i &- 1)..<i, with: EmptyCollection())
15091520
return result
15101521
}
1511-
1512-
/// Removes and returns the element at the specified position.
1513-
///
1514-
/// All the elements following the specified position are moved up to
1515-
/// close the gap.
1522+
%end
1523+
1524+
/// Inserts a new element at the specified position.
15161525
///
1517-
/// var measurements: [Double] = [1.1, 1.5, 2.9, 1.2, 1.5, 1.3, 1.2]
1518-
/// let removed = measurements.remove(at: 2)
1519-
/// print(measurements)
1520-
/// // Prints "[1.1, 1.5, 1.2, 1.5, 1.3, 1.2]"
1526+
/// The new element is inserted before the element currently at the specified
1527+
/// index. If you pass the array's `endIndex` property as the `index`
1528+
/// parameter, the new element is appended to the array.
15211529
///
1522-
/// - Parameter index: The position of the element to remove. `index` must
1523-
/// be a valid index of the array.
1524-
/// - Returns: The element at the specified index.
1530+
/// var numbers = [1, 2, 3, 4, 5]
1531+
/// numbers.insert(100, at: 3)
1532+
/// numbers.insert(200, at: numbers.endIndex)
15251533
///
1526-
/// - Complexity: O(*n*), where *n* is the length of the array.
1527-
@_inlineable
1528-
@discardableResult
1529-
public mutating func remove(at index: Int) -> Element {
1530-
let result = self[index]
1531-
self.replaceSubrange(index..<(index + 1), with: EmptyCollection())
1532-
return result
1533-
}
1534-
1535-
%else:
1536-
/// Removes and returns the last element of the array.
1534+
/// print(numbers)
1535+
/// // Prints "[1, 2, 3, 100, 4, 5, 200]"
15371536
///
1538-
/// - Returns: The last element of the array if the array is not empty;
1539-
/// otherwise, `nil`.
1537+
/// - Parameter newElement: The new element to insert into the array.
1538+
/// - Parameter i: The position at which to insert the new element.
1539+
/// `index` must be a valid index of the array or equal to its `endIndex`
1540+
/// property.
15401541
///
1541-
/// - Complexity: O(*n*) if the array is bridged, where *n* is the length
1542-
/// of the array; otherwise, O(1).
1542+
/// - Complexity: O(*n*), where *n* is the length of the array.
15431543
@_inlineable
1544-
public mutating func popLast() -> Element? {
1545-
guard !isEmpty else { return nil }
1546-
return _customRemoveLast().unsafelyUnwrapped
1544+
public mutating func insert(_ newElement: Element, at i: Int) {
1545+
_checkIndex(i)
1546+
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
15471547
}
15481548

1549-
@_inlineable
1550-
public mutating func _customRemoveLast() -> Element? {
1551-
_precondition(!isEmpty, "Can't removeLast from an empty ${Self}")
1552-
_makeUniqueAndReserveCapacityIfNotUnique()
1553-
let newCount = _getCount() - 1
1554-
let pointer = (_buffer.firstElementAddress + newCount)
1555-
let element = pointer.move()
1556-
_buffer.count = newCount
1557-
return element
1558-
}
15591549
/// Removes and returns the element at the specified position.
15601550
///
15611551
/// All the elements following the specified position are moved up to
@@ -1574,42 +1564,10 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
15741564
@_inlineable
15751565
@discardableResult
15761566
public mutating func remove(at index: Int) -> Element {
1577-
_precondition(index < endIndex, "Index out of range")
1578-
_precondition(index >= startIndex, "Index out of range")
1579-
_makeUniqueAndReserveCapacityIfNotUnique()
1580-
let newCount = _getCount() - 1
1581-
let pointer = (_buffer.firstElementAddress + index)
1582-
let result = pointer.move()
1583-
pointer.moveInitialize(from: pointer + 1, count: newCount - index)
1584-
_buffer.count = newCount
1567+
let result = self[index]
1568+
self.replaceSubrange(index..<(index + 1), with: EmptyCollection())
15851569
return result
15861570
}
1587-
%end
1588-
1589-
/// Inserts a new element at the specified position.
1590-
///
1591-
/// The new element is inserted before the element currently at the specified
1592-
/// index. If you pass the array's `endIndex` property as the `index`
1593-
/// parameter, the new element is appended to the array.
1594-
///
1595-
/// var numbers = [1, 2, 3, 4, 5]
1596-
/// numbers.insert(100, at: 3)
1597-
/// numbers.insert(200, at: numbers.endIndex)
1598-
///
1599-
/// print(numbers)
1600-
/// // Prints "[1, 2, 3, 100, 4, 5, 200]"
1601-
///
1602-
/// - Parameter newElement: The new element to insert into the array.
1603-
/// - Parameter i: The position at which to insert the new element.
1604-
/// `index` must be a valid index of the array or equal to its `endIndex`
1605-
/// property.
1606-
///
1607-
/// - Complexity: O(*n*), where *n* is the length of the array.
1608-
@_inlineable
1609-
public mutating func insert(_ newElement: Element, at i: Int) {
1610-
_checkIndex(i)
1611-
self.replaceSubrange(i..<i, with: CollectionOfOne(newElement))
1612-
}
16131571

16141572
/// Removes all elements from the array.
16151573
///
@@ -2400,6 +2358,35 @@ extension Array {
24002358
}
24012359
#endif
24022360

2361+
extension Array {
2362+
/// Removes and returns the last element of the array.
2363+
///
2364+
/// - Returns: The last element of the array if the array is not empty;
2365+
/// otherwise, `nil`.
2366+
///
2367+
/// - Complexity: O(*n*) if the array is bridged, where *n* is the length
2368+
/// of the array; otherwise, O(1).
2369+
@_inlineable
2370+
public mutating func popLast() -> Element? {
2371+
guard !isEmpty else { return nil }
2372+
return removeLast()
2373+
}
2374+
}
2375+
2376+
extension ContiguousArray {
2377+
/// Removes and returns the last element of the array.
2378+
///
2379+
/// - Returns: The last element of the array if the array is not empty;
2380+
/// otherwise, `nil`.
2381+
///
2382+
/// - Complexity: O(1)
2383+
@_inlineable
2384+
public mutating func popLast() -> Element? {
2385+
guard !isEmpty else { return nil }
2386+
return removeLast()
2387+
}
2388+
}
2389+
24032390
extension ArraySlice {
24042391
@_inlineable
24052392
public // @testable

0 commit comments

Comments
 (0)