mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
Align Iterable with proposal
Adds throwing and ~Escapable elements
This commit is contained in:
1 parent
4bda43f4a9
commit
dca015d7e8
37 files changed
+234
-221
No files matched your search
+1
-1
@@ -17,7 +17,7 @@ import PackageDescription
|
||||
let _traits: Set<Trait> = [
|
||||
.default(
|
||||
enabledTraits: [
|
||||
// "UnstableContainersPreview",
|
||||
"UnstableContainersPreview",
|
||||
// "UnstableHashedContainers",
|
||||
// "UnstableSortedCollections",
|
||||
]),
|
||||
|
||||
@@ -349,12 +349,12 @@ extension RigidArray {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
Source: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
) throws(E) {
|
||||
let target = _freeSpace
|
||||
_count += newElements._copyContents(intoPrefixOf: target)
|
||||
_count += try newElements._copyContents(intoPrefixOf: target)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -371,11 +371,11 @@ extension RigidArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
_append(copying: newElements)
|
||||
) throws(E) {
|
||||
try _append(copying: newElements)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -413,7 +413,7 @@ extension RigidArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Iterable_<Element> & Sequence<Element>
|
||||
Source: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(copying newElements: Source) {
|
||||
_append(copying: newElements)
|
||||
}
|
||||
|
||||
@@ -124,12 +124,12 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Iterable_<Element> & ~Copyable & ~Escapable>(
|
||||
public init<Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing Source
|
||||
) {
|
||||
) throws(E) {
|
||||
self.init(capacity: capacity)
|
||||
self.append(copying: contents)
|
||||
try self.append(copying: contents)
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -145,7 +145,7 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Iterable_<Element> & Sequence<Element>>(
|
||||
public init<Source: Iterable_<Element, Never> & Sequence<Element>>(
|
||||
capacity: Int,
|
||||
copying contents: Source
|
||||
) {
|
||||
|
||||
@@ -414,7 +414,7 @@ extension RigidArray {
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
var it = items.makeIterableIterator_()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
while !target.isFull {
|
||||
@@ -479,7 +479,7 @@ extension RigidArray {
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying newElements: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: newElements, newCount: newElements.count)
|
||||
}
|
||||
@@ -539,7 +539,7 @@ extension RigidArray {
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying newElements: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: newElements, newCount: newElements.count)
|
||||
}
|
||||
|
||||
@@ -590,7 +590,7 @@ extension RigidArray {
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
var it = items.makeIterableIterator_()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
while !target.isFull {
|
||||
@@ -668,7 +668,7 @@ extension RigidArray {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
@@ -749,7 +749,7 @@ extension RigidArray {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
|
||||
@@ -119,7 +119,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing S
|
||||
@@ -131,7 +131,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Collection<Element>
|
||||
S: Iterable_<Element, Never> & Collection<Element>
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
|
||||
@@ -261,7 +261,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
|
||||
@@ -310,14 +310,14 @@ extension UniqueArray {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
Source: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
) throws(E) {
|
||||
_ensureFreeCapacity(newElements.underestimatedCount_)
|
||||
var it = newElements.makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
_ensureFreeCapacity(span.count)
|
||||
_storage.append(copying: span)
|
||||
@@ -343,11 +343,11 @@ extension UniqueArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
self._append(copying: newElements)
|
||||
) throws(E) {
|
||||
try self._append(copying: newElements)
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -397,7 +397,7 @@ extension UniqueArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Iterable_<Element> & Sequence<Element>
|
||||
Source: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(copying newElements: Source) {
|
||||
self._append(copying: newElements)
|
||||
}
|
||||
|
||||
@@ -96,12 +96,12 @@ extension UniqueArray /*where Element: Copyable*/ {
|
||||
/// The sequence must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Iterable_<Element> & ~Copyable & ~Escapable>(
|
||||
public init<Source: Iterable_<Element, E> & ~Copyable & ~Escapable, E>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: borrowing Source
|
||||
) {
|
||||
) throws(E) {
|
||||
self.init(minimumCapacity: capacity ?? 0)
|
||||
self.append(copying: contents)
|
||||
try self.append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -132,7 +132,7 @@ extension UniqueArray /*where Element: Copyable*/ {
|
||||
/// - contents: The container whose contents to copy into the new array.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Iterable_<Element> & Sequence<Element>>(
|
||||
public init<Source: Iterable_<Element, Never> & Sequence<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: Source
|
||||
) {
|
||||
|
||||
@@ -396,7 +396,7 @@ extension UniqueArray {
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying newElements: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c)
|
||||
@@ -463,7 +463,7 @@ extension UniqueArray {
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying newElements: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c)
|
||||
|
||||
@@ -558,7 +558,7 @@ extension UniqueArray {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c - subrange.count)
|
||||
@@ -648,7 +648,7 @@ extension UniqueArray {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c - subrange.count)
|
||||
|
||||
@@ -95,7 +95,7 @@ extension UniqueSet where Element: Copyable {
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
|
||||
@@ -172,7 +172,7 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public protocol Container<Element>:
|
||||
Iterable_, ~Copyable, ~Escapable
|
||||
where Element: ~Copyable, Element == Element_
|
||||
where Element: ~Copyable, Element == Element_ //, Failure == Never
|
||||
{
|
||||
associatedtype Element: ~Copyable
|
||||
associatedtype Index: Equatable
|
||||
|
||||
@@ -427,13 +427,13 @@ where
|
||||
|
||||
@inlinable
|
||||
public mutating func append<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
) throws(Failure) {
|
||||
var it = items.makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
guard !span.isEmpty else { break }
|
||||
self.append(copying: span)
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ extension Iterable_
|
||||
public func reduce<Result: ~Copyable>(
|
||||
_ initialResult: consuming Result,
|
||||
_ nextPartialResult:
|
||||
(_ partialResult: consuming Result, borrowing Element_) throws -> Result
|
||||
) rethrows -> Result {
|
||||
(_ partialResult: consuming Result, borrowing Element_) throws(Failure) -> Result
|
||||
) throws(Failure) -> Result {
|
||||
try makeIterableIterator_().reduce(initialResult, nextPartialResult)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ extension Iterable_
|
||||
public func reduce<Result>(
|
||||
into initialResult: consuming Result,
|
||||
_ updateAccumulatingResult:
|
||||
(_ partialResult: inout Result, borrowing Element_) throws -> ()
|
||||
) rethrows -> Result {
|
||||
(_ partialResult: inout Result, borrowing Element_) throws(Failure) -> ()
|
||||
) throws(Failure) -> Result {
|
||||
try makeIterableIterator_().reduce(into: initialResult, updateAccumulatingResult)
|
||||
}
|
||||
}
|
||||
@@ -42,8 +42,8 @@ extension Sequence where Self: Iterable_ {
|
||||
public func reduce<Result: ~Copyable>(
|
||||
_ initialResult: consuming Result,
|
||||
_ nextPartialResult:
|
||||
(_ partialResult: consuming Result, borrowing Element_) throws -> Result
|
||||
) rethrows -> Result {
|
||||
(_ partialResult: consuming Result, borrowing Element_) throws(Failure) -> Result
|
||||
) throws(Failure) -> Result {
|
||||
try makeIterableIterator_().reduce(initialResult, nextPartialResult)
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ extension Sequence where Self: Iterable_ {
|
||||
public func reduce<Result>(
|
||||
into initialResult: consuming Result,
|
||||
_ updateAccumulatingResult:
|
||||
(_ partialResult: inout Result, borrowing Element_) throws -> ()
|
||||
) rethrows -> Result {
|
||||
(_ partialResult: inout Result, borrowing Element_) throws(Failure) -> ()
|
||||
) throws(Failure) -> Result {
|
||||
try makeIterableIterator_().reduce(into: initialResult, updateAccumulatingResult)
|
||||
}
|
||||
}
|
||||
@@ -64,24 +64,24 @@ extension Iterable_
|
||||
@inlinable
|
||||
public func elementsEqual<OtherSequence: Iterable_>(
|
||||
_ other: borrowing OtherSequence,
|
||||
by areEquivalent: (borrowing Element_, borrowing OtherSequence.Element_) throws -> Bool
|
||||
) rethrows -> Bool
|
||||
where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element_: ~Copyable
|
||||
by areEquivalent: (borrowing Element_, borrowing OtherSequence.Element_) throws(Failure) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element_: ~Copyable, OtherSequence.Failure == Failure
|
||||
{
|
||||
// FIXME: Forward to the iterator's implementation of same
|
||||
var iter1 = makeIterableIterator_()
|
||||
var iter2 = other.makeIterableIterator_()
|
||||
while true {
|
||||
var el1 = iter1.nextSpan_()
|
||||
var el1 = try iter1.nextSpan_()
|
||||
|
||||
if el1.isEmpty {
|
||||
// LHS is empty - sequences are equal iff RHS is also empty
|
||||
let el2 = iter2.nextSpan_(maximumCount: 1)
|
||||
let el2 = try iter2.nextSpan_(maximumCount: 1)
|
||||
return el2.isEmpty
|
||||
}
|
||||
|
||||
while el1.count > 0 {
|
||||
let el2 = iter2.nextSpan_(maximumCount: el1.count)
|
||||
let el2 = try iter2.nextSpan_(maximumCount: el1.count)
|
||||
if el2.isEmpty { return false }
|
||||
for i in 0..<el2.count {
|
||||
if try !areEquivalent(el1[i], el2[i]) { return false }
|
||||
@@ -96,14 +96,14 @@ extension Iterable_
|
||||
extension Iterable_
|
||||
where Self: ~Copyable & ~Escapable, Element_: ~Copyable & Equatable {
|
||||
@inlinable
|
||||
public func elementsEqual<OtherSequence: Iterable_<Element_>>(
|
||||
public func elementsEqual<OtherSequence: Iterable_<Element_, Failure>>(
|
||||
_ other: borrowing OtherSequence
|
||||
) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where
|
||||
OtherSequence: ~Copyable & ~Escapable,
|
||||
OtherSequence.Element_: ~Copyable
|
||||
{
|
||||
return self.elementsEqual(other, by: ==)
|
||||
return try self.elementsEqual(other, by: ==)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,17 +22,17 @@ extension Iterable_ where Self: ~Copyable & ~Escapable, Element_: Copyable {
|
||||
@inlinable
|
||||
package func _copyContents(
|
||||
intoPrefixOf buffer: UnsafeMutableBufferPointer<Element_>
|
||||
) -> Int {
|
||||
) throws(Failure) -> Int {
|
||||
var target = buffer
|
||||
var it = self.makeIterableIterator_()
|
||||
while target.count != 0 {
|
||||
let span = it.nextSpan_(maximumCount: target.count)
|
||||
let span = try it.nextSpan_(maximumCount: target.count)
|
||||
if span.isEmpty {
|
||||
return buffer.count - target.count
|
||||
}
|
||||
target._initializeAndDropPrefix(copying: span)
|
||||
}
|
||||
let test = it.nextSpan_()
|
||||
let test = try it.nextSpan_()
|
||||
precondition(test.isEmpty, "Contents do not fit in target buffer")
|
||||
return buffer.count
|
||||
}
|
||||
|
||||
@@ -14,9 +14,11 @@
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public protocol Iterable_<Element_>: ~Copyable, ~Escapable {
|
||||
associatedtype Element_: ~Copyable
|
||||
associatedtype IterableIterator_: IterableIteratorProtocol_<Element_> & ~Copyable & ~Escapable
|
||||
public protocol Iterable_<Element_, Failure>: ~Copyable, ~Escapable {
|
||||
associatedtype Element_: ~Copyable & ~Escapable
|
||||
associatedtype Failure: Error = Never
|
||||
|
||||
associatedtype IterableIterator_: IterableIteratorProtocol_<Element_, Failure> & ~Copyable & ~Escapable
|
||||
|
||||
var underestimatedCount_: Int { get }
|
||||
|
||||
@@ -58,12 +60,12 @@ where
|
||||
{
|
||||
/// Implementation demo of what borrowing for-in loops would need to expand into.
|
||||
@inlinable
|
||||
public func _borrowingForEach<E: Error>(
|
||||
_ body: (borrowing Element_) throws(E) -> Void
|
||||
) throws(E) -> Void {
|
||||
public func _borrowingForEach(
|
||||
_ body: (borrowing Element_) throws(Failure) -> Void
|
||||
) throws(Failure) -> Void {
|
||||
var it = makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
var i = 0
|
||||
while i < span.count {
|
||||
|
||||
@@ -25,15 +25,15 @@ where
|
||||
{
|
||||
@inlinable
|
||||
package func _elementsEqual<
|
||||
Other: Iterable_<Element_> & ~Copyable & ~Escapable
|
||||
Other: Iterable_<Element_, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: borrowing Other,
|
||||
) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element_: ~Copyable
|
||||
{
|
||||
let it1 = self.makeIterableIterator_()
|
||||
let it2 = other.makeIterableIterator_()
|
||||
return it1.elementsEqual(it2)
|
||||
return try it1.elementsEqual(it2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,13 +66,12 @@ where
|
||||
/// - Complexity: O(*m*), where *m* is the count of the longer of the input sequences.
|
||||
@inlinable
|
||||
package func _elementsEqual<
|
||||
E: Error,
|
||||
Other: Iterable_ & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: borrowing Other,
|
||||
by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(E) -> Bool
|
||||
) throws(E) -> Bool
|
||||
where Other.Element_: ~Copyable
|
||||
by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(Failure) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element_: ~Copyable, Other.Failure == Failure
|
||||
{
|
||||
let it1 = self.makeIterableIterator_()
|
||||
let it2 = other.makeIterableIterator_()
|
||||
@@ -88,14 +87,14 @@ where
|
||||
{
|
||||
@inlinable
|
||||
package consuming func elementsEqual<
|
||||
Other: IterableIteratorProtocol_<Element_> & ~Copyable & ~Escapable
|
||||
Other: IterableIteratorProtocol_<Element_, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element_: ~Copyable
|
||||
{
|
||||
var result = true
|
||||
_spanwiseZip(state: &result, with: other) { state, a, b in
|
||||
try _spanwiseZip(state: &result, with: other) { state, a, b in
|
||||
if a.isEmpty || b.isEmpty {
|
||||
state = false
|
||||
return false
|
||||
@@ -114,10 +113,10 @@ where
|
||||
|
||||
@inlinable
|
||||
package consuming func _directElementsEqual<
|
||||
Other: IterableIteratorProtocol_<Element_> & ~Copyable & ~Escapable
|
||||
Other: IterableIteratorProtocol_<Element_, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element_: ~Copyable
|
||||
{
|
||||
#if true // FIXME: rdar://150228920 Exclusive access scopes aren't expanded enough
|
||||
@@ -126,13 +125,13 @@ where
|
||||
// (It lets the two iterators run at their native speeds, with no artificial
|
||||
// maximumCounts.)
|
||||
while true {
|
||||
let a = self.nextSpan_()
|
||||
let a = try self.nextSpan_()
|
||||
var i = 0
|
||||
if a.isEmpty {
|
||||
return other.nextSpan_().isEmpty
|
||||
return try other.nextSpan_().isEmpty
|
||||
}
|
||||
while i < a.count {
|
||||
let b = other.nextSpan_(maximumCount: a.count - i)
|
||||
let b = try other.nextSpan_(maximumCount: a.count - i)
|
||||
if b.isEmpty {
|
||||
return false
|
||||
}
|
||||
@@ -182,16 +181,15 @@ where
|
||||
{
|
||||
@inlinable
|
||||
package consuming func elementsEqual<
|
||||
E: Error,
|
||||
Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(E) -> Bool
|
||||
) throws(E) -> Bool
|
||||
where Other.Element_: ~Copyable
|
||||
by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(Failure) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element_: ~Copyable, Other.Failure == Failure
|
||||
{
|
||||
var result = true
|
||||
try _spanwiseZip(state: &result, with: other) { state, a, b throws(E) in
|
||||
try _spanwiseZip(state: &result, with: other) { state, a, b throws(Failure) in
|
||||
assert(a.count == b.count || a.isEmpty || b.isEmpty)
|
||||
if a.isEmpty || b.isEmpty {
|
||||
state = false
|
||||
|
||||
@@ -34,9 +34,10 @@ public struct BorrowingFilter<
|
||||
>: ~Copyable, ~Escapable
|
||||
where Base.Element_: ~Copyable {
|
||||
public typealias Element_ = Base.Element_
|
||||
|
||||
public typealias Failure = Base.Failure
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public let _isIncluded: (borrowing Element_) -> Bool
|
||||
public let _isIncluded: (borrowing Element_) throws(Base.Failure) -> Bool
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public var _base: Base
|
||||
@@ -58,13 +59,13 @@ where Base.Element_: ~Copyable {
|
||||
extension BorrowingFilter: IterableIteratorProtocol_
|
||||
where Base: ~Copyable & ~Escapable, Base.Element_: ~Copyable {
|
||||
@_lifetime(&self)
|
||||
public mutating func nextSpan_(maximumCount: Int) -> Span<Element_> {
|
||||
public mutating func nextSpan_(maximumCount: Int) throws(Failure) -> Span<Element_> {
|
||||
// FIXME: This is quite inefficient compared to Container's filter
|
||||
while true {
|
||||
let span = _base.nextSpan_(maximumCount: 1)
|
||||
let span = try _base.nextSpan_(maximumCount: 1)
|
||||
if span.isEmpty { return span }
|
||||
precondition(span.count == 1, "Invalid BorrowingIterator")
|
||||
if _isIncluded(span[unchecked: 0]) { return span }
|
||||
if try _isIncluded(span[unchecked: 0]) { return span }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,8 +68,11 @@ where
|
||||
|
||||
@inlinable
|
||||
public mutating func next() throws(Failure) -> Element? {
|
||||
let span = _it.nextSpan_(maximumCount: 1)
|
||||
guard !span.isEmpty else { return nil }
|
||||
// FIXME: Probably wrong to eat the error from the base iterator
|
||||
guard
|
||||
let span = try? _it.nextSpan_(maximumCount: 1),
|
||||
!span.isEmpty
|
||||
else { return nil }
|
||||
return try _transform(span[unchecked: 0])
|
||||
}
|
||||
|
||||
@@ -82,8 +85,11 @@ where
|
||||
) throws(Error) -> Bool {
|
||||
var success = false
|
||||
while !target.isFull {
|
||||
let span = _it.nextSpan_(maximumCount: target.freeCapacity)
|
||||
guard !span.isEmpty else { break }
|
||||
// FIXME: Probably wrong to eat the error from the base iterator
|
||||
guard
|
||||
let span = try? _it.nextSpan_(maximumCount: target.freeCapacity),
|
||||
!span.isEmpty
|
||||
else { break }
|
||||
success = true
|
||||
var i = 0
|
||||
while i < span.count {
|
||||
|
||||
@@ -20,13 +20,13 @@ where
|
||||
Element_: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
public consuming func reduce<Result: ~Copyable, E: Error>(
|
||||
public consuming func reduce<Result: ~Copyable>(
|
||||
_ initialResult: consuming Result,
|
||||
_ nextPartialResult: (consuming Result, borrowing Element_) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
_ nextPartialResult: (consuming Result, borrowing Element_) throws(Failure) -> Result
|
||||
) throws(Failure) -> Result {
|
||||
var result = initialResult
|
||||
while true {
|
||||
let span = self.nextSpan_()
|
||||
let span = try self.nextSpan_()
|
||||
guard !span.isEmpty else { break }
|
||||
var i = 0
|
||||
while i < span.count {
|
||||
@@ -38,13 +38,13 @@ where
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public consuming func reduce<Result: ~Copyable, E: Error>(
|
||||
public consuming func reduce<Result: ~Copyable>(
|
||||
into initialResult: consuming Result,
|
||||
_ updateAccumulatingResult: (inout Result, borrowing Element_) throws(E) -> Void
|
||||
) throws(E) -> Result {
|
||||
_ updateAccumulatingResult: (inout Result, borrowing Element_) throws(Failure) -> Void
|
||||
) throws(Failure) -> Result {
|
||||
var result = initialResult
|
||||
while true {
|
||||
let span = self.nextSpan_()
|
||||
let span = try self.nextSpan_()
|
||||
guard !span.isEmpty else { break }
|
||||
var i = 0
|
||||
while i < span.count {
|
||||
|
||||
@@ -26,13 +26,13 @@ where
|
||||
@inlinable
|
||||
internal consuming func _spanwiseZip<
|
||||
Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable,
|
||||
State: ~Copyable, E: Error
|
||||
State: ~Copyable
|
||||
>(
|
||||
state: inout State,
|
||||
with other: consuming Other,
|
||||
by process: (inout State, Span<Element_>, Span<Other.Element_>) throws(E) -> Bool
|
||||
) throws(E)
|
||||
where Other.Element_: ~Copyable
|
||||
by process: (inout State, Span<Element_>, Span<Other.Element_>) throws(Failure) -> Bool
|
||||
) throws(Failure)
|
||||
where Other.Element_: ~Copyable, Other.Failure == Failure
|
||||
{
|
||||
#if true // FIXME: rdar://150228920 Exclusive access scopes aren't expanded enough
|
||||
// Note: This is the less efficient implementation of spanwiseZip. The
|
||||
@@ -41,17 +41,17 @@ where
|
||||
// maximumCounts.)
|
||||
loop:
|
||||
while true {
|
||||
var a = self.nextSpan_()
|
||||
var a = try self.nextSpan_()
|
||||
if a.isEmpty {
|
||||
while true {
|
||||
let b = other.nextSpan_()
|
||||
let b = try other.nextSpan_()
|
||||
guard !b.isEmpty else { break }
|
||||
guard try process(&state, a, b) else { break }
|
||||
}
|
||||
return
|
||||
}
|
||||
repeat {
|
||||
let b = other.nextSpan_(maximumCount: a.count)
|
||||
let b = try other.nextSpan_(maximumCount: a.count)
|
||||
if b.isEmpty {
|
||||
guard try process(&state, a, b) else { return }
|
||||
} else {
|
||||
|
||||
@@ -18,8 +18,9 @@ import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public protocol IterableIteratorProtocol_<Element_>: ~Copyable, ~Escapable {
|
||||
associatedtype Element_: ~Copyable
|
||||
public protocol IterableIteratorProtocol_<Element_, Failure>: ~Copyable, ~Escapable {
|
||||
associatedtype Element_: ~Copyable & ~Escapable
|
||||
associatedtype Failure: Error = Never
|
||||
|
||||
/// Advance the iterator, returning an ephemeral span over the elements
|
||||
/// that are ready to be visited.
|
||||
@@ -69,7 +70,7 @@ public protocol IterableIteratorProtocol_<Element_>: ~Copyable, ~Escapable {
|
||||
/// method may vary between different borrows of the same container.)
|
||||
@_lifetime(&self)
|
||||
@_lifetime(self: copy self)
|
||||
mutating func nextSpan_(maximumCount: Int) -> Span<Element_>
|
||||
mutating func nextSpan_(maximumCount: Int) throws(Failure) -> Span<Element_>
|
||||
|
||||
/// Advance the position of this iterator by the specified offset, or until
|
||||
/// the end of the underlying sequence.
|
||||
@@ -81,7 +82,7 @@ public protocol IterableIteratorProtocol_<Element_>: ~Copyable, ~Escapable {
|
||||
///
|
||||
/// `maximumOffset` must be nonnegative.
|
||||
@_lifetime(self: copy self)
|
||||
mutating func skip_(by maximumOffset: Int) -> Int
|
||||
mutating func skip_(by maximumOffset: Int) throws(Failure) -> Int
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -90,8 +91,8 @@ where Self: ~Copyable & ~Escapable, Element_: ~Copyable {
|
||||
@_lifetime(&self)
|
||||
@_lifetime(self: copy self)
|
||||
@_transparent
|
||||
public mutating func nextSpan_() -> Span<Element_> {
|
||||
nextSpan_(maximumCount: Int.max)
|
||||
public mutating func nextSpan_() throws(Failure) -> Span<Element_> {
|
||||
try nextSpan_(maximumCount: Int.max)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,10 +101,10 @@ extension IterableIteratorProtocol_
|
||||
where Self: ~Copyable & ~Escapable, Element_: ~Copyable {
|
||||
@_lifetime(self: copy self)
|
||||
@inlinable
|
||||
public mutating func skip_(by offset: Int) -> Int {
|
||||
public mutating func skip_(by offset: Int) throws(Failure) -> Int {
|
||||
var remainder = offset
|
||||
while remainder > 0 {
|
||||
let span = nextSpan_(maximumCount: remainder)
|
||||
let span = try nextSpan_(maximumCount: remainder)
|
||||
if span.isEmpty { break }
|
||||
remainder &-= span.count
|
||||
}
|
||||
@@ -126,16 +127,16 @@ extension IterableIteratorProtocol_ where Self: ~Copyable & ~Escapable {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension IterableIteratorProtocol_
|
||||
where Self: ~Copyable & ~Escapable, Element_: Copyable
|
||||
where Self: ~Copyable & ~Escapable, Element_: Copyable
|
||||
{
|
||||
@_lifetime(self: copy self)
|
||||
@inlinable
|
||||
@_transparent
|
||||
package mutating func _copyContents_(into target: inout OutputSpan<Element_>) {
|
||||
target.withUnsafeMutableBufferPointer { dst, dstCount in
|
||||
package mutating func _copyContents_(into target: inout OutputSpan<Element_>) throws(Failure) {
|
||||
try target.withUnsafeMutableBufferPointer { (dst, dstCount) throws(Failure) -> Void in
|
||||
var tail = dst._extracting(droppingFirst: dstCount)
|
||||
while !tail.isEmpty {
|
||||
let src = nextSpan_(maximumCount: tail.count)
|
||||
let src = try nextSpan_(maximumCount: tail.count)
|
||||
if src.isEmpty { break }
|
||||
tail._initializeAndDropPrefix(copying: src)
|
||||
dstCount += src.count
|
||||
|
||||
@@ -313,11 +313,11 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
internal mutating func _append<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
>(copying items: borrowing S) {
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(copying items: borrowing S) throws(E) {
|
||||
var it = items.makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
self.append(copying: span)
|
||||
}
|
||||
@@ -334,10 +334,10 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `newElements`.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<S: Iterable_<Element> & ~Copyable & ~Escapable>(
|
||||
public mutating func append<S: Iterable_<Element, E> & ~Copyable & ~Escapable, E>(
|
||||
copying newElements: borrowing S
|
||||
) {
|
||||
self._append(copying: newElements)
|
||||
) throws(E) {
|
||||
try self._append(copying: newElements)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -375,11 +375,11 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`. @available(SwiftStdlib 5.0, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: S
|
||||
) {
|
||||
self._append(copying: items)
|
||||
try self._append(copying: items)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -132,13 +132,13 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing S
|
||||
) {
|
||||
) throws(E) {
|
||||
self.init(capacity: capacity)
|
||||
self._append(copying: contents)
|
||||
try self._append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -187,7 +187,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing S
|
||||
@@ -208,7 +208,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Collection<Element>
|
||||
S: Iterable_<Element, Never> & Collection<Element>
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
|
||||
@@ -363,7 +363,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeIterableIterator_()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
@@ -424,7 +424,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
@@ -486,7 +486,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
@@ -290,8 +290,8 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
>(copying items: borrowing S) {
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(copying items: borrowing S) throws(E) {
|
||||
// We don't know the exact count of new elements, so we cannot initialize
|
||||
// them in place. Append them to the end of the deque first, then rotate
|
||||
// them to their correct location.
|
||||
@@ -300,20 +300,20 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
// then we should use that when possible to copy items to their final
|
||||
// location in a single pass.
|
||||
let oldCount = self.count
|
||||
self._append(copying: items) // Not a typo!
|
||||
try self._append(copying: items) // Not a typo!
|
||||
_handle.rotate(toStartAtOffset: oldCount)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying items: borrowing S,
|
||||
exactCount: Int
|
||||
) {
|
||||
) throws(E) {
|
||||
var it = items.makeIterableIterator_()
|
||||
self.prepend(addingCount: exactCount) { target in
|
||||
let span = it.nextSpan_(maximumCount: target.freeCapacity)
|
||||
try self.prepend(addingCount: exactCount) { (target) throws(E) in
|
||||
let span = try it.nextSpan_(maximumCount: target.freeCapacity)
|
||||
target._append(copying: span)
|
||||
}
|
||||
}
|
||||
@@ -333,10 +333,10 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<S: Iterable_<Element> & ~Copyable & ~Escapable>(
|
||||
public mutating func prepend<S: Iterable_<Element, E> & ~Copyable & ~Escapable, E>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
self._prepend(copying: items)
|
||||
) throws(E) {
|
||||
try self._prepend(copying: items)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
@@ -352,7 +352,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
@@ -437,7 +437,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<S: Iterable_<Element> & Sequence<Element>>(
|
||||
public mutating func prepend<S: Iterable_<Element, Never> & Sequence<Element>>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
self._prepend(copying: items)
|
||||
@@ -453,7 +453,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<S: Iterable_<Element> & Collection<Element>>(
|
||||
public mutating func prepend<S: Iterable_<Element, Never> & Collection<Element>>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
@@ -471,7 +471,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
@@ -487,7 +487,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -463,7 +463,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeIterableIterator_()
|
||||
@@ -536,7 +536,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
@@ -613,7 +613,7 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
|
||||
@@ -300,11 +300,11 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
internal mutating func _append<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
>(copying items: borrowing S) {
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(copying items: borrowing S) throws(E) {
|
||||
var it = items.makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
self.append(copying: span)
|
||||
}
|
||||
@@ -323,10 +323,10 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`, when amortized
|
||||
/// over many similar invocations on the same deque
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<S: Iterable_<Element> & ~Copyable & ~Escapable>(
|
||||
public mutating func append<S: Iterable_<Element, E> & ~Copyable & ~Escapable, E>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
self._append(copying: items)
|
||||
) throws(E) {
|
||||
try self._append(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -374,11 +374,11 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// amortized over many similar invocations over the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: S
|
||||
) {
|
||||
self._append(copying: items)
|
||||
try self._append(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -145,13 +145,13 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: borrowing S
|
||||
) {
|
||||
) throws(E) {
|
||||
self.init(minimumCapacity: capacity ?? 0)
|
||||
self._append(copying: contents)
|
||||
try self._append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -184,7 +184,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
@@ -203,7 +203,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable_<Element> & Collection<Element>
|
||||
S: Iterable_<Element, Never> & Collection<Element>
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
|
||||
@@ -381,7 +381,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeIterableIterator_()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
@@ -445,7 +445,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
@@ -511,7 +511,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
@@ -322,8 +322,8 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
>(copying items: borrowing S) {
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(copying items: borrowing S) throws(E) {
|
||||
// We don't know the exact count of new elements, so we cannot initialize
|
||||
// them in place. Append them to the end of the deque first, then rotate
|
||||
// them to their correct location.
|
||||
@@ -332,20 +332,20 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
// then we should use that when possible to copy items to their final
|
||||
// location in a single pass.
|
||||
let oldCount = self.count
|
||||
self._append(copying: items) // Not a typo!
|
||||
try self._append(copying: items) // Not a typo!
|
||||
_storage._handle.rotate(toStartAtOffset: oldCount)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying items: borrowing S,
|
||||
exactCount: Int
|
||||
) {
|
||||
) throws(E) {
|
||||
var it = items.makeIterableIterator_()
|
||||
self.prepend(addingCount: exactCount) { target in
|
||||
let span = it.nextSpan_(maximumCount: target.freeCapacity)
|
||||
try self.prepend(addingCount: exactCount) { (target) throws(E) in
|
||||
let span = try it.nextSpan_(maximumCount: target.freeCapacity)
|
||||
target._append(copying: span)
|
||||
}
|
||||
}
|
||||
@@ -367,11 +367,11 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<
|
||||
S: Iterable_<Element> & ~Copyable & ~Escapable
|
||||
S: Iterable_<Element, E> & ~Copyable & ~Escapable, E
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
self._prepend(copying: items)
|
||||
) throws(E) {
|
||||
try self._prepend(copying: items)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
@@ -388,7 +388,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
@@ -479,7 +479,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<
|
||||
S: Iterable_<Element> & Sequence<Element>
|
||||
S: Iterable_<Element, Never> & Sequence<Element>
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
@@ -498,7 +498,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<
|
||||
S: Iterable_<Element> & Collection<Element>
|
||||
S: Iterable_<Element, Never> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing S
|
||||
) {
|
||||
@@ -518,7 +518,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
@@ -535,7 +535,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -472,7 +472,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeIterableIterator_()
|
||||
@@ -546,7 +546,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
@@ -625,7 +625,7 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
) where C.Failure == Never {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
|
||||
@@ -65,11 +65,11 @@ final class BorrowingSequenceTests: XCTestCase {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension Iterable_ where Self: ~Copyable & ~Escapable, Element_: Copyable {
|
||||
func collectViaBorrowing() -> [Element_] {
|
||||
func collectViaBorrowing() throws(Failure) -> [Element_] {
|
||||
var borrowIterator = makeIterableIterator_()
|
||||
var result: [Element_] = []
|
||||
while true {
|
||||
let span = borrowIterator.nextSpan_(maximumCount: .max)
|
||||
let span = try borrowIterator.nextSpan_(maximumCount: .max)
|
||||
if span.isEmpty { break }
|
||||
for i in span.indices {
|
||||
result.append(span[i])
|
||||
|
||||
@@ -127,8 +127,9 @@ public func expectIterableContents<
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public func expectIterablePayloads<
|
||||
Payload: Equatable,
|
||||
E1: Iterable_<LifetimeTrackedStruct<Payload>> & ~Copyable & ~Escapable,
|
||||
E1: Iterable_<LifetimeTrackedStruct<Payload>, E> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Payload>,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing E1,
|
||||
equalTo right: C2,
|
||||
@@ -136,12 +137,12 @@ public func expectIterablePayloads<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) where E1.Element_: ~Copyable {
|
||||
) throws(E) where E1.Element_: ~Copyable {
|
||||
var it1 = left.makeIterableIterator_()
|
||||
var it2 = right.makeIterator()
|
||||
var i = 0
|
||||
while true {
|
||||
let next1 = it1.nextSpan_(maximumCount: 1)
|
||||
let next1 = try it1.nextSpan_(maximumCount: 1)
|
||||
let next2 = it2.next()
|
||||
switch (next1.isEmpty, next2) {
|
||||
case (true, nil):
|
||||
@@ -174,8 +175,9 @@ public func expectIterablePayloads<
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public func expectIterablePayloads<
|
||||
Payload: Equatable,
|
||||
E1: Iterable_<LifetimeTracked<Payload>> & ~Copyable & ~Escapable,
|
||||
E1: Iterable_<LifetimeTracked<Payload>, E> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Payload>,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing E1,
|
||||
equalTo right: C2,
|
||||
@@ -183,12 +185,12 @@ public func expectIterablePayloads<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
) throws(E) {
|
||||
var it1 = left.makeIterableIterator_()
|
||||
var it2 = right.makeIterator()
|
||||
var i = 0
|
||||
while true {
|
||||
let next1 = it1.nextSpan_(maximumCount: 1)
|
||||
let next1 = try it1.nextSpan_(maximumCount: 1)
|
||||
let next2 = it2.next()
|
||||
switch (next1.isEmpty, next2) {
|
||||
case (true, nil):
|
||||
@@ -230,12 +232,13 @@ public func expectIterablesWithEquivalentElements<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
)
|
||||
) throws(S1.Failure)
|
||||
where
|
||||
S1.Element_: ~Copyable,
|
||||
S2.Element_: ~Copyable
|
||||
S2.Element_: ~Copyable,
|
||||
S1.Failure == S2.Failure
|
||||
{
|
||||
if left._elementsEqual(right, by: areEquivalent) { return }
|
||||
if try left._elementsEqual(right, by: areEquivalent) { return }
|
||||
_expectFailure(
|
||||
"Containers do not have equivalent elements",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
@@ -245,8 +248,9 @@ where
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public func expectIterablesWithEqualElements<
|
||||
Element: Equatable,
|
||||
S1: Iterable_<Element> & ~Copyable & ~Escapable,
|
||||
S2: Iterable_<Element> & ~Copyable & ~Escapable,
|
||||
S1: Iterable_<Element, E> & ~Copyable & ~Escapable,
|
||||
S2: Iterable_<Element, E> & ~Copyable & ~Escapable,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing S1,
|
||||
_ right: borrowing S2,
|
||||
@@ -254,12 +258,12 @@ public func expectIterablesWithEqualElements<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
)
|
||||
) throws(E)
|
||||
where
|
||||
S1.Element_: ~Copyable,
|
||||
S2.Element_: ~Copyable
|
||||
{
|
||||
if left._elementsEqual(right) { return }
|
||||
if try left._elementsEqual(right) { return }
|
||||
_expectFailure(
|
||||
"Containers do not have equal elements",
|
||||
message, trapping: trapping, file: file, line: line)
|
||||
@@ -269,8 +273,9 @@ where
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public func expectIterableContents<
|
||||
Element: Equatable,
|
||||
S1: Iterable_<Element> & ~Copyable & ~Escapable,
|
||||
S1: Iterable_<Element, E> & ~Copyable & ~Escapable,
|
||||
C2: Collection<Element>,
|
||||
E,
|
||||
>(
|
||||
_ left: borrowing S1,
|
||||
equalTo right: C2,
|
||||
@@ -278,11 +283,11 @@ public func expectIterableContents<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) {
|
||||
) throws(E) {
|
||||
var it1 = left.makeIterableIterator_()
|
||||
var it2 = right.makeIterator()
|
||||
while true {
|
||||
let span = it1.nextSpan_()
|
||||
let span = try it1.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
for i in 0 ..< span.count {
|
||||
guard let b = it2.next() else {
|
||||
@@ -321,12 +326,12 @@ public func expectIterableContents<
|
||||
trapping: Bool = false,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) where S1.Element_: ~Copyable {
|
||||
) throws(S1.Failure) where S1.Element_: ~Copyable {
|
||||
var it1 = left.makeIterableIterator_()
|
||||
var it2 = right.makeIterator()
|
||||
var offset = 0
|
||||
while true {
|
||||
let span = it1.nextSpan_()
|
||||
let span = try it1.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
for i in 0 ..< span.count {
|
||||
guard let b = it2.next() else {
|
||||
|
||||
@@ -29,8 +29,8 @@ public func checkIterable<
|
||||
expectedContents: Expected,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) where S.Element_: Equatable {
|
||||
checkIterable(
|
||||
) throws(S.Failure) where S.Element_: Equatable {
|
||||
try checkIterable(
|
||||
iterable,
|
||||
expectedContents: expectedContents,
|
||||
by: ==,
|
||||
@@ -48,7 +48,7 @@ public func checkIterable<
|
||||
by areEquivalent: (S.Element_, S.Element_) -> Bool,
|
||||
file: StaticString = #filePath,
|
||||
line: UInt = #line
|
||||
) where S.Element_: Equatable {
|
||||
) throws(S.Failure) where S.Element_: Equatable {
|
||||
let entry = TestContext.current.push("checkIterable", file: file, line: line)
|
||||
defer { TestContext.current.pop(entry) }
|
||||
|
||||
@@ -57,13 +57,13 @@ public func checkIterable<
|
||||
expectLessThanOrEqual(iterable.underestimatedCount_, expectedContents.count)
|
||||
|
||||
// Check that the spans seem plausibly sized and that the indices are monotonic.
|
||||
let spanShapes: [Range<Int>] = {
|
||||
let spanShapes: [Range<Int>] = try { () throws(S.Failure) in
|
||||
var r: [Range<Int>] = []
|
||||
var pos = 0
|
||||
var it = iterable.makeIterableIterator_()
|
||||
while true {
|
||||
let origPos = pos
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
pos += span.count
|
||||
if span.isEmpty {
|
||||
break
|
||||
@@ -82,7 +82,7 @@ public func checkIterable<
|
||||
var it = iterable.makeIterableIterator_()
|
||||
var spanIndex = 0
|
||||
while true {
|
||||
let span = it.nextSpan_()
|
||||
let span = try it.nextSpan_()
|
||||
if span.isEmpty { break }
|
||||
expectEqual(
|
||||
span.count, spanShapes[spanIndex].count,
|
||||
@@ -102,7 +102,7 @@ public func checkIterable<
|
||||
var pos = 0
|
||||
var it = iterable.makeIterableIterator_()
|
||||
while true {
|
||||
let span = it.nextSpan_(maximumCount: 1)
|
||||
let span = try it.nextSpan_(maximumCount: 1)
|
||||
if span.isEmpty { break }
|
||||
expectEqual(span.count, 1)
|
||||
for i in 0 ..< span.count {
|
||||
@@ -119,7 +119,7 @@ public func checkIterable<
|
||||
var it = iterable.makeIterableIterator_()
|
||||
var spanIndex = 0
|
||||
while true {
|
||||
let span = it.nextSpan_(maximumCount: Int.max)
|
||||
let span = try it.nextSpan_(maximumCount: Int.max)
|
||||
if span.isEmpty { break }
|
||||
expectEqual(
|
||||
span.count, spanShapes[spanIndex].count,
|
||||
|
||||
Reference in new issue
Block a user