2019-06-17 19:33:10 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-07-01 12:23:38 +02:00
|
|
|
#include <stddef.h>
|
2019-06-17 19:33:10 +02:00
|
|
|
#include "terminal.h"
|
2019-07-01 12:23:38 +02:00
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
void grid_swap_row(struct grid *grid, int row_a, int row_b, bool initialize);
|
|
|
|
|
struct row *grid_row_alloc(int cols, bool initialize);
|
2019-07-10 16:27:55 +02:00
|
|
|
void grid_row_free(struct row *row);
|
|
|
|
|
|
2019-07-08 13:57:31 +02:00
|
|
|
static inline struct row *
|
2019-08-22 17:33:23 +02:00
|
|
|
_grid_row_maybe_alloc(struct grid *grid, int row_no, bool alloc_if_null)
|
2019-07-08 13:57:31 +02:00
|
|
|
{
|
|
|
|
|
assert(grid->offset >= 0);
|
2019-07-10 16:27:55 +02:00
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
int real_row = (grid->offset + row_no) & (grid->num_rows - 1);
|
2019-07-10 16:27:55 +02:00
|
|
|
struct row *row = grid->rows[real_row];
|
|
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
if (row == NULL && alloc_if_null) {
|
|
|
|
|
row = grid_row_alloc(grid->num_cols, false);
|
2019-07-10 16:27:55 +02:00
|
|
|
grid->rows[real_row] = row;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
assert(row != NULL);
|
2019-07-10 16:27:55 +02:00
|
|
|
return row;
|
2019-07-08 13:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
static inline struct row *
|
|
|
|
|
grid_row(struct grid *grid, int row_no)
|
|
|
|
|
{
|
|
|
|
|
return _grid_row_maybe_alloc(grid, row_no, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct row *
|
|
|
|
|
grid_row_and_alloc(struct grid *grid, int row_no)
|
|
|
|
|
{
|
|
|
|
|
return _grid_row_maybe_alloc(grid, row_no, true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 16:26:36 +02:00
|
|
|
static inline struct row *
|
2019-07-10 16:27:55 +02:00
|
|
|
grid_row_in_view(struct grid *grid, int row_no)
|
2019-07-09 16:26:36 +02:00
|
|
|
{
|
|
|
|
|
assert(grid->view >= 0);
|
|
|
|
|
|
2019-08-22 17:33:23 +02:00
|
|
|
int real_row = (grid->view + row_no) & (grid->num_rows - 1);
|
2019-07-10 16:27:55 +02:00
|
|
|
struct row *row = grid->rows[real_row];
|
|
|
|
|
|
|
|
|
|
assert(row != NULL);
|
|
|
|
|
return row;
|
|
|
|
|
}
|