I want to initialize a static array with 0 except at some indices where I want a value. It's going to be sparse and I'd like to specifies those value as a list. I came up with this:
const std = @import("std");
fn make_tuple(comptime LHST: type, comptime RHST: type) type {
return struct { LHST, RHST };
}
const initial_values = [_]make_tuple(usize, u32) {
.{ 0x02, 0xFF },
.{ 0x06, 0x42 },
};
fn init_csr(comptime T: type, values: []const make_tuple(usize, T)) [4096]T {
var tmp = [_]T{ 0 } ** 4096;
for (values) |value| {
tmp[value[0]] = value[1];
}
return tmp;
}
pub fn main() void {
var arr = init_csr(u32, &initial_values);
std.debug.print("{any}", .{ arr });
}