swift-algorithms icon indicating copy to clipboard operation
swift-algorithms copied to clipboard

Add `commonPrefix(with:)`, `commonSuffix(with:)`

Open timvermeulen opened this issue 4 years ago • 3 comments

Find the common prefix/suffix of two sequences or collections.

  • commonPrefix(with:)
  • commonSuffix(with:)
  • endOfCommonPrefix(with:): get a pair of indices that mark the end of the common prefix of two collections
  • startOfCommonSuffix(with:): get a pair of indices that mark the start of the common suffix of two collections
extension Sequence {
  public func commonPrefix<Other: Sequence>(
    with other: Other,
    by areEquivalent: (Element, Other.Element) throws -> Bool
  ) rethrows -> [Element]
}

extension Sequence where Element: Equatable {
  public func commonPrefix<Other: Sequence>(
    with other: Other
  ) -> CommonPrefix<Self, Other> where Other.Element == Element
}

extension LazySequenceProtocol {
  public func commonPrefix<Other: Sequence>(
    with other: Other,
    by areEquivalent: @escaping (Element, Other.Element) -> Bool
  ) -> CommonPrefix<Self, Other>
}

extension Collection {
  public func commonPrefix<Other: Sequence>(
    with other: Other,
    by areEquivalent: (Element, Other.Element) throws -> Bool
  ) rethrows -> SubSequence
}

extension Collection where Element: Equatable {
  public func commonPrefix<Other: Sequence>(
    with other: Other
  ) -> SubSequence where Other.Element == Element
}

extension LazyCollectionProtocol where Element: Equatable {
  public func commonPrefix<Other: Sequence>(
    with other: Other
  ) -> CommonPrefix<Self, Other> where Other.Element == Element
}

extension BidirectionalCollection {
  public func commonSuffix<Other: BidirectionalCollection>(
    with other: Other,
    by areEquivalent: (Element, Other.Element) throws -> Bool
  ) rethrows -> SubSequence
}

extension BidirectionalCollection where Element: Equatable {
  public func commonSuffix<Other: BidirectionalCollection>(
    with other: Other
  ) -> SubSequence where Other.Element == Element
}

extension Collection {
  public func endOfCommonPrefix<Other: Collection>(
    with other: Other,
    by areEquivalent: (Element, Other.Element) throws -> Bool
  ) rethrows -> (Index, Other.Index)
}

extension Collection where Element: Equatable {
  public func endOfCommonPrefix<Other: Collection>(
    with other: Other
  ) -> (Index, Other.Index) where Other.Element == Element
}

extension BidirectionalCollection {
  public func startOfCommonSuffix<Other: BidirectionalCollection>(
    with other: Other,
    by areEquivalent: (Element, Other.Element) throws -> Bool
  ) rethrows -> (Index, Other.Index)
}

extension BidirectionalCollection where Element: Equatable {
  public func startOfCommonSuffix<Other: BidirectionalCollection>(
    with other: Other
  ) -> (Index, Other.Index) where Other.Element == Element
}

Checklist

  • [x] I've added at least one test that validates that my change is working, if appropriate
  • [x] I've followed the code style of the rest of the project
  • [x] I've read the Contribution Guidelines
  • [x] I've updated the documentation if necessary

timvermeulen avatar Jul 23 '21 14:07 timvermeulen

I think it's more natural to not pluralize of start and end, i.e. just have endOfCommonPrefix, startOfCommonSuffix

kylemacomber avatar Jul 23 '21 14:07 kylemacomber

I think it's more natural to not pluralize of start and end, i.e. just have endOfCommonPrefix, startOfCommonSuffix

Fair point. I think I went with this because we already have endOfPrefix(while:) and startOfSuffix(while:) which both return a single index, but that's probably fine.

timvermeulen avatar Jul 23 '21 15:07 timvermeulen

Don't endsOfCommonPrefix(with:) and startssOfCommonSuffix(with:) overlap with diverges(from:) and converges(with:) from #37 ?

CTMacUser avatar Jul 27 '21 06:07 CTMacUser