I am looking for a way to iterate over the rows of a row-major order matrix, without any copying or additional allocations.
So far the matrix data lives on the heap.
const rows = 4;
const columns = 8;
m = Matrix(f32).random(allocator, rows, columns);
defer m.deinit();
From what I understand heap allocations are relatively expensive. So I was thinking of implementing a RowIterator, whos next() function would return a stack allocated Matrix who's data is a slice to a row in the original matrix.
var row_iterator = m.iter_rows();
while (row_iterator.next()) |row| {
// Do stuff
}
The assumption is that the programmer ensures that the matrix is not deinitalized, while iterating over the rows.
Is it a bad practise to have a datastructure which can be both stack or heap allocated, depending on how you initialize it?