Skip to content

Commit 833721a

Browse files
author
Max Moiseev
committed
[stdlib] Conditional conformances for LazyFilterCollection
1 parent 4ddac3f commit 833721a

File tree

6 files changed

+41
-68
lines changed

6 files changed

+41
-68
lines changed

benchmark/single-source/LazyFilter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public func run_LazilyFilteredArrays(_ N: Int) {
4747
CheckResults(res == 123)
4848
}
4949

50-
fileprivate var multiplesOfThree: LazyFilterBidirectionalCollection<Array<Int>>?
50+
fileprivate var multiplesOfThree: LazyFilterCollection<Array<Int>>?
5151

5252
fileprivate func setup_LazilyFilteredArrayContains() {
5353
multiplesOfThree = Array(1..<5_000).lazy.filter { $0 % 3 == 0 }

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ set(SWIFTLIB_ESSENTIAL
5454
Equatable.swift
5555
ErrorType.swift
5656
Existential.swift
57-
Filter.swift.gyb
57+
Filter.swift
5858
FixedArray.swift.gyb
5959
FlatMap.swift
6060
Flatten.swift.gyb

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

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Filter.swift.gyb -------------------------------------*- swift -*-===//
1+
//===--- Filter.swift -----------------------------------------*- swift -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,12 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
%{
14-
from gyb_stdlib_support import (
15-
collectionForTraversal
16-
)
17-
}%
18-
1913
/// An iterator over the elements traversed by some base iterator that also
2014
/// satisfy a given predicate.
2115
///
@@ -122,14 +116,6 @@ public struct LazyFilterSequence<Base : Sequence>
122116
@available(swift, deprecated: 3.1, obsoleted: 4.0, message: "Use Base.Index")
123117
public typealias LazyFilterIndex<Base : Collection> = Base.Index
124118

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-
133119
/// A lazy `Collection` wrapper that includes the elements of an
134120
/// underlying collection that satisfy a predicate.
135121
///
@@ -140,16 +126,16 @@ public typealias LazyFilterIndex<Base : Collection> = Base.Index
140126
/// general operations on `LazyFilterCollection` instances may not have the
141127
/// documented complexity.
142128
@_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 {
147132

148133
/// A type that represents a valid position in the collection.
149134
///
150135
/// Valid indices consist of the position of every element and a
151136
/// "past the end" position that's not valid for use as a subscript.
152137
public typealias Index = Base.Index
138+
public typealias Element = Base.Element
153139

154140

155141
/// Creates an instance containing the elements of `base` that
@@ -221,26 +207,6 @@ public struct ${Self}<
221207
i = index
222208
}
223209

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-
244210
/// Accesses the element at `position`.
245211
///
246212
/// - Precondition: `position` is a valid position in `self` and
@@ -250,7 +216,7 @@ public struct ${Self}<
250216
return _base[position]
251217
}
252218

253-
public typealias SubSequence = ${Self}<Base.SubSequence>
219+
public typealias SubSequence = LazyFilterCollection<Base.SubSequence>
254220

255221
@_inlineable // FIXME(sil-serialize-all)
256222
public subscript(bounds: Range<Index>) -> SubSequence {
@@ -290,7 +256,27 @@ public struct ${Self}<
290256
internal let _predicate: (Base.Element) -> Bool
291257
}
292258

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

295281
extension LazySequenceProtocol {
296282
/// Returns the elements of `self` that satisfy `isIncluded`.
@@ -303,20 +289,11 @@ extension LazySequenceProtocol {
303289
public func filter(
304290
_ isIncluded: @escaping (Elements.Element) -> Bool
305291
) -> LazyFilterSequence<Self.Elements> {
306-
return LazyFilterSequence(
307-
_base: self.elements, isIncluded)
292+
return LazyFilterSequence(_base: self.elements, isIncluded)
308293
}
309294
}
310295

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 {
320297
/// Returns the elements of `self` that satisfy `predicate`.
321298
///
322299
/// - Note: The elements of the result are computed on-demand, as
@@ -326,14 +303,10 @@ extension LazyCollectionProtocol
326303
@_inlineable // FIXME(sil-serialize-all)
327304
public func filter(
328305
_ 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)
332308
}
333309
}
334310

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

stdlib/public/core/FlatMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extension LazyCollectionProtocol
129129
public func flatMap<ElementOfResult>(
130130
_ transform: @escaping (Elements.Element) -> ElementOfResult?
131131
) -> LazyMapBidirectionalCollection<
132-
LazyFilterBidirectionalCollection<
132+
LazyFilterCollection<
133133
LazyMapBidirectionalCollection<Elements, ElementOfResult?>>,
134134
ElementOfResult
135135
> {

stdlib/public/core/Reverse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ extension LazyCollectionProtocol
312312
///
313313
/// - Complexity: O(1)
314314
@_inlineable
315-
public func reversed() -> LazyBidirectionalCollection<
315+
public func reversed() -> LazyCollection<
316316
ReversedCollection<Elements>
317317
> {
318318
return ReversedCollection(_base: elements).lazy

validation-test/stdlib/Lazy.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,11 +1167,11 @@ tests.test("LazyFilterCollection/AssociatedTypes") {
11671167

11681168
tests.test("LazyFilterBidirectionalCollection/AssociatedTypes") {
11691169
typealias Base = MinimalBidirectionalCollection<OpaqueValue<Int>>
1170-
typealias Subject = LazyFilterBidirectionalCollection<Base>
1170+
typealias Subject = LazyFilterCollection<Base>
11711171
expectBidirectionalCollectionAssociatedTypes(
11721172
collectionType: Subject.self,
11731173
iteratorType: LazyFilterIterator<Base.Iterator>.self,
1174-
subSequenceType: LazyFilterBidirectionalCollection<Base.SubSequence>.self,
1174+
subSequenceType: LazyFilterCollection<Base.SubSequence>.self,
11751175
indexType: LazyFilterIndex<Base>.self,
11761176
indicesType: DefaultBidirectionalIndices<Subject>.self)
11771177
}
@@ -1196,7 +1196,7 @@ tests.test("lazy.filter/TypeInference") {
11961196
var filtered = MinimalBidirectionalCollection(elements: baseArray)
11971197
.lazy.filter { _ in true }
11981198
expectType(
1199-
LazyFilterBidirectionalCollection<
1199+
LazyFilterCollection<
12001200
MinimalBidirectionalCollection<OpaqueValue<Int>>
12011201
>.self,
12021202
&filtered)
@@ -1205,7 +1205,7 @@ tests.test("lazy.filter/TypeInference") {
12051205
var filtered = MinimalRandomAccessCollection(elements: baseArray)
12061206
.lazy.filter { _ in true }
12071207
expectType(
1208-
LazyFilterBidirectionalCollection<
1208+
LazyFilterCollection<
12091209
MinimalRandomAccessCollection<OpaqueValue<Int>>
12101210
>.self,
12111211
&filtered)

0 commit comments

Comments
 (0)