mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[BasicContainers] Add Producer-based array operations
This commit is contained in:
6 files changed
+352
No files matched your search
@@ -225,6 +225,56 @@ extension UniqueArray where Element: ~Copyable {
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// 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) {
|
||||
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 {
|
||||
/// Copyies the elements of a fully initialized buffer pointer into this
|
||||
|
||||
Reference in new issue
Block a user