Files
swift-collections/Sources/BasicContainers/UniqueArray/UniqueArray+Removals.swift
T
Karoy Lorentey 842f125c47 Invert dependencies to ContainersPreview, moving all container APIs there
- Move `InputSpan` into its own module, `SpanPreview`. Add dependencies to it.
- Invert dependencies to `ContainersPreview`, so that no production target imports it, but it now depends on its previous dependents.
- Move all generic APIs and conformances using `Producer`/`Drain`/`Container` to the `ContainersPreview` module.
- Remove `UnstableContainersPreview` conditions around `Iterable` APIs

In summary, importing `BasicContainers` and `DequeModule` continue to get us the basic definitions for `RigidArray`, `UniqueSet`, `RigidDeque` etc., without most of their unstable APIs. (With the exception of core APIs that require `InputSpan`.)

To get any API that uses an unstable protocol like `Container` or `Drain`, we’ll now need to import `ContainersPreview` in addition to the type-defining module.

This will provide a slightly less horrible migration path when (if) the container/producer protocol family gets adopted/adapted into the stdlib.
2026-08-25 18:29:58 -07:00

127 lines
3.8 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
#if compiler(>=6.2)
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Removes all elements from the array, optionally preserving its
/// allocated capacity.
///
/// - Complexity: O(*n*), where *n* is the original count of the array.
@inlinable
@inline(__always)
public mutating func removeAll(keepingCapacity keepCapacity: Bool = false) {
if keepCapacity {
_storage.removeAll()
} else {
_storage = RigidArray(capacity: 0)
}
}
/// 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
@inline(__always)
@discardableResult
public mutating func removeLast() -> Element {
_storage.removeLast()
}
/// 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) {
_storage.removeLast(k)
}
/// 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(`self.count`)
@inlinable
@inline(__always)
@discardableResult
public mutating func remove(at index: Int) -> Element {
_storage.remove(at: index)
}
/// 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.
///
/// - Complexity: O(`self.count`)
@inlinable
public mutating func removeSubrange(_ bounds: Range<Int>) {
_storage.removeSubrange(bounds)
}
/// 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.
///
/// - Complexity: O(`self.count`)
@_alwaysEmitIntoClient
public mutating func removeSubrange(_ bounds: some RangeExpression<Int>) {
// FIXME: Remove this in favor of the RangeReplaceableContainer algorithm.
removeSubrange(bounds.relative(to: indices))
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray 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 RangeReplaceableContainer algorithm.
if isEmpty { return nil }
return removeLast()
}
}
#endif