Merge
public struct Merge : Codable, Equatable, Hashable, Identifiable
An algorithm that takes an array with two sorted partitions and produces one sorted partition in their place.
This algorithm operates entirely on indices and comparisons so there it has no need to pass arrays or copy elements. The partitions are defined by the bounds created from indices, and the output are transactions that need to be applied to the source array.
The left partition is the elements in the source array between [fromIndex
, middleIndex
].
The right partition is the elements in the source array between (middleIndex
, toIndex
).
-
The stable identity of the merge.
Declaration
Swift
public let id: UUID
-
The current position of the element being compared on behalf of the left partition.
This is the position of the element represented by
Comparison.left
when this is the ongoing merge. -
The transactions that need to be applied to the original array to sort the two sub-lists.
This value is “live” and will grow as the merge is executed. When the merge is finished this value will contain all of the transactions that need to be applied to the original array. It is primarily exposed to allow the internals of the merge to be observed.
This value should not be used as the final output of the merge unless it is known that the algorithm has finished. It may be easier to perform
callAsFunction()
and respond to the step that is returned – the output will be reported through that function if the algorithm is finished.See also
MergeSort.outputAfterTransactions
Declaration
Swift
public private(set) var output: Set<Transaction> { get }
-
The position in the outut of the winner of the comparison between the elements at
leftPartitionIndex
andrightPartitionIndex
. -
The current position of the element being compared on behalf of the right partition.
This is the position of the element represented by
Comparison.right
when this is the ongoing merge.
-
Creates an algorithm to merge two sorted partitions within the current output of an ongoing merge sort.
Declaration
Parameters
fromIndex
The position of the first element in the left paritition.
middleIndex
The position of last element in the left partition.
toIndex
The position past the last element in the right partition.
-
Returns a Boolean value indicating whether both the
leftPartitionIndex
andrightPartitionIndex
are within the bounds of their respective partitions.This value is
false
if either side is out of bounds.Declaration
Swift
public var arePartitionIndicesInBounds: Bool { get }
-
Answers the current comparison with the given side.
The merge is advanced to the state that follows the answer. If the answer is
left
then theleftTransaction
will be added tooutput
, otherwise therightTransaction
will be added to output.Declaration
Swift
public mutating func answer(_ answer: Comparison<MergeSort>.Side)
Parameters
answer
The answer to the current comparison.
-
Returns the output of the merge if it is finished.
If the merge is finished then the returned value is equal to
output
. If the merge is not finished then the returned value is equal tonil
.Declaration
Swift
public mutating func callAsFunction() -> Set<Transaction>?
-
A description of a move to be made in the source array to perform the merge.
A transaction is focused on a single element in the source array being moved to another position.
See moreDeclaration
Swift
public struct Transaction : Codable, Equatable, Hashable