From 9ef531f23f5d82eb143ff68af26e8434ede70c36 Mon Sep 17 00:00:00 2001 From: michiboo Date: Wed, 20 Jan 2021 15:16:25 +0200 Subject: [PATCH 1/8] add suffix while method --- Sources/Algorithms/Suffix.swift | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Sources/Algorithms/Suffix.swift diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift new file mode 100644 index 00000000..7ea18b52 --- /dev/null +++ b/Sources/Algorithms/Suffix.swift @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2020 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 { + @inlinable + public __consuming func Suffix(while predicate: (Element) throws -> Bool + ) rethrows -> [Element] { + var result = ContiguousArray() + self.reverse() + for element in self { + guard try predicate(element) else { + break + } + result.append(element) + } + result.reverse() + return Array(result) + } +} From 1acc2136d9bcbbfc2dea824ae9622e4ddcaa3bf1 Mon Sep 17 00:00:00 2001 From: michiboo Date: Wed, 20 Jan 2021 15:23:38 +0200 Subject: [PATCH 2/8] add test for suffix --- Tests/SwiftAlgorithmsTests/SuffixTests.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Tests/SwiftAlgorithmsTests/SuffixTests.swift diff --git a/Tests/SwiftAlgorithmsTests/SuffixTests.swift b/Tests/SwiftAlgorithmsTests/SuffixTests.swift new file mode 100644 index 00000000..3d9827ce --- /dev/null +++ b/Tests/SwiftAlgorithmsTests/SuffixTests.swift @@ -0,0 +1,20 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2020 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)) +} From 813ea7905e87457ac7ddd5d6bbc1bba6f5a2750c Mon Sep 17 00:00:00 2001 From: michiboo Date: Fri, 22 Jan 2021 16:17:28 +0200 Subject: [PATCH 3/8] add more test for suffix Tests --- Sources/Algorithms/Suffix.swift | 27 ++++++++++---------- Tests/SwiftAlgorithmsTests/SuffixTests.swift | 7 ++++- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift index 7ea18b52..1cc10ac9 100644 --- a/Sources/Algorithms/Suffix.swift +++ b/Sources/Algorithms/Suffix.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Algorithms open source project // -// Copyright (c) 2020 Apple Inc. and the Swift project authors +// 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 @@ -14,18 +14,17 @@ //===----------------------------------------------------------------------===// extension BidirectionalCollection { - @inlinable - public __consuming func Suffix(while predicate: (Element) throws -> Bool - ) rethrows -> [Element] { - var result = ContiguousArray() - self.reverse() - for element in self { - guard try predicate(element) else { - break - } - result.append(element) + 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 } - result.reverse() - return Array(result) + return self[result...] } -} +} \ No newline at end of file diff --git a/Tests/SwiftAlgorithmsTests/SuffixTests.swift b/Tests/SwiftAlgorithmsTests/SuffixTests.swift index 3d9827ce..8687639f 100644 --- a/Tests/SwiftAlgorithmsTests/SuffixTests.swift +++ b/Tests/SwiftAlgorithmsTests/SuffixTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Algorithms open source project // -// Copyright (c) 2020 Apple Inc. and the Swift project authors +// 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 @@ -17,4 +17,9 @@ 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 }), [9]) + XCTAssertEqualSequences(a.suffix(while: { $0 > -1 }), (0...10)) + let empty = (0...).prefix(0) + XCTAssertEqualSequences(empty.suffix(while: { $0 > 10 }), []) } From 08e424ea070b1ac995376d4ad0eba9d580e0bead Mon Sep 17 00:00:00 2001 From: michiboo Date: Sat, 23 Jan 2021 08:38:12 +0200 Subject: [PATCH 4/8] add doc on suffix method --- Sources/Algorithms/Suffix.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift index 1cc10ac9..1978bb9d 100644 --- a/Sources/Algorithms/Suffix.swift +++ b/Sources/Algorithms/Suffix.swift @@ -14,6 +14,15 @@ //===----------------------------------------------------------------------===// 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 { From 60bad3bbf46df1f5658fb0f6087ddfad4d79bbda Mon Sep 17 00:00:00 2001 From: michiboo Date: Sat, 23 Jan 2021 14:39:02 +0800 Subject: [PATCH 5/8] Update Sources/Algorithms/Suffix.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> --- Sources/Algorithms/Suffix.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift index 1978bb9d..9a60b251 100644 --- a/Sources/Algorithms/Suffix.swift +++ b/Sources/Algorithms/Suffix.swift @@ -36,4 +36,4 @@ extension BidirectionalCollection { } return self[result...] } -} \ No newline at end of file +} From 2f8f798d0c8dc11e6167d65b44464a1a2f5ec2bd Mon Sep 17 00:00:00 2001 From: michiboo Date: Sun, 24 Jan 2021 21:54:14 +0800 Subject: [PATCH 6/8] Update Sources/Algorithms/Suffix.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> --- Sources/Algorithms/Suffix.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift index 9a60b251..c42887a3 100644 --- a/Sources/Algorithms/Suffix.swift +++ b/Sources/Algorithms/Suffix.swift @@ -14,8 +14,8 @@ //===----------------------------------------------------------------------===// extension BidirectionalCollection { - /// Returns a subsequence containing the elements from the end until `predicate` - /// returns `false` and skipping the remaining elements. + /// 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 From f9a4f79027ebd8873734d73754964b961598d73f Mon Sep 17 00:00:00 2001 From: michiboo Date: Sun, 24 Jan 2021 21:54:26 +0800 Subject: [PATCH 7/8] Update Sources/Algorithms/Suffix.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> --- Sources/Algorithms/Suffix.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Algorithms/Suffix.swift b/Sources/Algorithms/Suffix.swift index c42887a3..1766349b 100644 --- a/Sources/Algorithms/Suffix.swift +++ b/Sources/Algorithms/Suffix.swift @@ -31,7 +31,6 @@ extension BidirectionalCollection { while result != start { let previous = index(before: result) guard try predicate(self[previous]) else { break } - result = previous } return self[result...] From 3b511072a639ffaafc3a79d4feb90831100edba5 Mon Sep 17 00:00:00 2001 From: michiboo Date: Tue, 9 Feb 2021 12:56:08 -0600 Subject: [PATCH 8/8] Fix suffix tests --- Tests/SwiftAlgorithmsTests/SuffixTests.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/SwiftAlgorithmsTests/SuffixTests.swift b/Tests/SwiftAlgorithmsTests/SuffixTests.swift index 8687639f..c2a03693 100644 --- a/Tests/SwiftAlgorithmsTests/SuffixTests.swift +++ b/Tests/SwiftAlgorithmsTests/SuffixTests.swift @@ -13,13 +13,14 @@ 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 }), [9]) + XCTAssertEqualSequences(a.suffix(while: { $0 > 9 }), [10]) XCTAssertEqualSequences(a.suffix(while: { $0 > -1 }), (0...10)) - let empty = (0...).prefix(0) + + let empty: [Int] = [] XCTAssertEqualSequences(empty.suffix(while: { $0 > 10 }), []) + } }