1
- //===--- Filter.swift.gyb -------------------------------------*- swift -*-===//
1
+ //===--- Filter.swift ---- -------------------------------------*- swift -*-===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- % {
14
- from gyb_stdlib_support import (
15
- collectionForTraversal
16
- )
17
- } %
18
-
19
13
/// An iterator over the elements traversed by some base iterator that also
20
14
/// satisfy a given predicate.
21
15
///
@@ -122,14 +116,6 @@ public struct LazyFilterSequence<Base : Sequence>
122
116
@available ( swift, deprecated: 3.1 , obsoleted: 4.0 , message: " Use Base.Index " )
123
117
public typealias LazyFilterIndex < Base : Collection > = Base . Index
124
118
125
- // FIXME(ABI)#27 (Conditional Conformance): `LazyFilter*Collection` types should be
126
- // collapsed into one `LazyFilterCollection` using conditional conformances.
127
- // Maybe even combined with `LazyFilterSequence`.
128
- // rdar://problem/17144340
129
-
130
- % for Traversal in [ 'Forward', 'Bidirectional'] :
131
- % Self = " LazyFilter " + collectionForTraversal( Traversal)
132
-
133
119
/// A lazy `Collection` wrapper that includes the elements of an
134
120
/// underlying collection that satisfy a predicate.
135
121
///
@@ -140,16 +126,16 @@ public typealias LazyFilterIndex<Base : Collection> = Base.Index
140
126
/// general operations on `LazyFilterCollection` instances may not have the
141
127
/// documented complexity.
142
128
@_fixed_layout // FIXME(sil-serialize-all)
143
- public struct ${ Self} <
144
- Base : ${ collectionForTraversal ( Traversal) }
145
- > : LazyCollectionProtocol, ${ collectionForTraversal ( Traversal) }
146
- {
129
+ public struct LazyFilterCollection <
130
+ Base : Collection
131
+ > : LazyCollectionProtocol , Collection {
147
132
148
133
/// A type that represents a valid position in the collection.
149
134
///
150
135
/// Valid indices consist of the position of every element and a
151
136
/// "past the end" position that's not valid for use as a subscript.
152
137
public typealias Index = Base . Index
138
+ public typealias Element = Base . Element
153
139
154
140
155
141
/// Creates an instance containing the elements of `base` that
@@ -221,26 +207,6 @@ public struct ${Self}<
221
207
i = index
222
208
}
223
209
224
- % if Traversal == 'Bidirectional':
225
- @_inlineable // FIXME(sil-serialize-all)
226
- public func index( before i: Index ) -> Index {
227
- var i = i
228
- formIndex ( before: & i)
229
- return i
230
- }
231
-
232
- @_inlineable // FIXME(sil-serialize-all)
233
- public func formIndex( before i: inout Index ) {
234
- // TODO: swift-3-indexing-model: _failEarlyRangeCheck i?
235
- var index = i
236
- _precondition ( index != _base. startIndex, " Can't retreat before startIndex " )
237
- repeat {
238
- _base. formIndex ( before: & index)
239
- } while !_predicate( _base [ index] )
240
- i = index
241
- }
242
- % end
243
-
244
210
/// Accesses the element at `position`.
245
211
///
246
212
/// - Precondition: `position` is a valid position in `self` and
@@ -250,7 +216,7 @@ public struct ${Self}<
250
216
return _base [ position]
251
217
}
252
218
253
- public typealias SubSequence = $ { Self } < Base . SubSequence>
219
+ public typealias SubSequence = LazyFilterCollection < Base . SubSequence >
254
220
255
221
@_inlineable // FIXME(sil-serialize-all)
256
222
public subscript( bounds: Range < Index > ) -> SubSequence {
@@ -290,7 +256,27 @@ public struct ${Self}<
290
256
internal let _predicate : ( Base . Element ) -> Bool
291
257
}
292
258
293
- % end
259
+ extension LazyFilterCollection : BidirectionalCollection
260
+ where Base : BidirectionalCollection {
261
+
262
+ @_inlineable // FIXME(sil-serialize-all)
263
+ public func index( before i: Index ) -> Index {
264
+ var i = i
265
+ formIndex ( before: & i)
266
+ return i
267
+ }
268
+
269
+ @_inlineable // FIXME(sil-serialize-all)
270
+ public func formIndex( before i: inout Index ) {
271
+ // TODO: swift-3-indexing-model: _failEarlyRangeCheck i?
272
+ var index = i
273
+ _precondition ( index != _base. startIndex, " Can't retreat before startIndex " )
274
+ repeat {
275
+ _base. formIndex ( before: & index)
276
+ } while !_predicate( _base [ index] )
277
+ i = index
278
+ }
279
+ }
294
280
295
281
extension LazySequenceProtocol {
296
282
/// Returns the elements of `self` that satisfy `isIncluded`.
@@ -303,20 +289,11 @@ extension LazySequenceProtocol {
303
289
public func filter(
304
290
_ isIncluded: @escaping ( Elements . Element ) -> Bool
305
291
) -> LazyFilterSequence < Self . Elements > {
306
- return LazyFilterSequence (
307
- _base: self . elements, isIncluded)
292
+ return LazyFilterSequence ( _base: self . elements, isIncluded)
308
293
}
309
294
}
310
295
311
- % for Traversal in [ 'Forward', 'Bidirectional'] :
312
-
313
- extension LazyCollectionProtocol
314
- % if Traversal != 'Forward':
315
- where
316
- Self : ${ collectionForTraversal ( Traversal) } ,
317
- Elements : ${ collectionForTraversal ( Traversal) }
318
- % end
319
- {
296
+ extension LazyCollectionProtocol {
320
297
/// Returns the elements of `self` that satisfy `predicate`.
321
298
///
322
299
/// - Note: The elements of the result are computed on-demand, as
@@ -326,14 +303,10 @@ extension LazyCollectionProtocol
326
303
@_inlineable // FIXME(sil-serialize-all)
327
304
public func filter(
328
305
_ isIncluded: @escaping ( Elements . Element ) -> Bool
329
- ) -> LazyFilter ${ collectionForTraversal( Traversal) } < Self . Elements > {
330
- return LazyFilter ${ collectionForTraversal ( Traversal) } (
331
- _base: self . elements, isIncluded)
306
+ ) -> LazyFilterCollection < Self . Elements > {
307
+ return LazyFilterCollection ( _base: self . elements, isIncluded)
332
308
}
333
309
}
334
310
335
- % end
336
-
337
- // ${'Local Variables'}:
338
- // eval: (read-only-mode 1)
339
- // End:
311
+ @available ( * , deprecated, renamed: " LazyFilterCollection " )
312
+ public typealias LazyFilterBidirectionalCollection < T> = LazyFilterCollection < T > where T : BidirectionalCollection
0 commit comments