mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
158 lines
5.2 KiB
Swift
158 lines
5.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !COLLECTIONS_SINGLE_MODULE
|
|
import InternalCollectionsUtilities
|
|
#endif
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidArray where Element: ~Copyable {
|
|
/// Removes all elements from the array, preserving its allocated capacity.
|
|
///
|
|
/// - Complexity: O(*n*), where *n* is the original count of the array.
|
|
@inlinable
|
|
public mutating func removeAll() {
|
|
unsafe _items._deinitializeElements()
|
|
unsafe _count = 0
|
|
}
|
|
|
|
/// Removes and returns the element at the specified position.
|
|
///
|
|
/// All the elements following the specified position are moved to close the
|
|
/// gap.
|
|
///
|
|
/// - Parameter index: The position of the element to remove. `index` must be
|
|
/// a valid index of the array that is not equal to the end index.
|
|
/// On return, `index` is updated to address the position following the
|
|
/// removed element.
|
|
/// - Returns: The removed element.
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@_alwaysEmitIntoClient
|
|
@inline(__always)
|
|
@discardableResult
|
|
public mutating func remove(at index: inout Int) -> Element {
|
|
remove(at: index)
|
|
}
|
|
|
|
/// Removes and returns the element at the specified position.
|
|
///
|
|
/// All the elements following the specified position are moved to close the
|
|
/// gap.
|
|
///
|
|
/// - Parameter index: The position of the element to remove. `index` must be
|
|
/// a valid index of the array that is not equal to the end index.
|
|
/// - Returns: The removed element.
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func remove(at index: Int) -> Element {
|
|
_checkItemIndex(index)
|
|
let old = unsafe _storage.moveElement(from: index)
|
|
unsafe _closeGap(at: index, count: 1)
|
|
unsafe _count -= 1
|
|
return old
|
|
}
|
|
|
|
/// Removes and returns the last element of the array.
|
|
///
|
|
/// The array must not be empty.
|
|
///
|
|
/// - Returns: The last element of the original array.
|
|
///
|
|
/// - Complexity: O(1)
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func removeLast() -> Element {
|
|
precondition(!isEmpty, "Cannot remove last element from an empty array")
|
|
let old = unsafe _storage.moveElement(from: self.count - 1)
|
|
unsafe _count -= 1
|
|
return old
|
|
}
|
|
|
|
/// Removes and discards the specified number of elements from the end of the
|
|
/// array.
|
|
///
|
|
/// Attempting to remove more elements than exist in the array
|
|
/// triggers a runtime error.
|
|
///
|
|
/// - Parameter k: The number of elements to remove from the array.
|
|
/// `k` must be greater than or equal to zero and must not exceed
|
|
/// the count of the array.
|
|
///
|
|
/// - Complexity: O(`k`)
|
|
@inlinable
|
|
public mutating func removeLast(_ k: Int) {
|
|
if k == 0 { return }
|
|
precondition(
|
|
k >= 0 && k <= self.count,
|
|
"Count of elements to remove is out of bounds")
|
|
unsafe _storage.extracting(
|
|
Range(uncheckedBounds: (self.count - k, self.count))
|
|
)._deinitializeElements()
|
|
unsafe _count &-= k
|
|
}
|
|
|
|
/// Removes the specified subrange of elements from the array.
|
|
///
|
|
/// All the elements following the specified subrange are moved to close the
|
|
/// resulting gap.
|
|
///
|
|
/// - Parameter bounds: The subrange of the array to remove. The bounds
|
|
/// of the range must be valid indices of the array.
|
|
/// - Returns: A valid index addressing the position following the removed
|
|
/// subrange.
|
|
/// - Complexity: O(`count`)
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func removeSubrange(_ bounds: Range<Int>) -> Int {
|
|
_checkValidBounds(bounds)
|
|
guard !bounds.isEmpty else { return bounds.lowerBound }
|
|
unsafe _storage.extracting(bounds)._deinitializeElements()
|
|
unsafe _closeGap(at: bounds.lowerBound, count: bounds.count)
|
|
unsafe _count -= bounds.count
|
|
return bounds.lowerBound
|
|
}
|
|
|
|
/// Removes the specified subrange of elements from the array.
|
|
///
|
|
/// - Parameter bounds: The subrange of the array to remove. The bounds of the
|
|
/// range must be valid indices of the array.
|
|
/// - Returns: A valid index addressing the position following the removed
|
|
/// subrange.
|
|
/// - Complexity: O(`count`)
|
|
@_alwaysEmitIntoClient
|
|
@discardableResult
|
|
public mutating func removeSubrange(_ bounds: some RangeExpression<Int>) -> Int {
|
|
// FIXME: Remove this in favor of the DrainableContainer algorithm.
|
|
removeSubrange(bounds.relative(to: indices))
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RigidArray where Element: ~Copyable {
|
|
/// Removes and returns the last element of the array, if there is one.
|
|
///
|
|
/// - Returns: The last element of the array if the array is not empty;
|
|
/// otherwise, `nil`.
|
|
///
|
|
/// - Complexity: O(1)
|
|
@_alwaysEmitIntoClient
|
|
public mutating func popLast() -> Element? {
|
|
// FIXME: Remove this in favor of the DrainableContainer algorithm.
|
|
if isEmpty { return nil }
|
|
return removeLast()
|
|
}
|
|
}
|