-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathAdjacentPairs.swift
309 lines (268 loc) · 8.63 KB
/
AdjacentPairs.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Algorithms open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//
extension Sequence {
/// Returns a sequence of overlapping adjacent pairs of the elements of this
/// sequence.
///
/// In the `AdjacentPairsSequence` returned by this method, the elements of
/// the *i*th pair are the *i*th and *(i+1)*th elements of the underlying
/// sequence. The following example uses the `adjacentPairs()` method to
/// iterate over adjacent pairs of integers:
///
/// for pair in (1...).prefix(5).adjacentPairs() {
/// print(pair)
/// }
/// // Prints "(1, 2)"
/// // Prints "(2, 3)"
/// // Prints "(3, 4)"
/// // Prints "(4, 5)"
///
/// The resulting sequence is empty when called on an empty or single-element
/// sequence.
///
/// - Complexity: O(1)
@inlinable
public func adjacentPairs() -> AdjacentPairsSequence<Self> {
AdjacentPairsSequence(base: self)
}
}
extension Collection {
/// Returns a collection of overlapping adjacent pairs of the elements of this
/// collection.
///
/// In an `AdjacentPairsCollection`, the elements of the *i*th pair are the
/// *i*th and *(i+1)*th elements of the underlying sequence. The following
/// example uses the `adjacentPairs()` method to iterate over adjacent pairs
/// of integers:
///
/// for pair in (1...5).adjacentPairs() {
/// print(pair)
/// }
/// // Prints "(1, 2)"
/// // Prints "(2, 3)"
/// // Prints "(3, 4)"
/// // Prints "(4, 5)"
///
/// The resulting collection is empty when called on an empty or
/// single-element collection.
///
/// - Complexity: O(1)
@inlinable
public func adjacentPairs() -> AdjacentPairsCollection<Self> {
AdjacentPairsCollection(base: self)
}
}
/// A sequence of adjacent pairs of elements built from an underlying sequence.
///
/// Use the `adjacentPairs()` method on a sequence to create an
/// `AdjacentPairsSequence` instance.
public struct AdjacentPairsSequence<Base: Sequence> {
@usableFromInline
internal let base: Base
/// Creates an instance that makes pairs of adjacent elements from `base`.
@inlinable
internal init(base: Base) {
self.base = base
}
}
extension AdjacentPairsSequence {
/// The iterator for an `AdjacentPairsSequence` or `AdjacentPairsCollection`
/// instance.
public struct Iterator {
@usableFromInline
internal var base: Base.Iterator
@usableFromInline
internal var previousElement: Base.Element?
@inlinable
internal init(base: Base.Iterator) {
self.base = base
}
}
}
extension AdjacentPairsSequence.Iterator: IteratorProtocol {
public typealias Element = (Base.Element, Base.Element)
@inlinable
public mutating func next() -> Element? {
if previousElement == nil {
previousElement = base.next()
}
guard let previous = previousElement, let next = base.next() else {
return nil
}
previousElement = next
return (previous, next)
}
}
extension AdjacentPairsSequence: Sequence {
@inlinable
public func makeIterator() -> Iterator {
Iterator(base: base.makeIterator())
}
@inlinable
public var underestimatedCount: Int {
Swift.max(0, base.underestimatedCount - 1)
}
}
extension AdjacentPairsSequence: LazySequenceProtocol
where Base: LazySequenceProtocol {}
/// A collection of adjacent pairs of elements built from an underlying
/// collection.
///
/// Use the `adjacentPairs()` method on a collection to create an
/// `AdjacentPairsCollection` instance.
public struct AdjacentPairsCollection<Base: Collection> {
@usableFromInline
internal let base: Base
@usableFromInline
internal let secondBaseIndex: Base.Index
@inlinable
internal init(base: Base) {
self.base = base
self.secondBaseIndex = base.isEmpty
? base.endIndex
: base.index(after: base.startIndex)
}
}
extension AdjacentPairsCollection {
/// A position in an `AdjacentPairsCollection` instance.
public struct Index: Comparable {
@usableFromInline
internal var first: Base.Index
@usableFromInline
internal var second: Base.Index
@inlinable
internal init(first: Base.Index, second: Base.Index) {
self.first = first
self.second = second
}
@inlinable
public static func == (lhs: Index, rhs: Index) -> Bool {
lhs.first == rhs.first
}
@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
lhs.first < rhs.first
}
}
}
extension AdjacentPairsCollection: Collection {
@inlinable
public var startIndex: Index {
Index(
first: secondBaseIndex == base.endIndex ? base.endIndex : base.startIndex,
second: secondBaseIndex)
}
@inlinable
public var endIndex: Index {
Index(first: base.endIndex, second: base.endIndex)
}
@inlinable
public subscript(position: Index) -> (Base.Element, Base.Element) {
(base[position.first], base[position.second])
}
@inlinable
public func index(after i: Index) -> Index {
precondition(i != endIndex, "Can't advance beyond endIndex")
let next = base.index(after: i.second)
return next == base.endIndex
? endIndex
: Index(first: i.second, second: next)
}
@inlinable
public func index(_ i: Index, offsetBy distance: Int) -> Index {
guard distance != 0 else { return i }
guard let result = distance > 0
? offsetForward(i, by: distance, limitedBy: endIndex)
: offsetBackward(i, by: -distance, limitedBy: startIndex)
else { fatalError("Index out of bounds") }
return result
}
@inlinable
public func index(
_ i: Index, offsetBy distance: Int, limitedBy limit: Index
) -> Index? {
guard distance != 0 else { return i }
guard limit != i else { return nil }
if distance > 0 {
let limit = limit > i ? limit : endIndex
return offsetForward(i, by: distance, limitedBy: limit)
} else {
let limit = limit < i ? limit : startIndex
return offsetBackward(i, by: -distance, limitedBy: limit)
}
}
@inlinable
internal func offsetForward(
_ i: Index, by distance: Int, limitedBy limit: Index
) -> Index? {
assert(distance > 0)
assert(limit > i)
guard let newFirst = base.index(i.second, offsetBy: distance - 1, limitedBy: limit.first),
newFirst != base.endIndex
else { return nil }
let newSecond = base.index(after: newFirst)
precondition(newSecond <= base.endIndex, "Can't advance beyond endIndex")
return newSecond == base.endIndex
? endIndex
: Index(first: newFirst, second: newSecond)
}
@inlinable
internal func offsetBackward(
_ i: Index, by distance: Int, limitedBy limit: Index
) -> Index? {
assert(distance > 0)
assert(limit < i)
let offset = i == endIndex ? 0 : 1
guard let newSecond = base.index(
i.first,
offsetBy: -(distance - offset),
limitedBy: limit.second)
else { return nil }
let newFirst = base.index(newSecond, offsetBy: -1)
precondition(newFirst >= base.startIndex, "Can't move before startIndex")
return Index(first: newFirst, second: newSecond)
}
@inlinable
public func distance(from start: Index, to end: Index) -> Int {
// While there's a 2-step gap between the `first` base index values in
// `endIndex` and the penultimate index of this collection, the `second`
// base index values are consistently one step apart throughout the
// entire collection.
base.distance(from: start.second, to: end.second)
}
@inlinable
public var count: Int {
Swift.max(0, base.count - 1)
}
}
extension AdjacentPairsCollection: BidirectionalCollection
where Base: BidirectionalCollection
{
@inlinable
public func index(before i: Index) -> Index {
precondition(i != startIndex, "Can't offset before startIndex")
let second = i == endIndex
? base.index(before: base.endIndex)
: i.first
let first = base.index(before: second)
return Index(first: first, second: second)
}
}
extension AdjacentPairsCollection: RandomAccessCollection
where Base: RandomAccessCollection {}
extension AdjacentPairsCollection: LazySequenceProtocol, LazyCollectionProtocol
where Base: LazySequenceProtocol {}
extension AdjacentPairsCollection.Index: Hashable where Base.Index: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(first)
}
}