Created
September 26, 2018 06:26
-
-
Save Lisoph/e51af8be3dca4ffdbe0d2c58d6395a03 to your computer and use it in GitHub Desktop.
Zig generic Matrix struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn main() void { | |
const foo = Matrix(f32, 4, 4).identity(); | |
} | |
fn Matrix(comptime Scalar: type, comptime Width: usize, comptime Height: usize) type { | |
return struct { | |
const Self = @This(); | |
pub elems: [Width * Height]Scalar, | |
pub fn identity() Self { | |
comptime { | |
var x: usize = 0; | |
var y: usize = 0; | |
var mat: Self = undefined; | |
inline while (y < Height) : (y += 1) { | |
inline while (x < Width) : (x += 1) { | |
comptime const idx = y * Width + x; | |
mat.elems[idx] = if (x == y) 1 else 0; | |
} | |
} | |
return mat; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment