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(_:)`.
54 lines
1.7 KiB
Swift
54 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 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 BasicContainers
|
|
#endif
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: Container where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
@_lifetime(borrow self)
|
|
public func makeBorrowingIterator(
|
|
from start: Index, to end: Index
|
|
) -> BorrowingIterator {
|
|
_storage.makeBorrowingIterator(from: start, to: end)
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
|
_storage.currentIndex(of: &iterator)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: BidirectionalContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: RandomAccessContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: MutableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: DrainableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: RangeReplaceableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueArray: DynamicContainer where Element: ~Copyable {}
|
|
|
|
#endif
|