Skip to content

Commit 2380950

Browse files
[stdlib] Collapse DropWhileBidirectionalCollection and de-gyb DropWhile.swift (swiftlang#14906)
Collapse DropWhileBidirectionalCollection and de-gyb DropWhile.swift
1 parent 496122b commit 2380950

File tree

4 files changed

+150
-142
lines changed

4 files changed

+150
-142
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ set(SWIFTLIB_ESSENTIAL
4848
CTypes.swift
4949
DebuggerSupport.swift
5050
DoubleWidth.swift.gyb
51-
DropWhile.swift.gyb
51+
DropWhile.swift
5252
Dump.swift
5353
EmptyCollection.swift
5454
Equatable.swift

stdlib/public/core/DropWhile.swift.gyb renamed to stdlib/public/core/DropWhile.swift

Lines changed: 135 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- DropWhile.swift.gyb - Lazy views for drop(while:) ----*- swift -*-===//
1+
//===--- DropWhile.swift - Lazy views for drop(while:) --------*- swift -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,27 +10,57 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

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+
}
1933

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+
}
2151

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+
}
3160

61+
extension LazyDropWhileSequence.Iterator: IteratorProtocol {
3262
@_inlineable // FIXME(sil-serialize-all)
33-
public mutating func next() -> Base.Element? {
63+
public mutating func next() -> Element? {
3464
// Once the predicate has failed for the first time, the base iterator
3565
// can be used for the rest of the elements.
3666
if _predicateHasFailed {
@@ -46,52 +76,23 @@ public struct LazyDropWhileIterator<Base : IteratorProtocol> :
4676
}
4777
}
4878
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+
}
6480
}
6581

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> // >:(
7284

7385
/// Returns an iterator over the elements of this sequence.
7486
///
7587
/// - Complexity: O(1).
7688
@_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)
8991
}
92+
}
9093

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
9596
}
9697

9798
extension LazySequenceProtocol {
@@ -110,125 +111,127 @@ extension LazySequenceProtocol {
110111
}
111112
}
112113

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.
117124
@_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+
122128
@_inlineable // FIXME(sil-serialize-all)
123129
@_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+
}
126168
}
169+
}
127170

171+
extension LazyDropWhileCollection.Index: Equatable, Comparable {
128172
@_inlineable // FIXME(sil-serialize-all)
129173
public static func == (
130-
lhs: LazyDropWhileIndex, rhs: LazyDropWhileIndex
174+
lhs: LazyDropWhileCollection<Base>.Index,
175+
rhs: LazyDropWhileCollection<Base>.Index
131176
) -> Bool {
132177
return lhs.base == rhs.base
133178
}
134179

135180
@_inlineable // FIXME(sil-serialize-all)
136181
public static func < (
137-
lhs: LazyDropWhileIndex, rhs: LazyDropWhileIndex
182+
lhs: LazyDropWhileCollection<Base>.Index,
183+
rhs: LazyDropWhileCollection<Base>.Index
138184
) -> Bool {
139185
return lhs.base < rhs.base
140186
}
141187
}
142188

143-
extension LazyDropWhileIndex : Hashable where Base.Index : Hashable {
189+
extension LazyDropWhileCollection.Index: Hashable where Base.Index: Hashable {
144190
public var hashValue: Int {
145191
return base.hashValue
146192
}
147193
}
148194

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 {
171196
@_inlineable // FIXME(sil-serialize-all)
172197
public var startIndex: Index {
173198
var index = _base.startIndex
174199
while index != _base.endIndex && _predicate(_base[index]) {
175200
_base.formIndex(after: &index)
176201
}
177-
return LazyDropWhileIndex(base: index)
202+
return Index(_base: index)
178203
}
179204

180205
@_inlineable // FIXME(sil-serialize-all)
181206
public var endIndex: Index {
182-
return LazyDropWhileIndex(base: _base.endIndex)
207+
return Index(_base: _base.endIndex)
183208
}
184209

185210
@_inlineable // FIXME(sil-serialize-all)
186211
public func index(after i: Index) -> Index {
187212
_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))
189214
}
190215

191-
% if Traversal == 'Bidirectional':
192216

193217
@_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 {
203219
return _base[position.base]
204220
}
221+
}
205222

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 { }
211224

225+
extension LazyDropWhileCollection: BidirectionalCollection
226+
where Base: BidirectionalCollection {
212227
@_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))
217231
}
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
223232
}
224233

225-
extension LazyCollectionProtocol
226-
% if Collection != 'Collection':
227-
where
228-
Self : ${Collection},
229-
Elements : ${Collection}
230-
% end
231-
{
234+
extension LazyCollectionProtocol {
232235
/// Returns a lazy collection that skips any initial elements that satisfy
233236
/// `predicate`.
234237
///
@@ -239,14 +242,16 @@ Elements : ${Collection}
239242
@_inlineable // FIXME(sil-serialize-all)
240243
public func drop(
241244
while predicate: @escaping (Elements.Element) -> Bool
242-
) -> LazyDropWhile${Collection}<Self.Elements> {
243-
return LazyDropWhile${Collection}(
245+
) -> LazyDropWhileCollection<Self.Elements> {
246+
return LazyDropWhileCollection(
244247
_base: self.elements, predicate: predicate)
245248
}
246249
}
247250

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
249257

250-
// ${'Local Variables'}:
251-
// eval: (read-only-mode 1)
252-
// End:

stdlib/public/core/PrefixWhile.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ extension LazyPrefixWhileSequence.Iterator: IteratorProtocol, Sequence {
7474
}
7575

7676
extension LazyPrefixWhileSequence: Sequence {
77+
public typealias SubSequence = AnySequence<Element> // >:(
78+
7779
@_inlineable // FIXME(sil-serialize-all)
7880
public func makeIterator() -> Iterator {
7981
return Iterator(_base: _base.makeIterator(), predicate: _predicate)
@@ -112,6 +114,7 @@ extension LazySequenceProtocol {
112114
@_fixed_layout // FIXME(sil-serialize-all)
113115
public struct LazyPrefixWhileCollection<Base: Collection> {
114116
public typealias Element = Base.Element
117+
public typealias SubSequence = Slice<LazyPrefixWhileCollection<Base>>
115118

116119
@_inlineable // FIXME(sil-serialize-all)
117120
@_versioned // FIXME(sil-serialize-all)

0 commit comments

Comments
 (0)