replaceSubrange(_:...) → replace(removing:...)

This commit is contained in:
Karoy Lorentey committed 2026-02-05 14:56:51 -08:00
1 parent 6c1d4ab9c8
commit 653a60b8e7
17 files changed
+1200 -409

No files matched your search

@@ -66,36 +66,6 @@ extension UniqueArray where Element: ~Copyable {
addingCount: newItemCount,
initializingWith: initializer)
}
/// 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.
///
/// - Parameters
/// - count: The number of items to append to the array.
/// - 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 initializer: (inout OutputSpan<Element>) throws(E) -> Result
) throws(E) -> Result {
var result: Result? = nil
try append(addingCount: count) { target throws(E) in
result = try initializer(&target)
}
return result.take()!
}
}
@available(SwiftStdlib 5.0, *)
@@ -0,0 +1,429 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2)
@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.
///
/// - Parameters
/// - count: The number of items to append to the array.
/// - 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 initializer: (inout OutputSpan<Element>) throws(E) -> Result
) throws(E) -> Result {
var result: Result? = nil
try append(addingCount: count) { target throws(E) in
result = try initializer(&target)
}
return result.take()!
}
/// 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 {
var result: Result? = nil
self.insert(addingCount: count, at: index) { target in
result = body(&target)
}
return result!
}
/// 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.
///
/// This method has the effect of removing the specified range of elements
/// from the array and inserting room for 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 does not have sufficient capacity to perform the replacement,
/// then this reallocates storage to extend its capacity, using a geometric
/// growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, then
/// this method is equivalent to calling
/// `insert(count: newCount, initializingWith: body)`.
///
/// Likewise, if you pass a zero for `newCount`, then this method
/// removes the elements in the given subrange without any replacement.
/// Calling `removeSubrange(subrange)` is preferred in this case.
///
/// - Parameters
/// - 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 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: "replace(removing:addingCount:initializingWith:)")
@inlinable
public mutating func replaceSubrange<Result: ~Copyable>(
_ subrange: Range<Int>,
newCount: Int,
initializingWith body: (inout OutputSpan<Element>) -> Result
) -> Result {
var result: Result? = nil
self.replace(removing: subrange, addingCount: newCount) { target in
result = body(&target)
}
return result!
}
/// Replaces the specified range of elements by moving the elements of a
/// fully initialized buffer into their place. On return, the buffer is left
/// in an uninitialized state.
///
/// 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 does not have sufficient capacity to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: A fully initialized buffer whose contents to move into
/// the array.
///
/// - Complexity: O(`self.count` + `newElements.count`)
@available(*, deprecated, renamed: "replace(removing:moving:)")
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
moving newElements: UnsafeMutableBufferPointer<Element>,
) {
self.replace(removing: subrange, moving: newElements)
}
/// Replaces the specified range of elements by moving the contents of an
/// output span into their place. On return, the span is left empty.
///
/// 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 does not have sufficient capacity to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(moving:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - items: An output span whose contents are to be moved into the array.
///
/// - Complexity: O(`self.count` + `items.count`)
@available(*, deprecated, renamed: "replace(removing:moving:)")
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
moving items: inout OutputSpan<Element>
) {
self.replace(removing: subrange, moving: &items)
}
/// Replaces the specified range of elements by moving the elements of a
/// another array into their place. On return, the source array
/// becomes empty, but it is not destroyed, and it preserves its original
/// storage capacity.
///
/// 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 does not have sufficient capacity to hold enough elements,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: An array whose contents to move into `self`.
///
/// - Complexity: O(`self.count` + `newElements.count`)
@available(*, deprecated, renamed: "replace(removing:moving:)")
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
moving newElements: inout RigidArray<Element>,
) {
self.replace(removing: subrange, moving: &newElements)
}
/// Replaces the specified range of elements by moving the elements of a
/// given array into their place, consuming it in the process.
///
/// 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 does not have sufficient capacity to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: An array whose contents to move into `self`.
///
/// - Complexity: O(`self.count` + `newElements.count`)
@available(*, deprecated, renamed: "replace(removing:consuming:)")
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
consuming newElements: consuming RigidArray<Element>,
) {
self.replace(removing: subrange, consuming: newElements)
}
}
@available(SwiftStdlib 5.0, *)
extension UniqueArray {
/// Replaces the specified subrange of elements by copying the elements of
/// the given buffer pointer, which must be fully initialized.
///
/// 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 capacity of the array isn't sufficient to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters:
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: The new elements to copy into the collection.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@available(*, deprecated, renamed: "replace(removing:copying:)")
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
copying newElements: UnsafeBufferPointer<Element>
) {
self.replace(removing: subrange, copying: newElements)
}
/// Replaces the specified subrange of elements by copying the elements of
/// the given buffer pointer, which must be fully initialized.
///
/// 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 capacity of the array isn't sufficient to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length buffer as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters:
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: The new elements to copy into the collection.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@available(*, deprecated, renamed: "replace(removing:copying:)")
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
copying newElements: UnsafeMutableBufferPointer<Element>
) {
self.replace(removing: subrange, copying: newElements)
}
/// Replaces the specified subrange of elements by copying the elements of
/// the given span.
///
/// 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 capacity of the array isn't sufficient to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length span as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters:
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: The new elements to copy into the collection.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@available(*, deprecated, renamed: "replace(removing:copying:)")
@inlinable
public mutating func replaceSubrange(
_ subrange: Range<Int>,
copying newElements: Span<Element>
) {
self.replace(removing: subrange, copying: newElements)
}
/// Replaces the specified subrange of elements by copying the elements of
/// the given collection.
///
/// 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 capacity of the array isn't sufficient to perform the replacement,
/// then this reallocates the array's storage to extend its capacity, using a
/// geometric growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, this method
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
/// the `insert(copying:at:)` method instead is preferred in this case.
///
/// Likewise, if you pass a zero-length collection as the `newElements`
/// parameter, this method removes the elements in the given subrange
/// without replacement. Calling the `removeSubrange(_:)` method instead is
/// preferred in this case.
///
/// - Parameters:
/// - subrange: The subrange of the array to replace. The bounds of
/// the range must be valid indices in the array.
/// - newElements: The new elements to copy into the collection.
///
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@available(*, deprecated, renamed: "replace(removing:copying:)")
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
copying newElements: __owned some Collection<Element>
) {
self.replace(removing: subrange, copying: newElements)
}
}
#endif
@@ -92,41 +92,6 @@ extension UniqueArray where Element: ~Copyable {
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 {
var result: Result? = nil
self.insert(addingCount: count, at: index) { target in
result = body(&target)
}
return result!
}
}
@available(SwiftStdlib 5.0, *)
@@ -350,7 +315,6 @@ extension UniqueArray {
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if false // FIXME: This needs a container with an exact count.
/// Copies the elements of a container into this array at the specified
/// position.
///
@@ -384,7 +348,6 @@ extension UniqueArray {
_ensureFreeCapacity(c)
_storage._insertContainer(at: index, copying: newElements, newCount: c)
}
#endif
#endif
/// Copies the elements of a collection into this array at the specified
@@ -419,7 +382,6 @@ extension UniqueArray {
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if false // FIXME: This needs a container with an exact count.
/// Copies the elements of a container into this array at the specified
/// position.
///
@@ -454,7 +416,6 @@ extension UniqueArray {
_storage._insertContainer(at: index, copying: newElements, newCount: c)
}
#endif
#endif
}
#endif
@@ -60,8 +60,8 @@ extension UniqueArray where Element: ~Copyable {
/// - Complexity: O(`self.count` + `newItemCount`) in addition to the complexity
/// of the callback invocations.
@inlinable
public mutating func replaceSubrange<E: Error>(
_ subrange: Range<Int>,
public mutating func replace<E: Error>(
removing subrange: Range<Int>,
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
@@ -71,55 +71,10 @@ extension UniqueArray where Element: ~Copyable {
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.
///
/// This method has the effect of removing the specified range of elements
/// from the array and inserting room for 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 does not have sufficient capacity to perform the replacement,
/// then this reallocates storage to extend its capacity, using a geometric
/// growth rate.
///
/// If you pass a zero-length range as the `subrange` parameter, then
/// this method is equivalent to calling
/// `insert(count: newCount, initializingWith: body)`.
///
/// Likewise, if you pass a zero for `newCount`, then this method
/// removes the elements in the given subrange without any replacement.
/// Calling `removeSubrange(subrange)` is preferred in this case.
///
/// - Parameters
/// - 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 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 {
var result: Result? = nil
self.replaceSubrange(subrange, addingCount: newCount) { target in
result = body(&target)
}
return result!
try _storage._uncheckedReplace(
removing: subrange,
addingCount: newItemCount,
initializingWith: initializer)
}
}
@@ -164,10 +119,10 @@ extension UniqueArray where Element: ~Copyable {
/// - 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.
/// - 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 deque. The function
/// is always called with an empty output span.
@@ -175,10 +130,10 @@ extension UniqueArray where Element: ~Copyable {
/// - 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,
public mutating func replace<E: Error>(
removing subrange: Range<Int>,
consumingWith consumer: (inout InputSpan<Element>) -> Void,
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) -> Void {
precondition(
@@ -186,10 +141,10 @@ extension UniqueArray where Element: ~Copyable {
"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,
try _storage._uncheckedReplace(
removing: subrange,
consumingWith: consumer,
addingCount: newItemCount,
initializingWith: initializer)
}
#endif
@@ -227,13 +182,13 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Complexity: O(`self.count` + `newElements.count`)
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
moving newElements: UnsafeMutableBufferPointer<Element>,
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(newElements.count - subrange.count)
_storage.replaceSubrange(subrange, moving: newElements)
_storage.replace(removing: subrange, moving: newElements)
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@@ -265,13 +220,13 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Complexity: O(`self.count` + `items.count`)
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
moving items: inout InputSpan<Element>
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(items.count - subrange.count)
_storage.replaceSubrange(subrange, moving: &items)
_storage.replace(removing: subrange, moving: &items)
}
#endif
@@ -303,13 +258,13 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Complexity: O(`self.count` + `items.count`)
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
moving items: inout OutputSpan<Element>
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(items.count - subrange.count)
_storage.replaceSubrange(subrange, moving: &items)
_storage.replace(removing: subrange, moving: &items)
}
/// Replaces the specified range of elements by moving the elements of a
@@ -342,13 +297,13 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Complexity: O(`self.count` + `newElements.count`)
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
moving newElements: inout RigidArray<Element>,
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(newElements.count - subrange.count)
_storage.replaceSubrange(subrange, moving: &newElements)
_storage.replace(removing: subrange, moving: &newElements)
}
}
@@ -382,11 +337,11 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Complexity: O(`self.count` + `newElements.count`)
@_alwaysEmitIntoClient
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
consuming newElements: consuming RigidArray<Element>,
) {
replaceSubrange(subrange, moving: &newElements)
replace(removing: subrange, moving: &newElements)
}
}
@@ -422,13 +377,13 @@ extension UniqueArray {
/// *m* is the count of `newElements`.
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
copying newElements: UnsafeBufferPointer<Element>
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(newElements.count)
unsafe _storage.replaceSubrange(subrange, copying: newElements)
unsafe _storage.replace(removing: subrange, copying: newElements)
}
/// Replaces the specified subrange of elements by copying the elements of
@@ -461,12 +416,13 @@ extension UniqueArray {
/// *m* is the count of `newElements`.
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
copying newElements: UnsafeMutableBufferPointer<Element>
) {
unsafe self.replaceSubrange(
subrange, copying: UnsafeBufferPointer(newElements))
unsafe self.replace(
removing: subrange,
copying: UnsafeBufferPointer(newElements))
}
/// Replaces the specified subrange of elements by copying the elements of
@@ -498,17 +454,16 @@ extension UniqueArray {
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
/// *m* is the count of `newElements`.
@inlinable
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
copying newElements: Span<Element>
) {
// FIXME: Avoid moving the subsequent elements twice.
_ensureFreeCapacity(newElements.count)
_storage.replaceSubrange(subrange, copying: newElements)
_storage.replace(removing: subrange, copying: newElements)
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if false // FIXME: This needs a container with an exact count.
/// Replaces the specified subrange of elements by copying the elements of
/// the given container.
///
@@ -539,19 +494,20 @@ extension UniqueArray {
/// *m* is the count of `newElements`.
@inlinable
@inline(__always)
public mutating func replaceSubrange<
public mutating func replace<
C: Container<Element> & ~Copyable & ~Escapable
>(
_ subrange: Range<Int>,
removing subrange: Range<Int>,
copying newElements: borrowing C
) {
// FIXME: Avoid moving the subsequent elements twice.
let c = newElements.count
_ensureFreeCapacity(c - subrange.count)
_storage._replaceSubrange(
subrange, copyingContainer: newElements, newCount: c)
_storage._replace(
removing: subrange,
copyingContainer: newElements,
newCount: c)
}
#endif
#endif
/// Replaces the specified subrange of elements by copying the elements of
@@ -584,19 +540,20 @@ extension UniqueArray {
/// *m* is the count of `newElements`.
@inlinable
@inline(__always)
public mutating func replaceSubrange(
_ subrange: Range<Int>,
public mutating func replace(
removing subrange: Range<Int>,
copying newElements: __owned some Collection<Element>
) {
// FIXME: Avoid moving the subsequent elements twice.
let c = newElements.count
_ensureFreeCapacity(c)
_storage._replaceSubrange(
subrange, copyingCollection: newElements, newCount: c)
_storage._replace(
removing: subrange,
copyingCollection: newElements,
newCount: c)
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if false // FIXME: This needs a container with an exact count.
/// Replaces the specified subrange of elements by copying the elements of
/// the given container.
///
@@ -636,10 +593,11 @@ extension UniqueArray {
// FIXME: Avoid moving the subsequent elements twice.
let c = newElements.count
_ensureFreeCapacity(c - subrange.count)
_storage._replaceSubrange(
subrange, copyingContainer: newElements, newCount: c)
_storage._replace(
removing: subrange,
copyingContainer: newElements,
newCount: c)
}
#endif
#endif
}
#endif