diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift new file mode 100644 index 00000000..1766349b --- /dev/null +++ b/Sources/Algorithms/Suffix.swift @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +//===----------------------------------------------------------------------===// +// Suffix(while:) +//===----------------------------------------------------------------------===// + +extension BidirectionalCollection { + /// Returns a subsequence containing the elements from the end until + /// `predicate` returns `false` and skipping the remaining elements. + /// + /// - Parameter predicate: A closure that takes an element of the + /// sequence as its argument and returns `true` if the element should + /// be included or `false` if it should be excluded. Once the predicate + /// returns `false` it will not be called again. + /// + /// - Complexity: O(*n*), where *n* is the length of the collection. + public func suffix( + while predicate: (Element) throws -> Bool + ) rethrows -> SubSequence { + let start = startIndex + var result = endIndex + while result != start { + let previous = index(before: result) + guard try predicate(self[previous]) else { break } + result = previous + } + return self[result...] + } +} diff --git a/Tests/SwiftAlgorithmsTests/SuffixTests.swift b/Tests/SwiftAlgorithmsTests/SuffixTests.swift new file mode 100644 index 00000000..c2a03693 --- /dev/null +++ b/Tests/SwiftAlgorithmsTests/SuffixTests.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +import XCTest +import Algorithms + +final class SuffixTests: XCTestCase { + func testSuffix() { + let a = 0...10 + XCTAssertEqualSequences(a.suffix(while: { $0 > 5 }), (6...10)) + XCTAssertEqualSequences(a.suffix(while: { $0 > 10 }), []) + XCTAssertEqualSequences(a.suffix(while: { $0 > 9 }), [10]) + XCTAssertEqualSequences(a.suffix(while: { $0 > -1 }), (0...10)) + + let empty: [Int] = [] + XCTAssertEqualSequences(empty.suffix(while: { $0 > 10 }), []) + } +}