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

@@ -130,6 +130,7 @@ extension RigidArray where Element: ~Copyable {
public mutating func append(
moving items: inout InputSpan<Element>
) {
// FIXME: Remove this when `InputSpan` starts conforming to RangeReplaceableContainer
items.withUnsafeMutableBufferPointer { buffer, count in
let source = buffer._extracting(last: count)
unsafe self.append(moving: source)
@@ -152,6 +153,7 @@ extension RigidArray where Element: ~Copyable {
public mutating func append(
moving items: inout OutputSpan<Element>
) {
// FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer
items.withUnsafeMutableBufferPointer { buffer, count in
let source = buffer._extracting(first: count)
unsafe self.append(moving: source)
@@ -159,6 +161,7 @@ extension RigidArray where Element: ~Copyable {
}
}
#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.
@@ -174,107 +177,10 @@ extension RigidArray 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
items.edit { span in
self.append(moving: &span)
}
}
}
@available(SwiftStdlib 5.0, *)
extension RigidArray 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 triggers a runtime error.
///
/// - Parameters:
/// - items: A container whose contents to move into 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
}
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
#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
{
try self.append(addingCount: newItemCount) { target throws(E) in
while !target.isFull {
guard try producer.generate(into: &target) else { break }
}
}
}
#endif
#if compiler(>=6.4) && UnstableContainersPreview
/// Appends all the items generated by a producer to the end of this deque.
///
/// If the deque doesn't have sufficient capacity to accommodate all the
/// elements, then this method reallocates the deque's storage
/// to grow it, using a geometric growth rate.
///
/// - Parameters:
/// - producer: A producer that generates the items to append.
///
/// - Complexity: O(*n*) where *n* is the number of new items, when
/// amortized over many similarly-sized invocations on the same deque.
@_alwaysEmitIntoClient
public mutating func append<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
from producer: inout P
) throws(E)
where P.Element: ~Copyable
{
var done = false
while !done {
try self.append(addingCount: freeCapacity) { target throws(E) in
while !target.isFull, !done {
done = try !producer.generate(into: &target)
}
}
}
}
#endif
}
@@ -138,7 +138,7 @@ extension RigidArray where Element: ~Copyable {
#endif
#if compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
@_alwaysEmitIntoClient
@inline(__always)
@@ -148,9 +148,9 @@ extension RigidArray where Element: ~Copyable {
}
}
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
@frozen
public struct SubrangeConsumer: ~Copyable, ~Escapable {
// FIXME: We have to use our own MutableRef because the standard one
@@ -191,11 +191,11 @@ extension RigidArray where Element: ~Copyable {
}
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension RigidArray.SubrangeConsumer: Drain where Element: ~Copyable {
}
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension RigidArray.SubrangeConsumer where Element: ~Copyable {
@inlinable
public var count: Int {
@@ -44,7 +44,7 @@ extension RigidArray: RandomAccessContainer where Element: ~Copyable {}
@available(SwiftStdlib 5.0, *)
extension RigidArray: MutableContainer where Element: ~Copyable {}
@available(SwiftStdlib 6.4, *)
@available(SwiftStdlib 5.0, *)
extension RigidArray: RangeReplaceableContainer where Element: ~Copyable {}
#endif
#endif
@@ -170,6 +170,7 @@ extension RigidArray where Element: ~Copyable {
moving items: inout InputSpan<Element>,
at index: Int
) {
// FIXME: Remove when InputSpan starts conforming to RangeReplaceableContainer
items.withUnsafeMutableBufferPointer { buffer, count in
let source = buffer._extracting(last: count)
unsafe self.insert(moving: source, at: index)
@@ -199,6 +200,7 @@ extension RigidArray where Element: ~Copyable {
moving items: inout OutputSpan<Element>,
at index: Int
) {
// FIXME: Remove when OutputSpan starts conforming to RangeReplaceableContainer
items.withUnsafeMutableBufferPointer { buffer, count in
let source = buffer._extracting(first: count)
unsafe self.insert(moving: source, at: index)
@@ -206,6 +208,7 @@ extension RigidArray where Element: ~Copyable {
}
}
#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
@@ -234,88 +237,9 @@ extension RigidArray where Element: ~Copyable {
self.insert(moving: &source, at: index)
}
}
}
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
#if UnstableContainersPreview
/// Inserts the elements of a given array into the given position in this
/// array by consuming the source container.
///
/// All existing elements at or following the specified position are moved to
/// make room for the new items.
///
/// If the capacity of the array isn't sufficient to accommodate the new
/// elements, then this method triggers a runtime error.
///
/// - Parameters:
/// - items: The array whose contents to move into `self`.
/// - index: The position at which to insert the new items.
/// `index` must be a valid index in the array.
///
/// - Complexity: O(`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
var items = items
self.insert(moving: &items, at: index)
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension RigidArray 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 target array does not have sufficient capacity to hold the
/// specified number of new items, then this triggers a runtime error.
///
/// 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 ring buffer 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`)
@_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 RigidArray {
/// Copies the elements of a fully initialized buffer pointer into this
@@ -406,82 +330,28 @@ extension RigidArray {
}
}
#if compiler(>=6.4) && UnstableContainersPreview
#if !UnstableContainersPreview
@_alwaysEmitIntoClient
internal mutating func _insertContainer<
C: Container<Element> & ~Copyable & ~Escapable
>(
at index: Int,
copying items: borrowing C,
newCount: Int
package mutating func _insertCollection(
addingCount newCount: Int,
copying items: some Collection<Element>,
at index: Index
) {
var it = items.makeIterableIterator_()
insert(addingCount: newCount, at: index) { target in
while !target.isFull {
let source = it.nextSpan_(maximumCount: target.freeCapacity)
precondition(!source.isEmpty, "Broken container: mismatching count")
target._append(copying: source)
let done: Void? = items.withContiguousStorageIfAvailable { buffer in
precondition(buffer.count == newCount, "Broken Collection: mismatching count")
self.insert(addingCount: buffer.count, at: index) { target in
target._append(copying: buffer)
}
}
precondition(it.nextSpan_().isEmpty, "Broken container: mismatching count")
}
#endif
@inlinable
internal mutating func _insertCollection(
at index: Int,
copying items: some Collection<Element>,
newCount: Int
) {
_checkValidIndex(index)
precondition(newCount <= freeCapacity, "RigidArray capacity overflow")
let gap = unsafe _openGap(at: index, count: newCount)
let done: Void? = items.withContiguousStorageIfAvailable { buffer in
let i = unsafe gap._initializePrefix(copying: buffer)
precondition(
i == newCount,
"Broken Collection: count doesn't match contents")
_count += newCount
}
if done != nil { return }
var (it, copied) = unsafe items._copyContents(initializing: gap)
precondition(
it.next() == nil && copied == newCount,
"Broken Collection: count doesn't match contents")
_count += newCount
}
#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 capacity of the array isn't sufficient to accommodate the new
/// elements, then this method triggers a runtime error.
///
/// - 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(`self.count` + `newElements.count`).
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
C: Container<Element> & ~Copyable & ~Escapable
>(
copying newElements: borrowing C, at index: Int
) {
_insertContainer(
at: index, copying: newElements, newCount: newElements.count)
var it = items.makeIterator()
self.insert(addingCount: newCount, at: index) { target in
while !target.isFull {
guard let item = it.next() else { preconditionFailure() }
target.append(item)
}
}
precondition(it.next() == nil, "Broken Collection")
}
#endif
@@ -504,46 +374,16 @@ extension RigidArray {
/// a valid index of the array.
///
/// - Complexity: O(`count` + `newElements.count`)
@inlinable
@inline(__always)
public mutating func insert(
copying newElements: some Collection<Element>, at index: Int
) {
_insertCollection(
at: index, copying: newElements, newCount: newElements.count)
}
#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 capacity of the array isn't sufficient to accommodate the new
/// elements, then this method triggers a runtime error.
///
/// - 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(`self.count` + `newElements.count`)
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
C: Container<Element> & Collection<Element>
>(
copying newElements: borrowing C, at index: Int
public mutating func insert(
copying items: some Collection<Element>, at index: Int
) {
_insertContainer(
at: index, copying: newElements, newCount: newElements.count)
// FIXME: Remove this -- RangeReplaceContainer already has this algorithm.
// (Note that this would be source breaking while RRC lives in the separate
// ContainersPreview module.)
_insertCollection(addingCount: items.count, copying: items, at: index)
}
#endif
}
#endif
@@ -114,13 +114,14 @@ extension RigidArray where Element: ~Copyable {
/// - Complexity: O(`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 RigidArray 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;
@@ -129,10 +130,11 @@ extension RigidArray where Element: ~Copyable {
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public mutating func popLast() -> Element? {
// FIXME: Remove this in favor of a standard algorithm.
// FIXME: Remove this in favor of the RangeReplaceableContainer algorithm.
if isEmpty { return nil }
return removeLast()
}
#endif
}
#endif
@@ -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