Introduce Container.nextSpan(after:maxCount:limitedBy:) and make it the core Container primitive

This commit is contained in:
Karoy Lorentey committed 2026-07-18 14:14:13 -07:00
1 parent 391693a8ba
commit e70b4df228
13 files changed
+290 -252

No files matched your search

@@ -354,10 +354,6 @@ extension RigidArray where Element: ~Copyable {
/// On return, `n` is set to zero if the operation succeeded without
/// hitting the limit; otherwise, `n` reflects the number of steps that
/// couldn't be taken.
/// - Parameter limit: A valid index of the array to use as a limit.
/// If `n > 0`, a limit that is less than `index` has no effect.
/// Likewise, if `n < 0`, a limit that is greater than `index` has no
/// effect.
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public func formIndex(
@@ -366,6 +362,14 @@ extension RigidArray where Element: ~Copyable {
index._advance(by: &n, limitedBy: limit)
}
@inlinable
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int
) -> Span<Element> {
self.span._nextSpan(after: &index)
}
/// Return a span over the array'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 `maxCount` items.
@@ -429,6 +433,10 @@ extension RigidArray where Element: ~Copyable {
/// to process immediately. `maxCount` must be greater than zero.
/// If you are able to process an arbitrary number of items, set
/// `maxCount` to `Int.max`, or call the `nextSpan(after:)` method.
/// - Parameter limit: A valid index of the array to use as a limit.
/// If `n > 0`, a limit that is less than `index` has no effect.
/// Likewise, if `n < 0`, a limit that is greater than `index` has no
/// effect.
/// - Returns: A 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
@@ -437,17 +445,9 @@ extension RigidArray where Element: ~Copyable {
@inlinable
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, maxCount: Int
after index: inout Int, maxCount: Int, limitedBy limit: Index
) -> Span<Element> {
self.span._nextSpan(after: &index, maxCount: maxCount)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, limitedBy limit: Index?
) -> Span<Element> {
self.span._nextSpan(after: &index, limitedBy: limit)
self.span._nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@inlinable
@@ -21,7 +21,10 @@ extension RigidArray where Element: ~Copyable {
public func nextSpan(
after index: inout Int, maximumCount: Int
) -> Span<Element> {
self.nextSpan(after: &index, maxCount: maximumCount)
self.nextSpan(
after: &index,
maxCount: maximumCount,
limitedBy: self.endIndex)
}
@available(*, deprecated, renamed: "nextMutableSpan(after:maxCount:)")
@@ -387,17 +387,17 @@ extension UniqueArray where Element: ~Copyable {
@inlinable
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, maxCount: Int
after index: inout Int
) -> Span<Element> {
_storage.nextSpan(after: &index, maxCount: maxCount)
_storage.nextSpan(after: &index)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, limitedBy limit: Int?
after index: inout Int, maxCount: Int, limitedBy limit: Int
) -> Span<Element> {
_storage.nextSpan(after: &index, limitedBy: limit)
_storage.nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@inlinable
@@ -21,7 +21,10 @@ extension UniqueArray where Element: ~Copyable {
public func nextSpan(
after index: inout Int, maximumCount: Int
) -> Span<Element> {
self.nextSpan(after: &index, maxCount: maximumCount)
self.nextSpan(
after: &index,
maxCount: maximumCount,
limitedBy: self.endIndex)
}
@available(*, deprecated, renamed: "nextMutableSpan(after:maxCount:)")
@@ -47,25 +47,16 @@ where Element: ~Copyable
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Index, maxCount: Int) -> Span<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
precondition(maxCount > 0, "maxCount must be positive")
let c = Swift.min(count &- index, maxCount)
let start = capacity &- count &+ index
return _uncheckedSpan(in: Range(uncheckedBounds: (start, start &+ c)))
public func nextSpan(after index: inout Index) -> Span<Element> {
self.span._nextSpan(after: &index)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, limitedBy limit: Index?
after index: inout Index, maxCount: Int, limitedBy limit: Index
) -> Span<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
let end = Swift.min(limit ?? count, count)
let base = capacity &- count
let range = Range(uncheckedBounds: (base &+ index, base &+ end))
index = end
return _uncheckedSpan(in: range)
self.span._nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@_alwaysEmitIntoClient
@@ -57,17 +57,17 @@ extension MutableSpan: RandomAccessContainer where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, maxCount: Int
after index: inout Int
) -> Span<Element> {
self.span._nextSpan(after: &index, maxCount: maxCount)
self.span._nextSpan(after: &index)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, limitedBy limit: Index?
after index: inout Index, maxCount: Int, limitedBy limit: Index
) -> Span<Element> {
self.span._nextSpan(after: &index, limitedBy: limit)
self.span._nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@_alwaysEmitIntoClient
@@ -46,14 +46,16 @@ where Element: ~Copyable
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Index, maxCount: Int) -> Span<Element> {
self.span._nextSpan(after: &index, maxCount: maxCount)
public func nextSpan(after index: inout Index) -> Span<Element> {
self.span._nextSpan(after: &index)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Index, limitedBy limit: Int?) -> Span<Element> {
self.span._nextSpan(after: &index, limitedBy: limit)
public func nextSpan(
after index: inout Index, maxCount: Int, limitedBy limit: Int
) -> Span<Element> {
self.span._nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@_alwaysEmitIntoClient
@@ -52,47 +52,43 @@ extension Span where Element: ~Copyable {
count
}
// FIXME: This has the proper lifetime declaration but can't fulfill the Container requirement.
@_alwaysEmitIntoClient
@_lifetime(copy self)
public func _nextSpan(after index: inout Int, maxCount: Int) -> Self {
public func _nextSpan(after index: inout Int) -> Self {
precondition(index >= 0 && index <= count, "Index out of bounds")
precondition(maxCount > 0, "maxCount must be positive")
let limit = index &+ Swift.min(maxCount, count &- index)
return self.extracting(unchecked: Range(uncheckedBounds: (index, limit)))
let result = self.extracting(last: count - index)
index = count
return result
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Int, maxCount: Int) -> Self {
_nextSpan(after: &index, maxCount: maxCount)
public func nextSpan(after index: inout Int) -> Self {
_nextSpan(after: &index)
}
// FIXME: This has the proper lifetime declaration but can't fulfill the Container requirement.
@_alwaysEmitIntoClient
@_lifetime(copy self)
public func _nextSpan(
after index: inout Index, limitedBy limit: Index?
after index: inout Int, maxCount: Int, limitedBy limit: Int
) -> Self {
precondition(index >= 0 && index <= count, "Index out of bounds")
let end: Index
if let limit {
precondition(limit >= 0 && limit <= count, "Index out of bounds")
end = Swift.min(limit, count)
} else {
end = count
precondition(maxCount > 0, "maxCount must be positive")
precondition(limit >= 0 && limit <= count, "Index out of bounds")
var end = index &+ Swift.min(maxCount, count &- index)
if limit >= index, limit < end {
end = limit
}
let range = Range(uncheckedBounds: (index, end))
index = end
return self.extracting(unchecked: range)
return self.extracting(unchecked: Range(uncheckedBounds: (index, end)))
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, limitedBy limit: Index?
after index: inout Int, maxCount: Int, limitedBy limit: Int
) -> Self {
_nextSpan(after: &index, limitedBy: limit)
_nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@_alwaysEmitIntoClient
@@ -64,6 +64,130 @@ public protocol Container<Element>:
/// Complexity: Must be O(1) on all container types.
var endIndex: Index { get }
/// Return a 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. 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 {
/// let span = items.nextSpan(after: &index)
/// if span.isEmpty { break }
/// // Process items in `span`
/// }
///
/// - Note: The spans returned by this method are not guaranteed to be
/// disjunct. Some containers may use the same storage chunk (or parts of a
/// storage chunk) multiple times, to repeat their contents.
///
/// - 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.
/// - Returns: A 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(borrow self)
func nextSpan(after index: inout Index) -> Span<Element>
/// Return a 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 `maxCount` items, and no further than
/// the given limiting index (if any).
///
/// On return, the index is updated to address the item following the
/// last element 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 {
/// let span = items.nextSpan(
/// after: &index,
/// maxCount: 4,
/// limitedBy: items.endIndex)
/// if span.isEmpty { break }
/// // Process items in `span`
/// }
///
/// The `maxCount` 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 `maxCount`, or simply by calling the
/// `nextSpan(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.
///
/// `maxCount` sets an upper bound. To read a specific number of items,
/// the caller usually needs to invoke `nextSpan` in a loop:
///
/// var items: some Container<Int>
/// var index = items.startIndex
/// var remainder = numberOfItemsToRead
/// while remainder > 0 {
/// let next = items.nextSpan(after: &index, maxCount: remainder)
/// guard !next.isEmpty else {
/// // Container does not have enough items
/// break
/// }
/// remainder -= next.count
/// // Process items in `next`
/// }
///
/// - Note: The spans returned by this method are not guaranteed to be
/// disjunct. Some containers may use the same storage chunk (or parts of a
/// storage chunk) multiple times, to repeat their contents.
///
/// - 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 maxCount: The maximum number of items the caller is ready
/// to process. `maxCount` must be greater than zero.
/// If the client is able to process an arbitrary number of items, then
/// this can be signalled by setting `maxCount` to `Int.max`; calling
/// the `nextSpan(after:)` method is a more direct way to express the same.
/// - Parameter limit: A valid index of the container to use as a limit.
/// A limit that precedes `index` has no effect. Otherwise the method
/// stops before exceeding `limit`. If you don't have a specific limiting
/// index to use, you can set this to `endIndex` to allow the method to
/// traverse the entire container.
/// - Returns: A span over contiguous storage that starts at the given index.
/// If the input index is equal to the end index or `limit`, then this
/// returns an empty span.
/// Otherwise the result is non-empty, with its first element matching the
/// element at the input index.
/// - Complexity: Recommended to be O(1). Conforming types must clearly
/// document deviations from this expectation.
@_lifetime(borrow self)
func nextSpan(
after index: inout Index,
maxCount: Int,
limitedBy limit: Index
) -> Span<Element>
/// Returns the position immediately after the given index.
///
/// - Parameter index: A valid index of the container. `i` must be less
@@ -147,84 +271,6 @@ public protocol Container<Element>:
/// - Complexity: O(1). This is a hard requirement.
subscript(index: Index) -> Element { borrow }
/// Return a 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 `maxCount` items.
///
/// On return, the index is updated to address the item following the
/// last element 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 {
/// let span = items.nextSpan(after: &index, maxCount: 4)
/// if span.isEmpty { break }
/// // Process items in `span`
/// }
///
/// The `maxCount` 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 `maxCount`, or simply by calling the
/// `nextSpan(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.
///
/// `maxCount` sets an upper bound. To read a specific number of items,
/// the caller usually needs to invoke `nextSpan` in a loop:
///
/// var items: some Container<Int>
/// var index = items.startIndex
/// var remainder = numberOfItemsToRead
/// while remainder > 0 {
/// let next = items.nextSpan(after: &index, maxCount: remainder)
/// guard !next.isEmpty else {
/// // Container does not have enough items
/// break
/// }
/// remainder -= next.count
/// // Process items in `next`
/// }
///
/// - Note: The spans returned by this method are not guaranteed to be
/// disjunct. Some containers may use the same storage chunk (or parts of a
/// storage chunk) multiple times, to repeat their contents.
///
/// - 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 maxCount: The maximum number of items the caller is able
/// to process immediately. `maxCount` must be greater than zero.
/// If you are able to process an arbitrary number of items, set
/// `maxCount` to `Int.max`, or call the `nextSpan(after:)` method.
/// - Returns: A 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.
/// - Complexity: Recommended to be O(1). Conforming types must clearly
/// document deviations from this expectation.
@_lifetime(borrow self)
func nextSpan(after index: inout Index, maxCount: Int) -> Span<Element>
@_lifetime(borrow self)
func nextSpan(
after index: inout Index,
limitedBy limit: Index?
) -> Span<Element>
func _customIndexOfEquatableElement(_ element: borrowing Element) -> Index??
func _customLastIndexOfEquatableElement(_ element: borrowing Element) -> Index??
}
@@ -238,44 +284,33 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
@available(SwiftStdlib 6.4, *)
extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
/// Return a 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. 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 {
/// let span = items.nextSpan(after: &index)
/// if span.isEmpty { break }
/// // Process items in `span`
/// }
///
/// - Note: The spans returned by this method are not guaranteed to be
/// disjunct. Some containers may use the same storage chunk (or parts of a
/// storage chunk) multiple times, to repeat their contents.
///
/// - 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.
/// - Returns: A 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.
@_alwaysEmitIntoClient
@_transparent
@_lifetime(borrow self)
public func nextSpan(after index: inout Index) -> Span<Element> {
nextSpan(after: &index, limitedBy: nil)
public func nextSpan(
after index: inout Index
) -> Span<Element> {
self.nextSpan(after: &index, maxCount: Int.max, limitedBy: self.endIndex)
}
@_alwaysEmitIntoClient
@_transparent
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index,
maxCount: Int
) -> Span<Element> {
self.nextSpan(after: &index, maxCount: maxCount, limitedBy: self.endIndex)
}
@_alwaysEmitIntoClient
@_transparent
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index,
limitedBy limit: Index
) -> Span<Element> {
self.nextSpan(after: &index, maxCount: Int.max, limitedBy: limit)
}
}
@@ -286,13 +321,22 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
count == 0
}
#if false // count is required to be O(1), so it cannot have a default implementation
#if false // count is required to be O(1), so it cannot have this default implementation
@_alwaysEmitIntoClient
public var count: Int {
distance(from: startIndex, to: endIndex)
}
#endif
@_alwaysEmitIntoClient
public func index(after index: Index) -> Index {
let end = self.endIndex
var index = index
let c = self.nextSpan(after: &index, maxCount: 1, limitedBy: end).count
precondition(c == 1, "Can't advance beyond endIndex")
return index
}
@_alwaysEmitIntoClient
public func formIndex(after index: inout Index) {
index = self.index(after: index)
@@ -300,30 +344,14 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
@_alwaysEmitIntoClient
public func index(_ index: Index, offsetBy n: Int) -> Index {
precondition(
n >= 0,
"Only BidirectionalContainers can be advanced by a negative amount")
#if false
// This is tempting, but it would lead to mutual recursion with the
// default implementation of `nextSpan(after:maxCount:)`.
var index = index
var n = n
let end = self.endIndex
while n > 0 {
let span = self.nextSpan(after: &index, maxCount: n)
precondition(
!span.isEmpty,
"Cannot advance index beyond the end of the container")
n &-= span.count
let c = self.nextSpan(after: &index, maxCount: n, limitedBy: end).count
precondition(c > 0, "Cannot advance index beyond the end of the container")
n &-= c
}
#else
var index = index
var n = n
while n > 0 {
self.formIndex(after: &index)
n &-= 1
}
#endif
return index
}
@@ -335,15 +363,10 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
n >= 0,
"Only BidirectionalContainers can be advanced by a negative amount")
while n > 0 {
var j = index
let span = self.nextSpan(after: &j, limitedBy: limit)
if span.count > n {
index = self.index(index, offsetBy: n)
n = 0
break
}
index = j
n &-= span.count
let c = self.nextSpan(after: &index, maxCount: n, limitedBy: limit).count
precondition(c <= n, "Invalid container")
guard c > 0 else { break }
n &-= c
}
}
@@ -358,6 +381,21 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
return index
}
@_alwaysEmitIntoClient
public func _customIndexOfEquatableElement(_: borrowing Element) -> Index?? {
nil
}
@_alwaysEmitIntoClient
public func _customLastIndexOfEquatableElement(
_ element: borrowing Element
) -> Index?? {
nil
}
}
@available(SwiftStdlib 6.4, *)
extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
@_alwaysEmitIntoClient
public func distance(from start: Index, to end: Index) -> Int {
// This variant allows bulk iteration, but as we can't decide if
@@ -373,31 +411,6 @@ extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable {
if j == start { return d2 }
}
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Index, maxCount: Int) -> Span<Element> {
var j = index
let span = self.nextSpan(after: &j)
if span.count <= maxCount {
index = j
return span
}
index = self.index(index, offsetBy: maxCount)
return span.extracting(first: maxCount)
}
@_alwaysEmitIntoClient
public func _customIndexOfEquatableElement(_: borrowing Element) -> Index?? {
nil
}
@_alwaysEmitIntoClient
public func _customLastIndexOfEquatableElement(
_ element: borrowing Element
) -> Index?? {
nil
}
}
@available(SwiftStdlib 6.4, *)
@@ -154,12 +154,9 @@ extension RigidDeque where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Int, maxCount: Int) -> Span<Element> {
public func nextSpan(after index: inout Int) -> Span<Element> {
_checkValidIndex(index)
precondition(maxCount > 0, "maxCount must be positive")
let segment = self._handle
.nextSegment(after: index)
._extracting(first: maxCount)
let segment = self._handle.nextSegment(after: index)
index &+= segment.count
return _overrideLifetime(Span(_unsafeElements: segment), borrowing: self)
}
@@ -167,18 +164,18 @@ extension RigidDeque where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, limitedBy limit: Int?
after index: inout Int, maxCount: Int, limitedBy limit: Int
) -> Span<Element> {
_checkValidIndex(index)
let segment = self._handle.nextSegment(after: index)
_checkValidIndex(limit)
let segment = self._handle
.nextSegment(after: index)
._extracting(first: maxCount)
var span = _overrideLifetime(Span(_unsafeElements: segment), borrowing: self)
if let limit {
_checkValidIndex(limit)
if limit >= index, span.count > limit - index {
span = span.extracting(first: limit - index)
}
if limit >= index, span.count > limit &- index {
span = span.extracting(first: limit &- index)
}
index += span.count
index &+= span.count
return span
}
@@ -100,14 +100,16 @@ extension UniqueDeque where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Int, maxCount: Int) -> Span<Element> {
_storage.nextSpan(after: &index, maxCount: maxCount)
public func nextSpan(after index: inout Int) -> Span<Element> {
_storage.nextSpan(after: &index)
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(after index: inout Int, limitedBy limit: Int?) -> Span<Element> {
_storage.nextSpan(after: &index, limitedBy: limit)
public func nextSpan(
after index: inout Int, maxCount: Int, limitedBy limit: Int
) -> Span<Element> {
_storage.nextSpan(after: &index, maxCount: maxCount, limitedBy: limit)
}
@_alwaysEmitIntoClient
@@ -110,4 +110,35 @@ extension Span where Element: Hashable /* & ~Copyable */ {
}
#endif
#if compiler(>=6.4)
@available(SwiftStdlib 5.0, *)
extension Span where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(copy self)
package func clamped(to limits: borrowing Self) -> Self {
if self.isEmpty || limits.isEmpty { return .init() }
let buffer = self.withUnsafeBufferPointer { buffer in
limits.withUnsafeBufferPointer { limits in
let start = buffer.baseAddress!
let end = start + buffer.count
let limitStart = limits.baseAddress!
let limitEnd = limitStart + limits.count
let clampedStart = (
limitStart > start ? limitStart
: limitEnd < start ? limitEnd
: start)
let clampedEnd = (
limitEnd < end ? limitEnd
: limitStart > end ? limitStart
: end)
let count = clampedEnd.distance(to: clampedStart)
return UnsafeBufferPointer(start: clampedStart, count: count)
}
}
return _overrideLifetime(Span(_unsafeElements: buffer), copying: self)
}
}
#endif
#endif
@@ -204,28 +204,28 @@ extension StaccatoContainer: Container where Element: ~Copyable {
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, maxCount: Int
after index: inout Index
) -> Span<Element> {
precondition(_isValid(index))
let startOffset = index._offset
let endOffset = _params.endOffset(fromOffset: index._offset, maxCount: maxCount)
let endOffset = _params.endOffset(fromOffset: index._offset, maxCount: Int.max)
index._offset = endOffset
return _contents.span.extracting(startOffset ..< endOffset)
}
@_lifetime(borrow self)
public func nextSpan(
after index: inout Index, limitedBy limit: Index?
after index: inout Index, maxCount: Int, limitedBy limit: Index
) -> Span<Element> {
precondition(_isValid(index))
var limit = limit ?? endIndex
precondition(_isValid(limit))
var limit = limit
if limit < index { limit = endIndex }
let startOffset = index._offset
let endOffset = _params.endOffset(
fromOffset: index._offset,
maxCount: limit._offset - index._offset)
maxCount: Swift.min(limit._offset - index._offset, maxCount))
index._offset = endOffset
return _contents.span.extracting(startOffset ..< endOffset)
}