|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | +// EndsWith |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +extension BidirectionalCollection where Element: Equatable { |
| 17 | + |
| 18 | + |
| 19 | + /// Returns a Boolean value indicating whether the final elements of the |
| 20 | + /// collection are the same as the elements in another collection. |
| 21 | + /// |
| 22 | + /// This example tests whether one countable range ends with the elements |
| 23 | + /// of another countable range. |
| 24 | + /// |
| 25 | + /// let a = 8...10 |
| 26 | + /// let b = 1...10 |
| 27 | + /// |
| 28 | + /// print(b.ends(with: a)) |
| 29 | + /// // Prints "true" |
| 30 | + /// |
| 31 | + /// Passing a collection with no elements or an empty collection as |
| 32 | + /// `possibleSuffix` always results in `true`. |
| 33 | + /// |
| 34 | + /// print(b.ends(with: [])) |
| 35 | + /// // Prints "true" |
| 36 | + /// |
| 37 | + /// - Parameter possibleSuffix: A collection to compare to this collection. |
| 38 | + /// - Returns: `true` if the initial elements of the collection are the same as |
| 39 | + /// the elements of `possibleSuffix`; otherwise, `false`. If |
| 40 | + /// `possibleSuffix` has no elements, the return value is `true`. |
| 41 | + /// |
| 42 | + /// - Complexity: O(*m*), where *m* is the lesser of the length of the |
| 43 | + /// collection and the length of `possibleSuffix`. |
| 44 | + @inlinable |
| 45 | + public func ends<PossibleSuffix: BidirectionalCollection>( |
| 46 | + with possibleSuffix: PossibleSuffix |
| 47 | + ) -> Bool where PossibleSuffix.Element == Element { |
| 48 | + return self.ends(with: possibleSuffix, by: ==) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension BidirectionalCollection { |
| 53 | + /// Returns a Boolean value indicating whether the final elements of the |
| 54 | + /// collection are equivalent to the elements in another collection, using |
| 55 | + /// the given predicate as the equivalence test. |
| 56 | + /// |
| 57 | + /// The predicate must be an *equivalence relation* over the elements. That |
| 58 | + /// is, for any elements `a`, `b`, and `c`, the following conditions must |
| 59 | + /// hold: |
| 60 | + /// |
| 61 | + /// - `areEquivalent(a, a)` is always `true`. (Reflexivity) |
| 62 | + /// - `areEquivalent(a, b)` implies `areEquivalent(b, a)`. (Symmetry) |
| 63 | + /// - If `areEquivalent(a, b)` and `areEquivalent(b, c)` are both `true`, then |
| 64 | + /// `areEquivalent(a, c)` is also `true`. (Transitivity) |
| 65 | + /// |
| 66 | + /// - Parameters: |
| 67 | + /// - possibleSuffix: A collection to compare to this collection. |
| 68 | + /// - areEquivalent: A predicate that returns `true` if its two arguments |
| 69 | + /// are equivalent; otherwise, `false`. |
| 70 | + /// - Returns: `true` if the initial elements of the collection are equivalent |
| 71 | + /// to the elements of `possibleSuffix`; otherwise, `false`. If |
| 72 | + /// `possibleSuffix` has no elements, the return value is `true`. |
| 73 | + /// |
| 74 | + /// - Complexity: O(*m*), where *m* is the lesser of the length of the |
| 75 | + /// collection and the length of `possibleSuffix`. |
| 76 | + @inlinable |
| 77 | + public func ends<PossibleSuffix: BidirectionalCollection>( |
| 78 | + with possibleSuffix: PossibleSuffix, |
| 79 | + by areEquivalent: (Element, PossibleSuffix.Element) throws -> Bool |
| 80 | + ) rethrows -> Bool { |
| 81 | + try self.reversed().starts(with: possibleSuffix.reversed(), by: areEquivalent) |
| 82 | + } |
| 83 | +} |
| 84 | + |
0 commit comments