Skip to content

Commit 5d03483

Browse files
committed
Move Array.popLast to RangeReplaceableCollection
1 parent 3bdb92c commit 5d03483

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,24 +1518,6 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
15181518
}
15191519

15201520
%else:
1521-
/// Removes and returns the last element of the array.
1522-
///
1523-
/// - Returns: The last element of the array if the array is not empty;
1524-
/// otherwise, `nil`.
1525-
///
1526-
/// - Complexity: O(*n*) if the array is bridged, where *n* is the length
1527-
/// of the array; otherwise, O(1).
1528-
@_inlineable
1529-
public mutating func popLast() -> Element? {
1530-
let newCount = _getCount() - 1
1531-
guard !isEmpty else { return nil }
1532-
return _customRemoveLast().unsafelyUnwrapped
1533-
let pointer = (_buffer.firstElementAddress + newCount)
1534-
let element = pointer.move()
1535-
_buffer.count = newCount
1536-
return element
1537-
}
1538-
15391521
@_inlineable
15401522
public mutating func _customRemoveLast() -> Element? {
15411523
let newCount = _getCount() - 1

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,24 @@ extension RangeReplaceableCollection
798798
}
799799

800800
extension RangeReplaceableCollection where Self : BidirectionalCollection {
801+
/// Removes and returns the last element of the collection.
802+
///
803+
/// Calling this method may invalidate all saved indices of this
804+
/// collection. Do not rely on a previously stored index value after
805+
/// altering a collection with any operation that can change its length.
806+
///
807+
/// - Returns: The last element of the collection if the collection is not
808+
/// empty; otherwise, `nil`.
809+
///
810+
/// - Complexity: O(1)
811+
@_inlineable
812+
public mutating func popLast() -> Element? {
813+
if isEmpty { return nil }
814+
// duplicate of removeLast logic below, to avoid redundant precondition
815+
if let result = _customRemoveLast() { return result }
816+
return remove(at: index(before: endIndex))
817+
}
818+
801819
/// Removes and returns the last element of the collection.
802820
///
803821
/// The collection must not be empty.
@@ -813,9 +831,8 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
813831
@discardableResult
814832
public mutating func removeLast() -> Element {
815833
_precondition(!isEmpty, "Can't remove last element from an empty collection")
816-
if let result = _customRemoveLast() {
817-
return result
818-
}
834+
// NOTE if you change this implementation, change popLast above as well
835+
if let result = _customRemoveLast() { return result }
819836
return remove(at: index(before: endIndex))
820837
}
821838

0 commit comments

Comments
 (0)