Created
September 14, 2019 11:25
-
-
Save yakubpashask/d791b2737a1cd3c409c3bf3b020b1d94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Array2D<T> { | |
let columns: Int | |
let rows: Int | |
var array: Array<T?> | |
init(columns: Int, rows: Int) { | |
self.columns = columns | |
self.rows = rows | |
array = Array<T?>(count:rows * columns, repeatedValue: nil) | |
} | |
subscript(column: Int, row: Int) -> T? { | |
get { | |
return array[(row * columns) + column] | |
} | |
set(newValue) { | |
array[(row * columns) + column] = newValue | |
} | |
} | |
} | |
//Usage : | |
/* | |
let boolArray = Array2D<Bool>(columns: totalColoumns, rows: totalRows) | |
boolArray[0,1] = false | |
boolArray[0,2] = true//..etc | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment