Made `Matrix<Scalar>` conform to `Collection` (row-wise, just like it already does with `Sequence`)

This commit is contained in:
Vincent Esche 2019-08-06 13:42:28 +02:00
parent 668b2d26a6
commit 54a059fe91
1 changed files with 24 additions and 0 deletions

View File

@ -206,6 +206,30 @@ extension Matrix: Sequence {
}
}
// MARK: - Collection
extension Matrix: Collection {
public subscript(_ row: Int) -> ArraySlice<Scalar> {
get {
let startIndex = row * columns
let endIndex = startIndex + columns
return self.grid[startIndex..<endIndex]
}
}
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return self.rows
}
public func index(after i: Int) -> Int {
return i + 1
}
}
extension Matrix: Equatable {}
public func ==<T> (lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid