kodecocodes/swift-algorithm-club

Array2D - Enhance to include ability to create 2D array using either [col][row] or [row][col] subscripting.

l-rettberg opened this issue · 0 comments

Brief Intro

The Array2D algorithm is based on using column then row, ie [col][row] when accessing cells. Not sure if that is the commonly used approach? I thought the most common was row, column, ie [row][col], but perhaps that's really up to the developer.

More Details

Suggest updating the algorithm to include a boolean flag that the caller can define whether the 2D array should be created as [col][row] or [row][col] for most flexibility.

import Foundation

/// A flexible 2D array class that can be configured to use either row-major or column-major access.
public class FlexibleArray2D {
private var data: [[Int]]
private var isColumnMajor: Bool

/// Initializes the 2D array.
/// - Parameters:
///   - rows: The number of rows in the 2D array.
///   - columns: The number of columns in the 2D array.
///   - isColumnMajor: A flag that determines the access order:
///     - true: column-major order ([col][row])
///     - false: row-major order ([row][col])
public init(rows: Int, columns: Int, isColumnMajor: Bool) {
    self.isColumnMajor = isColumnMajor
    self.data = Array(repeating: Array(repeating: 0, count: columns), count: rows)
}

/// Accessor method for setting an element.
/// - Parameters:
///   - row: The row index.
///   - col: The column index.
public func setValue(_ value: Int, atRow row: Int, col: Int) {
    if isColumnMajor {
        data[col][row] = value  // column-major order
    } else {
        data[row][col] = value  // row-major order
    }
}

/// Accessor method for getting an element.
/// - Parameters:
///   - row: The row index.
///   - col: The column index.
public func getValue(atRow row: Int, col: Int) -> Int {
    if isColumnMajor {
        return data[col][row]  // column-major order
    } else {
        return data[row][col]  // row-major order
    }
}

/// A method to print the 2D array.
public func printArray() {
    for row in data {
        print(row)
    }
}

}

// Example usage:

// Create a 2D array with row-major order ([row][col])
let rowMajorArray = FlexibleArray2D(rows: 3, columns: 3, isColumnMajor: false)
rowMajorArray.setValue(1, atRow: 0, col: 0)
rowMajorArray.setValue(2, atRow: 0, col: 1)
rowMajorArray.setValue(3, atRow: 0, col: 2)
rowMajorArray.setValue(4, atRow: 1, col: 0)
rowMajorArray.setValue(5, atRow: 1, col: 1)
rowMajorArray.setValue(6, atRow: 1, col: 2)
rowMajorArray.setValue(7, atRow: 2, col: 0)
rowMajorArray.setValue(8, atRow: 2, col: 1)
rowMajorArray.setValue(9, atRow: 2, col: 2)

print("Row-major order:")
rowMajorArray.printArray()

// Create a 2D array with column-major order ([col][row])
let columnMajorArray = FlexibleArray2D(rows: 3, columns: 3, isColumnMajor: true)
columnMajorArray.setValue(1, atRow: 0, col: 0)
columnMajorArray.setValue(2, atRow: 1, col: 0)
columnMajorArray.setValue(3, atRow: 2, col: 0)
columnMajorArray.setValue(4, atRow: 0, col: 1)
columnMajorArray.setValue(5, atRow: 1, col: 1)
columnMajorArray.setValue(6, atRow: 2, col: 1)
columnMajorArray.setValue(7, atRow: 0, col: 2)
columnMajorArray.setValue(8, atRow: 1, col: 2)
columnMajorArray.setValue(9, atRow: 2, col: 2)

print("Column-major order:")
columnMajorArray.printArray()