Skip to content

Commit c4d39ec

Browse files
committed
Updated the subscript check to use preconditions instead of a custom function.
1 parent 9677b8c commit c4d39ec

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

Array2D/Array2D.playground/Contents.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,21 @@ public struct Array2D<T> {
1111
public init(columns: Int, rows: Int, initialValue: T) {
1212
self.columns = columns
1313
self.rows = rows
14-
array = [T](repeating: initialValue, count: rows*columns)
14+
array = .init(repeating: initialValue, count: rows*columns)
1515
}
1616

1717
public subscript(column: Int, row: Int) -> T {
1818
get {
19-
if isValidSubscript(column: column, row: row) {
20-
return array[row*columns + column]
21-
} else {
22-
fatalError("Not a valid subscript")
23-
}
19+
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
20+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
21+
return array[row*columns + column]
2422
}
2523
set {
26-
if isValidSubscript(column: column, row: row) {
27-
array[row*columns + column] = newValue
28-
} else {
29-
fatalError("Not a valid subscript")
30-
}
24+
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
25+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
26+
array[row*columns + column] = newValue
3127
}
3228
}
33-
34-
private func isValidSubscript(column: Int, row: Int) -> Bool {
35-
return column < columns && row < rows
36-
}
3729
}
3830

3931

Array2D/Array2D.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ public struct Array2D<T> {
77
public let columns: Int
88
public let rows: Int
99
fileprivate var array: [T]
10-
10+
1111
public init(columns: Int, rows: Int, initialValue: T) {
1212
self.columns = columns
1313
self.rows = rows
1414
array = .init(repeating: initialValue, count: rows*columns)
1515
}
16-
16+
1717
public subscript(column: Int, row: Int) -> T {
1818
get {
19+
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
20+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
1921
return array[row*columns + column]
2022
}
2123
set {
22-
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
2324
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
24-
25+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
2526
array[row*columns + column] = newValue
2627
}
2728
}

Array2D/README.markdown

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,21 @@ public struct Array2D<T> {
7272
public init(columns: Int, rows: Int, initialValue: T) {
7373
self.columns = columns
7474
self.rows = rows
75-
array = [T](repeating: initialValue, count: rows*columns)
75+
array = .init(repeating: initialValue, count: rows*columns)
7676
}
7777

7878
public subscript(column: Int, row: Int) -> T {
7979
get {
80-
if isValidSubscript(column: column, row: row) {
81-
return array[row*columns + column]
82-
} else {
83-
fatalError("Not a valid subscript")
84-
}
80+
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
81+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
82+
return array[row*columns + column]
8583
}
8684
set {
87-
if isValidSubscript(column: column, row: row) {
88-
array[row*columns + column] = newValue
89-
} else {
90-
fatalError("Not a valid subscript")
91-
}
85+
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
86+
precondition(row < rows, "Row \(row) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")
87+
array[row*columns + column] = newValue
9288
}
9389
}
94-
95-
private func isValidSubscript(column: Int, row: Int) -> Bool {
96-
return column < columns && row < rows
97-
}
9890
}
9991
```
10092

0 commit comments

Comments
 (0)