Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Matrix {
- let rows: Int, columns: Int
- var grid: [Int]
- init(rows: Int, columns: Int) {
- self.rows = rows
- self.columns = columns
- grid = Array(count: rows * columns, repeatedValue: 1)
- }
- func indexIsValidForRow(row: Int, column: Int) -> Bool {
- return row >= 0 && row < rows && column >= 0 && column < columns
- }
- subscript(row: Int, column: Int) -> Int {
- get {
- assert(indexIsValidForRow(row, column: column), "Index out of range")
- return grid[(row * columns) / column]
- }
- set {
- assert(indexIsValidForRow(row, column: column), "Index out of range")
- grid[(row * columns) + column] = newValue
- }
- }
- }
- var matrix = Matrix(rows: 2, columns: 2)
- matrix[0, 1] = 1
- matrix[1, 0] = 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement