This repository showcases how I've implemented auto-resetting iterators in C.
- Git clone this repository.
- Open this repository in VS Code.
- Go to the Run and Debug tab in VS Code and use the dropdown menu at the top to select what example program you want to run.
- Run the program and see the terminal output.
This:
void foo(t_data *data)
{
int x;
int y;
y = 0;
while (y < data->height)
{
x = 0;
while (x < data->width)
{
printf("x: %d, y: %d\n", data->it.x, data->it.y);
x++;
}
y++;
}
}
Becomes this:
void foo(t_data *data)
{
while (iterate_grid(data) != FINISHED)
printf("x: %d, y: %d\n", data->it.x, data->it.y);
}
iterate_grid()
just calls iterate_width()
and iterate_height()
.
This means these iterators are very scalable, since it means you won't ever have to reimplement any subiterators for high-level iterators. In other words, you won't have to reinvent the wheel.
Note that iterators also automatically reset themselves once they reach their FINISHED
state. reset_iterate_grid()
should only be called when you want to return from an iterator while-loop early.