1
1
mirror of https://github.com/github/semantic.git synced 2024-12-03 13:56:04 +03:00
semantic/prototype/Doubt/Matrix.swift

41 lines
857 B
Swift
Raw Normal View History

2015-10-06 00:11:56 +03:00
struct Matrix<A> {
init(width: Int, height: Int, compute: (Int, Int) -> A) {
var values: [Memo<A>] = []
values.reserveCapacity(width * height)
for i in 0..<width {
for j in 0..<height {
values[i + j * height] = Memo<A> { compute(i, j) }
}
}
self.init(width: width, height: height, values: values)
}
2015-10-06 00:11:56 +03:00
let width: Int
let height: Int
let values: [Memo<A>]
subscript (i: Int, j: Int) -> Memo<A>? {
guard i < width && j < height else { return nil }
return values[i + j * height]
}
2015-10-06 00:18:40 +03:00
// MARK: Functor
func map<Other>(transform: A -> Other) -> Matrix<Other> {
return Matrix<Other>(width: width, height: height, values: values.map { $0.map(transform) })
}
// MARK: Implementation details
private init(width: Int, height: Int, values: [Memo<A>]) {
self.width = width
self.height = height
self.values = values
}
2015-10-06 00:11:56 +03:00
}