diff --git a/Sources/Algorithms/FindRange.swift b/Sources/Algorithms/FindRange.swift index 8ad0f7b..66efaeb 100644 --- a/Sources/Algorithms/FindRange.swift +++ b/Sources/Algorithms/FindRange.swift @@ -15,11 +15,17 @@ public struct SubSequenceFinder internal var base: Base internal var other: Other internal var firstRange: Range? + internal var areEquivalent: (Base.Element, Base.Element) -> Bool - internal init(base: Base, other: Other) { + internal init( + base: Base, + other: Other, + by areEquivalent: @escaping (Base.Element, Base.Element) -> Bool) + { self.base = base self.other = other - self.firstRange = base.firstRange(of: other) + self.areEquivalent = areEquivalent + self.firstRange = base.firstRange(of: other, by: areEquivalent) } } @@ -86,14 +92,58 @@ 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, + by areEquivalent: @escaping (Element, Element) -> Bool + ) + -> SubSequenceFinder + { + SubSequenceFinder(base: self, other: other, by: areEquivalent) + } +} + extension Collection where Element: Equatable { - public func firstRange(of other: Other) -> Range? + public func allRanges(of other: Other) -> SubSequenceFinder + where Element == Other.Element + { + allRanges(of: other, by: ==) + } +} + +// MARK: - firstRange(of:by:) / firstRange(of:) + +extension Collection { + public func firstRange( + of other: Other, + by areEquivalent: (Element, Element) throws -> Bool + ) rethrows -> Range? where Other.Element == Element { var searchStart = startIndex guard let needleFirst = other.first else { return nil } - while let matchStart = self[searchStart...].firstIndex(of: needleFirst) { + while let matchStart = try self[searchStart...] + .firstIndex(where: { try areEquivalent($0, needleFirst) }) + { var selfPos = index(after: matchStart) var otherPos = other.index(after: other.startIndex) @@ -104,7 +154,7 @@ extension Collection where Element: Equatable { if selfPos == self.endIndex { return nil } - if self[selfPos] != other[otherPos] { + if try !areEquivalent(self[selfPos], other[otherPos]) { break } @@ -117,14 +167,18 @@ extension Collection where Element: Equatable { return nil } - - public func allRanges(of other: Other) - -> SubSequenceFinder +} + +extension Collection where Element: Equatable { + public func firstRange(of other: Other) -> Range? + where Element == Other.Element { - SubSequenceFinder(base: self, other: other) + firstRange(of: other, by: ==) } } +// MARK: - lastRange(of:by:) / lastRange(of:) + extension BidirectionalCollection where Element: Equatable { public func lastRange(of other: Other) -> Range? where Other.Element == Element @@ -235,59 +289,3 @@ extension SubSequenceFinderFromEnd: Collection { 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