Browse Source

Add type alias for size_t/ssize_t to USz/ISz

master
cancel 6 years ago
parent
commit
5b6174b6a9
  1. 2
      base.h
  2. 112
      field.c
  3. 24
      field.h
  4. 28
      sim.c

2
base.h

@ -18,6 +18,8 @@ typedef uint32_t U32;
typedef int32_t I32;
typedef uint64_t U64;
typedef int64_t I64;
typedef size_t USz;
typedef ssize_t ISz;
typedef struct {
Term* buffer;

112
field.c

@ -7,18 +7,18 @@ void field_init(Field* f) {
f->width = 0;
}
void field_init_fill(Field* f, size_t height, size_t width, Term fill_char) {
void field_init_fill(Field* f, USz height, USz width, Term fill_char) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
size_t num_cells = height * width;
USz num_cells = height * width;
f->buffer = malloc(num_cells * sizeof(Term));
memset(f->buffer, fill_char, num_cells);
f->height = (U16)height;
f->width = (U16)width;
}
void field_resize_raw(Field* f, size_t height, size_t width) {
void field_resize_raw(Field* f, USz height, USz width) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
size_t cells = height * width;
USz cells = height * width;
f->buffer = realloc(f->buffer, cells * sizeof(Term));
f->height = (U16)height;
f->width = (U16)width;
@ -26,37 +26,36 @@ void field_resize_raw(Field* f, size_t height, size_t width) {
void field_deinit(Field* f) { free(f->buffer); }
void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
size_t dest_y, size_t dest_x, size_t height,
size_t width) {
size_t src_height = src->height;
size_t src_width = src->width;
size_t dest_height = dest->height;
size_t dest_width = dest->width;
void field_copy_subrect(Field* src, Field* dest, USz src_y, USz src_x,
USz dest_y, USz dest_x, USz height, USz width) {
USz src_height = src->height;
USz src_width = src->width;
USz dest_height = dest->height;
USz dest_width = dest->width;
if (src_height <= src_y || src_width <= src_x || dest_height <= dest_y ||
dest_width <= dest_x)
return;
size_t ny_0 = src_height - src_y;
size_t ny_1 = dest_height - dest_y;
size_t ny = height;
USz ny_0 = src_height - src_y;
USz ny_1 = dest_height - dest_y;
USz ny = height;
if (ny_0 < ny)
ny = ny_0;
if (ny_1 < ny)
ny = ny_1;
if (ny == 0)
return;
size_t row_copy_0 = src_width - src_x;
size_t row_copy_1 = dest_width - dest_x;
size_t row_copy = width;
USz row_copy_0 = src_width - src_x;
USz row_copy_1 = dest_width - dest_x;
USz row_copy = width;
if (row_copy_0 < row_copy)
row_copy = row_copy_0;
if (row_copy_1 < row_copy)
row_copy = row_copy_1;
size_t copy_bytes = row_copy * sizeof(Term);
USz copy_bytes = row_copy * sizeof(Term);
Term* src_p = src->buffer + src_y * src_width + src_x;
Term* dest_p = dest->buffer + dest_y * dest_width + dest_x;
size_t src_stride;
size_t dest_stride;
USz src_stride;
USz dest_stride;
if (src_y >= dest_y) {
src_stride = src_width;
dest_stride = dest_width;
@ -66,7 +65,7 @@ void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
src_stride = -src_width;
dest_stride = -dest_width;
}
size_t iy = 0;
USz iy = 0;
for (;;) {
memmove(dest_p, src_p, copy_bytes);
++iy;
@ -77,25 +76,25 @@ void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
}
}
void field_fill_subrect(Field* f, size_t y, size_t x, size_t height,
size_t width, Term fill_char) {
size_t f_height = f->height;
size_t f_width = f->width;
void field_fill_subrect(Field* f, USz y, USz x, USz height, USz width,
Term fill_char) {
USz f_height = f->height;
USz f_width = f->width;
if (y >= f_height || x >= f_width)
return;
size_t rows_0 = f_height - y;
size_t rows = height;
USz rows_0 = f_height - y;
USz rows = height;
if (rows_0 < rows)
rows = rows_0;
if (rows == 0)
return;
size_t columns_0 = f_width - x;
size_t columns = width;
USz columns_0 = f_width - x;
USz columns = width;
if (columns_0 < columns)
columns = columns_0;
size_t fill_bytes = columns * sizeof(Term);
USz fill_bytes = columns * sizeof(Term);
Term* p = f->buffer + y * f_width + x;
size_t iy = 0;
USz iy = 0;
for (;;) {
memset(p, fill_char, fill_bytes);
++iy;
@ -105,41 +104,40 @@ void field_fill_subrect(Field* f, size_t y, size_t x, size_t height,
}
}
Term field_peek(Field* f, size_t y, size_t x) {
size_t f_height = f->height;
size_t f_width = f->width;
Term field_peek(Field* f, USz y, USz x) {
USz f_height = f->height;
USz f_width = f->width;
assert(y < f_height && x < f_width);
if (y >= f_height || x >= f_width)
return '\0';
return f->buffer[y * f_width + x];
}
Term field_peek_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
ssize_t offs_x) {
ssize_t f_height = f->height;
ssize_t f_width = f->width;
ssize_t y0 = (ssize_t)y + (ssize_t)offs_y;
ssize_t x0 = (ssize_t)x + (ssize_t)offs_x;
Term field_peek_relative(Field* f, USz y, USz x, ISz offs_y, ISz offs_x) {
ISz f_height = f->height;
ISz f_width = f->width;
ISz y0 = (ISz)y + (ISz)offs_y;
ISz x0 = (ISz)x + (ISz)offs_x;
if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0)
return '.';
return f->buffer[y0 * f_width + x0];
}
void field_poke(Field* f, size_t y, size_t x, Term term) {
size_t f_height = f->height;
size_t f_width = f->width;
void field_poke(Field* f, USz y, USz x, Term term) {
USz f_height = f->height;
USz f_width = f->width;
assert(y < f_height && x < f_width);
if (y >= f_height || x >= f_width)
return;
f->buffer[y * f_width + x] = term;
}
void field_poke_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
ssize_t offs_x, Term term) {
ssize_t f_height = f->height;
ssize_t f_width = f->width;
ssize_t y0 = (ssize_t)y + (ssize_t)offs_y;
ssize_t x0 = (ssize_t)x + (ssize_t)offs_x;
void field_poke_relative(Field* f, USz y, USz x, ISz offs_y, ISz offs_x,
Term term) {
ISz f_height = f->height;
ISz f_width = f->width;
ISz y0 = (ISz)y + (ISz)offs_y;
ISz x0 = (ISz)x + (ISz)offs_x;
if (y0 >= f_height || x0 >= f_width || y0 < 0 || x0 < 0)
return;
f->buffer[y0 * f_width + x0] = term;
@ -150,14 +148,14 @@ static inline bool term_char_is_valid(char c) { return c >= '#' && c <= '~'; }
void field_fput(Field* f, FILE* stream) {
enum { Column_buffer_count = 4096 };
char out_buffer[Column_buffer_count];
size_t f_height = f->height;
size_t f_width = f->width;
USz f_height = f->height;
USz f_width = f->width;
Term* f_buffer = f->buffer;
if (f_width > Column_buffer_count - 2)
return;
for (size_t iy = 0; iy < f_height; ++iy) {
for (USz iy = 0; iy < f_height; ++iy) {
Term* row_p = f_buffer + f_width * iy;
for (size_t ix = 0; ix < f_width; ++ix) {
for (USz ix = 0; ix < f_width; ++ix) {
char c = row_p[ix];
out_buffer[ix] = term_char_is_valid(c) ? c : '!';
}
@ -174,8 +172,8 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
}
enum { Bufsize = 4096 };
char buf[Bufsize];
size_t first_row_columns = 0;
size_t rows = 0;
USz first_row_columns = 0;
USz rows = 0;
for (;;) {
char* s = fgets(buf, Bufsize, file);
if (s == NULL)
@ -184,7 +182,7 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
fclose(file);
return Field_load_error_too_many_rows;
}
size_t len = strlen(buf);
USz len = strlen(buf);
if (len == Bufsize - 1 && buf[len - 1] != '\n' && !feof(file)) {
fclose(file);
return Field_load_error_too_many_columns;
@ -211,7 +209,7 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
}
field_resize_raw(field, rows + 1, first_row_columns);
Term* rowbuff = field->buffer + first_row_columns * rows;
for (size_t i = 0; i < len; ++i) {
for (USz i = 0; i < len; ++i) {
char c = buf[i];
rowbuff[i] = term_char_is_valid(c) ? c : '.';
}

24
field.h

@ -2,20 +2,18 @@
#include "base.h"
void field_init(Field* f);
void field_init_fill(Field* f, size_t height, size_t width, Term fill_char);
void field_resize_raw(Field* f, size_t height, size_t width);
void field_init_fill(Field* f, USz height, USz width, Term fill_char);
void field_resize_raw(Field* f, USz height, USz width);
void field_deinit(Field* f);
void field_copy_subrect(Field* src, Field* dest, size_t src_y, size_t src_x,
size_t dest_y, size_t dest_x, size_t height,
size_t width);
void field_fill_subrect(Field* f, size_t y, size_t x, size_t height,
size_t width, Term fill_char);
Term field_peek(Field* f, size_t y, size_t x);
Term field_peek_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
ssize_t offs_x);
void field_poke(Field* f, size_t y, size_t x, Term term);
void field_poke_relative(Field* f, size_t y, size_t x, ssize_t offs_y,
ssize_t offs_x, Term term);
void field_copy_subrect(Field* src, Field* dest, USz src_y, USz src_x,
USz dest_y, USz dest_x, USz height, USz width);
void field_fill_subrect(Field* f, USz y, USz x, USz height, USz width,
Term fill_char);
Term field_peek(Field* f, USz y, USz x);
Term field_peek_relative(Field* f, USz y, USz x, ISz offs_y, ISz offs_x);
void field_poke(Field* f, USz y, USz x, Term term);
void field_poke_relative(Field* f, USz y, USz x, ISz offs_y, ISz offs_x,
Term term);
void field_fput(Field* f, FILE* stream);

28
sim.c

@ -9,8 +9,8 @@ static Term const indexed_terms[] = {
enum { Terms_array_num = sizeof indexed_terms };
static inline size_t index_of_term(Term c) {
for (size_t i = 0; i < Terms_array_num; ++i) {
static inline USz index_of_term(Term c) {
for (USz i = 0; i < Terms_array_num; ++i) {
if (indexed_terms[i] == c)
return i;
}
@ -24,9 +24,9 @@ static inline Term term_lowered(Term c) {
// Always returns 0 through (sizeof indexed_terms) - 1, and works on
// capitalized terms as well. The index of the lower-cased term is returned if
// the term is capitalized.
static inline size_t semantic_index_of_term(Term c) {
static inline USz semantic_index_of_term(Term c) {
Term c0 = term_lowered(c);
for (size_t i = 0; i < Terms_array_num; ++i) {
for (USz i = 0; i < Terms_array_num; ++i) {
if (indexed_terms[i] == c0)
return i;
}
@ -34,18 +34,18 @@ static inline size_t semantic_index_of_term(Term c) {
}
static inline Term terms_sum(Term a, Term b) {
size_t ia = semantic_index_of_term(a);
size_t ib = semantic_index_of_term(b);
USz ia = semantic_index_of_term(a);
USz ib = semantic_index_of_term(b);
return indexed_terms[(ia + ib) % Terms_array_num];
}
static inline Term terms_mod(Term a, Term b) {
size_t ia = semantic_index_of_term(a);
size_t ib = semantic_index_of_term(b);
USz ia = semantic_index_of_term(a);
USz ib = semantic_index_of_term(b);
return indexed_terms[ib == 0 ? 0 : (ia % ib)];
}
static inline void act_a(Field* f, size_t y, size_t x) {
static inline void act_a(Field* f, USz y, USz x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') {
@ -54,7 +54,7 @@ static inline void act_a(Field* f, size_t y, size_t x) {
}
}
static inline void act_m(Field* f, size_t y, size_t x) {
static inline void act_m(Field* f, USz y, USz x) {
Term inp0 = field_peek_relative(f, y, x, 0, 1);
Term inp1 = field_peek_relative(f, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') {
@ -64,12 +64,12 @@ static inline void act_m(Field* f, size_t y, size_t x) {
}
void orca_run(Field* f) {
size_t ny = f->height;
size_t nx = f->width;
USz ny = f->height;
USz nx = f->width;
Term* f_buffer = f->buffer;
for (size_t iy = 0; iy < ny; ++iy) {
for (USz iy = 0; iy < ny; ++iy) {
Term* row = f_buffer + iy * nx;
for (size_t ix = 0; ix < nx; ++ix) {
for (USz ix = 0; ix < nx; ++ix) {
Term c = row[ix];
switch (c) {
case 'a':

Loading…
Cancel
Save