Comparison

public struct Comparison<Algorithm> where Algorithm : SortingAlgorithm
extension Comparison: Codable where Algorithm: Codable
extension Comparison: Equatable where Algorithm: Equatable
extension Comparison: Hashable where Algorithm: Hashable

A decision about the inherent order of two elements being sorted by an algorithm.

A comparison presents two elements – left and right – whose order needs to be determined, accepts an answer for that decision, and produces the next state of the algorithm.

The inherent order of the elements is left to the caller. Whether an element is on the left or right is irrelevant. A comparison could be used to represent “greater than,” “less than,” “better than,” “worse than,” “talller than,” etc. As long as the answers to the comparisons are consistent the sorted outcome will reflect the intended order.

A comparison is technically a lense over an algorithm. It has no bespoke state itself.

Public Instance Interface

  • The left element in the comparison.

    Corresponds to Answer.left.

    Declaration

    Swift

    public var left: Algorithm.Element { get }
  • The right element in the comparison.

    Corresponds to Answer.right.

    Declaration

    Swift

    public var right: Algorithm.Element { get }
  • Accesses the element for the specified side.

    Declaration

    Swift

    public subscript(side: Side) -> Algorithm.Element { get }

    Parameters

    side

    The side whose element will be accessed.

    Return Value

    The element for the specified side.

  • Answers the comparison with the given side.

    The given side “wins” the comparison. What “winning” means is left to the caller, but it is important that the same criteria is applied to all elements consistently.

    The returned algorithm will be the algorithm at its next state, after the comparison is made. The algorithm can be executed to continue producing comparisons and eventually the sorted output.

    Declaration

    Swift

    public func callAsFunction(_ answer: Side) -> Algorithm

    Parameters

    answer

    The side that wins the comparison.

    Return Value

    The state of the algorithm after the comparison.

  • Answers the comparison with whether or not the left side won.

    The returned algorithm will be the algorithm at its next state, after the comparison is made. The algorithm can be executed to continue producing comparisons and eventually the sorted output.

    This function is provided for convenience when there is already a Boolean value that can be used for the comparison. Otherwise, callAsFunction(_ answer: Side) should be preferred for readability.

    Declaration

    Swift

    public func callAsFunction(_ bool: Bool) -> Algorithm

Comparison.Side Definition

Public Instance Interface

  • Answers the comparison with whether or not the left side is less than the right side.

    The returned algorithm will be the algorithm at its next state, after the comparison is made. The algorithm can be executed to continue producing comparisons and eventually the sorted output.

    Declaration

    Swift

    public func callAsFunction() -> Algorithm