@@ -1493,6 +1493,17 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1493
1493
}
1494
1494
1495
1495
% 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.
1496
1507
@_inlineable
1497
1508
public mutating func _customRemoveLast( ) - > Element? {
1498
1509
_precondition ( count > 0 , " Can't removeLast from an empty ${Self} " )
@@ -1508,54 +1519,33 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1508
1519
self . replaceSubrange ( ( i &- 1 ) ..< i, with: EmptyCollection ( ) )
1509
1520
return result
1510
1521
}
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.
1516
1525
///
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.
1521
1529
///
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)
1525
1533
///
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]"
1537
1536
///
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.
1540
1541
///
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.
1543
1543
@_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 ) )
1547
1547
}
1548
1548
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
- }
1559
1549
/// Removes and returns the element at the specified position.
1560
1550
///
1561
1551
/// All the elements following the specified position are moved up to
@@ -1574,42 +1564,10 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
1574
1564
@_inlineable
1575
1565
@discardableResult
1576
1566
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 ( ) )
1585
1569
return result
1586
1570
}
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
- }
1613
1571
1614
1572
/// Removes all elements from the array.
1615
1573
///
@@ -2400,6 +2358,35 @@ extension Array {
2400
2358
}
2401
2359
#endif
2402
2360
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
+
2403
2390
extension ArraySlice {
2404
2391
@_inlineable
2405
2392
public // @testable
0 commit comments