1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
extension AsyncSequence where Element: Equatable {
/// Creates an asynchronous sequence that omits repeated elements.
public func removeDuplicates() -> AsyncRemoveDuplicatesSequence<Self> {
AsyncRemoveDuplicatesSequence(self) { lhs, rhs in
lhs == rhs
}
}
}
extension AsyncSequence {
/// Creates an asynchronous sequence that omits repeated elements by testing them with a predicate.
public func removeDuplicates(by predicate: @escaping @Sendable (Element, Element) async -> Bool) -> AsyncRemoveDuplicatesSequence<Self> {
return AsyncRemoveDuplicatesSequence(self, predicate: predicate)
}
/// Creates an asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
public func removeDuplicates(by predicate: @escaping @Sendable (Element, Element) async throws -> Bool) -> AsyncThrowingRemoveDuplicatesSequence<Self> {
return AsyncThrowingRemoveDuplicatesSequence(self, predicate: predicate)
}
}
/// An asynchronous sequence that omits repeated elements by testing them with a predicate.
public struct AsyncRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
public typealias Element = Base.Element
/// The iterator for an `AsyncRemoveDuplicatesSequence` instance.
public struct Iterator: AsyncIteratorProtocol {
@usableFromInline
var iterator: Base.AsyncIterator
@usableFromInline
let predicate: @Sendable (Element, Element) async -> Bool
@usableFromInline
var last: Element?
@usableFromInline
init(iterator: Base.AsyncIterator, predicate: @escaping @Sendable (Element, Element) async -> Bool) {
self.iterator = iterator
self.predicate = predicate
}
@inlinable
public mutating func next() async rethrows -> Element? {
guard let last = last else {
last = try await iterator.next()
return last
}
while let element = try await iterator.next() {
if await !predicate(last, element) {
self.last = element
return element
}
}
return nil
}
}
@usableFromInline
let base: Base
@usableFromInline
let predicate: @Sendable (Element, Element) async -> Bool
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async -> Bool) {
self.base = base
self.predicate = predicate
}
@inlinable
public func makeAsyncIterator() -> Iterator {
Iterator(iterator: base.makeAsyncIterator(), predicate: predicate)
}
}
/// An asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
public struct AsyncThrowingRemoveDuplicatesSequence<Base: AsyncSequence>: AsyncSequence {
public typealias Element = Base.Element
/// The iterator for an `AsyncThrowingRemoveDuplicatesSequence` instance.
public struct Iterator: AsyncIteratorProtocol {
@usableFromInline
var iterator: Base.AsyncIterator
@usableFromInline
let predicate: @Sendable (Element, Element) async throws -> Bool
@usableFromInline
var last: Element?
@usableFromInline
init(iterator: Base.AsyncIterator, predicate: @escaping @Sendable (Element, Element) async throws -> Bool) {
self.iterator = iterator
self.predicate = predicate
}
@inlinable
public mutating func next() async throws -> Element? {
guard let last = last else {
last = try await iterator.next()
return last
}
while let element = try await iterator.next() {
if try await !predicate(last, element) {
self.last = element
return element
}
}
return nil
}
}
@usableFromInline
let base: Base
@usableFromInline
let predicate: @Sendable (Element, Element) async throws -> Bool
init(_ base: Base, predicate: @escaping @Sendable (Element, Element) async throws -> Bool) {
self.base = base
self.predicate = predicate
}
@inlinable
public func makeAsyncIterator() -> Iterator {
Iterator(iterator: base.makeAsyncIterator(), predicate: predicate)
}
}
extension AsyncRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
extension AsyncThrowingRemoveDuplicatesSequence: Sendable where Base: Sendable, Base.Element: Sendable { }
@available(*, unavailable)
extension AsyncRemoveDuplicatesSequence.Iterator: Sendable { }
@available(*, unavailable)
extension AsyncThrowingRemoveDuplicatesSequence.Iterator: Sendable { }
|