- Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathAsyncRemoveDuplicatesSequence.swift
163 lines (135 loc) · 5.11 KB
/
AsyncRemoveDuplicatesSequence.swift
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
152
153
154
155
156
157
158
159
160
161
162
163
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
@available(AsyncAlgorithms 1.0,*)
extensionAsyncSequencewhere Element:Equatable{
/// Creates an asynchronous sequence that omits repeated elements.
@available(AsyncAlgorithms 1.0,*)
publicfunc removeDuplicates()->AsyncRemoveDuplicatesSequence<Self>{
AsyncRemoveDuplicatesSequence(self){ lhs, rhs in
lhs == rhs
}
}
}
@available(AsyncAlgorithms 1.0,*)
extensionAsyncSequence{
/// Creates an asynchronous sequence that omits repeated elements by testing them with a predicate.
@available(AsyncAlgorithms 1.0,*)
publicfunc removeDuplicates(
by predicate:@escaping@Sendable(Element, Element)async->Bool
)->AsyncRemoveDuplicatesSequence<Self>{
returnAsyncRemoveDuplicatesSequence(self, predicate: predicate)
}
/// Creates an asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
@available(AsyncAlgorithms 1.0,*)
publicfunc removeDuplicates(
by predicate:@escaping@Sendable(Element, Element)asyncthrows->Bool
)->AsyncThrowingRemoveDuplicatesSequence<Self>{
returnAsyncThrowingRemoveDuplicatesSequence(self, predicate: predicate)
}
}
/// An asynchronous sequence that omits repeated elements by testing them with a predicate.
@available(AsyncAlgorithms 1.0,*)
publicstructAsyncRemoveDuplicatesSequence<Base:AsyncSequence>:AsyncSequence{
publictypealiasElement=Base.Element
/// The iterator for an `AsyncRemoveDuplicatesSequence` instance.
publicstructIterator:AsyncIteratorProtocol{
@usableFromInline
variterator:Base.AsyncIterator
@usableFromInline
letpredicate:@Sendable(Element, Element)async->Bool
@usableFromInline
varlast:Element?
@usableFromInline
init(iterator:Base.AsyncIterator, predicate:@escaping@Sendable(Element, Element)async->Bool){
self.iterator = iterator
self.predicate = predicate
}
@inlinable
publicmutatingfunc next()asyncrethrows->Element?{
guardlet last = last else{
last =tryawait iterator.next()
return last
}
whilelet element =tryawait iterator.next(){
ifawait !predicate(last, element){
self.last = element
return element
}
}
return nil
}
}
@usableFromInline
letbase:Base
@usableFromInline
letpredicate:@Sendable(Element, Element)async->Bool
init(_ base:Base, predicate:@escaping@Sendable(Element, Element)async->Bool){
self.base = base
self.predicate = predicate
}
@inlinable
publicfunc makeAsyncIterator()->Iterator{
Iterator(iterator: base.makeAsyncIterator(), predicate: predicate)
}
}
/// An asynchronous sequence that omits repeated elements by testing them with an error-throwing predicate.
@available(AsyncAlgorithms 1.0,*)
publicstruct AsyncThrowingRemoveDuplicatesSequence<Base:AsyncSequence>:AsyncSequence{
publictypealiasElement=Base.Element
/// The iterator for an `AsyncThrowingRemoveDuplicatesSequence` instance.
publicstruct Iterator:AsyncIteratorProtocol{
@usableFromInline
variterator:Base.AsyncIterator
@usableFromInline
letpredicate:@Sendable(Element, Element)asyncthrows->Bool
@usableFromInline
varlast:Element?
@usableFromInline
init(iterator:Base.AsyncIterator, predicate:@escaping@Sendable(Element, Element)asyncthrows->Bool){
self.iterator = iterator
self.predicate = predicate
}
@inlinable
publicmutatingfunc next()asyncthrows->Element?{
guardlet last = last else{
last =tryawait iterator.next()
return last
}
whilelet element =tryawait iterator.next(){
if tryawait !predicate(last, element){
self.last = element
return element
}
}
returnnil
}
}
@usableFromInline
letbase:Base
@usableFromInline
letpredicate:@Sendable(Element, Element)asyncthrows->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)
}
}
@available(AsyncAlgorithms 1.0,*)
extension AsyncRemoveDuplicatesSequence: Sendable where Base: Sendable,Base.Element: Sendable {}
@available(AsyncAlgorithms 1.0,*)
extensionAsyncThrowingRemoveDuplicatesSequence:Sendablewhere Base:Sendable, Base.Element:Sendable{}
@available(*, unavailable)
extensionAsyncRemoveDuplicatesSequence.Iterator:Sendable{}
@available(*, unavailable)
extensionAsyncThrowingRemoveDuplicatesSequence.Iterator:Sendable{}