Made `Matrix<Scalar>` conform to `Collection` (row-wise, just like it already does with `Sequence`)
This commit is contained in:
parent
668b2d26a6
commit
54a059fe91
|
@ -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 {}
|
extension Matrix: Equatable {}
|
||||||
public func ==<T> (lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
|
public func ==<T> (lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
|
||||||
return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid
|
return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid
|
||||||
|
|
Loading…
Reference in New Issue