1
- //===--- DropWhile.swift.gyb - Lazy views for drop(while:) ----*- swift -*-===//
1
+ //===--- DropWhile.swift - Lazy views for drop(while:) ---- ----*- 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
- TRAVERSALS,
16
- collectionForTraversal
17
- )
18
- } %
13
+ /// A sequence whose elements consist of the elements that follow the initial
14
+ /// consecutive elements of some base sequence that satisfy a given predicate.
15
+ @_fixed_layout // FIXME(sil-serialize-all)
16
+ public struct LazyDropWhileSequence < Base: Sequence > {
17
+ public typealias Element = Base . Element
18
+
19
+ /// Create an instance with elements `transform(x)` for each element
20
+ /// `x` of base.
21
+ @_inlineable // FIXME(sil-serialize-all)
22
+ @_versioned // FIXME(sil-serialize-all)
23
+ internal init ( _base: Base , predicate: @escaping ( Element ) -> Bool ) {
24
+ self . _base = _base
25
+ self . _predicate = predicate
26
+ }
27
+
28
+ @_versioned // FIXME(sil-serialize-all)
29
+ internal var _base : Base
30
+ @_versioned // FIXME(sil-serialize-all)
31
+ internal let _predicate : ( Element ) -> Bool
32
+ }
19
33
20
- //===--- Iterator & Sequence ----------------------------------------------===//
34
+ extension LazyDropWhileSequence {
35
+ /// An iterator over the elements traversed by a base iterator that follow the
36
+ /// initial consecutive elements that satisfy a given predicate.
37
+ ///
38
+ /// This is the associated iterator for the `LazyDropWhileSequence`,
39
+ /// `LazyDropWhileCollection`, and `LazyDropWhileBidirectionalCollection`
40
+ /// types.
41
+ @_fixed_layout // FIXME(sil-serialize-all)
42
+ public struct Iterator {
43
+ public typealias Element = Base . Element
44
+
45
+ @_inlineable // FIXME(sil-serialize-all)
46
+ @_versioned // FIXME(sil-serialize-all)
47
+ internal init ( _base: Base . Iterator , predicate: @escaping ( Element ) -> Bool ) {
48
+ self . _base = _base
49
+ self . _predicate = predicate
50
+ }
21
51
22
- /// An iterator over the elements traversed by a base iterator that follow the
23
- /// initial consecutive elements that satisfy a given predicate.
24
- ///
25
- /// This is the associated iterator for the `LazyDropWhileSequence`,
26
- /// `LazyDropWhileCollection`, and `LazyDropWhileBidirectionalCollection`
27
- /// types.
28
- @_fixed_layout // FIXME(sil-serialize-all)
29
- public struct LazyDropWhileIterator < Base : IteratorProtocol > :
30
- IteratorProtocol , Sequence {
52
+ @_versioned // FIXME(sil-serialize-all)
53
+ internal var _predicateHasFailed = false
54
+ @_versioned // FIXME(sil-serialize-all)
55
+ internal var _base : Base . Iterator
56
+ @_versioned // FIXME(sil-serialize-all)
57
+ internal let _predicate : ( Element ) -> Bool
58
+ }
59
+ }
31
60
61
+ extension LazyDropWhileSequence . Iterator : IteratorProtocol {
32
62
@_inlineable // FIXME(sil-serialize-all)
33
- public mutating func next( ) -> Base . Element ? {
63
+ public mutating func next( ) -> Element ? {
34
64
// Once the predicate has failed for the first time, the base iterator
35
65
// can be used for the rest of the elements.
36
66
if _predicateHasFailed {
@@ -46,52 +76,23 @@ public struct LazyDropWhileIterator<Base : IteratorProtocol> :
46
76
}
47
77
}
48
78
return nil
49
- }
50
-
51
- @_inlineable // FIXME(sil-serialize-all)
52
- @_versioned // FIXME(sil-serialize-all)
53
- internal init ( _base: Base , predicate: @escaping ( Base . Element ) -> Bool ) {
54
- self . _base = _base
55
- self . _predicate = predicate
56
- }
57
-
58
- @_versioned // FIXME(sil-serialize-all)
59
- internal var _predicateHasFailed = false
60
- @_versioned // FIXME(sil-serialize-all)
61
- internal var _base : Base
62
- @_versioned // FIXME(sil-serialize-all)
63
- internal let _predicate : ( Base . Element ) -> Bool
79
+ }
64
80
}
65
81
66
- /// A sequence whose elements consist of the elements that follow the initial
67
- /// consecutive elements of some base sequence that satisfy a given predicate.
68
- @_fixed_layout // FIXME(sil-serialize-all)
69
- public struct LazyDropWhileSequence < Base : Sequence > : LazySequenceProtocol {
70
-
71
- public typealias Elements = LazyDropWhileSequence
82
+ extension LazyDropWhileSequence : Sequence {
83
+ public typealias SubSequence = AnySequence < Element > // >:(
72
84
73
85
/// Returns an iterator over the elements of this sequence.
74
86
///
75
87
/// - Complexity: O(1).
76
88
@_inlineable // FIXME(sil-serialize-all)
77
- public func makeIterator( ) -> LazyDropWhileIterator < Base . Iterator > {
78
- return LazyDropWhileIterator (
79
- _base: _base. makeIterator ( ) , predicate: _predicate)
80
- }
81
-
82
- /// Create an instance with elements `transform(x)` for each element
83
- /// `x` of base.
84
- @_inlineable // FIXME(sil-serialize-all)
85
- @_versioned // FIXME(sil-serialize-all)
86
- internal init ( _base: Base , predicate: @escaping ( Base . Element ) -> Bool ) {
87
- self . _base = _base
88
- self . _predicate = predicate
89
+ public func makeIterator( ) -> Iterator {
90
+ return Iterator ( _base: _base. makeIterator ( ) , predicate: _predicate)
89
91
}
92
+ }
90
93
91
- @_versioned // FIXME(sil-serialize-all)
92
- internal var _base : Base
93
- @_versioned // FIXME(sil-serialize-all)
94
- internal let _predicate : ( Base . Element ) -> Bool
94
+ extension LazyDropWhileSequence : LazySequenceProtocol {
95
+ public typealias Elements = LazyDropWhileSequence
95
96
}
96
97
97
98
extension LazySequenceProtocol {
@@ -110,125 +111,127 @@ extension LazySequenceProtocol {
110
111
}
111
112
}
112
113
113
- //===--- Collections ------------------------------------------------------===//
114
-
115
- /// A position in a `LazyDropWhileCollection` or
116
- /// `LazyDropWhileBidirectionalCollection` instance.
114
+ /// A lazy wrapper that includes the elements of an underlying
115
+ /// collection after any initial consecutive elements that satisfy a
116
+ /// predicate.
117
+ ///
118
+ /// - Note: The performance of accessing `startIndex`, `first`, or any methods
119
+ /// that depend on `startIndex` depends on how many elements satisfy the
120
+ /// predicate at the start of the collection, and may not offer the usual
121
+ /// performance given by the `Collection` protocol. Be aware, therefore,
122
+ /// that general operations on lazy collections may not have the
123
+ /// documented complexity.
117
124
@_fixed_layout // FIXME(sil-serialize-all)
118
- public struct LazyDropWhileIndex < Base : Collection > : Comparable {
119
- /// The position corresponding to `self` in the underlying collection.
120
- public let base : Base . Index
121
-
125
+ public struct LazyDropWhileCollection < Base: Collection > {
126
+ public typealias Element = Base . Element
127
+
122
128
@_inlineable // FIXME(sil-serialize-all)
123
129
@_versioned // FIXME(sil-serialize-all)
124
- internal init ( base: Base . Index ) {
125
- self . base = base
130
+ internal init ( _base: Base , predicate: @escaping ( Element ) -> Bool ) {
131
+ self . _base = _base
132
+ self . _predicate = predicate
133
+ }
134
+
135
+ @_versioned // FIXME(sil-serialize-all)
136
+ internal var _base : Base
137
+ @_versioned // FIXME(sil-serialize-all)
138
+ internal let _predicate : ( Element ) -> Bool
139
+ }
140
+
141
+ extension LazyDropWhileCollection : Sequence {
142
+ public typealias Iterator = LazyDropWhileSequence < Base > . Iterator
143
+
144
+ /// Returns an iterator over the elements of this sequence.
145
+ ///
146
+ /// - Complexity: O(1).
147
+ @_inlineable // FIXME(sil-serialize-all)
148
+ public func makeIterator( ) -> Iterator {
149
+ return Iterator ( _base: _base. makeIterator ( ) , predicate: _predicate)
150
+ }
151
+ }
152
+
153
+ extension LazyDropWhileCollection {
154
+ public typealias SubSequence = Slice < LazyDropWhileCollection < Base > >
155
+
156
+ /// A position in a `LazyDropWhileCollection` or
157
+ /// `LazyDropWhileBidirectionalCollection` instance.
158
+ @_fixed_layout // FIXME(sil-serialize-all)
159
+ public struct Index {
160
+ /// The position corresponding to `self` in the underlying collection.
161
+ public let base : Base . Index
162
+
163
+ @_inlineable // FIXME(sil-serialize-all)
164
+ @_versioned // FIXME(sil-serialize-all)
165
+ internal init ( _base: Base . Index ) {
166
+ self . base = _base
167
+ }
126
168
}
169
+ }
127
170
171
+ extension LazyDropWhileCollection . Index : Equatable , Comparable {
128
172
@_inlineable // FIXME(sil-serialize-all)
129
173
public static func == (
130
- lhs: LazyDropWhileIndex , rhs: LazyDropWhileIndex
174
+ lhs: LazyDropWhileCollection < Base > . Index ,
175
+ rhs: LazyDropWhileCollection < Base > . Index
131
176
) -> Bool {
132
177
return lhs. base == rhs. base
133
178
}
134
179
135
180
@_inlineable // FIXME(sil-serialize-all)
136
181
public static func < (
137
- lhs: LazyDropWhileIndex , rhs: LazyDropWhileIndex
182
+ lhs: LazyDropWhileCollection < Base > . Index ,
183
+ rhs: LazyDropWhileCollection < Base > . Index
138
184
) -> Bool {
139
185
return lhs. base < rhs. base
140
186
}
141
187
}
142
188
143
- extension LazyDropWhileIndex : Hashable where Base. Index : Hashable {
189
+ extension LazyDropWhileCollection . Index : Hashable where Base. Index: Hashable {
144
190
public var hashValue : Int {
145
191
return base. hashValue
146
192
}
147
193
}
148
194
149
- % for Traversal in [ 'Forward', 'Bidirectional'] :
150
- % Collection = collectionForTraversal ( Traversal)
151
- % Self = " LazyDropWhile " + Collection
152
-
153
- /// A lazy `${Collection}` wrapper that includes the elements of an underlying
154
- /// collection after any initial consecutive elements that satisfy a
155
- /// predicate.
156
- ///
157
- /// - Note: The performance of accessing `startIndex`, `first`, or any methods
158
- /// that depend on `startIndex` depends on how many elements satisfy the
159
- /// predicate at the start of the collection, and may not offer the usual
160
- /// performance given by the `Collection` protocol. Be aware, therefore,
161
- /// that general operations on `${Self}` instances may not have the
162
- /// documented complexity.
163
- @_fixed_layout // FIXME(sil-serialize-all)
164
- public struct ${ Self} <
165
- Base : ${ Collection}
166
- > : LazyCollectionProtocol, ${ Collection} {
167
-
168
- // FIXME(compiler limitation): should be inferable.
169
- public typealias Index = LazyDropWhileIndex < Base >
170
-
195
+ extension LazyDropWhileCollection : Collection {
171
196
@_inlineable // FIXME(sil-serialize-all)
172
197
public var startIndex : Index {
173
198
var index = _base. startIndex
174
199
while index != _base. endIndex && _predicate ( _base [ index] ) {
175
200
_base. formIndex ( after: & index)
176
201
}
177
- return LazyDropWhileIndex ( base : index)
202
+ return Index ( _base : index)
178
203
}
179
204
180
205
@_inlineable // FIXME(sil-serialize-all)
181
206
public var endIndex : Index {
182
- return LazyDropWhileIndex ( base : _base. endIndex)
207
+ return Index ( _base : _base. endIndex)
183
208
}
184
209
185
210
@_inlineable // FIXME(sil-serialize-all)
186
211
public func index( after i: Index ) -> Index {
187
212
_precondition ( i. base < _base. endIndex, " Can't advance past endIndex " )
188
- return LazyDropWhileIndex ( base : _base. index ( after: i. base) )
213
+ return Index ( _base : _base. index ( after: i. base) )
189
214
}
190
215
191
- % if Traversal == 'Bidirectional':
192
216
193
217
@_inlineable // FIXME(sil-serialize-all)
194
- public func index( before i: Index ) -> Index {
195
- _precondition ( i > startIndex, " Can't move before startIndex " )
196
- return LazyDropWhileIndex ( base: _base. index ( before: i. base) )
197
- }
198
-
199
- % end
200
-
201
- @_inlineable // FIXME(sil-serialize-all)
202
- public subscript( position: Index) - > Base. Element {
218
+ public subscript( position: Index ) -> Element {
203
219
return _base [ position. base]
204
220
}
221
+ }
205
222
206
- @_inlineable // FIXME(sil-serialize-all)
207
- public func makeIterator( ) -> LazyDropWhileIterator < Base . Iterator > {
208
- return LazyDropWhileIterator (
209
- _base: _base. makeIterator ( ) , predicate: _predicate)
210
- }
223
+ extension LazyDropWhileCollection : LazyCollectionProtocol { }
211
224
225
+ extension LazyDropWhileCollection : BidirectionalCollection
226
+ where Base: BidirectionalCollection {
212
227
@_inlineable // FIXME(sil-serialize-all)
213
- @_versioned // FIXME(sil-serialize-all)
214
- internal init( _base: Base, predicate: @escaping ( Base . Element) -> Bool) {
215
- self . _base = _base
216
- self . _predicate = predicate
228
+ public func index( before i: Index ) -> Index {
229
+ _precondition ( i > startIndex, " Can't move before startIndex " )
230
+ return Index ( _base: _base. index ( before: i. base) )
217
231
}
218
-
219
- @_versioned // FIXME(sil-serialize-all)
220
- internal var _base : Base
221
- @_versioned // FIXME(sil-serialize-all)
222
- internal let _predicate : ( Base . Element ) -> Bool
223
232
}
224
233
225
- extension LazyCollectionProtocol
226
- % if Collection != 'Collection':
227
- where
228
- Self : ${ Collection} ,
229
- Elements : ${ Collection}
230
- % end
231
- {
234
+ extension LazyCollectionProtocol {
232
235
/// Returns a lazy collection that skips any initial elements that satisfy
233
236
/// `predicate`.
234
237
///
@@ -239,14 +242,16 @@ Elements : ${Collection}
239
242
@_inlineable // FIXME(sil-serialize-all)
240
243
public func drop(
241
244
while predicate: @escaping ( Elements . Element ) -> Bool
242
- ) -> LazyDropWhile $ { Collection } < Self . Elements > {
243
- return LazyDropWhile $ { Collection } (
245
+ ) -> LazyDropWhileCollection < Self . Elements > {
246
+ return LazyDropWhileCollection (
244
247
_base: self . elements, predicate: predicate)
245
248
}
246
249
}
247
250
248
- % end
251
+ @available ( * , deprecated, renamed: " LazyDropWhileSequence.Iterator " )
252
+ public typealias LazyDropWhileIterator < T> = LazyDropWhileSequence < T > . Iterator where T: Sequence
253
+ @available ( * , deprecated, renamed: " LazyDropWhileCollection.Index " )
254
+ public typealias LazyDropWhileIndex < T> = LazyDropWhileCollection < T > . Index where T: Collection
255
+ @available ( * , deprecated, renamed: " LazyDropWhileCollection " )
256
+ public typealias LazyDropWhileBidirectionalCollection < T> = LazyDropWhileCollection < T > where T: BidirectionalCollection
249
257
250
- // ${'Local Variables'}:
251
- // eval: (read-only-mode 1)
252
- // End:
0 commit comments