mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
Rigid, Unique types: Rework OutputSpan-based append/prepend/insert/replaceSubrange primitives
This commit is contained in:
19 files changed
+887
-260
No files matched your search
@@ -18,6 +18,65 @@ import ContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
/// Replaces the specified range of elements by a given count of new items,
|
||||
/// using a callback to directly initialize array storage by populating
|
||||
/// an output span.
|
||||
///
|
||||
/// The number of new items need not match the number of elements being
|
||||
/// removed.
|
||||
///
|
||||
/// This method has the same overall effect as calling
|
||||
///
|
||||
/// try array.removeSubrange(subrange)
|
||||
/// try array.insert(
|
||||
/// addingCount: newItemCount,
|
||||
/// at: subrange.lowerBound,
|
||||
/// initializingWith: initializer)
|
||||
///
|
||||
/// Except it performs faster (by a constant factor), by avoiding moving
|
||||
/// some items in the array twice.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to perform the replacement,
|
||||
/// then this reallocates storage to extend its capacity, using a geometric
|
||||
/// growth rate.
|
||||
///
|
||||
/// If the callback fails to fully populate its output span or if
|
||||
/// it throws an error, then the array keeps all items that were
|
||||
/// successfully initialized before the callback terminated the prepend.
|
||||
///
|
||||
/// Partial insertions create a gap in array storage that needs to be
|
||||
/// closed by moving newly inserted 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 replace the old subrange.
|
||||
/// - initializer: A callback that gets called at most once to directly
|
||||
/// populate newly reserved storage within the array. The function
|
||||
/// is always called with an empty output span.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`) in addition to the complexity
|
||||
/// of the callback invocations.
|
||||
@inlinable
|
||||
public mutating func replaceSubrange<E: Error>(
|
||||
_ subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
||||
) throws(E) -> Void {
|
||||
precondition(
|
||||
subrange.lowerBound >= 0 && subrange.upperBound <= self.count,
|
||||
"Index range out of bounds")
|
||||
precondition(newItemCount >= 0, "Cannot add a negative number of items")
|
||||
// FIXME: Avoid moving the subsequent elements twice on resize.
|
||||
_ensureFreeCapacity(newItemCount - subrange.count)
|
||||
try _storage._uncheckedReplaceSubrange(
|
||||
subrange,
|
||||
addingCount: newItemCount,
|
||||
initializingWith: initializer)
|
||||
}
|
||||
|
||||
/// Replaces the specified range of elements by a given count of new items,
|
||||
/// using a callback to directly initialize array storage by populating
|
||||
/// an output span.
|
||||
@@ -43,28 +102,99 @@ extension UniqueArray where Element: ~Copyable {
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newCount: the number of items to replace the old subrange.
|
||||
/// - body: A callback that gets called at most once to directly
|
||||
/// - body: A callback that gets called exactly once to directly
|
||||
/// populate newly reserved storage within the array. The function
|
||||
/// is called with an empty output span of capacity `newCount`,
|
||||
/// and it must fully populate it before returning.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newCount`)
|
||||
@available(*, deprecated, renamed: "replaceSubrange(_:addingCapacity:initializingWith:)")
|
||||
@inlinable
|
||||
public mutating func replaceSubrange<Result: ~Copyable>(
|
||||
_ subrange: Range<Int>,
|
||||
newCount: Int,
|
||||
initializingWith body: (inout OutputSpan<Element>) -> Result
|
||||
) -> Result {
|
||||
// FIXME: Should we allow throwing (and a partially filled output span)?
|
||||
// FIXME: Should we have a version of this with two closures, to allow custom-consuming the old items?
|
||||
// replaceSubrange(5..<10, newCount: 3, consumingWith: {...}, initializingWith: {...})
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
_ensureFreeCapacity(newCount - subrange.count)
|
||||
return _storage.replaceSubrange(
|
||||
subrange, newCount: newCount, initializingWith: body)
|
||||
var result: Result? = nil
|
||||
self.replaceSubrange(subrange, addingCount: newCount) { target in
|
||||
result = body(&target)
|
||||
}
|
||||
return result!
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// Replaces the specified range of elements by a given count of new items,
|
||||
/// using callbacks to consume old items, and to then insert new ones.
|
||||
///
|
||||
/// The number of new items need not match the number of elements being
|
||||
/// removed.
|
||||
///
|
||||
/// This method has the same overall effect as calling
|
||||
///
|
||||
/// try array.consume(subrange, consumingWith: consumer)
|
||||
/// try array.insert(
|
||||
/// addingCount: newItemCount,
|
||||
/// at: subrange.lowerBound,
|
||||
/// initializingWith: initializer)
|
||||
///
|
||||
/// Except it performs faster (by a constant factor), by avoiding moving
|
||||
/// some items in the deque twice.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to perform the replacement,
|
||||
/// then this reallocates storage to extend its capacity, using a geometric
|
||||
/// growth rate.
|
||||
///
|
||||
/// The `consumer` callback is not required to fully depopulate its input
|
||||
/// span. Any items the callback leaves in the span still get removed and
|
||||
/// discarded from the array before insertions begin.
|
||||
///
|
||||
/// The `initializer` callback is not required to fully populate its
|
||||
/// output span, and it is allowed to throw an error. In such cases, the
|
||||
/// deque keeps all items that were successfully initialized before the
|
||||
/// callback terminated.
|
||||
///
|
||||
/// Partial insertions create a gap in array storage that needs to be
|
||||
/// closed by moving newly inserted 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 deque to replace. The bounds of
|
||||
/// the range must be valid indices in the deque.
|
||||
/// - newItemCount: the maximum number of items to replace the old subrange.
|
||||
/// - consumer: A callback that gets called at most once to consume
|
||||
/// the elements to be removed directly from the deque's storage. The
|
||||
/// function is always called with a non-empty input span.
|
||||
/// - initializer: A callback that gets called at most once to directly
|
||||
/// populate newly reserved storage within the deque. The function
|
||||
/// is always called with an empty output span.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`) in addition to the
|
||||
/// complexity of the callback invocations.
|
||||
@inlinable
|
||||
public mutating func replaceSubrange<E: Error>(
|
||||
_ subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
consumingWith consumer: (inout InputSpan<Element>) -> Void,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
||||
) throws(E) -> Void {
|
||||
precondition(
|
||||
subrange.lowerBound >= 0 && subrange.upperBound <= self.count,
|
||||
"Index range out of bounds")
|
||||
precondition(newItemCount >= 0, "Cannot add a negative number of items")
|
||||
_ensureFreeCapacity(newItemCount - subrange.count)
|
||||
try _storage._uncheckedReplaceSubrange(
|
||||
subrange,
|
||||
addingCount: newItemCount,
|
||||
consumingWith: consumer,
|
||||
initializingWith: initializer)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
/// Replaces the specified range of elements by moving the elements of a
|
||||
|
||||
Reference in new issue
Block a user