Array

extension Array where Element == Int

Declare an extension to Int array to be able to easily prepare an array either

  • with integers from a specified range and count of numbers, or
  • with sequence of integers from 1 to count of numbers.

Author

Antti Juustila

Available where Element == Int

  • Prepare an array with a count of random numbers from a specified range.

    Declaration

    Swift

    mutating func prepare(range: ClosedRange<Int>, count: Int)

    Parameters

    range

    The range of values the array is holding, e.g. -10..-10.

    count

    The count of numbers to generate to the array, randomly.

  • Prepare an array from a specified range sequentially.

    Declaration

    Swift

    mutating func prepare(range: ClosedRange<Int>)

    Parameters

    range

    The range of values the array is holding, e.g. -10..-10.

  • Prepare an array of numbers from 1 to count sequentially.

    Declaration

    Swift

    mutating func prepare(count: Int)

    Parameters

    count

    The count of numbers to generate.

  • Does the sort operation to the array, depending on the operation parameter. If operation is .moveValue, moves the value in the “first” item to index in the second item. If operation is .swap, swaps the values indicated by the indexes in first and second.

    Declaration

    Swift

    mutating func handleSortOperation(operation: SwappedItems)

    Parameters

    operation

    The indexes in the array to operate on, depending on the operation type.

  • Utility function to test if an array is sorted.

    Declaration

    Swift

    func isSorted() -> Bool
  • Utility function used in testing to check if arrays contain the same elements in the same order.

    Declaration

    Swift

    func containsSameElements(as other: [Element]) -> Bool