99//
1010//===----------------------------------------------------------------------===//
1111
12+ /// A sequence that produces the longest common prefix of two sequences.
1213public struct CommonPrefix < Base: Sequence , Other: Sequence > {
1314 @usableFromInline
1415 internal let base : Base
@@ -32,6 +33,7 @@ public struct CommonPrefix<Base: Sequence, Other: Sequence> {
3233}
3334
3435extension CommonPrefix : Sequence {
36+ /// The iterator for a `CommonPrefix` sequence.
3537 public struct Iterator : IteratorProtocol {
3638 @usableFromInline
3739 internal var base : Base . Iterator
@@ -74,6 +76,7 @@ extension CommonPrefix: Sequence {
7476}
7577
7678extension CommonPrefix : Collection where Base: Collection , Other: Collection {
79+ /// The index for a `CommonPrefix` collection.
7780 public struct Index {
7881 @usableFromInline
7982 internal let base : Base . Index
@@ -150,6 +153,21 @@ extension CommonPrefix: LazyCollectionProtocol
150153//===----------------------------------------------------------------------===//
151154
152155extension 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.
153171 @inlinable
154172 public func commonPrefix< Other: Sequence > (
155173 with other: Other ,
@@ -171,6 +189,18 @@ extension Sequence {
171189}
172190
173191extension 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)
174204 @inlinable
175205 public func commonPrefix< Other: Sequence > (
176206 with other: Other
@@ -184,6 +214,8 @@ extension Sequence where Element: Equatable {
184214//===----------------------------------------------------------------------===//
185215
186216extension LazySequenceProtocol {
217+ /// Returns a lazy sequence of the longest common prefix of this sequence and
218+ /// another sequence, according to the given equivalence function.
187219 @inlinable
188220 public func commonPrefix< Other: Sequence > (
189221 with other: Other ,
@@ -198,6 +230,20 @@ extension LazySequenceProtocol {
198230//===----------------------------------------------------------------------===//
199231
200232extension 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.
201247 @inlinable
202248 public func commonPrefix< Other: Sequence > (
203249 with other: Other ,
@@ -220,6 +266,19 @@ extension Collection {
220266}
221267
222268extension 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.
223282 @inlinable
224283 public func commonPrefix< Other: Sequence > (
225284 with other: Other
@@ -228,34 +287,26 @@ extension Collection where Element: Equatable {
228287 }
229288}
230289
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-
254290//===----------------------------------------------------------------------===//
255291// BidirectionalCollection.commonSuffix(with:)
256292//===----------------------------------------------------------------------===//
257293
258294extension 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.
259310 @inlinable
260311 public func commonSuffix< Other: BidirectionalCollection > (
261312 with other: Other ,
@@ -267,6 +318,19 @@ extension BidirectionalCollection {
267318}
268319
269320extension 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.
270334 @inlinable
271335 public func commonSuffix< Other: BidirectionalCollection > (
272336 with other: Other
@@ -280,6 +344,24 @@ extension BidirectionalCollection where Element: Equatable {
280344//===----------------------------------------------------------------------===//
281345
282346extension 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.
283365 @inlinable
284366 public func endOfCommonPrefix< Other: Collection > (
285367 with other: Other ,
@@ -300,6 +382,22 @@ extension Collection {
300382}
301383
302384extension 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.
303401 @inlinable
304402 public func endOfCommonPrefix< Other: Collection > (
305403 with other: Other
@@ -309,10 +407,28 @@ extension Collection where Element: Equatable {
309407}
310408
311409//===----------------------------------------------------------------------===//
312- // BidirectionalCollection.startOfCommonPrefix (with:)
410+ // BidirectionalCollection.startOfCommonSuffix (with:)
313411//===----------------------------------------------------------------------===//
314412
315413extension 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.
316432 @inlinable
317433 public func startOfCommonSuffix< Other: BidirectionalCollection > (
318434 with other: Other ,
@@ -341,6 +457,22 @@ extension BidirectionalCollection {
341457}
342458
343459extension 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.
344476 @inlinable
345477 public func startOfCommonSuffix< Other: BidirectionalCollection > (
346478 with other: Other
0 commit comments