Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Proposal and Code to add C++'s mismatch algorithm to Swift.
extension CollectionType {
func diverges<PossiblePrefix: CollectionType where PossiblePrefix.Generator.Element == Generator.Element>(from possiblePrefix: PossiblePrefix, isEquivalent: (Generator.Element, Generator.Element) throws -> Bool) rethrows -> (Index, PossiblePrefix.Index) {
var selfIndex = startIndex
var fromIndex = possiblePrefix.startIndex
while selfIndex != endIndex && fromIndex != possiblePrefix.endIndex {
let areEqual = try isEquivalent(self[selfIndex], possiblePrefix[fromIndex])
guard areEqual else {
break
}
selfIndex = selfIndex.successor()
fromIndex = fromIndex.successor()
}
return (selfIndex, fromIndex)
}
}
extension CollectionType where Generator.Element: Equatable {
func diverges<PossiblePrefix: CollectionType where PossiblePrefix.Generator.Element == Generator.Element>(from possiblePrefix: PossiblePrefix) -> (Index, PossiblePrefix.Index) {
return diverges(from: possiblePrefix, isEquivalent: ==)
}
}
extension CollectionType where Index: BidirectionalIndexType {
func converges<PossibleSuffix: CollectionType where PossibleSuffix.Generator.Element == Generator.Element, PossibleSuffix.Index: BidirectionalIndexType>(with possibleSuffix: PossibleSuffix, isEquivalent: (Generator.Element, Generator.Element) throws -> Bool) rethrows -> (Index, PossibleSuffix.Index) {
var selfIndex = endIndex
var fromIndex = possibleSuffix.endIndex
while selfIndex != startIndex && fromIndex != possibleSuffix.startIndex {
let previousSelfIndex = selfIndex.predecessor()
let previousFromIndex = fromIndex.predecessor()
let areEqual = try isEquivalent(self[previousSelfIndex], possibleSuffix[previousFromIndex])
guard areEqual else {
break
}
selfIndex = previousSelfIndex
fromIndex = previousFromIndex
}
return (selfIndex, fromIndex)
}
}
extension CollectionType where Index: BidirectionalIndexType, Generator.Element: Equatable {
func converges<PossibleSuffix: CollectionType where PossibleSuffix.Generator.Element == Generator.Element, PossibleSuffix.Index: BidirectionalIndexType>(with possibleSuffix: PossibleSuffix) -> (Index, PossibleSuffix.Index) {
return converges(with: possibleSuffix, isEquivalent: ==)
}
}
/*let a = [1, 2, 3, 4]
let b = [1, 5, 3, 4]
let c = [1, 2, 3]
let d = [1, 2, 3, 4, 5]
let e = [2, 3, 4]
a.diverges(from: b)
a.diverges(from: c)
a.diverges(from: d)
a.diverges(from: e)
a.converges(with: b)
a.converges(with: c)
a.converges(with: d)
a.converges(with: e)*/

Implement a mismatch algorithm, equivalent to std::mismatch() in C++

Introduction

This proposal is to add difference detection to Swift's standard library collections.

Swift-evolution thread: Discussion thread topic for that proposal

Motivation

Finding where two similar collections differ is needed in algorithms that have different policies on handling the common part versus the uncommon parts. Similar tests already exist in the standard library: the elementsEqual methods in Sequence for instance; the methods can indicate two sequences are different but not where they diverged. Flipping it around, it means that sequence equivalence, and several other sequence methods, can be expressed in terms of mismatch-finding. However, returning the divergence point means returning references to the diverging elements, which means an index, which means that collections are required instead of plain sequences.

Proposed solution

The Swift standard library should provide generic implementations of the "mismatch" algorithm for forward searches on prefixes and backward searches on suffixes. The forward/prefix form is called diverges(from: isEquivalent:). The backward/suffix form is called converges(with: isEquivalent:), and is present only when the collection type supports bidirectional indexing. If the collection's element type conforms to Equatable, there variants of the method(s) that drop the second argument and instead use == for the equivalency test.

Detailed design

diverges(from:isEquivalent:) and diverges(from:)

Forward mismatching on prefixes will be added to the Collection protocol requirements with a default implementation. Its variant will extend Collection with a default implementation. These methods will have the following declarations:

protocol Collection {
	// existing declarations

	/**
		Compares the collection against a given collection element-wise until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its end index.

		The predicate must be an equivalence relation over the elements.

		- parameter from: A collection to compare to this one.
		- parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.
		- returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection's end index.  If both tuple members are at their respective collection's end index, then the collections were equivalent.
		- complexity: `min(count, from.count)` comparisons.
		- throws: Whatever `isEquivalent` may throw.
	*/
    func diverges<PossiblePrefix: Collection where PossiblePrefix.Iterator.Element == Iterator.Element>(from possiblePrefix: PossiblePrefix, isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> (Index, PossiblePrefix.Index)
}

extension Collection {
	/**
		Compares the collection against a given collection element-wise until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its end index.

		The predicate must be an equivalence relation over the elements.

		- parameter from: A collection to compare to this one.
		- parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.
		- returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection's end index.  If both tuple members are at their respective collection's end index, then the collections were equivalent.
		- complexity: `min(count, from.count)` comparisons.
		- throws: Whatever `isEquivalent` may throw.
	*/
    func diverges<PossiblePrefix: Collection where PossiblePrefix.Iterator.Element == Iterator.Element>(from possiblePrefix: PossiblePrefix, isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> (Index, PossiblePrefix.Index)
}

extension Collection where Iterator.Element: Equatable {
	/**
		Compares the collection against a given collection element-wise until either corresponding elements are no longer equal, or at least one collection reaches its end index.

		- parameter from: A collection to compare to this one.
		- returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equal until that point, then exactly one member of the tuple will be at its collection's end index.  If both tuple members are at their respective collection's end index, then the collections were equal.
		- complexity: `min(count, from.count)` comparisons.
	*/
    func diverges<PossiblePrefix: Collection where PossiblePrefix.Iterator.Element == Iterator.Element>(from possiblePrefix: PossiblePrefix) -> (Index, PossiblePrefix.Index)
}

I don't know if we should insist that at least one (or both) of the collections tested should be finite. I don't know if the results should be discardable.

converges(with:isEquivalent:) and converges(with:)

Backward mismatching on suffixes will be added to the BidirectionalCollection protocol requirements with a default implementation. Its variant will extend BidirectionalCollection with a default implementation. These methods will have the following declarations:

protocol BidirectionalCollection {
	// existing declarations

	/**
		Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its start index.

		Both collections must be finite.

		The predicate must be an equivalence relation over the elements.

		- parameter with: A collection to compare to this one.
		- parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.
		- returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection's start index.  If both tuple members are at their respective collection's start index, then the collections were equivalent.  If both tuple members are at their respective collection's end index, then either the collections' last elements differ or at least one collection was empty.
		- complexity: `min(count, from.count)` comparisons.
		- throws: Whatever `isEquivalent` may throw.
	*/
    func converges<PossibleSuffix: BidirectionalCollection where PossibleSuffix.Iterator.Element == Iterator.Element>(with possibleSuffix: PossibleSuffix, isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> (Index, PossibleSuffix.Index)
}

extension BidirectionalCollection {
	/**
		Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its start index.

		Both collections must be finite.

		The predicate must be an equivalence relation over the elements.

		- parameter with: A collection to compare to this one.
		- parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.
		- returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection's start index.  If both tuple members are at their respective collection's start index, then the collections were equivalent.  If both tuple members are at their respective collection's end index, then either the collections' last elements differ or at least one collection was empty.
		- complexity: `min(count, from.count)` comparisons.
		- throws: Whatever `isEquivalent` may throw.
	*/
    func converges<PossibleSuffix: BidirectionalCollection where PossibleSuffix.Iterator.Element == Iterator.Element>(with possibleSuffix: PossibleSuffix, isEquivalent: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> (Index, PossibleSuffix.Index)
}

extension BidirectionalCollection where Iterator.Element: Equatable {
	/**
		Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equal, or at least one collection reaches its start index.

		Both collections must be finite.

		- parameter with: A collection to compare to this one.
		- returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection's start index.  If both tuple members are at their respective collection's start index, then the collections were equivalent.  If both tuple members are at their respective collection's end index, then either the collections' last elements differ or at least one collection was empty.
		- complexity: `min(count, from.count)` comparisons.
	*/
    func converges<PossibleSuffix: BidirectionalCollection where PossibleSuffix.Iterator.Element == Iterator.Element>(with possibleSuffix: PossibleSuffix) -> (Index, PossibleSuffix.Index)
}

I don't know if the results should be discardable.

Impact on existing code

The comparison methods are an additive feature that doesn’t impact existing code.

Alternatives considered

The alternative is to not include these methods in the standard library, but the user will need to develop their custom implementation of the mismatch algorithms tailored for their needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.