Rigid, Unique types: Rework OutputSpan-based append/prepend/insert/replaceSubrange primitives

This commit is contained in:
Karoy Lorentey committed 2026-02-04 17:29:43 -08:00
1 parent 9503f8fe57
commit 06f2ec073d
19 files changed
+887 -260

No files matched your search

@@ -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!
}
}