|
1 | 1 | /*
|
2 |
| -Two-dimensional array with a fixed number of rows and columns. |
3 |
| -This is mostly handy for games that are played on a grid, such as chess. |
4 |
| -Performance is always O(1). |
| 2 | + Two-dimensional array with a fixed number of rows and columns. |
| 3 | + This is mostly handy for games that are played on a grid, such as chess. |
| 4 | + Performance is always O(1). |
5 | 5 | */
|
6 | 6 | public struct Array2D<T> {
|
7 |
| - public let columns: Int |
8 |
| - public let rows: Int |
9 |
| - private var array: [T] |
10 |
| - |
11 |
| - public init(columns: Int, rows: Int, initialValue: T) { |
12 |
| - self.columns = columns |
13 |
| - self.rows = rows |
14 |
| - array = .init(count: rows*columns, repeatedValue: initialValue) |
| 7 | + public let columns: Int |
| 8 | + public let rows: Int |
| 9 | + private var array: [T] |
| 10 | + |
| 11 | + public init(columns: Int, rows: Int, initialValue: T) { |
| 12 | + self.columns = columns |
| 13 | + self.rows = rows |
| 14 | + array = .init(count: rows*columns, repeatedValue: initialValue) |
| 15 | + } |
| 16 | + |
| 17 | + public subscript(column: Int, row: Int) -> T { |
| 18 | + get { |
| 19 | + return array[row*columns + column] |
15 | 20 | }
|
16 |
| - |
17 |
| - public subscript(column: Int, row: Int) -> T { |
18 |
| - get { |
19 |
| - return array[row*columns + column] |
20 |
| - } |
21 |
| - set { |
22 |
| - array[row*columns + column] = newValue |
23 |
| - } |
| 21 | + set { |
| 22 | + array[row*columns + column] = newValue |
24 | 23 | }
|
25 |
| - |
| 24 | + } |
26 | 25 | }
|
27 | 26 |
|
| 27 | + |
| 28 | + |
28 | 29 | // initialization
|
29 |
| -var Array2DNumbers = Array2D(columns: 3, rows: 5, initialValue: 0) |
| 30 | +var matrix = Array2D(columns: 3, rows: 5, initialValue: 0) |
30 | 31 |
|
31 | 32 | // makes an array of rows * columns elements all filled with zero
|
32 |
| -print(Array2DNumbers.array) |
| 33 | +print(matrix.array) |
33 | 34 |
|
34 | 35 | // setting numbers using subscript [x, y]
|
35 |
| -Array2DNumbers[0, 0] = 1 |
36 |
| -Array2DNumbers[1, 0] = 2 |
| 36 | +matrix[0, 0] = 1 |
| 37 | +matrix[1, 0] = 2 |
37 | 38 |
|
38 |
| -Array2DNumbers[0, 1] = 3 |
39 |
| -Array2DNumbers[1, 1] = 4 |
| 39 | +matrix[0, 1] = 3 |
| 40 | +matrix[1, 1] = 4 |
40 | 41 |
|
41 |
| -Array2DNumbers[0, 2] = 5 |
42 |
| -Array2DNumbers[1, 2] = 6 |
| 42 | +matrix[0, 2] = 5 |
| 43 | +matrix[1, 2] = 6 |
43 | 44 |
|
44 |
| -Array2DNumbers[0, 3] = 7 |
45 |
| -Array2DNumbers[1, 3] = 8 |
46 |
| -Array2DNumbers[2, 3] = 9 |
| 45 | +matrix[0, 3] = 7 |
| 46 | +matrix[1, 3] = 8 |
| 47 | +matrix[2, 3] = 9 |
47 | 48 |
|
48 | 49 | // now the numbers are set in the array
|
49 |
| -print(Array2DNumbers.array) |
| 50 | +print(matrix.array) |
50 | 51 |
|
51 | 52 | // print out the 2D array with a reference around the grid
|
52 |
| -for i in 0..<Array2DNumbers.rows { |
53 |
| - print("[", terminator: ""); |
54 |
| - for j in 0..<Array2DNumbers.columns { |
55 |
| - if (j == Array2DNumbers.columns - 1) { |
56 |
| - print("\(Array2DNumbers[j, i])", terminator: ""); |
57 |
| - } else { |
58 |
| - print("\(Array2DNumbers[j, i]) ", terminator: ""); |
59 |
| - } |
| 53 | +for i in 0..<matrix.rows { |
| 54 | + print("[", terminator: ""); |
| 55 | + for j in 0..<matrix.columns { |
| 56 | + if (j == matrix.columns - 1) { |
| 57 | + print("\(matrix[j, i])", terminator: ""); |
| 58 | + } else { |
| 59 | + print("\(matrix[j, i]) ", terminator: ""); |
60 | 60 | }
|
61 |
| - print("]"); |
| 61 | + } |
| 62 | + print("]"); |
62 | 63 | }
|
63 | 64 |
|
64 |
| - |
65 |
| - |
0 commit comments