mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
[BasicContainers] Add Producer-based array operations
This commit is contained in:
6 files changed
+352
No files matched your search
@@ -176,6 +176,71 @@ extension UniqueArray where Element: ~Copyable {
|
||||
self.append(moving: &items)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// 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) {
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage.append(addingCount: newItemCount, from: &producer)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// 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) {
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
_ensureFreeCapacity(c)
|
||||
try self.append(addingCount: freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user