- Proposal: SE-NNNN
- Author: Daryle Walker
- Status: Awaiting review
- Review manager: TBD
This proposal is to add difference detection to Swift's standard library collections.
Swift-evolution thread: Discussion thread topic for that proposal
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.
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.
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.
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.
The comparison methods are an additive feature that doesn’t impact existing code.
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.