mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +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(_:)`.
107 lines
2.7 KiB
Swift
107 lines
2.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
|
|
import SpanPreview
|
|
#endif
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview && UnstableHashedContainers
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueSet: Container where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
|
_storage.currentIndex(of: &iterator)
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
@_lifetime(borrow self)
|
|
public func makeBorrowingIterator(
|
|
from start: Index, to end: Index
|
|
) -> BorrowingIterator {
|
|
_storage.makeBorrowingIterator(from: start, to: end)
|
|
}
|
|
}
|
|
|
|
#if false // FIXME
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension UniqueSet: DrainableContainer where Element: ~Copyable {
|
|
public struct SubrangeConsumer: ~Copyable & ~Escapable {
|
|
//...
|
|
}
|
|
|
|
public func consume(_ subrange: Range<Index>) -> SubrangeConsumer {
|
|
//...
|
|
}
|
|
}
|
|
#endif
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueSet where Element: ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
public init<
|
|
E: Error,
|
|
P: Producer<Element, E> & ~Copyable & ~Escapable
|
|
>(
|
|
minimumCapacity: Int? = nil, from producer: inout P
|
|
) throws(E)
|
|
where P.Element: ~Copyable
|
|
{
|
|
let c = producer.underestimatedCount
|
|
if let minimumCapacity {
|
|
self.init(minimumCapacity: Swift.min(minimumCapacity, c))
|
|
} else {
|
|
self.init(minimumCapacity: c)
|
|
}
|
|
try self.insert(from: &producer)
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
public mutating func insert<
|
|
E: Error,
|
|
P: Producer<Element, E> & ~Copyable & ~Escapable
|
|
>(
|
|
from producer: inout P
|
|
) throws(E)
|
|
where P.Element: ~Copyable
|
|
{
|
|
var done = false
|
|
while !done {
|
|
_ensureFreeCapacity(Swift.max(producer.underestimatedCount, 1))
|
|
try self.insert(addingCount: self.freeCapacity) { target throws(E) in
|
|
while !target.isFull, !done {
|
|
done = try !producer.generate(into: &target)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
public mutating func insert<
|
|
D: Drain<Element> & ~Copyable & ~Escapable
|
|
>(
|
|
from drain: inout D
|
|
) {
|
|
while true {
|
|
var span = drain.drainNext()
|
|
guard !span.isEmpty else { break }
|
|
while let next = span.popFirst() {
|
|
self.insert(next)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|