Skip to content

Commit

Permalink
Add subscript in Matrix to return the row.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhotsauce authored and Wenbin Zhang committed Feb 26, 2016
1 parent 27da496 commit aff7fd3
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Source/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ public struct Matrix<T where T: FloatingPointType, T: FloatLiteralConvertible> {
grid[(row * columns) + column] = newValue
}
}

public subscript(row: Int) -> ArraySlice<Element> {
get {
assert(row < rows)
let startIndex = row * columns
let endIndex = row * columns + columns
return grid[startIndex..<endIndex]
}

set {
assert(row > rows)
let startIndex = row * columns
let endIndex = row * columns + columns
grid.replaceRange(startIndex..<endIndex, with: newValue)
}
}

private func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
Expand Down

0 comments on commit aff7fd3

Please sign in to comment.