mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
- @inlinable → @_alwaysEmitIntoClient for now, to avoid temporary ABI issues
- Review RangeReplaceableContainer algorithms; add missing ones:
- New requirements: `removeFirst()`, `removeFirst(_:)`, `_customRemoveLast()`, `_customRemoveLast(_:) (with default implementations)
- `func removeAll()`
- `func removeLast()`, `func removeLast(_:)`, `func popLast()` when self is also bidirectional
- Review RigidArray, UniqueArray algorithms; remove ones needlessly duplicating functionality already provided by RRC. Try not to break source compatibility.
114 lines
4.6 KiB
Swift
114 lines
4.6 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 compiler(>=6.4) && UnstableContainersPreview
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
public protocol MutableContainer<Element>:
|
|
PermutableContainer, ~Copyable, ~Escapable
|
|
where
|
|
Element: ~Copyable
|
|
{
|
|
/// Accesses the element at the specified position.
|
|
///
|
|
/// - Parameter position: The position of the element to access.
|
|
/// `position` must be a valid index of the container that is not equal
|
|
/// to the `endIndex` property.
|
|
/// - Complexity: O(1)
|
|
subscript(index: Index) -> Element { borrow mutate }
|
|
|
|
/// Return a mutable span over the container's storage that begins with the
|
|
/// element at the given index, and extends to the end of the contiguous
|
|
/// storage chunk that contains it, but no more than `maximumCount` items.
|
|
///
|
|
/// On return, the index is updated to address the next item following the
|
|
/// end of the returned span.
|
|
///
|
|
/// This method can be used to efficiently process the items of a container in
|
|
/// bulk, by directly iterating over its piecewise contiguous pieces of
|
|
/// storage:
|
|
///
|
|
/// var index = items.startIndex
|
|
/// while true {
|
|
/// var span = items.nextMutableSpan(after: &index, maximumCount: 4)
|
|
/// if span.isEmpty { break }
|
|
/// // Process items in `span`
|
|
/// }
|
|
///
|
|
/// The `maximumCount` argument gives the caller control over the number of
|
|
/// items it receives from the iterator. This lets the caller avoid getting
|
|
/// more elements than it would be able to immediately process, which would
|
|
/// significantly complicate container use.
|
|
///
|
|
/// If the caller is able to process any number available items, it can signal
|
|
/// that by passing `Int.max` as the `maximumCount`, or simply by calling the
|
|
/// `nexSpan(after:)` method, which does precisely that. This is frequently
|
|
/// the case when the caller simply wants to iterate over the entire
|
|
/// container in a single loop.
|
|
///
|
|
/// `maximumCount` sets an upper bound. To read a specific number of items,
|
|
/// the caller usually needs to invoke `nextSpan` in a loop:
|
|
///
|
|
/// var items: some MutableContainer<Int>
|
|
/// var index = items.startIndex
|
|
/// var remainder = numberOfItemsToRead
|
|
/// while remainder > 0 {
|
|
/// let next = items.nextMutableSpan(after: &index, maximumCount: remainder)
|
|
/// guard !next.isEmpty else {
|
|
/// // Container does not have enough items
|
|
/// break
|
|
/// }
|
|
/// remainder -= next.count
|
|
/// // Process items in `next`
|
|
/// }
|
|
///
|
|
/// - Note: Repeated invocations of `nextSpan` on the same container and index
|
|
/// are not guaranteed to return identical results. (This is particularly
|
|
/// the case with containers that can store contents in their "inline"
|
|
/// representation. Such containers may not always have a unique address
|
|
/// in memory; the locations of the spans exposed by this method may vary
|
|
/// between different borrows of the same container.)
|
|
///
|
|
/// - Parameter index: A valid index in the container, including the end
|
|
/// index. On return, this index is advanced by the count of the resulting
|
|
/// span, to simplify iteration.
|
|
/// - Parameter maximumCount: The maximum number of items the caller is able
|
|
/// to process immediately. `maximumCount` must be greater than zero.
|
|
/// If you are able to process an arbitrary number of items, set
|
|
/// `maximumCount` to `Int.max`, or call the `nextSpan(after:)` method.
|
|
/// - Returns: A mutable span over contiguous storage that starts at the given
|
|
/// index. If the input index is the end index, then this returns an empty
|
|
/// span. Otherwise the result is non-empty, with its first element
|
|
/// matching the element at the input index.
|
|
@_lifetime(&self)
|
|
mutating func nextMutableSpan(
|
|
after index: inout Index,
|
|
maximumCount: Int
|
|
) -> MutableSpan<Element>
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension MutableContainer
|
|
where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
|
{
|
|
@_alwaysEmitIntoClient
|
|
@_lifetime(&self)
|
|
mutating func nextMutableSpan(
|
|
after index: inout Index,
|
|
) -> MutableSpan<Element> {
|
|
nextMutableSpan(after: &index, maximumCount: Int.max)
|
|
}
|
|
}
|
|
|
|
#endif
|