[BasicContainers] Add Producer-based array operations

This commit is contained in:
Karoy Lorentey committed 2026-02-27 17:58:21 -08:00
1 parent 50978111dc
commit bc7b0ff73d
6 files changed
+352

No files matched your search

@@ -341,6 +341,65 @@ extension UniqueArray where Element: ~Copyable {
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
/// Replaces the specified range of elements by at most `maximumCount` items
/// generated by a producer.
///
/// This method has the effect of removing the specified range of elements
/// from the array and inserting the new elements starting at the same
/// location. The number of new elements need not match the number of elements
/// being removed.
///
/// If the array doesn't have sufficient capacity to accommodate the new
/// elements, then this method reallocates the array's storage to grow it,
/// using a geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
/// case is more directly expressed by calling `insert(moving:at:)`.
///
/// Likewise, if the given producer is empty, then this method removes the
/// elements in the given subrange without replacement. This case is more
/// directly expressed by calling `removeSubrange`.
///
/// This operation inserts as many items as the producer can generate before
/// either reaching `maximumCount`, or the producer hitting its end, or
/// throwing an error. If the producer has more than `maximumCount` 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:
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newItemCount: The maximum number of items to insert into the deque.
/// - producer: A producer that generates the items to append.
///
/// - Complexity: O(`self.count` + `newItemCount`)
@_alwaysEmitIntoClient
public mutating func replace<
E: Error,
P: Producer<Element, E> & ~Copyable & ~Escapable
>(
removing subrange: Range<Int>,
addingCount newItemCount: Int,
from producer: inout P
) throws(E) {
try replace(removing: subrange, addingCount: newItemCount) { target throws(E) in
while !target.isFull, try producer.generate(into: &target) {
// Do nothing
}
}
}
#endif
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray {
/// Replaces the specified subrange of elements by copying the elements of