Skip to content

Instantly share code, notes, and snippets.

@Lisoph
Created September 26, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lisoph/e51af8be3dca4ffdbe0d2c58d6395a03 to your computer and use it in GitHub Desktop.
Save Lisoph/e51af8be3dca4ffdbe0d2c58d6395a03 to your computer and use it in GitHub Desktop.
Zig generic Matrix struct
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