-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merging Sorted Sequences #43
Open
CTMacUser
wants to merge
9
commits into
apple:main
Choose a base branch
from
CTMacUser:merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82f36cc
Add methods to merge sorted sequences
CTMacUser aef67fe
Improve sorted merged sequences
CTMacUser f6e0f32
Add test to confirm code paths
CTMacUser 1229245
Change implementation of merging
CTMacUser 59a7e7b
Remove unnecessary payload
CTMacUser e0ac76a
Conform sorted merged sequences to Collection
CTMacUser bcb7f2c
Add documentation for sorted merged sequences
CTMacUser 4899331
Add test with custom predicate
CTMacUser a0a59ff
Merge branch 'main' into merge
CTMacUser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# Permutations | ||
|
||
[[Source](../Sources/Algorithms/MergeSorted.swift) | | ||
[Tests](../Tests/SwiftAlgorithmsTests/MergeSortedTests.swift)] | ||
|
||
A method that returns the merger of the sorted receiver and the sorted argument, | ||
or a subset of that merger. The result is also sorted, with the same criteria. | ||
|
||
## Detailed Design | ||
|
||
The `mergeSorted(with:keeping:by:)` method is declared as a `Sequence` | ||
extension, and returns a standard `Array` of the same element type. | ||
|
||
```swift | ||
extension Sequence { | ||
/// Returns an array listing the merger of this sequence and the given | ||
/// sequence, but keeping only the selected subset, assuming both sources are | ||
/// sorted according to the given predicate that can compare elements. | ||
public func mergeSorted<S: Sequence>( | ||
with second: S, | ||
keeping selection: SetCombination, | ||
by areInIncreasingOrder: (Element, Element) throws -> Bool | ||
) rethrows -> [Element] where S.Element == Element | ||
} | ||
``` | ||
|
||
Besides the sequence that will be combined with the receiver and the predicate | ||
to be used as the sorting criteria, the following subsets of the merged sequence | ||
can be selected: | ||
|
||
```swift | ||
/// The manners two (multi)sets may be combined. | ||
public enum SetCombination: CaseIterable { | ||
case nothing, firstMinusSecond, secondMinusFirst, symmetricDifference, | ||
intersection, first, second, union, sum | ||
} | ||
``` | ||
|
||
The `.sum` case is the usual merge sort. The `.nothing`, `.first`, `.second` | ||
cases are somewhat degenerate and aren't generally used. The other cases are | ||
the usual subsets. The difference between `.union` and `.sum` is that the | ||
former generates mergers where common elements are included only once, while the | ||
latter includes both copies of each shared value. When `.sum` is in place, the | ||
copies from the second sequence go after all the copies from the first. | ||
|
||
When the `Element` type is `Comparable`, the `mergeSorted(with:keeping:)` method | ||
is added, which defaults the comparison predicate to the standard `<` operator: | ||
|
||
```swift | ||
extension Sequence where Element: Comparable { | ||
/// Returns an array listing the merger of this sequence and the given | ||
/// sequence, but keeping only the selected subset, and assuming both sources | ||
/// are sorted. | ||
public func mergeSorted<S: Sequence>( | ||
with second: S, | ||
keeping selection: SetCombination | ||
) -> [Element] where S.Element == Element | ||
} | ||
``` | ||
|
||
If the ordering predicate does not throw, then the merged sequence may be | ||
computed on-demand by making at least the receiver lazy: | ||
|
||
```swift | ||
extension LazySequenceProtocol { | ||
/// Returns a lazy sequence listing the merger of this lazy sequence and the | ||
/// given lazy sequence, but keeping only the selected subset, assuming both | ||
/// sources are sorted according to the given predicate that can compare | ||
/// elements. | ||
public func mergeSorted<S: LazySequenceProtocol>( | ||
with second: S, | ||
keeping selection: SetCombination, | ||
by areInIncreasingOrder: @escaping (Element, Element) -> Bool | ||
) -> MergedSequence<Elements, S.Elements> where S.Element == Element | ||
|
||
/// Returns a lazy sequence listing the merger of this lazy sequence and the | ||
/// given sequence, but keeping only the selected subset, assuming both | ||
/// sources are sorted according to the given predicate that can compare | ||
/// elements. | ||
public func mergeSorted<S: Sequence>( | ||
with second: S, | ||
keeping selection: SetCombination, | ||
by areInIncreasingOrder: @escaping (Element, Element) -> Bool | ||
) -> MergedSequence<Elements, S> where S.Element == Element | ||
} | ||
|
||
extension LazySequenceProtocol where Element: Comparable { | ||
/// Returns a lazy sequence listing the merger of this lazy sequence and the | ||
/// given lazy sequence, but keeping only the selected subset, and assuming | ||
/// both sources are sorted. | ||
public func mergeSorted<S: LazySequenceProtocol>( | ||
with second: S, | ||
keeping selection: SetCombination | ||
) -> MergedSequence<Elements, S.Elements> where S.Element == Element | ||
|
||
/// Returns a lazy sequence listing the merger of this lazy sequence and the | ||
/// given sequence, but keeping only the selected subset, and assuming both | ||
/// sources are sorted. | ||
public func mergeSorted<S: Sequence>( | ||
with second: S, | ||
keeping selection: SetCombination | ||
) -> MergedSequence<Elements, S> where S.Element == Element | ||
} | ||
``` | ||
|
||
If both source sequences also conform to (at least) `Collection`, then the | ||
returned sequence representing the merger is also a collection. | ||
|
||
### Complexity | ||
|
||
Calling `mergeSorted(with:keeping:by:)` or `mergeSorted(with:keeping:)` is an | ||
O(*n* + *m*) operation, where *n* and *m* are the lengths of the operand | ||
sequences. Creating an iterator and/or lazy sequence is O(1), while iterating | ||
through all of lazy sequence will be O(*n* + *m*). If the kept subset is one of | ||
the degenerate cases, the complexity will be shorter. | ||
|
||
### Comparison with other languages | ||
|
||
**C++:** The `<algorithm>` library defines the `set_difference`, | ||
`set_intersection`, `set_symmetric_difference`, `set_union`, and `merge` | ||
functions. They can be all distilled into one algorithm, which the | ||
`mergeSorted(with:keeping:by:)` method and its overloads do for Swift. The | ||
`.firstMinusSecond` and `.secondMinusFirst` subsets are equivalent to calls to | ||
`set_difference`; `.intersection` to `set_intersection`; `.symmetricDifference` | ||
to `set_symmetric_difference`; `.union` to `set_union`; and `.sum` to `merge`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.