1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 18:23:44 +03:00

Extract row-major ordering into a private function.

This commit is contained in:
Rob Rix 2015-10-06 14:15:21 -04:00
parent 934390a0d5
commit 092087fc54

View File

@ -5,16 +5,7 @@
/// Values are retrieved by subscripting with row/column indices. Out-of-bound indices produce `nil` values, rather than asserting.
public struct Matrix<A> {
public init(width: Int, height: Int, compute: (Int, Int) -> A) {
var values: [Memo<A>] = []
values.reserveCapacity(width * height)
for j in 0..<height {
for i in 0..<width {
values.append(Memo<A> { compute(i, j) })
}
}
self.init(width: width, height: height, values: values)
self.init(width: width, height: height, values: constructRowMajor(width, height: height, forEach: compute >>> Memo.init))
}
public let width: Int
@ -43,3 +34,14 @@ public struct Matrix<A> {
self.values = values
}
}
private func constructRowMajor<A>(width: Int, height: Int, @noescape forEach: (Int, Int) -> A) -> [A] {
var values: [A] = []
values.reserveCapacity(width * height)
for j in 0..<height {
for i in 0..<width {
values.append(forEach(i, j))
}
}
return values
}