From 5ebcf5c6664b7a45a360080a831deeff28aa5a34 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 5 Nov 2020 13:58:56 -0600 Subject: [PATCH] Add methods for finding subsequences in a collection --- Sources/Algorithms/FindRange.swift | 177 ++++++++++++++++++ .../SwiftAlgorithmsTests/FindRangeTests.swift | 89 +++++++++ .../SwiftAlgorithmsTests/TestUtilities.swift | 22 ++- 3 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 Sources/Algorithms/FindRange.swift create mode 100644 Tests/SwiftAlgorithmsTests/FindRangeTests.swift diff --git a/Sources/Algorithms/FindRange.swift b/Sources/Algorithms/FindRange.swift new file mode 100644 index 0000000..492aec7 --- /dev/null +++ b/Sources/Algorithms/FindRange.swift @@ -0,0 +1,177 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +public struct SubSequenceFinder + where Base.Element == Other.Element, Base.Element: Equatable +{ + internal var base: Base + internal var other: Other + internal var firstRange: Range? + + internal init(base: Base, other: Other, allowingOverlaps: Bool) { + self.base = base + self.other = other + self.firstRange = base.firstRange(of: other) + } +} + +extension SubSequenceFinder: 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 { + firstRange.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") + } + let startingPoint = base.index(after: range.lowerBound) + return base[startingPoint...] + .firstRange(of: other) + .map { Index($0) } ?? .end + } + + public subscript(position: Index) -> Range { + guard case .match(let range) = position.base else { + preconditionFailure("Can't subscript with endIndex") + } + return range + } +} + +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 + { + var searchStart = startIndex + guard let needleFirst = other.first else { return nil } + + while let matchStart = self[searchStart...].firstIndex(of: needleFirst) { + var selfPos = index(after: matchStart) + var otherPos = other.index(after: other.startIndex) + + while true { + if otherPos == other.endIndex { + return matchStart..( + of other: Other, + allowingOverlaps: Bool = true) -> SubSequenceFinder + { + SubSequenceFinder(base: self, other: other, allowingOverlaps: allowingOverlaps) + } +} + +extension BidirectionalCollection where Element: Equatable { + public func lastRange(of other: Other) -> Range? + where Other.Element == Element + { + var searchEnd = endIndex + guard let needleLast = other.last else { return nil } + let otherLastIndex = other.index(before: other.endIndex) + + while let matchEnd = self[..) -> Range { + let low = index(startIndex, offsetBy: offsets.lowerBound) + let high = index(low, offsetBy: offsets.count) + return low..( _ expression1: @autoclosure () throws -> S1, _ expression2: @autoclosure () throws -> S2, - _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line ) rethrows where S1.Element: Equatable, S1.Element == S2.Element { - try XCTAssert(expression1().elementsEqual(expression2()), message(), file: file, line: line) + var iterator1 = try expression1().makeIterator() + var iterator2 = try expression2().makeIterator() + var offset = 0 + + while true { + guard let element1 = iterator1.next() else { + if iterator2.next() != nil { + XCTFail("First sequence had fewer elements than the second", file: file, line: line) + } + return + } + + guard let element2 = iterator2.next() else { + XCTFail("First sequence had more elements than the second", file: file, line: line) + return + } + + XCTAssertEqual(element1, element2, "Elements not equal at offset \(offset)", file: file, line: line) + offset += 1 + } } func XCTAssertEqualSequences(