mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
Range replacement operations generally need to invalidate all indices, and that means that we lose our position in the container after each insertion/removal, unless we happen to be using a container with looser index invalidation (such as arrays or deques). A linked list is an example for a container type where insertions/removals need to invalidate all indices — as we need to track index validation using a single version counter that is stored in the list’s head. However, it would not be acceptable to lose our place whenever we insert a new item to the list; therefore, to mitigate the validation issue, this commit changes all range-replacement operations to return a valid index range to the affected items in the container. This is a radical break from `RangeReplaceableCollection`. It fixes a long-standing defect with it, but it also makes it tricky to conform the same type to both protocol families, as the two require the “same” methods to return different types (`Void` vs `Range<Index>`). Given that the LSG has intentionally diverged `UniqueArray.removeAll()` from RangeReplaceableCollection semantics, I expect this isn’t going to be an issue. For example, types with existing collection conformances can expose container views, mitigating such semantic differences. To allow SubrangeConsumers to work without losing position, add a new protocol, `ContainerDrain` with a consuming `finalize` operation that returns a valid index in the resulting container state. While we’re here, apply the API tweaks from SE-0527: - Revert the `replace(removing:…:)` names, rolling back to `replaceSubrange(_:…:)` . - Similarly, rename `consume(_:)` → `consumeSubrange(_:)` - Rename `reallocate(capacity:)` → `setCapacity(_:)`.
295 lines
9.5 KiB
Swift
295 lines
9.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !COLLECTIONS_SINGLE_MODULE
|
|
import SpanPreview
|
|
#endif
|
|
|
|
#if compiler(>=6.2)
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension UniqueArray where Element: ~Copyable {
|
|
@available(*, deprecated, renamed: "nextSpan(after:maxCount:)")
|
|
@inlinable
|
|
@_lifetime(borrow self)
|
|
public func nextSpan(
|
|
after index: inout Int, maximumCount: Int
|
|
) -> Span<Element> {
|
|
self.nextSpan(
|
|
after: &index,
|
|
maxCount: maximumCount,
|
|
limitedBy: self.endIndex)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "nextMutableSpan(after:maxCount:)")
|
|
@inlinable
|
|
@_lifetime(&self)
|
|
public mutating func nextMutableSpan(
|
|
after index: inout Int, maximumCount: Int
|
|
) -> MutableSpan<Element> {
|
|
self.nextMutableSpan(
|
|
after: &index,
|
|
maxCount: maximumCount,
|
|
limitedBy: self.endIndex)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "previousSpan(before:maxCount:)")
|
|
@inlinable
|
|
@_lifetime(borrow self)
|
|
public func previousSpan(
|
|
before index: inout Int, maximumCount: Int
|
|
) -> Span<Element> {
|
|
self.previousSpan(before: &index, maxCount: maximumCount)
|
|
}
|
|
|
|
/// Initializes a new unique array with the specified capacity and no elements.
|
|
@available(*, deprecated, renamed: "init(minimumCapacity:)")
|
|
@inlinable
|
|
public init(capacity: Int) {
|
|
self.init(minimumCapacity: capacity)
|
|
}
|
|
|
|
@available(*, deprecated)
|
|
@inlinable
|
|
@inline(__always)
|
|
public mutating func removeAll(keepingCapacity keepCapacity: Bool) {
|
|
if keepCapacity {
|
|
_storage.removeAll()
|
|
} else {
|
|
_storage = RigidArray(capacity: 0)
|
|
}
|
|
}
|
|
|
|
/// Append a given number of items to the end of this array by populating
|
|
/// an output span.
|
|
///
|
|
/// If the array does not have sufficient capacity to hold the requested
|
|
/// number of new elements, then this reallocates the array's storage to
|
|
/// grow its capacity, using a geometric growth rate.
|
|
///
|
|
/// - Parameters:
|
|
/// - count: The number of items to append to the array.
|
|
/// - initializer: A callback that gets called exactly once to directly
|
|
/// populate newly reserved storage within the array. The function
|
|
/// is allowed to initialize fewer than `count` items. The array is
|
|
/// appended however many items the callback adds to the output span
|
|
/// before it returns (or before it throws an error).
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@available(*, deprecated, renamed: "append(addingCount:initializingWith:)")
|
|
@_alwaysEmitIntoClient
|
|
@inline(__always)
|
|
public mutating func append<E: Error, Result: ~Copyable>(
|
|
count: Int,
|
|
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Result
|
|
) throws(E) -> Result {
|
|
var result: Result? = nil
|
|
try append(addingCount: count) { target throws(E) in
|
|
result = try initializer(&target)
|
|
}
|
|
return result.take()!
|
|
}
|
|
|
|
/// Inserts a given number of new items into this array at the specified
|
|
/// position, using a callback to directly initialize array storage by
|
|
/// populating an output span.
|
|
///
|
|
/// All existing elements at or following the specified position are moved to
|
|
/// make room for the new items.
|
|
///
|
|
/// If the array does not have sufficient capacity to hold the new elements,
|
|
/// then this reallocates storage to extend its capacity, using a geometric
|
|
/// growth rate.
|
|
///
|
|
/// - Parameters:
|
|
/// - count: The number of items to insert into the array.
|
|
/// - index: The position at which to insert the new items.
|
|
/// `index` must be a valid index in the array.
|
|
/// - body: A callback that gets called exactly once to directly
|
|
/// populate newly reserved storage within the array. The function
|
|
/// is called with an empty output span of capacity matching the
|
|
/// supplied count, and it must fully populate it before returning.
|
|
///
|
|
/// - Complexity: O(`self.count` + `count`)
|
|
@available(*, deprecated, renamed: "insert(addingCount:at:initializingWith:)")
|
|
@inlinable
|
|
public mutating func insert<Result: ~Copyable>(
|
|
count: Int,
|
|
at index: Int,
|
|
initializingWith body: (inout OutputSpan<Element>) -> Result
|
|
) -> Result {
|
|
var result: Result? = nil
|
|
self.insert(addingCount: count, at: index) { target in
|
|
result = body(&target)
|
|
}
|
|
return result!
|
|
}
|
|
|
|
/// Grow or shrink the capacity of a unique array instance without discarding
|
|
/// its contents.
|
|
///
|
|
/// This operation replaces the array's storage buffer with a newly allocated
|
|
/// buffer of the specified capacity, moving all existing elements
|
|
/// to its new storage. The old storage is then deallocated.
|
|
///
|
|
/// - Parameter capacity: The desired new capacity. `capacity` must be
|
|
/// greater than or equal to the current count.
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@available(*, deprecated, renamed: "setCapacity(_:)")
|
|
@inlinable
|
|
public mutating func reallocate(capacity: Int) {
|
|
_storage.reallocate(capacity: capacity)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:addingCount:initializingWith:)")
|
|
@inlinable
|
|
public mutating func replace<E: Error>(
|
|
removing subrange: Range<Int>,
|
|
addingCount newItemCount: Int,
|
|
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
|
) throws(E) -> Void {
|
|
try replaceSubrange(
|
|
subrange, addingCount: newItemCount, initializingWith: initializer)
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:consumingWith:addingCount:initializingWith:)")
|
|
@inlinable
|
|
public mutating func replace<E: Error>(
|
|
removing subrange: Range<Int>,
|
|
consumingWith consumer: (inout InputSpan<Element>) -> Void,
|
|
addingCount newItemCount: Int,
|
|
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
|
|
) throws(E) -> Void {
|
|
try replaceSubrange(
|
|
subrange,
|
|
consumingWith: consumer,
|
|
addingCount: newItemCount,
|
|
initializingWith: initializer)
|
|
}
|
|
#endif
|
|
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:moving:)")
|
|
@_alwaysEmitIntoClient
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
moving newElements: UnsafeMutableBufferPointer<Element>,
|
|
) {
|
|
replaceSubrange(subrange, moving: newElements)
|
|
}
|
|
|
|
#if UnstableContainersPreview
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:moving:)")
|
|
@_alwaysEmitIntoClient
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
moving items: inout InputSpan<Element>
|
|
) {
|
|
replaceSubrange(subrange, moving: &items)
|
|
}
|
|
#endif
|
|
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:moving:)")
|
|
@_alwaysEmitIntoClient
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
moving items: inout OutputSpan<Element>
|
|
) {
|
|
replaceSubrange(subrange, moving: &items)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:moving:)")
|
|
@_alwaysEmitIntoClient
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
moving newElements: inout RigidArray<Element>,
|
|
) {
|
|
replaceSubrange(subrange, moving: &newElements)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replaceSubrange(_:consuming:)")
|
|
@_alwaysEmitIntoClient
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
consuming newElements: consuming RigidArray<Element>,
|
|
) {
|
|
replaceSubrange(subrange, consuming: newElements)
|
|
}
|
|
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension UniqueArray {
|
|
/// Copy the contents of this array into a newly allocated unique array
|
|
/// instance with just enough capacity to hold all its elements.
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@available(*, deprecated, renamed: "clone()")
|
|
@inlinable
|
|
public func copy() -> Self {
|
|
self.clone()
|
|
}
|
|
|
|
/// Copy the contents of this array into a newly allocated unique array
|
|
/// instance with the specified capacity.
|
|
///
|
|
/// - Parameter capacity: The desired capacity of the resulting unique array.
|
|
/// `capacity` must be greater than or equal to `count`.
|
|
///
|
|
/// - Complexity: O(`count`)
|
|
@available(*, deprecated, renamed: "clone(capacity:)")
|
|
@inlinable
|
|
public func copy(capacity: Int) -> Self {
|
|
clone(capacity: capacity)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replace(_:copying:)")
|
|
@inlinable
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
copying newElements: UnsafeBufferPointer<Element>
|
|
) {
|
|
replaceSubrange(subrange, copying: newElements)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replace(_:copying:)")
|
|
@inlinable
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
copying newElements: UnsafeMutableBufferPointer<Element>
|
|
) {
|
|
self.replaceSubrange(subrange, copying: newElements)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replace(_:copying:)")
|
|
@inlinable
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
copying newElements: Span<Element>
|
|
) {
|
|
replaceSubrange(subrange, copying: newElements)
|
|
}
|
|
|
|
@available(*, deprecated, renamed: "replace(_:copying:)")
|
|
@inlinable
|
|
public mutating func replace(
|
|
removing subrange: Range<Int>,
|
|
copying newElements: __owned some Collection<Element>
|
|
) {
|
|
replaceSubrange(subrange, copying: newElements)
|
|
}
|
|
}
|
|
|
|
#endif
|