9
9
//
10
10
//===----------------------------------------------------------------------===//
11
11
12
+ /// A sequence that produces the longest common prefix of two sequences.
12
13
public struct CommonPrefix < Base: Sequence , Other: Sequence > {
13
14
@usableFromInline
14
15
internal let base : Base
@@ -32,6 +33,7 @@ public struct CommonPrefix<Base: Sequence, Other: Sequence> {
32
33
}
33
34
34
35
extension CommonPrefix : Sequence {
36
+ /// The iterator for a `CommonPrefix` sequence.
35
37
public struct Iterator : IteratorProtocol {
36
38
@usableFromInline
37
39
internal var base : Base . Iterator
@@ -74,6 +76,7 @@ extension CommonPrefix: Sequence {
74
76
}
75
77
76
78
extension CommonPrefix : Collection where Base: Collection , Other: Collection {
79
+ /// The index for a `CommonPrefix` collection.
77
80
public struct Index {
78
81
@usableFromInline
79
82
internal let base : Base . Index
@@ -150,6 +153,21 @@ extension CommonPrefix: LazyCollectionProtocol
150
153
//===----------------------------------------------------------------------===//
151
154
152
155
extension Sequence {
156
+ /// Returns an array of the longest common prefix of this sequence and the
157
+ /// other sequence, according to the given equivalence function.
158
+ ///
159
+ /// let characters = AnySequence("abcde")
160
+ /// characters.commonPrefix(with: "abce", by: ==) // ["a", "b", "c"]
161
+ /// characters.commonPrefix(with: "bcde", by: ==) // []
162
+ ///
163
+ /// - Parameters:
164
+ /// - other: The other sequence.
165
+ /// - areEquivalent: The equivalence function.
166
+ /// - Returns: An array containing the elements in the longest common prefix
167
+ /// of `self` and `other`, according to `areEquivalent`.
168
+ ///
169
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
170
+ /// prefix.
153
171
@inlinable
154
172
public func commonPrefix< Other: Sequence > (
155
173
with other: Other ,
@@ -171,6 +189,18 @@ extension Sequence {
171
189
}
172
190
173
191
extension Sequence where Element: Equatable {
192
+ /// Returns an array of the longest common prefix of this sequence and the
193
+ /// other sequence.
194
+ ///
195
+ /// let characters = AnySequence("abcde")
196
+ /// characters.commonPrefix(with: "abce") // ["a", "b", "c"]
197
+ /// characters.commonPrefix(with: "bcde") // []
198
+ ///
199
+ /// - Parameter other: The other sequence.
200
+ /// - Returns: An array containing the elements in the longest common prefix
201
+ /// of `self` and `other`.
202
+ ///
203
+ /// - Complexity: O(1)
174
204
@inlinable
175
205
public func commonPrefix< Other: Sequence > (
176
206
with other: Other
@@ -184,6 +214,8 @@ extension Sequence where Element: Equatable {
184
214
//===----------------------------------------------------------------------===//
185
215
186
216
extension LazySequenceProtocol {
217
+ /// Returns a lazy sequence of the longest common prefix of this sequence and
218
+ /// another sequence, according to the given equivalence function.
187
219
@inlinable
188
220
public func commonPrefix< Other: Sequence > (
189
221
with other: Other ,
@@ -198,6 +230,20 @@ extension LazySequenceProtocol {
198
230
//===----------------------------------------------------------------------===//
199
231
200
232
extension Collection {
233
+ /// Returns the longest prefix of this collection that it has in common with
234
+ /// another sequence, according to the given equivalence function.
235
+ ///
236
+ /// let string = "abcde"
237
+ /// string.commonPrefix(with: "abce", by: ==) // "abc"
238
+ /// string.commonPrefix(with: "bcde", by: ==) // ""
239
+ ///
240
+ /// - Parameters:
241
+ /// - other: The other sequence.
242
+ /// - areEquivalent: The equivalence function.
243
+ /// - Returns: The longest prefix of `self` that it has in common with
244
+ /// `other`, according to `areEquivalent`.
245
+ ///
246
+ /// - Complexity: O(*n*), where *n* is the length of the common prefix.
201
247
@inlinable
202
248
public func commonPrefix< Other: Sequence > (
203
249
with other: Other ,
@@ -220,6 +266,19 @@ extension Collection {
220
266
}
221
267
222
268
extension Collection where Element: Equatable {
269
+ /// Returns the longest prefix of this collection that it has in common with
270
+ /// another sequence.
271
+ ///
272
+ /// let string = "abcde"
273
+ /// string.commonPrefix(with: "abce") // "abc"
274
+ /// string.commonPrefix(with: "bcde") // ""
275
+ ///
276
+ /// - Parameter other: The other sequence.
277
+ /// - Returns: The longest prefix of `self` that it has in common with
278
+ /// `other`.
279
+ ///
280
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
281
+ /// prefix.
223
282
@inlinable
224
283
public func commonPrefix< Other: Sequence > (
225
284
with other: Other
@@ -228,34 +287,26 @@ extension Collection where Element: Equatable {
228
287
}
229
288
}
230
289
231
- //===----------------------------------------------------------------------===//
232
- // LazyCollectionProtocol.commonPrefix(with:)
233
- //===----------------------------------------------------------------------===//
234
-
235
- extension LazyCollectionProtocol {
236
- @inlinable
237
- public func commonPrefix< Other: Sequence > (
238
- with other: Other ,
239
- by areEquivalent: @escaping ( Element , Other . Element ) -> Bool
240
- ) -> CommonPrefix < Self , Other > {
241
- CommonPrefix ( base: self , other: other, areEquivalent: areEquivalent)
242
- }
243
- }
244
-
245
- extension LazyCollectionProtocol where Element: Equatable {
246
- @inlinable
247
- public func commonPrefix< Other: Sequence > (
248
- with other: Other
249
- ) -> CommonPrefix < Self , Other > where Other. Element == Element {
250
- commonPrefix ( with: other, by: == )
251
- }
252
- }
253
-
254
290
//===----------------------------------------------------------------------===//
255
291
// BidirectionalCollection.commonSuffix(with:)
256
292
//===----------------------------------------------------------------------===//
257
293
258
294
extension BidirectionalCollection {
295
+ /// Returns the longest suffix of this collection that it has in common with
296
+ /// another collection, according to the given equivalence function.
297
+ ///
298
+ /// let string = "abcde"
299
+ /// string.commonSuffix(with: "acde", by: ==) // "acde"
300
+ /// string.commonSuffix(with: "abcd", by: ==) // ""
301
+ ///
302
+ /// - Parameters:
303
+ /// - other: The other collection.
304
+ /// - areEquivalent: The equivalence function.
305
+ /// - Returns: The longest suffix of `self` that it has in common with
306
+ /// `other`, according to `areEquivalent`.
307
+ ///
308
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
309
+ /// suffix.
259
310
@inlinable
260
311
public func commonSuffix< Other: BidirectionalCollection > (
261
312
with other: Other ,
@@ -267,6 +318,19 @@ extension BidirectionalCollection {
267
318
}
268
319
269
320
extension BidirectionalCollection where Element: Equatable {
321
+ /// Returns the longest suffix of this collection that it has in common with
322
+ /// another collection.
323
+ ///
324
+ /// let string = "abcde"
325
+ /// string.commonSuffix(with: "acde") // "cde"
326
+ /// string.commonSuffix(with: "abcd") // ""
327
+ ///
328
+ /// - Parameter other: The other collection.
329
+ /// - Returns: The longest suffix of `self` that it has in common with
330
+ /// `other`.
331
+ ///
332
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
333
+ /// suffix.
270
334
@inlinable
271
335
public func commonSuffix< Other: BidirectionalCollection > (
272
336
with other: Other
@@ -280,6 +344,24 @@ extension BidirectionalCollection where Element: Equatable {
280
344
//===----------------------------------------------------------------------===//
281
345
282
346
extension Collection {
347
+ /// Finds the longest common prefix of this collection and another collection,
348
+ /// according to the given equivalence function, and returns the index from
349
+ /// each collection that marks the end of this prefix.
350
+ ///
351
+ /// let string1 = "abcde"
352
+ /// let string2 = "abce"
353
+ /// let (i1, i2) = string1.endOfCommonPrefix(with: string2, by: ==)
354
+ /// print(string1[..<i1], string1[i1...]) // "abc", "de"
355
+ /// print(string2[..<i2], string2[i2...]) // "abc", "e"
356
+ ///
357
+ /// - Parameters:
358
+ /// - other: The other collection.
359
+ /// - areEquivalent: The equivalence function.
360
+ /// - Returns: A pair of indices from `self` and `other` that mark the end of
361
+ /// their longest common prefix according to `areEquivalent`.
362
+ ///
363
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
364
+ /// prefix.
283
365
@inlinable
284
366
public func endOfCommonPrefix< Other: Collection > (
285
367
with other: Other ,
@@ -300,6 +382,22 @@ extension Collection {
300
382
}
301
383
302
384
extension Collection where Element: Equatable {
385
+ /// Finds the longest common prefix of this collection and another collection,
386
+ /// and returns the index from each collection that marks the end of this
387
+ /// prefix.
388
+ ///
389
+ /// let string1 = "abcde"
390
+ /// let string2 = "abce"
391
+ /// let (i1, i2) = string1.endOfCommonPrefix(with: string2)
392
+ /// print(string1[..<i1], string1[i1...]) // "abc", "de"
393
+ /// print(string2[..<i2], string2[i2...]) // "abc", "e"
394
+ ///
395
+ /// - Parameter other: The other collection.
396
+ /// - Returns: A pair of indices from `self` and `other` that mark the end of
397
+ /// their longest common prefix.
398
+ ///
399
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
400
+ /// prefix.
303
401
@inlinable
304
402
public func endOfCommonPrefix< Other: Collection > (
305
403
with other: Other
@@ -309,10 +407,28 @@ extension Collection where Element: Equatable {
309
407
}
310
408
311
409
//===----------------------------------------------------------------------===//
312
- // BidirectionalCollection.startOfCommonPrefix (with:)
410
+ // BidirectionalCollection.startOfCommonSuffix (with:)
313
411
//===----------------------------------------------------------------------===//
314
412
315
413
extension BidirectionalCollection {
414
+ /// Finds the longest common suffix of this collection and another collection,
415
+ /// according to the given equivalence function, and returns the index from
416
+ /// each collection that marks the start of this suffix.
417
+ ///
418
+ /// let string1 = "abcde"
419
+ /// let string2 = "acde"
420
+ /// let (i1, i2) = string1.startOfCommonSuffix(with: string2, by: ==)
421
+ /// print(string1[..<i1], string1[i1...]) // "ab", "cde"
422
+ /// print(string2[..<i2], string2[i2...]) // "a", "cde"
423
+ ///
424
+ /// - Parameters:
425
+ /// - other: The other collection.
426
+ /// - areEquivalent: The equivalence function.
427
+ /// - Returns: A pair of indices from `self` and `other` that mark the start
428
+ /// of their longest common suffix according to `areEquivalent`.
429
+ ///
430
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
431
+ /// suffix.
316
432
@inlinable
317
433
public func startOfCommonSuffix< Other: BidirectionalCollection > (
318
434
with other: Other ,
@@ -341,6 +457,22 @@ extension BidirectionalCollection {
341
457
}
342
458
343
459
extension BidirectionalCollection where Element: Equatable {
460
+ /// Finds the longest common suffix of this collection and another collection,
461
+ /// and returns the index from each collection that marks the start of this
462
+ /// suffix.
463
+ ///
464
+ /// let string1 = "abcde"
465
+ /// let string2 = "acde"
466
+ /// let (i1, i2) = string1.startOfCommonSuffix(with: string2)
467
+ /// print(string1[..<i1], string1[i1...]) // "ab", "cde"
468
+ /// print(string2[..<i2], string2[i2...]) // "a", "cde"
469
+ ///
470
+ /// - Parameter other: The other collection.
471
+ /// - Returns: A pair of indices from `self` and `other` that mark the start
472
+ /// of their longest common suffix.
473
+ ///
474
+ /// - Complexity: O(*n*), where *n* is the length of the longest common
475
+ /// suffix.
344
476
@inlinable
345
477
public func startOfCommonSuffix< Other: BidirectionalCollection > (
346
478
with other: Other
0 commit comments