2019-06-17 19:33:10 +02:00
|
|
|
#include "grid.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2019-06-17 21:15:20 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2019-06-19 10:27:31 +02:00
|
|
|
#define LOG_MODULE "grid"
|
2019-07-03 20:21:03 +02:00
|
|
|
#define LOG_ENABLE_DBG 0
|
2019-06-19 10:27:31 +02:00
|
|
|
#include "log.h"
|
2019-07-08 13:57:31 +02:00
|
|
|
#if 0
|
2019-07-01 12:23:38 +02:00
|
|
|
struct cell *
|
2019-07-03 10:45:49 +02:00
|
|
|
grid_get_range(struct grid *grid, int start, int *length)
|
2019-07-01 12:23:38 +02:00
|
|
|
{
|
|
|
|
|
#define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
|
assert(*length <= grid->size);
|
|
|
|
|
|
2019-07-03 10:45:49 +02:00
|
|
|
int real_start = (grid->offset + start) % grid->size;
|
|
|
|
|
if (real_start < 0)
|
|
|
|
|
real_start += grid->size;
|
|
|
|
|
assert(real_start >= 0);
|
2019-07-01 12:23:38 +02:00
|
|
|
assert(real_start < grid->size);
|
|
|
|
|
|
|
|
|
|
*length = min(*length, grid->size - real_start);
|
|
|
|
|
assert(real_start + *length <= grid->size);
|
|
|
|
|
|
|
|
|
|
return &grid->cells[real_start];
|
|
|
|
|
#undef min
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2019-07-07 17:10:15 +02:00
|
|
|
grid_memclear(struct grid *grid, int start, int length)
|
2019-07-01 12:23:38 +02:00
|
|
|
{
|
2019-07-03 10:45:49 +02:00
|
|
|
int left = length;
|
2019-07-01 12:23:38 +02:00
|
|
|
while (left > 0) {
|
2019-07-03 10:45:49 +02:00
|
|
|
int count = left;
|
2019-07-01 12:23:38 +02:00
|
|
|
struct cell *cells = grid_get_range(grid, start, &count);
|
|
|
|
|
|
|
|
|
|
assert(count > 0);
|
|
|
|
|
assert(count <= left);
|
|
|
|
|
|
2019-07-07 17:10:15 +02:00
|
|
|
memset(cells, 0, count * sizeof(cells[0]));
|
2019-07-01 12:23:38 +02:00
|
|
|
|
|
|
|
|
left -= count;
|
|
|
|
|
start += count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 19:18:52 +02:00
|
|
|
|
|
|
|
|
void
|
2019-07-03 10:45:49 +02:00
|
|
|
grid_memmove(struct grid *grid, int dst, int src, int length)
|
2019-07-01 19:18:52 +02:00
|
|
|
{
|
2019-07-07 17:10:30 +02:00
|
|
|
/* Fast path, we can move everything in one swoop */
|
|
|
|
|
{
|
|
|
|
|
int count = length;
|
|
|
|
|
struct cell *dst_cells = grid_get_range(grid, dst, &count);
|
|
|
|
|
if (count == length) {
|
|
|
|
|
struct cell *src_cells = grid_get_range(grid, src, &count);
|
|
|
|
|
if (count == length) {
|
|
|
|
|
memmove(dst_cells, src_cells, length * sizeof(dst_cells[0]));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 10:45:49 +02:00
|
|
|
int left = length;
|
|
|
|
|
int copy_idx = 0;
|
2019-07-01 19:18:52 +02:00
|
|
|
struct cell copy[left];
|
|
|
|
|
|
|
|
|
|
while (left > 0) {
|
2019-07-03 10:45:49 +02:00
|
|
|
int count = left;
|
2019-07-01 19:18:52 +02:00
|
|
|
struct cell *src_cells = grid_get_range(grid, src, &count);
|
|
|
|
|
|
|
|
|
|
memcpy(©[copy_idx], src_cells, count * sizeof(copy[0]));
|
|
|
|
|
|
|
|
|
|
left -= count;
|
|
|
|
|
src += count;
|
|
|
|
|
copy_idx += count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
left = length;
|
|
|
|
|
copy_idx = 0;
|
|
|
|
|
|
|
|
|
|
while (left > 0) {
|
2019-07-03 10:45:49 +02:00
|
|
|
int count = left;
|
2019-07-01 19:18:52 +02:00
|
|
|
struct cell *dst_cells = grid_get_range(grid, dst, &count);
|
|
|
|
|
|
|
|
|
|
memcpy(dst_cells, ©[copy_idx], count * sizeof(copy[0]));
|
|
|
|
|
|
|
|
|
|
left -= count;
|
|
|
|
|
dst += count;
|
|
|
|
|
copy_idx += count;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-08 13:57:31 +02:00
|
|
|
#endif
|