-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathMap.swift
229 lines (198 loc) · 7.06 KB
/
Map.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//===--- Map.swift - Lazily map over a Sequence ---------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A `Sequence` whose elements consist of those in a `Base`
/// `Sequence` passed through a transform function returning `Element`.
/// These elements are computed lazily, each time they're read, by
/// calling the transform function on a base element.
@frozen
public struct LazyMapSequence<Base : Sequence, Element> {
public typealias Elements = LazyMapSequence
@usableFromInline
internal var _base: Base
@usableFromInline
internal let _transform: (Base.Element) -> Element
/// Creates an instance with elements `transform(x)` for each element
/// `x` of base.
@inlinable
internal init(_base: Base, transform: @escaping (Base.Element) -> Element) {
self._base = _base
self._transform = transform
}
}
extension LazyMapSequence {
@frozen
public struct Iterator {
@usableFromInline
internal var _base: Base.Iterator
@usableFromInline
internal let _transform: (Base.Element) -> Element
@inlinable
public var base: Base.Iterator { return _base }
@inlinable
internal init(
_base: Base.Iterator,
_transform: @escaping (Base.Element) -> Element
) {
self._base = _base
self._transform = _transform
}
}
}
extension LazyMapSequence.Iterator: IteratorProtocol, Sequence {
/// Advances to the next element and returns it, or `nil` if no next element
/// exists.
///
/// Once `nil` has been returned, all subsequent calls return `nil`.
///
/// - Precondition: `next()` has not been applied to a copy of `self`
/// since the copy was made.
@inlinable
public mutating func next() -> Element? {
return _base.next().map(_transform)
}
}
extension LazyMapSequence: LazySequenceProtocol {
/// Returns an iterator over the elements of this sequence.
///
/// - Complexity: O(1).
@inlinable
public __consuming func makeIterator() -> Iterator {
return Iterator(_base: _base.makeIterator(), _transform: _transform)
}
/// A value less than or equal to the number of elements in the sequence,
/// calculated nondestructively.
///
/// The default implementation returns 0. If you provide your own
/// implementation, make sure to compute the value nondestructively.
///
/// - Complexity: O(1), except if the sequence also conforms to `Collection`.
/// In this case, see the documentation of `Collection.underestimatedCount`.
@inlinable
public var underestimatedCount: Int {
return _base.underestimatedCount
}
}
/// A `Collection` whose elements consist of those in a `Base`
/// `Collection` passed through a transform function returning `Element`.
/// These elements are computed lazily, each time they're read, by
/// calling the transform function on a base element.
public typealias LazyMapCollection<T: Collection,U> = LazyMapSequence<T,U>
extension LazyMapCollection: Collection {
public typealias Index = Base.Index
public typealias Indices = Base.Indices
public typealias SubSequence = LazyMapCollection<Base.SubSequence, Element>
@inlinable
public var startIndex: Base.Index { return _base.startIndex }
@inlinable
public var endIndex: Base.Index { return _base.endIndex }
@inlinable
public func index(after i: Index) -> Index { return _base.index(after: i) }
@inlinable
public func formIndex(after i: inout Index) { _base.formIndex(after: &i) }
/// Accesses the element at `position`.
///
/// - Precondition: `position` is a valid position in `self` and
/// `position != endIndex`.
@inlinable
public subscript(position: Base.Index) -> Element {
return _transform(_base[position])
}
@inlinable
public subscript(bounds: Range<Base.Index>) -> SubSequence {
return SubSequence(_base: _base[bounds], transform: _transform)
}
@inlinable
public var indices: Indices {
return _base.indices
}
/// A Boolean value indicating whether the collection is empty.
@inlinable
public var isEmpty: Bool { return _base.isEmpty }
/// The number of elements in the collection.
///
/// To check whether the collection is empty, use its `isEmpty` property
/// instead of comparing `count` to zero. Unless the collection guarantees
/// random-access performance, calculating `count` can be an O(*n*)
/// operation.
///
/// - Complexity: O(1) if `Index` conforms to `RandomAccessIndex`; O(*n*)
/// otherwise.
@inlinable
public var count: Int {
return _base.count
}
@inlinable
public func index(_ i: Index, offsetBy n: Int) -> Index {
return _base.index(i, offsetBy: n)
}
@inlinable
public func index(
_ i: Index, offsetBy n: Int, limitedBy limit: Index
) -> Index? {
return _base.index(i, offsetBy: n, limitedBy: limit)
}
@inlinable
public func distance(from start: Index, to end: Index) -> Int {
return _base.distance(from: start, to: end)
}
}
extension LazyMapCollection : BidirectionalCollection
where Base : BidirectionalCollection {
/// A value less than or equal to the number of elements in the collection.
///
/// - Complexity: O(1) if the collection conforms to
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
/// of the collection.
@inlinable
public func index(before i: Index) -> Index { return _base.index(before: i) }
@inlinable
public func formIndex(before i: inout Index) {
_base.formIndex(before: &i)
}
}
extension LazyMapCollection: LazyCollectionProtocol { }
extension LazyMapCollection : RandomAccessCollection
where Base : RandomAccessCollection { }
//===--- Support for s.lazy -----------------------------------------------===//
extension LazySequenceProtocol {
/// Returns a `LazyMapSequence` over this `Sequence`. The elements of
/// the result are computed lazily, each time they are read, by
/// calling `transform` function on a base element.
@inlinable
public func map<U>(
_ transform: @escaping (Element) -> U
) -> LazyMapSequence<Elements, U> {
return LazyMapSequence(_base: elements, transform: transform)
}
}
extension LazyMapSequence {
@inlinable
@available(swift, introduced: 5)
public func map<ElementOfResult>(
_ transform: @escaping (Element) -> ElementOfResult
) -> LazyMapSequence<Base, ElementOfResult> {
return LazyMapSequence<Base, ElementOfResult>(
_base: _base,
transform: { transform(self._transform($0)) })
}
}
extension LazyMapCollection {
@inlinable
@available(swift, introduced: 5)
public func map<ElementOfResult>(
_ transform: @escaping (Element) -> ElementOfResult
) -> LazyMapCollection<Base, ElementOfResult> {
return LazyMapCollection<Base, ElementOfResult>(
_base: _base,
transform: {transform(self._transform($0))})
}
}