SortCoordinator

class SortCoordinator : ObservableObject

SortCoordinator coordinates, as the name implies, sorting of arrays using different sorting methods. It is an ObservableObject, being observerd by a View that holds the coordinator as an @ObservedObject to show the state of the sorting in the UI.

SortCoordinator:

  • holds the array to be sorted, giving it to each sort method by calling SortMethod.nextStep().
  • times the sorting process using a Timer
  • publishes the array to the Views so that when the array is updated, view is redrawn.
  • collects the timing results using SortMethod.realAlgorithm(...), to show to the user the time the algoritms take to sort the array without any animations.

SortCoordinator is to be used so that the client (a SwiftUI View):

  1. creates the SortCoordinator object
  2. calls execute() when user is tapping some element in the UI
  3. reacts to the events in the SortCoordinator when the array within changes, by updating the UI
  4. calls stop() if user wants to stop the sorting by tapping in the View.

For details, see the properties and methods in this class as well as the SortMethod protocol which all the sorting methods implement.

  • The data to be sorted is generated to originalArray first, then copied to the array member. This is to make sure that all sorting methods start from exactly the same data. This produces comparable performance metrics, since how the data is organized in the array influences the sorting methods’ performance.

    Declaration

    Swift

    var originalArray: [Int]!
  • The array that is actually used in the sorting. This is also displayed in the UI, the reason why it is @Published.

    Declaration

    Swift

    @Published
    var array: [Int]! { get set }
  • The (current) sorting methods used. Value changes when execution moves from one method to another.

    Declaration

    Swift

    @Published
    var description: String { get set }
  • This table will include the real time performance metrics of the sorting methods after the measuring phase.

    Declaration

    Swift

    @Published
    var performanceTable: [TimingResult] { get set }
  • The currently executing sorthing method reference.

    Declaration

    Swift

    private var currentMethod: SortMethod?
  • A timer is used to control the execution of the sorting.

    Declaration

    Swift

    private var timer: Timer?
  • Holds the current interval used in the timing.

    Declaration

    Swift

    private var timerInterval: Double
  • Is true, if sorting is ongoing, otherwise false.

    Declaration

    Swift

    private var executing: Bool
  • Which of the sorting methods in the sortingMethod array is currently executed.

    Declaration

    Swift

    private var currentMethodIndex: Int
  • All the supported sorting methods are placed in the array before starting the execution.

    Declaration

    Swift

    private var sortingMethods: [SortMethod]
  • The different states of the execution of the sort coordinator.

    See more

    Declaration

    Swift

    enum State
  • The state variable, holding the execution state.

    Declaration

    Swift

    private(set) var state: SortCoordinator.State { get }
  • Gets the count of supported sorting methods

    Declaration

    Swift

    func getCountOfSupportedMethods() -> Int

    Return Value

    The count of implemented sorting methods

  • Gets the name of the currently executing sorting method.

    Declaration

    Swift

    func getName() -> String

    Return Value

    The name of the currently executing sorting method.

  • Prepares the coordinator for sorting.

    Declaration

    Swift

    func prepare(count: Int)

    Parameters

    count

    The number of elements to hold in the array to be sorted.

  • Gets a description for the sorting method by method name

    Declaration

    Swift

    func getDescription(for methodName: String) -> String

    Parameters

    methodName

    The name of the sorting method.

    Return Value

    The description for the sorting method.

  • Gets the sorting method by the method’s name.

    Declaration

    Swift

    func getMethod(for methodName: String) -> SortMethod?

    Parameters

    methodName

    The name of the sorting method.

    Return Value

    The sorting method protocol referring to the method struct, nil if not found.

  • Executes the different sorting methods, using a repeating timer within a closure.

    See also nextStep() and nextMethod() as well as stop(), which all contribute to the state manamement of the coordinator.

    See the State enum values, coordinating the execution of the sorting in different phases.

    Declaration

    Swift

    func execute()
  • Is the coordinator executing or not

    Declaration

    Swift

    func isExecuting() -> Bool

    Return Value

    True if the coordinator is executing the sorting methods.

  • Stops the execution of the sorthing phase, advances to the next phase, if any.

    Declaration

    Swift

    func stop()
  • Executes the next step of any sorting method when animating the methods.

    Declaration

    Swift

    private func nextStep() -> Bool

    Return Value

    Returns true if the sort method finished sorting and the array is now sorted.