Review and update container protocols and conformances

- @inlinable → @_alwaysEmitIntoClient for now, to avoid temporary ABI issues
- Review RangeReplaceableContainer algorithms; add missing ones:
    - New requirements: `removeFirst()`, `removeFirst(_:)`, `_customRemoveLast()`, `_customRemoveLast(_:) (with default implementations)
    - `func removeAll()`
    - `func removeLast()`, `func removeLast(_:)`, `func popLast()` when self is also bidirectional
- Review RigidArray, UniqueArray algorithms; remove ones needlessly duplicating functionality already provided by RRC. Try not to break source compatibility.
This commit is contained in:
Karoy Lorentey committed 2026-07-07 14:18:09 -07:00
1 parent 204e94cb63
commit 3ba8d70ec3
36 files changed
+415 -644

No files matched your search

@@ -132,6 +132,7 @@ extension UniqueArray where Element: ~Copyable {
_storage.append(moving: &items)
}
#if !UnstableContainersPreview
/// Appends the elements of a given array to the end of this array by moving
/// them between the containers. On return, the input array becomes empty, but
/// it is not destroyed, and it preserves its original storage capacity.
@@ -149,70 +150,19 @@ extension UniqueArray where Element: ~Copyable {
public mutating func append(
moving items: inout RigidArray<Element>
) {
// FIXME: Remove this in favor of a generic algorithm over range-replaceable containers
_ensureFreeCapacity(items.count)
_storage.append(moving: &items)
items.edit { source in
source.withUnsafeMutableBufferPointer { buffer, count in
_storage.append(moving: buffer._extracting(first: count))
count = 0
}
}
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
#if UnstableContainersPreview
/// Appends the elements of a given container to the end of this array by
/// consuming the source container.
///
/// If the target array does not have sufficient capacity to hold all items
/// in the source array, then this automatically grows the target array's
/// capacity, using a geometric growth rate.
///
/// - Parameters:
/// - items: An array whose items to move to the end of this array.
///
/// - Complexity: O(`items.count`)
@_alwaysEmitIntoClient
public mutating func append(
consuming items: consuming RigidArray<Element>
) {
// FIXME: Remove this in favor of a generic algorithm over consumable containers
var items = items
self.append(moving: &items)
}
#endif
#if compiler(>=6.4) && UnstableContainersPreview
/// Appends at most `newItemCount` items generated by a producer to the end of
/// this array.
///
/// If the array doesn't have sufficient capacity to accommodate the specified
/// number of new elements, then this method reallocates the array's storage
/// to grow it, using a geometric growth rate.
///
/// This operation appends as many items as the producer can generate before
/// either reaching its end (or throwing an error), or filling the specified
/// capacity. This operation only consumes the first `newItemCount` items in the
/// producer; if the producer has more, then they remain available after this
/// method returns.
///
/// - Parameters:
/// - newItemCount: The maximum number of items to append to the array.
/// - producer: A producer that generates the items to append.
///
/// - Complexity: O(*n*) where *n* is the number of new items, when
/// amortized over many similar invocations on the same array.
@_alwaysEmitIntoClient
public mutating func append<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
addingCount newItemCount: Int,
from producer: inout P
) throws(E)
where P.Element: ~Copyable
{
_ensureFreeCapacity(newItemCount)
try _storage.append(addingCount: newItemCount, from: &producer)
}
#endif
#if compiler(>=6.4) && UnstableContainersPreview
/// Appends all the items generated by a producer to the end of this deque.
@@ -117,7 +117,7 @@ extension UniqueArray where Element: ~Copyable {
#endif
#if compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
public typealias SubrangeConsumer = RigidArray<Element>.SubrangeConsumer
@@ -48,10 +48,10 @@ extension UniqueArray: RandomAccessContainer where Element: ~Copyable {}
extension UniqueArray: MutableContainer where Element: ~Copyable {}
#if compiler(>=6.4)
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension UniqueArray: RangeReplaceableContainer where Element: ~Copyable {}
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension UniqueArray: DynamicContainer where Element: ~Copyable {}
#endif
#endif
@@ -144,6 +144,7 @@ extension UniqueArray where Element: ~Copyable {
moving items: inout InputSpan<Element>,
at index: Int
) {
// FIXME: Remove when InputSpan starts conforming to RangeReplaceableContainer
_ensureFreeCapacity(items.count)
_storage.insert(moving: &items, at: index)
}
@@ -171,10 +172,12 @@ extension UniqueArray where Element: ~Copyable {
moving items: inout OutputSpan<Element>,
at index: Int
) {
// FIXME: Remove when OutputSpan starts conforming to RangeReplaceableContainer
_ensureFreeCapacity(items.count)
_storage.insert(moving: &items, at: index)
}
#if !UnstableContainersPreview
/// Inserts the elements of a given array into the given position in this
/// array by moving them between the containers. On return, the input array
/// becomes empty, but it is not destroyed, and it preserves its original
@@ -186,6 +189,7 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Parameters:
/// - items: An array whose contents to move into `self`.
/// - index: The position in `self` at which to insert the new items.
///
/// - Complexity: O(`self.count` + `items.count`)
@_alwaysEmitIntoClient
@@ -197,88 +201,9 @@ extension UniqueArray where Element: ~Copyable {
_ensureFreeCapacity(items.count)
_storage.insert(moving: &items, at: index)
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
#if UnstableContainersPreview
/// Inserts the elements of a given array into the given position in this
/// array by consuming the source container.
///
/// If the array does not have sufficient capacity to hold all elements,
/// then this reallocates storage to extend its capacity, using a geometric
/// growth rate.
///
/// - Parameters:
/// - items: A fully initialized buffer whose contents to move into
/// the array.
///
/// - Complexity: O(`self.count` + `items.count`)
@_alwaysEmitIntoClient
public mutating func insert(
consuming items: consuming RigidArray<Element>,
at index: Int
) {
// FIXME: Remove this in favor of a generic algorithm over consumable containers
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(items.count)
_storage.insert(consuming: items, at: index)
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
#if compiler(>=6.4) && UnstableContainersPreview
/// Inserts at most `newItemCount` items generated by a producer into this
/// array, starting at the given index.
///
/// Existing elements in the array's storage are moved as needed to make room
/// for the new items.
///
/// If the array does not have sufficient capacity to hold enough items,
/// then this reallocates the array's storage to grow its capacity, using a
/// geometric growth rate.
///
/// This operation inserts as many items as the producer can generate before
/// either reaching `newItemCount`, or the producer hitting its end, or
/// throwing an error. If the producer has more than `newItemCount` items
/// left in its underlying sequence, then extra items remain available after
/// this method returns.
///
/// If the operation inserts fewer than `newItemCount` items, then it results
/// in a gap in array storage that needs to be closed by moving some
/// items to their correct positions given the adjusted count. This adds some
/// overhead compared to adding exactly as many items as promised.
///
/// - Parameters:
/// - newItemCount: The maximum number of items to insert into the array.
/// - index: The position at which to insert the new items.
/// `index` must be a valid index in the array.
/// - producer: A producer that generates the items to append.
///
/// - Complexity: O(`self.count` + `newItemCount`) when amortized over many
/// similar invocations on the same array.
@_alwaysEmitIntoClient
public mutating func insert<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
addingCount newItemCount: Int,
from producer: inout P,
at index: Int
) throws(E)
where P.Element: ~Copyable
{
try insert(addingCount: newItemCount, at: index) { target throws(E) in
while !target.isFull, try producer.generate(into: &target) {
// Do nothing
}
}
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray {
/// Copies the elements of a fully initialized buffer pointer into this
@@ -368,42 +293,6 @@ extension UniqueArray {
_storage.insert(copying: newElements, at: index)
}
#if compiler(>=6.4) && UnstableContainersPreview
/// Copies the elements of a container into this array at the specified
/// position.
///
/// The new elements are inserted before the element currently at the
/// specified index. If you pass the array's `endIndex` as the `index`
/// parameter, then the new elements are appended to the end of the array.
///
/// All existing elements at or following the specified position are moved to
/// make room for the new item.
///
/// If the array does not have sufficient capacity to hold enough elements,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// - Parameters:
/// - newElements: The new elements to insert into the array.
/// - index: The position at which to insert the new elements. It must be
/// a valid index of the array.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
C: Container<Element> & ~Copyable & ~Escapable
>(
copying newElements: borrowing C, at index: Int
) {
// FIXME: Avoid moving the subsequent elements twice.
let c = newElements.count
_ensureFreeCapacity(c)
_storage._insertContainer(at: index, copying: newElements, newCount: c)
}
#endif
/// Copies the elements of a collection into this array at the specified
/// position.
///
@@ -428,48 +317,15 @@ extension UniqueArray {
public mutating func insert(
copying newElements: some Collection<Element>, at index: Int
) {
// FIXME: Remove this -- RangeReplaceContainer already has this algorithm.
// (Note that this would be source breaking while RRC lives in the separate
// ContainersPreview module.)
// FIXME: Avoid moving the subsequent elements twice.
let newCount = newElements.count
_ensureFreeCapacity(newCount)
_storage._insertCollection(
at: index, copying: newElements, newCount: newCount)
addingCount: newCount, copying: newElements, at: index)
}
#if compiler(>=6.4) && UnstableContainersPreview
/// Copies the elements of a container into this array at the specified
/// position.
///
/// The new elements are inserted before the element currently at the
/// specified index. If you pass the array's `endIndex` as the `index`
/// parameter, then the new elements are appended to the end of the array.
///
/// All existing elements at or following the specified position are moved to
/// make room for the new item.
///
/// If the array does not have sufficient capacity to hold enough elements,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// - Parameters:
/// - newElements: The new elements to insert into the array.
/// - index: The position at which to insert the new elements. It must be
/// a valid index of the array.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
C: Container<Element> & Collection<Element>
>(
copying newElements: borrowing C, at index: Int
) {
// FIXME: Avoid moving the subsequent elements twice.
let c = newElements.count
_ensureFreeCapacity(c)
_storage._insertContainer(at: index, copying: newElements, newCount: c)
}
#endif
}
#endif
@@ -103,13 +103,14 @@ extension UniqueArray where Element: ~Copyable {
/// - Complexity: O(`self.count`)
@_alwaysEmitIntoClient
public mutating func removeSubrange(_ bounds: some RangeExpression<Int>) {
// FIXME: Remove this in favor of a standard algorithm.
// FIXME: Remove this in favor of the RangeReplaceableContainer algorithm.
removeSubrange(bounds.relative(to: indices))
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
#if !UnstableContainersPreview
/// 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;
@@ -118,9 +119,11 @@ extension UniqueArray where Element: ~Copyable {
/// - 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
}
#endif