From b33c3881abac79347c47c7ac7045aedca2cd2fa8 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 7 Jan 2021 12:55:45 -0600 Subject: [PATCH] Split out searching from beginning and end --- Sources/Algorithms/FindRange.swift | 149 ++++++++++++++++-- .../SwiftAlgorithmsTests/FindRangeTests.swift | 9 +- 2 files changed, 135 insertions(+), 23 deletions(-) diff --git a/Sources/Algorithms/FindRange.swift b/Sources/Algorithms/FindRange.swift index 492aec7..97e342b 100644 --- a/Sources/Algorithms/FindRange.swift +++ b/Sources/Algorithms/FindRange.swift @@ -86,22 +86,6 @@ extension SubSequenceFinder: Collection { } } -extension SubSequenceFinder: BidirectionalCollection - where Base: BidirectionalCollection, Other: BidirectionalCollection -{ - public func index(before i: Index) -> Index { - let endingPoint: Base.Index - switch i.base { - case .match(let range): - precondition(range != firstRange, "Can't move before startIndex") - endingPoint = base.index(before: range.upperBound) - case .end: - endingPoint = base.endIndex - } - return Index(base[..(of other: Other) -> Range? where Other.Element == Element @@ -175,3 +159,136 @@ extension BidirectionalCollection where Element: Equatable { return nil } } + + +public struct SubSequenceFinderFromEnd + where Base.Element == Other.Element, Base.Element: Equatable +{ + internal var base: Base + internal var other: Other + internal var lastRange: Range? + + internal init(base: Base, other: Other, allowingOverlaps: Bool) { + self.base = base + self.other = other + self.lastRange = base.lastRange(of: other) + } +} + +extension SubSequenceFinderFromEnd: Collection { + public struct Index: Comparable { + internal enum Representation: Comparable { + case match(Range) + case end + + public static func < (lhs: Representation, rhs: Representation) -> Bool { + switch (lhs, rhs) { + case (.match, .end): + return true + case (.end, _): + return false + case (.match(let lhs), .match(let rhs)): + return lhs.lowerBound > rhs.lowerBound + } + } + } + + internal var base: Representation + + internal static var end: Index { + Index(base: .end) + } + + private init(base: Representation) { + self.base = base + } + + internal init(_ range: Range) { + self.base = .match(range) + } + + public static func < (lhs: Index, rhs: Index) -> Bool { + lhs.base < rhs.base + } + } + + public var startIndex: Index { + lastRange.map { Index($0) } ?? .end + } + + public var endIndex: Index { + .end + } + + public func index(after i: Index) -> Index { + guard case .match(let range) = i.base else { + preconditionFailure("Can't advance past endIndex") + } + return base[.. Range { + guard case .match(let range) = position.base else { + preconditionFailure("Can't subscript with endIndex") + } + return range + } +} + +public protocol CollectionOfRanges: Collection where + Element == Range +{ + associatedtype Bound: Comparable + associatedtype AscendingRangeCollection: Collection + where AscendingRangeCollection.Element == Element + + func ascendingRanges() -> AscendingRangeCollection +} + +extension CollectionOfRanges where Element == Range { + public func ascendingRanges() -> [Range] { + sorted(by: { $0.lowerBound < $1.lowerBound }) + } +} + +extension CollectionOfRanges where AscendingRangeCollection == Self { + public func ascendingRanges() -> Self { + self + } +} + +extension SubSequenceFinder: CollectionOfRanges { + public typealias Bound = Base.Index + public typealias AscendingRangeCollection = Self +} + +extension SubSequenceFinderFromEnd: CollectionOfRanges { + public typealias Bound = Base.Index + public typealias AscendingRangeCollection = [Range] + + public func ascendingRanges() -> [Range] { + self.reversed() + } +} + +extension Slice: CollectionOfRanges where Base: CollectionOfRanges { + public typealias Bound = Base.Bound + public typealias AscendingRangeCollection = [Range] +} + +extension ReversedCollection: CollectionOfRanges + where Base: CollectionOfRanges, + Base.AscendingRangeCollection: BidirectionalCollection +{ + public typealias Bound = Base.Bound + public typealias AscendingRangeCollection = ReversedCollection + + public func ascendingRanges() -> ReversedCollection { + // base.ascendingRanges().reversed() + fatalError() + } +} + +extension LazyF diff --git a/Tests/SwiftAlgorithmsTests/FindRangeTests.swift b/Tests/SwiftAlgorithmsTests/FindRangeTests.swift index 8f60e26..04b6f7e 100644 --- a/Tests/SwiftAlgorithmsTests/FindRangeTests.swift +++ b/Tests/SwiftAlgorithmsTests/FindRangeTests.swift @@ -72,18 +72,13 @@ final class FindTests: XCTestCase { XCTAssertEqualSequences( input.allRanges(of: "ababa"), [input.range(at: 0..<5), input.range(at: 2..<7)]) - XCTAssertEqual(input.allRanges(of: "ababa").last, input.range(at: 2..<7)) XCTAssertEqualSequences( input.allRanges(of: "aaba"), [input.range(at: 6..<10), input.range(at: 9..<13)]) XCTAssertEqualSequences( input.allRanges(of: "aaba").reversed(), [input.range(at: 9..<13), input.range(at: 6..<10)]) - XCTAssertEqual(input.allRanges(of: "aaba").last, input.range(at: 9..<13)) - } - - func testCollectionConformance() { - let matches = input.allRanges(of: "ab") - validateIndexTraversals(matches) + + print(input.allRanges(of: "aaba").prefix(2)) } }