diff --git a/Sources/BasicContainers/CMakeLists.txt b/Sources/BasicContainers/CMakeLists.txt index f481716c..d0c1af97 100644 --- a/Sources/BasicContainers/CMakeLists.txt +++ b/Sources/BasicContainers/CMakeLists.txt @@ -27,6 +27,7 @@ target_sources(${module_name} PRIVATE "RigidArray/RigidArray+Append.swift" "RigidArray/RigidArray+Consumption.swift" "RigidArray/RigidArray+Container.swift" + "RigidArray/RigidArray+Deprecated.swift" "RigidArray/RigidArray+Descriptions.swift" "RigidArray/RigidArray+Equatable.swift" "RigidArray/RigidArray+Experimental.swift" @@ -39,6 +40,7 @@ target_sources(${module_name} PRIVATE "UniqueArray/UniqueArray+Append.swift" "UniqueArray/UniqueArray+Consumption.swift" "UniqueArray/UniqueArray+Container.swift" + "UniqueArray/UniqueArray+Deprecated.swift" "UniqueArray/UniqueArray+Descriptions.swift" "UniqueArray/UniqueArray+Equatable.swift" "UniqueArray/UniqueArray+Experimental.swift" diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 9ae0b3cd..302ce36a 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -87,35 +87,6 @@ extension RigidArray where Element: ~Copyable { } return try initializer(&span) } - - /// 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 store the new items in - /// the buffer, then this triggers a runtime error. - /// - /// - Parameters: - /// - count: 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 `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( - count: Int, - initializingWith initializer: (inout OutputSpan) 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, *) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift new file mode 100644 index 00000000..1ae26624 --- /dev/null +++ b/Sources/BasicContainers/RigidArray/RigidArray+Deprecated.swift @@ -0,0 +1,390 @@ +//===----------------------------------------------------------------------===// +// +// 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 RigidArray 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 store the new items in + /// the buffer, then this triggers a runtime error. + /// + /// - Parameters: + /// - count: 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 `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( + count: Int, + initializingWith initializer: (inout OutputSpan) 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 capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// - 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( + count: Int, + at index: Int, + initializingWith body: (inout OutputSpan) -> 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 capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// If you pass a zero-length range as the `subrange` parameter, then + /// this method is equivalent to calling + /// `insert(count: newCount, at: subrange.lowerbound, initializingWith: body)`. + /// + /// Likewise, if you pass a zero for `newCount`, then this method + /// removes the elements in the given subrange without any replacement. + /// This case is more directly expressed by `removeSubrange(subrange)`. + /// + /// - 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:addingCapacity:initializingWith:)") + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, + newCount: Int, + initializingWith body: (inout OutputSpan) -> 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 capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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. + /// - 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, + moving newElements: UnsafeMutableBufferPointer, + ) { + 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 capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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, + moving items: inout OutputSpan + ) { + 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 capacity of the array isn't sufficient to accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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, + moving newElements: inout RigidArray, + ) { + self.replace(removing: subrange, moving: &newElements) + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidArray { + /// 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 accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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(`self.count` + `newElements.count`) + @available(*, deprecated, renamed: "replace(removing:copying:)") + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, + copying newElements: UnsafeBufferPointer + ) { + 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 accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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(`self.count` + `newElements.count`) + @available(*, deprecated, renamed: "replace(removing:copying:)") + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, + copying newElements: UnsafeMutableBufferPointer + ) { + 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 accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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(`self.count` + `newElements.count`) + @available(*, deprecated, renamed: "replace(removing:copying:)") + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, + copying newElements: Span + ) { + self.replace(removing: subrange, copying: newElements) + } + + @available(*, unavailable) + @usableFromInline + internal mutating func _replaceSubrange( + _ subrange: Range, + copyingCollection newElements: __owned some Collection, + newCount: Int + ) { + self._replace( + removing: subrange, + copyingCollection: newElements, + newCount: newCount) + } + + /// 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 accommodate the new + /// elements, then this method triggers a runtime error. + /// + /// 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(`self.count` + `newElements.count`) + @available(*, deprecated, renamed: "replace(removing:copying:)") + @inlinable + @inline(__always) + public mutating func replaceSubrange( + _ subrange: Range, + copying newElements: __owned some Collection + ) { + self.replace(removing: subrange, copying: newElements) + } +} + +#endif diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift index d56b06ad..d924fee9 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift @@ -115,40 +115,6 @@ extension RigidArray where Element: ~Copyable { } try initializer(&span) } - - /// 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 capacity of the array isn't sufficient to accommodate the new - /// elements, then this method triggers a runtime error. - /// - /// - 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( - count: Int, - at index: Int, - initializingWith body: (inout OutputSpan) -> Result - ) -> Result { - var result: Result? = nil - self.insert(addingCount: count, at: index) { target in - result = body(&target) - } - return result! - } } @available(SwiftStdlib 5.0, *) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift index 5b8ba7a2..511b0aa5 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift @@ -59,8 +59,8 @@ extension RigidArray where Element: ~Copyable { /// - Complexity: O(`self.count` + `newItemCount`) in addition to the complexity /// of the callback invocations. @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { @@ -71,15 +71,15 @@ extension RigidArray where Element: ~Copyable { precondition( newItemCount - subrange.count <= freeCapacity, "RigidArray capacity overflow") - try _uncheckedReplaceSubrange( - subrange, + try _uncheckedReplace( + removing: subrange, addingCount: newItemCount, initializingWith: initializer) } @_alwaysEmitIntoClient - internal mutating func _uncheckedReplaceSubrange( - _ subrange: Range, + internal mutating func _uncheckedReplace( + removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { @@ -99,50 +99,6 @@ extension RigidArray where Element: ~Copyable { } try initializer(&span) } - - /// 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 capacity of the array isn't sufficient to accommodate the new - /// elements, then this method triggers a runtime error. - /// - /// If you pass a zero-length range as the `subrange` parameter, then - /// this method is equivalent to calling - /// `insert(count: newCount, at: subrange.lowerbound, initializingWith: body)`. - /// - /// Likewise, if you pass a zero for `newCount`, then this method - /// removes the elements in the given subrange without any replacement. - /// This case is more directly expressed by `removeSubrange(subrange)`. - /// - /// - 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( - _ subrange: Range, - newCount: Int, - initializingWith body: (inout OutputSpan) -> Result - ) -> Result { - var result: Result? = nil - self.replaceSubrange(subrange, addingCount: newCount) { target in - result = body(&target) - } - return result! - } } @available(SwiftStdlib 5.0, *) @@ -196,10 +152,10 @@ extension RigidArray where Element: ~Copyable { /// - Complexity: O(`self.count` + `newItemCount`) in addition to the /// complexity of the callback invocations. @inlinable - public mutating func replaceSubrange( - _ subrange: Range, - addingCount newItemCount: Int, + public mutating func replace( + removing subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { precondition( @@ -209,18 +165,18 @@ extension RigidArray where Element: ~Copyable { precondition( newItemCount - subrange.count <= freeCapacity, "RigidArray capacity overflow") - try _uncheckedReplaceSubrange( - subrange, - addingCount: newItemCount, + try _uncheckedReplace( + removing: subrange, consumingWith: consumer, + addingCount: newItemCount, initializingWith: initializer) } @_alwaysEmitIntoClient - internal mutating func _uncheckedReplaceSubrange( - _ subrange: Range, - addingCount newItemCount: Int, + internal mutating func _uncheckedReplace( + removing subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { do { @@ -281,11 +237,11 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving newElements: UnsafeMutableBufferPointer, ) { - replaceSubrange(subrange, addingCount: newElements.count) { target in + replace(removing: subrange, addingCount: newElements.count) { target in target._withUnsafeMutableBufferPointer { buffer, count in count = unsafe buffer._moveInitializePrefix(from: newElements) } @@ -320,13 +276,13 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving items: inout InputSpan ) { items.withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(last: count) - unsafe self.replaceSubrange(subrange, moving: source) + unsafe self.replace(removing: subrange, moving: source) count = 0 } } @@ -359,13 +315,13 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving items: inout OutputSpan ) { items._withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(first: count) - unsafe self.replaceSubrange(subrange, moving: source) + unsafe self.replace(removing: subrange, moving: source) count = 0 } } @@ -399,14 +355,14 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving newElements: inout RigidArray, ) { // FIXME: Remove this in favor of a generic algorithm over consumable containers unsafe newElements._unsafeEdit { buffer, count in let source = buffer._extracting(first: count) - unsafe self.replaceSubrange(subrange, moving: source) + unsafe self.replace(removing: subrange, moving: source) count = 0 } } @@ -442,12 +398,12 @@ extension RigidArray where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `newElements.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, consuming newElements: consuming RigidArray, ) { // FIXME: Remove this in favor of a generic algorithm over consumable containers - replaceSubrange(subrange, moving: &newElements) + replace(removing: subrange, moving: &newElements) } #endif } @@ -481,11 +437,11 @@ extension RigidArray { /// /// - Complexity: O(`self.count` + `newElements.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying newElements: UnsafeBufferPointer ) { - replaceSubrange(subrange, addingCount: newElements.count) { target in + replace(removing: subrange, addingCount: newElements.count) { target in target._withUnsafeMutableBufferPointer { buffer, count in count = unsafe buffer._initializePrefix(copying: newElements) } @@ -519,12 +475,12 @@ extension RigidArray { /// /// - Complexity: O(`self.count` + `newElements.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying newElements: UnsafeMutableBufferPointer ) { - unsafe self.replaceSubrange( - subrange, + unsafe self.replace( + removing: subrange, copying: UnsafeBufferPointer(newElements)) } @@ -555,26 +511,26 @@ extension RigidArray { /// /// - Complexity: O(`self.count` + `newElements.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying newElements: Span ) { unsafe newElements.withUnsafeBufferPointer { buffer in - unsafe self.replaceSubrange(subrange, copying: buffer) + unsafe self.replace(removing: subrange, copying: buffer) } } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @inlinable - internal mutating func _replaceSubrange< + internal mutating func _replace< C: Container & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, copyingContainer items: borrowing C, newCount: Int ) { var it = items.makeBorrowingIterator() - self.replaceSubrange(subrange, addingCount: newCount) { target in + self.replace(removing: subrange, addingCount: newCount) { target in while !target.isFull { let source = it.nextSpan(maximumCount: target.freeCapacity) precondition( @@ -590,12 +546,12 @@ extension RigidArray { #endif @inlinable - internal mutating func _replaceSubrange( - _ subrange: Range, + internal mutating func _replace( + removing subrange: Range, copyingCollection newElements: __owned some Collection, newCount: Int ) { - self.replaceSubrange(subrange, addingCount: newCount) { target in + self.replace(removing: subrange, addingCount: newCount) { target in target._withUnsafeMutableBufferPointer { dst, dstCount in let done: Void? = newElements.withContiguousStorageIfAvailable { src in let i = unsafe dst._initializePrefix(copying: src) @@ -645,14 +601,14 @@ extension RigidArray { /// - Complexity: O(`self.count` + `newElements.count`) @inlinable @inline(__always) - public mutating func replaceSubrange< + public mutating func replace< C: Container & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, copying newElements: borrowing C ) { - _replaceSubrange( - subrange, + _replace( + removing: subrange, copyingContainer: newElements, newCount: newElements.count) } @@ -686,12 +642,14 @@ extension RigidArray { /// - Complexity: O(`self.count` + `newElements.count`) @inlinable @inline(__always) - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying newElements: __owned some Collection ) { - _replaceSubrange( - subrange, copyingCollection: newElements, newCount: newElements.count) + _replace( + removing: subrange, + copyingCollection: newElements, + newCount: newElements.count) } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @@ -724,14 +682,16 @@ extension RigidArray { /// *m* is the count of `newElements`. @inlinable @inline(__always) - public mutating func replaceSubrange< + public mutating func replace< C: Container & Collection >( - _ subrange: Range, + removing subrange: Range, copying newElements: C ) { - _replaceSubrange( - subrange, copyingContainer: newElements, newCount: newElements.count) + _replace( + removing: subrange, + copyingContainer: newElements, + newCount: newElements.count) } #endif } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index cdb73ed9..1cc82d2c 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -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( - count: Int, - initializingWith initializer: (inout OutputSpan) 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, *) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift new file mode 100644 index 00000000..bde410a4 --- /dev/null +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Deprecated.swift @@ -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( + count: Int, + initializingWith initializer: (inout OutputSpan) 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( + count: Int, + at index: Int, + initializingWith body: (inout OutputSpan) -> 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( + _ subrange: Range, + newCount: Int, + initializingWith body: (inout OutputSpan) -> 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, + moving newElements: UnsafeMutableBufferPointer, + ) { + 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, + moving items: inout OutputSpan + ) { + 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, + moving newElements: inout RigidArray, + ) { + 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, + consuming newElements: consuming RigidArray, + ) { + 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, + copying newElements: UnsafeBufferPointer + ) { + 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, + copying newElements: UnsafeMutableBufferPointer + ) { + 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, + copying newElements: Span + ) { + 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, + copying newElements: __owned some Collection + ) { + self.replace(removing: subrange, copying: newElements) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift index 90dd25d4..0da21c28 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift @@ -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( - count: Int, - at index: Int, - initializingWith body: (inout OutputSpan) -> 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 diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift index 8c7c0e2a..56be6e92 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift @@ -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( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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( - _ subrange: Range, - newCount: Int, - initializingWith body: (inout OutputSpan) -> 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( - _ subrange: Range, - addingCount newItemCount: Int, + public mutating func replace( + removing subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) 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, + public mutating func replace( + removing subrange: Range, moving newElements: UnsafeMutableBufferPointer, ) { // 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, + public mutating func replace( + removing subrange: Range, moving items: inout InputSpan ) { // 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, + public mutating func replace( + removing subrange: Range, moving items: inout OutputSpan ) { // 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, + public mutating func replace( + removing subrange: Range, moving newElements: inout RigidArray, ) { // 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, + public mutating func replace( + removing subrange: Range, consuming newElements: consuming RigidArray, ) { - 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, + public mutating func replace( + removing subrange: Range, copying newElements: UnsafeBufferPointer ) { // 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, + public mutating func replace( + removing subrange: Range, copying newElements: UnsafeMutableBufferPointer ) { - 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, + public mutating func replace( + removing subrange: Range, copying newElements: Span ) { // 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 & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, 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, + public mutating func replace( + removing subrange: Range, copying newElements: __owned some Collection ) { // 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 diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift index 137a92f2..aee4d20c 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift @@ -59,8 +59,8 @@ extension RigidDeque where Element: ~Copyable { /// - Complexity: O(`self.count` + `newItemCount`) in addition to the complexity /// of the callback invocations. @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { @@ -71,8 +71,8 @@ extension RigidDeque where Element: ~Copyable { precondition( count - subrange.count + newItemCount <= capacity, "RigidDeque capacity overflow") - try _handle.uncheckedReplaceSubrange( - subrange, + try _handle.uncheckedReplace( + removing: subrange, addingCount: newItemCount, initializingWith: initializer) } @@ -135,10 +135,10 @@ extension RigidDeque where Element: ~Copyable { /// - Complexity: O(`self.count` + `newItemCount`) in addition to the /// complexity of the callback invocations. @inlinable - public mutating func replaceSubrange( - _ subrange: Range, - addingCount newItemCount: Int, + public mutating func replace( + removing subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { precondition( @@ -148,10 +148,10 @@ extension RigidDeque where Element: ~Copyable { precondition( count - subrange.count + newItemCount <= capacity, "RigidDeque capacity overflow") - try _handle.uncheckedReplaceSubrange( - subrange, - addingCount: newItemCount, + try _handle.uncheckedReplace( + removing: subrange, consumingWith: consumer, + addingCount: newItemCount, initializingWith: initializer) } #endif @@ -188,12 +188,12 @@ extension RigidDeque where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving items: UnsafeMutableBufferPointer, ) { var remainder = items - replaceSubrange(subrange, addingCount: remainder.count) { target in + replace(removing: subrange, addingCount: remainder.count) { target in target._withUnsafeMutableBufferPointer { buffer, count in buffer.moveInitializeAll( fromContentsOf: remainder._trim(first: buffer.count)) @@ -231,13 +231,13 @@ extension RigidDeque where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving items: inout InputSpan ) { items.withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(last: count) - unsafe self.replaceSubrange(subrange, moving: source) + unsafe self.replace(removing: subrange, moving: source) count = 0 } } @@ -270,13 +270,13 @@ extension RigidDeque where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `items.count`) @_alwaysEmitIntoClient - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, moving items: inout OutputSpan ) { items._withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(first: count) - unsafe self.replaceSubrange(subrange, moving: source) + unsafe self.replace(removing: subrange, moving: source) count = 0 } } @@ -320,14 +320,17 @@ extension RigidDeque where Element: ~Copyable { /// /// - Complexity: O(`self.count` + `maximumCount`) @_alwaysEmitIntoClient - public mutating func replaceSubrange< + public mutating func replace< P: Producer & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, addingCount newItemCount: Int, from producer: inout P ) throws(P.ProducerError) { - try replaceSubrange(subrange, addingCount: newItemCount) { target throws(P.ProducerError) in + try replace( + removing: subrange, + addingCount: newItemCount + ) { target throws(P.ProducerError) in try producer.generate(into: &target) } } @@ -363,12 +366,12 @@ extension RigidDeque /* where Element: Copyable */ { /// /// - Complexity: O(`self.count` + `items.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying items: UnsafeBufferPointer ) { var remainder = items - replaceSubrange(subrange, addingCount: remainder.count) { target in + replace(removing: subrange, addingCount: remainder.count) { target in target._withUnsafeMutableBufferPointer { dst, dstCount in dst.initializeAll(fromContentsOf: remainder._trim(first: dst.count)) dstCount += dst.count @@ -404,12 +407,12 @@ extension RigidDeque /* where Element: Copyable */ { /// /// - Complexity: O(`self.count` + `items.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying items: UnsafeMutableBufferPointer ) { - unsafe self.replaceSubrange( - subrange, + unsafe self.replace( + removing: subrange, copying: UnsafeBufferPointer(items)) } @@ -440,28 +443,28 @@ extension RigidDeque /* where Element: Copyable */ { /// /// - Complexity: O(`self.count` + `items.count`) @inlinable - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying items: Span ) { unsafe items.withUnsafeBufferPointer { buffer in - unsafe self.replaceSubrange(subrange, copying: buffer) + unsafe self.replace(removing: subrange, copying: buffer) } } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @inlinable - internal mutating func _replaceSubrange< + internal mutating func _replace< C: Container & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, copyingContainer items: borrowing C, newCount: Int ) { let expectedCount = self.count - subrange.count + newCount var it = items.makeBorrowingIterator() - self.replaceSubrange(subrange, addingCount: newCount) { target in + self.replace(removing: subrange, addingCount: newCount) { target in it.copyContents(into: &target) } precondition( @@ -471,8 +474,8 @@ extension RigidDeque /* where Element: Copyable */ { #endif @inlinable - internal mutating func _replaceSubrange( - _ subrange: Range, + internal mutating func _replace( + removing subrange: Range, copyingCollection items: __owned some Collection, newCount: Int ) { @@ -480,12 +483,12 @@ extension RigidDeque /* where Element: Copyable */ { precondition( src.count == newCount, "Broken Collection: count doesn't match contents") - self.replaceSubrange(subrange, copying: src) + self.replace(removing: subrange, copying: src) } if done != nil { return } var i = items.startIndex - self.replaceSubrange(subrange, addingCount: newCount) { target in + self.replace(removing: subrange, addingCount: newCount) { target in while !target.isFull { target.append(items[i]) items.formIndex(after: &i) @@ -522,14 +525,14 @@ extension RigidDeque /* where Element: Copyable */ { /// - Complexity: O(`self.count` + `items.count`) @inlinable @inline(__always) - public mutating func replaceSubrange< + public mutating func replace< C: Container & ~Copyable & ~Escapable >( - _ subrange: Range, + removing subrange: Range, copying items: borrowing C ) { - _replaceSubrange( - subrange, copyingContainer: items, newCount: items.count) + _replace( + removing: subrange, copyingContainer: items, newCount: items.count) } #endif @@ -561,12 +564,12 @@ extension RigidDeque /* where Element: Copyable */ { /// - Complexity: O(`self.count` + `items.count`) @inlinable @inline(__always) - public mutating func replaceSubrange( - _ subrange: Range, + public mutating func replace( + removing subrange: Range, copying items: __owned some Collection ) { - _replaceSubrange( - subrange, copyingCollection: items, newCount: items.count) + _replace( + removing: subrange, copyingCollection: items, newCount: items.count) } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @@ -599,14 +602,14 @@ extension RigidDeque /* where Element: Copyable */ { /// *m* is the count of `items`. @inlinable @inline(__always) - public mutating func replaceSubrange< + public mutating func replace< C: Container & Collection >( - _ subrange: Range, + removing subrange: Range, copying items: C ) { - _replaceSubrange( - subrange, copyingContainer: items, newCount: items.count) + _replace( + removing: subrange, copyingContainer: items, newCount: items.count) } #endif } diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift index ac319b09..a2ca9d54 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift @@ -83,8 +83,8 @@ extension UniqueDeque where Element: ~Copyable { "Subrange out of bounds") precondition(newItemCount >= 0, "Cannot add a negative number of items") _ensureFreeCapacity(newItemCount - subrange.count) - try _storage._handle.uncheckedReplaceSubrange( - subrange, + try _storage._handle.uncheckedReplace( + removing: subrange, addingCount: capacity, initializingWith: initializer) } @@ -144,8 +144,8 @@ extension UniqueDeque where Element: ~Copyable { @inlinable public mutating func replaceSubrange( _ subrange: Range, - addingCount newItemCount: Int, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { precondition( @@ -153,10 +153,10 @@ extension UniqueDeque where Element: ~Copyable { "Subrange out of bounds") precondition(newItemCount >= 0, "Cannot add a negative number of items") _ensureFreeCapacity(newItemCount - subrange.count) - try _storage._handle.uncheckedReplaceSubrange( - subrange, - addingCount: newItemCount, + try _storage._handle.uncheckedReplace( + removing: subrange, consumingWith: consumer, + addingCount: newItemCount, initializingWith: initializer) } #endif diff --git a/Sources/DequeModule/_UnsafeDequeHandle.swift b/Sources/DequeModule/_UnsafeDequeHandle.swift index f020dbaa..05853add 100644 --- a/Sources/DequeModule/_UnsafeDequeHandle.swift +++ b/Sources/DequeModule/_UnsafeDequeHandle.swift @@ -1371,7 +1371,10 @@ extension _UnsafeDequeHandle where Element: ~Copyable { self.closeGap( offsets: (left ? subrange.prefix(-delta) : subrange.suffix(-delta))) } - let newRange = Range(uncheckedBounds: (subrange.lowerBound, newItemCount)) + let newRange = Range( + uncheckedBounds: ( + subrange.lowerBound, + subrange.lowerBound &+ newItemCount)) let gap = self.mutableSegments(forOffsets: newRange) var c = 0 @@ -1387,8 +1390,8 @@ extension _UnsafeDequeHandle where Element: ~Copyable { } @_alwaysEmitIntoClient - internal mutating func uncheckedReplaceSubrange( - _ subrange: Range, + internal mutating func uncheckedReplace( + removing subrange: Range, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { @@ -1406,10 +1409,10 @@ extension _UnsafeDequeHandle where Element: ~Copyable { #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @_alwaysEmitIntoClient - internal mutating func uncheckedReplaceSubrange( - _ subrange: Range, - addingCount newItemCount: Int, + internal mutating func uncheckedReplace( + removing subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, + addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void ) throws(E) -> Void { assert( diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index ac2887eb..bed56314 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -1183,14 +1183,14 @@ class RigidArrayTests: CollectionTestCase { } #endif - func test_replaceSubrange_addingCount_full() { + func test_replace_addingCount_full() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 20]) { layout in withEveryRange("subrange", in: 0 ..< layout.count) { subrange in withEvery("c", in: 0 ..< subrange.count + layout.freeCapacity) { c in withLifetimeTracking { tracker in var a = tracker.rigidArray(layout: layout) - a.replaceSubrange(subrange, addingCount: c) { target in + a.replace(removing: subrange, addingCount: c) { target in expectEqual(target.freeCapacity, c) for i in 0 ..< c { target.append(tracker.instance(for: layout.count + i)) @@ -1213,7 +1213,7 @@ class RigidArrayTests: CollectionTestCase { } } - func test_replaceSubrange_addingCount_partial() { + func test_replace_addingCount_partial() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in guard !layout.isFull else { return } withEveryRange("subrange", in: 0 ..< layout.count) { subrange in @@ -1222,7 +1222,7 @@ class RigidArrayTests: CollectionTestCase { withLifetimeTracking { tracker in var a = tracker.rigidArray(layout: layout) - a.replaceSubrange(subrange, addingCount: c) { target in + a.replace(removing: subrange, addingCount: c) { target in expectTrue(target.isEmpty) expectEqual(target.freeCapacity, c) for i in 0 ..< n { @@ -1248,7 +1248,7 @@ class RigidArrayTests: CollectionTestCase { } } - func test_replaceSubrange_Collection() { + func test_replace_Collection() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("isContiguous", in: [false, true]) { isContiguous in @@ -1262,7 +1262,7 @@ class RigidArrayTests: CollectionTestCase { addition.map { tracker.instance(for: $0) }, isContiguous: isContiguous) var a = tracker.rigidArray(layout: layout) - a.replaceSubrange(range, copying: trackedAddition) + a.replace(removing: range, copying: trackedAddition) expectIterableContents( a, @@ -1276,7 +1276,7 @@ class RigidArrayTests: CollectionTestCase { } } } - func test_replaceSubrange_Span() { + func test_replace_Span() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("c", in: 0 ..< layout.capacity - layout.count + range.count) { c in @@ -1287,7 +1287,7 @@ class RigidArrayTests: CollectionTestCase { var a = tracker.rigidArray(layout: layout) let trackedAddition = RigidArray(copying: addition.map { tracker.instance(for: $0) }) - a.replaceSubrange(range, copying: trackedAddition.span) + a.replace(removing: range, copying: trackedAddition.span) expectIterableContents( a, @@ -1302,7 +1302,7 @@ class RigidArrayTests: CollectionTestCase { } #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW - func test_replaceSubrange_Container() { + func test_replace_Container() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("c", in: 0 ..< layout.capacity - layout.count + range.count) { c in @@ -1316,7 +1316,7 @@ class RigidArrayTests: CollectionTestCase { let trackedAddition = StaccatoContainer( contents: RigidArray(copying: addition.map { tracker.instance(for: $0) }), spanCounts: [spanCount]) - a.replaceSubrange(range, copying: trackedAddition) + a.replace(removing: range, copying: trackedAddition) expectIterableContents( a, diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index 6ae8d9d8..26ca3a1f 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -868,7 +868,7 @@ class UniqueArrayTests: CollectionTestCase { #endif #endif - func test_replaceSubrange_copying_Collection() { + func test_replace_copying_Collection() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("c", in: [0, 1, 10, 100]) { c in @@ -879,7 +879,7 @@ class UniqueArrayTests: CollectionTestCase { var a = tracker.uniqueArray(layout: layout) let trackedAddition = addition.map { tracker.instance(for: $0) } - a.replaceSubrange(range, copying: trackedAddition) + a.replace(removing: range, copying: trackedAddition) expectIterableContents( a, @@ -893,7 +893,7 @@ class UniqueArrayTests: CollectionTestCase { } } - func test_replaceSubrange_copying_Span() { + func test_replace_copying_Span() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("c", in: [0, 1, 10, 100]) { c in @@ -905,7 +905,7 @@ class UniqueArrayTests: CollectionTestCase { var a = tracker.uniqueArray(layout: layout) let trackedAddition = RigidArray( copying: addition.map { tracker.instance(for: $0) }) - a.replaceSubrange(range, copying: trackedAddition.span) + a.replace(removing: range, copying: trackedAddition.span) expectIterableContents( a, @@ -921,7 +921,7 @@ class UniqueArrayTests: CollectionTestCase { #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW #if false // FIXME: Update - func test_replaceSubrange_copying_Container() { + func test_replace_copying_Container() { withSomeArrayLayouts("layout", ofCapacities: [0, 5, 10]) { layout in withEveryRange("range", in: 0 ..< layout.count) { range in withEvery("c", in: [0, 1, 10, 100]) { c in diff --git a/Tests/DequeTests/DequeInternals.swift b/Tests/DequeTests/DequeInternals.swift index 8bf76c0a..cb38992d 100644 --- a/Tests/DequeTests/DequeInternals.swift +++ b/Tests/DequeTests/DequeInternals.swift @@ -26,6 +26,14 @@ internal struct RigidTestData: ~Copyable { self.deque = deque self.contents = contents } + + mutating func take() -> RigidDeque { + exchange(&self.deque, with: .init()) + } + + consuming func consume() -> RigidDeque { + exchange(&self.deque, with: .init()) + } } @available(SwiftStdlib 5.0, *) diff --git a/Tests/DequeTests/RigidDequeTests.swift b/Tests/DequeTests/RigidDequeTests.swift index a915b31f..d1b4ed43 100644 --- a/Tests/DequeTests/RigidDequeTests.swift +++ b/Tests/DequeTests/RigidDequeTests.swift @@ -1284,6 +1284,168 @@ final class RigidDequeTests: CollectionTestCase { } #endif + func test_replace_addingCount_full() { + withEveryDeque("layout", ofCapacities: [0, 5, 10]) { layout in + withEveryRange("subrange", in: 0 ..< layout.count) { subrange in + withEvery("c", in: 0 ..< subrange.count + layout.freeCapacity) { c in + withLifetimeTracking { tracker in + var a = tracker.rigidDeque(with: layout).consume() + + var i = 0 + a.replace(removing: subrange, addingCount: c) { target in + while !target.isFull { + target.append(tracker.instance(for: layout.count + i)) + i += 1 + } + } + expectEqual(i, c) + + var expected = Array(0 ..< layout.count) + expected.replaceSubrange( + subrange, + with: layout.count ..< layout.count + c) + + expectIterableContents( + a, + equivalentTo: expected, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + } + + func test_replace_addingCount_partial() { + withEveryDeque("layout", ofCapacities: [0, 5, 10]) { layout in + withEveryRange("subrange", in: 0 ..< layout.count) { subrange in + guard layout.freeCapacity - subrange.count > 0 else { return } + withEvery("c", in: 1 ..< subrange.count + layout.freeCapacity) { c in + withEvery("n", in: 0 ..< c - 1) { n in + withLifetimeTracking { tracker in + var a = tracker.rigidDeque(with: layout).consume() + + var i = 0 + a.replace(removing: subrange, addingCount: c) { target in + while !target.isFull, i < n { + target.append(tracker.instance(for: layout.count + i)) + i += 1 + } + } + expectEqual(i, n) + + var expected = Array(0 ..< layout.count) + expected.replaceSubrange( + subrange, + with: layout.count ..< layout.count + n) + + expectIterableContents( + a, + equivalentTo: expected, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + } + } + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + func test_replace_consuming_addingCount_full() { + withEveryDeque("layout", ofCapacities: [0, 5, 10]) { layout in + withEveryRange("subrange", in: 0 ..< layout.count) { subrange in + withEvery("c", in: 0 ..< subrange.count + layout.freeCapacity) { c in + withLifetimeTracking { tracker in + var a = tracker.rigidDeque(with: layout).consume() + + var i = subrange.lowerBound + var j = 0 + a.replace( + removing: subrange, + consumingWith: { source in + while let next = source.popFirst() { + expectEqual(next.payload, i) + i += 1 + } + }, + addingCount: c, + initializingWith: { target in + expectEqual(i, subrange.upperBound) + while !target.isFull { + target.append(tracker.instance(for: layout.count + j)) + j += 1 + } + }) + expectEqual(i, subrange.upperBound) + expectEqual(j, c) + + var expected = Array(0 ..< layout.count) + expected.replaceSubrange( + subrange, + with: layout.count ..< layout.count + c) + + expectIterableContents( + a, + equivalentTo: expected, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + } +#endif + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + func test_replace_consuming_addingCount_partial() { + withEveryDeque("layout", ofCapacities: [0, 5, 10]) { layout in + withEveryRange("subrange", in: 0 ..< layout.count) { subrange in + guard layout.freeCapacity - subrange.count > 0 else { return } + withEvery("c", in: 1 ..< subrange.count + layout.freeCapacity) { c in + withEvery("n", in: 0 ..< c - 1) { n in + withLifetimeTracking { tracker in + var a = tracker.rigidDeque(with: layout).consume() + + var i = subrange.lowerBound + var j = 0 + a.replace( + removing: subrange, + consumingWith: { source in + while let next = source.popFirst() { + expectEqual(next.payload, i) + i += 1 + } + }, + addingCount: c, + initializingWith: { target in + expectEqual(i, subrange.upperBound) + while !target.isFull, j < n { + target.append(tracker.instance(for: layout.count + j)) + j += 1 + } + }) + expectEqual(i, subrange.upperBound) + expectEqual(j, n) + + var expected = Array(0 ..< layout.count) + expected.replaceSubrange( + subrange, + with: layout.count ..< layout.count + n) + + expectIterableContents( + a, + equivalentTo: expected, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + } + } + } +#endif + func test_removeOne() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("offset", in: 0 ..< layout.count) { offset in diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 98c82788..b24fe715 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -84,6 +84,8 @@ 7D57A2CB2F342F43002543BD /* UniqueDeque+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D57A2C82F342F43002543BD /* UniqueDeque+Equatable.swift */; }; 7D57A2CC2F342F43002543BD /* UniqueDeque+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D57A2C92F342F43002543BD /* UniqueDeque+Hashable.swift */; }; 7D57A2CE2F343CA9002543BD /* ContainerAlgorithms.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D57A2CD2F343CA2002543BD /* ContainerAlgorithms.swift */; }; + 7D57A2D02F353946002543BD /* RigidArray+Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D57A2CF2F35393E002543BD /* RigidArray+Deprecated.swift */; }; + 7D57A2D22F353CA5002543BD /* UniqueArray+Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D57A2D12F353CA1002543BD /* UniqueArray+Deprecated.swift */; }; 7D9748D42E9748930098631A /* BigString+Chunk+UTF16.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D9748D32E9748930098631A /* BigString+Chunk+UTF16.swift */; }; 7D9748D52E9748930098631A /* BigString+Chunk+UnicodeScalar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D9748D12E9748930098631A /* BigString+Chunk+UnicodeScalar.swift */; }; 7D9748D62E9748930098631A /* BigString+Chunk+Character.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D9748CF2E9748930098631A /* BigString+Chunk+Character.swift */; }; @@ -585,6 +587,8 @@ 7D57A2C82F342F43002543BD /* UniqueDeque+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Equatable.swift"; sourceTree = ""; }; 7D57A2C92F342F43002543BD /* UniqueDeque+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Hashable.swift"; sourceTree = ""; }; 7D57A2CD2F343CA2002543BD /* ContainerAlgorithms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerAlgorithms.swift; sourceTree = ""; }; + 7D57A2CF2F35393E002543BD /* RigidArray+Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidArray+Deprecated.swift"; sourceTree = ""; }; + 7D57A2D12F353CA1002543BD /* UniqueArray+Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueArray+Deprecated.swift"; sourceTree = ""; }; 7D5A64D229CCE8CC00CB2595 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 7D5A64D329CCEE9A00CB2595 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 7D5A64D829CCF0FE00CB2595 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; @@ -1190,6 +1194,7 @@ 7DA23F3A2F329A2300E4C03A /* RigidArray+Insertions.swift */, 7DA23F3B2F329A2300E4C03A /* RigidArray+Removals.swift */, 7DA23F3C2F329A2300E4C03A /* RigidArray+Replacements.swift */, + 7D57A2CF2F35393E002543BD /* RigidArray+Deprecated.swift */, ); path = RigidArray; sourceTree = ""; @@ -1209,6 +1214,7 @@ 7DA23F422F329A2300E4C03A /* UniqueArray+Insertions.swift */, 7DA23F432F329A2300E4C03A /* UniqueArray+Removals.swift */, 7DA23F442F329A2300E4C03A /* UniqueArray+Replacements.swift */, + 7D57A2D12F353CA1002543BD /* UniqueArray+Deprecated.swift */, ); path = UniqueArray; sourceTree = ""; @@ -2498,6 +2504,7 @@ 7D2A773D2F27F1E4005780C7 /* BorrowingSequence.swift in Sources */, 7D2A773E2F27F1E4005780C7 /* BorrowingSequence+Standard Conformances.swift in Sources */, 7D2A773F2F27F1E4005780C7 /* Borrow.swift in Sources */, + 7D57A2D02F353946002543BD /* RigidArray+Deprecated.swift in Sources */, 7D2A77402F27F1E4005780C7 /* BorrowingIteratorProtocol.swift in Sources */, 7D2A77412F27F1E4005780C7 /* Inout.swift in Sources */, 7D2A77422F27F1E4005780C7 /* OutputSpan+Extras.swift in Sources */, @@ -2520,6 +2527,7 @@ 7DE9208029CA70F4004483EB /* BigString+Hashing.swift in Sources */, 7DE920F829CA70F4004483EB /* BitArray+Testing.swift in Sources */, 7DE920D629CA70F4004483EB /* BitSet+Equatable.swift in Sources */, + 7D57A2D22F353CA5002543BD /* UniqueArray+Deprecated.swift in Sources */, 7DE9217A29CA70F4004483EB /* Heap+Descriptions.swift in Sources */, 7DE9216429CA70F4004483EB /* _HashNode.swift in Sources */, 7DAD89A52E8B09D900DD5786 /* _UnsafeBitSet+_Word.swift in Sources */,