mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +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
@@ -36,6 +36,37 @@ extension UniqueArray where Element: ~Copyable {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
/// Append a given number of items to the end of this array by populating
|
||||
/// an output span.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold the requested
|
||||
/// number of new elements, then this reallocates the array's storage to
|
||||
/// grow 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 insertion.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The number of items to append to the array.
|
||||
/// - initializer: A callback that gets called at most once to directly
|
||||
/// populate newly reserved storage within the array. The function
|
||||
/// is allowed to initialize fewer than `uninitializedCount` items.
|
||||
/// The array is appended however many items the callback adds to the
|
||||
/// output span before it returns (or before it throws an error).
|
||||
///
|
||||
/// - Complexity: O(`uninitializedCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<E: Error>(
|
||||
addingCount newItemCount: Int,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
||||
) throws(E) {
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
return try _storage.append(
|
||||
addingCount: newItemCount,
|
||||
initializingWith: initializer)
|
||||
}
|
||||
|
||||
/// Append a given number of items to the end of this array by populating
|
||||
/// an output span.
|
||||
///
|
||||
@@ -45,20 +76,25 @@ extension UniqueArray where Element: ~Copyable {
|
||||
///
|
||||
/// - Parameters
|
||||
/// - count: The number of items to append to the array.
|
||||
/// - body: A callback that gets called at most once to directly
|
||||
/// - initializer: A callback that gets called exactly once to directly
|
||||
/// populate newly reserved storage within the array. The function
|
||||
/// is allowed to initialize fewer than `count` items. The array is
|
||||
/// appended however many items the callback adds to the output span
|
||||
/// before it returns (or before it throws an error).
|
||||
///
|
||||
/// - Complexity: O(`count`)
|
||||
@available(*, deprecated, renamed: "append(addingCount:initializingWith:)")
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<E: Error, Result: ~Copyable>(
|
||||
count: Int,
|
||||
initializingWith body: (inout OutputSpan<Element>) throws(E) -> Result
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
_ensureFreeCapacity(count)
|
||||
return try _storage.append(count: count, initializingWith: body)
|
||||
var result: Result? = nil
|
||||
try append(addingCount: count) { target throws(E) in
|
||||
result = try initializer(&target)
|
||||
}
|
||||
return result.take()!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,12 +51,23 @@ extension UniqueArray where Element: ~Copyable {
|
||||
/// position, using a callback to directly initialize array storage by
|
||||
/// populating an output span.
|
||||
///
|
||||
/// All existing elements at or following the specified position are moved to
|
||||
/// make room for the new items.
|
||||
/// Existing elements in the array's storage are moved towards the back as
|
||||
/// needed to make room for the new items.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold the new elements,
|
||||
/// then this reallocates storage to extend its capacity, using a geometric
|
||||
/// growth rate.
|
||||
/// then this operation reallocates storage to extend its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// var buffer = UniqueArray<Int>()
|
||||
/// buffer.append([-999, 999])
|
||||
/// var i = 0
|
||||
/// buffer.insert(capacity: 3, at: 1) { target in
|
||||
/// while !target.isFull {
|
||||
/// target.append(i)
|
||||
/// i += 1
|
||||
/// }
|
||||
/// }
|
||||
/// // `buffer` now contains [-999, 0, 1, 2, 999]
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - count: The number of items to insert into the array.
|
||||
@@ -68,15 +79,53 @@ extension UniqueArray where Element: ~Copyable {
|
||||
/// supplied count, and it must fully populate it before returning.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `count`)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<E: Error>(
|
||||
addingCount newItemCount: Int,
|
||||
at index: Int,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
||||
) throws(E) {
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage.insert(
|
||||
addingCount: newItemCount,
|
||||
at: index,
|
||||
initializingWith: initializer)
|
||||
}
|
||||
|
||||
/// Inserts a given number of new items into this array at the specified
|
||||
/// position, using a callback to directly initialize array storage by
|
||||
/// populating an output span.
|
||||
///
|
||||
/// All existing elements at or following the specified position are moved to
|
||||
/// make room for the new items.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold the new elements,
|
||||
/// then this reallocates storage to extend its capacity, using a geometric
|
||||
/// growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - count: The 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.
|
||||
/// - 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 matching the
|
||||
/// supplied count, and it must fully populate it before returning.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `count`)
|
||||
@available(*, deprecated, renamed: "insert(addingCount:at:initializingWith:)")
|
||||
@inlinable
|
||||
public mutating func insert<Result: ~Copyable>(
|
||||
count: Int,
|
||||
at index: Int,
|
||||
initializingWith body: (inout OutputSpan<Element>) -> Result
|
||||
) -> Result {
|
||||
// FIXME: This does not allow `body` to throw, to prevent having to move the tail twice. Is that okay?
|
||||
_ensureFreeCapacity(count)
|
||||
return _storage.insert(count: count, at: index, initializingWith: body)
|
||||
var result: Result? = nil
|
||||
self.insert(addingCount: count, at: index) { target in
|
||||
result = body(&target)
|
||||
}
|
||||
return result!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ extension UniqueArray where Element: ~Copyable {
|
||||
/// passing an input span to the given function to consume them in place.
|
||||
///
|
||||
/// - Parameter bounds: The subrange of items to consume from this array.
|
||||
/// - Parameter body: A function taking an input span of the removed items,
|
||||
/// - Parameter consumer: A function taking an input span of the removed items,
|
||||
/// allowing them to be consumed straight out of the array's storage.
|
||||
/// The function is not required to consume all items in the span;
|
||||
/// however, the span's remaining items will still be removed from
|
||||
@@ -167,9 +167,9 @@ extension UniqueArray where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func consumeSubrange<E: Error, Result: ~Copyable>(
|
||||
_ bounds: Range<Int>,
|
||||
consumingWith body: (inout InputSpan<Element>) throws(E) -> Result
|
||||
consumingWith consumer: (inout InputSpan<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try _storage.consumeSubrange(bounds, consumingWith: body)
|
||||
try _storage.consumeSubrange(bounds, consumingWith: consumer)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -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