Skip to content

Commit 9677b8c

Browse files
committed
Fixed indenting, and changed a scope modifier on the array property from public to fileprivate.
1 parent 8c2a3d2 commit 9677b8c

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

Array2D/Array2D.playground/Contents.swift

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@
44
Performance is always O(1).
55
*/
66
public struct Array2D<T> {
7-
public let columns: Int
8-
public let rows: Int
9-
public var array: [T]
10-
11-
public init(columns: Int, rows: Int, initialValue: T) {
12-
self.columns = columns
13-
self.rows = rows
14-
array = [T](repeating: initialValue, count: rows*columns)
7+
public let columns: Int
8+
public let rows: Int
9+
fileprivate var array: [T]
10+
11+
public init(columns: Int, rows: Int, initialValue: T) {
12+
self.columns = columns
13+
self.rows = rows
14+
array = [T](repeating: initialValue, count: rows*columns)
15+
}
16+
17+
public subscript(column: Int, row: Int) -> T {
18+
get {
19+
if isValidSubscript(column: column, row: row) {
20+
return array[row*columns + column]
21+
} else {
22+
fatalError("Not a valid subscript")
23+
}
1524
}
16-
17-
public subscript(column: Int, row: Int) -> T {
18-
get {
19-
if isValidSubscript(column: column, row: row) {
20-
return array[row*columns + column]
21-
} else {
22-
fatalError("Not a valid subscript")
23-
}
24-
}
25-
set {
26-
if isValidSubscript(column: column, row: row) {
27-
array[row*columns + column] = newValue
28-
} else {
29-
fatalError("Not a valid subscript")
30-
}
31-
}
32-
}
33-
34-
private func isValidSubscript(column: Int, row: Int) -> Bool {
35-
return column < columns && row < rows
25+
set {
26+
if isValidSubscript(column: column, row: row) {
27+
array[row*columns + column] = newValue
28+
} else {
29+
fatalError("Not a valid subscript")
30+
}
3631
}
32+
}
33+
34+
private func isValidSubscript(column: Int, row: Int) -> Bool {
35+
return column < columns && row < rows
36+
}
3737
}
3838

3939

@@ -62,13 +62,13 @@ print(matrix.array)
6262

6363
// print out the 2D array with a reference around the grid
6464
for i in 0..<matrix.rows {
65-
print("[", terminator: "")
66-
for j in 0..<matrix.columns {
67-
if j == matrix.columns - 1 {
68-
print("\(matrix[j, i])", terminator: "")
69-
} else {
70-
print("\(matrix[j, i]) ", terminator: "")
71-
}
65+
print("[", terminator: "")
66+
for j in 0..<matrix.columns {
67+
if j == matrix.columns - 1 {
68+
print("\(matrix[j, i])", terminator: "")
69+
} else {
70+
print("\(matrix[j, i]) ", terminator: "")
7271
}
73-
print("]")
72+
}
73+
print("]")
7474
}

Array2D/README.markdown

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,34 @@ So instead let's create our own type that acts like a 2-D array and that is more
6767
public struct Array2D<T> {
6868
public let columns: Int
6969
public let rows: Int
70-
private var array: [T]
71-
70+
fileprivate var array: [T]
71+
7272
public init(columns: Int, rows: Int, initialValue: T) {
7373
self.columns = columns
7474
self.rows = rows
75-
array = .init(repeating: initialValue, count: rows*columns)
75+
array = [T](repeating: initialValue, count: rows*columns)
7676
}
77-
77+
7878
public subscript(column: Int, row: Int) -> T {
7979
get {
80-
return array[row*columns + column]
80+
if isValidSubscript(column: column, row: row) {
81+
return array[row*columns + column]
82+
} else {
83+
fatalError("Not a valid subscript")
84+
}
8185
}
8286
set {
83-
array[row*columns + column] = newValue
87+
if isValidSubscript(column: column, row: row) {
88+
array[row*columns + column] = newValue
89+
} else {
90+
fatalError("Not a valid subscript")
91+
}
8492
}
8593
}
94+
95+
private func isValidSubscript(column: Int, row: Int) -> Bool {
96+
return column < columns && row < rows
97+
}
8698
}
8799
```
88100

0 commit comments

Comments
 (0)