From 3dd60ae92f30cf736aa1eeaa9ad234b69a250c36 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 23 Jul 2022 01:47:44 +0000 Subject: [PATCH] Moved utilities out --- source/engine/circbuf.c | 53 -------------- source/engine/circbuf.h | 20 ------ source/engine/util.c | 21 ------ source/engine/util.h | 7 -- source/engine/vec.c | 150 ---------------------------------------- source/engine/vec.h | 33 --------- 6 files changed, 284 deletions(-) delete mode 100644 source/engine/circbuf.c delete mode 100644 source/engine/circbuf.h delete mode 100644 source/engine/util.c delete mode 100644 source/engine/util.h delete mode 100755 source/engine/vec.c delete mode 100755 source/engine/vec.h diff --git a/source/engine/circbuf.c b/source/engine/circbuf.c deleted file mode 100644 index 7f63bbc..0000000 --- a/source/engine/circbuf.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "circbuf.h" - -#include "util.h" -#include "assert.h" - -struct circbuf *circbuf_make(size_t size, unsigned int len) -{ - struct circbuf *new = malloc(sizeof(*new)); - new->len = powof2(len); - new->data = calloc(sizeof(short), new->len); - new->read = new->write = 0; - return new; -} - -struct circbuf circbuf_init(size_t size, unsigned int len) -{ - struct circbuf new; - new.len = powof2(len); - new.data = calloc(sizeof(short), new.len); - new.read = new.write = 0; - - return new; -} - -int cbuf_size(struct circbuf *buf) { - return buf->write - buf->read; -} - -int cbuf_empty(struct circbuf *buf) { - return buf->read == buf->write; -} - -int cbuf_full(struct circbuf *buf) { - return cbuf_size(buf) >= buf->len; -} - -uint32_t cbuf_mask(struct circbuf *buf, uint32_t n) { - return n & (buf->len-1); -} - -void cbuf_push(struct circbuf *buf, short data) { - //assert(!cbuf_full(buf)); - - buf->data[cbuf_mask(buf,buf->write++)] = data; -} - -short cbuf_shift(struct circbuf *buf) { - //assert(!cbuf_empty(buf)); - return buf->data[cbuf_mask(buf, buf->read++)]; -} - - - diff --git a/source/engine/circbuf.h b/source/engine/circbuf.h deleted file mode 100644 index 84af514..0000000 --- a/source/engine/circbuf.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CIRCBUF_H -#define CIRCBUF_H - -#include -#include - -struct circbuf { - int16_t *data; - uint32_t read; - uint32_t write; - unsigned int len; -}; - -struct circbuf *circbuf_make(size_t size, unsigned int len); -struct circbuf circbuf_init(size_t size, unsigned int len); -void cbuf_push(struct circbuf *buf, short data); -short cbuf_shift(struct circbuf *buf); - - -#endif \ No newline at end of file diff --git a/source/engine/util.c b/source/engine/util.c deleted file mode 100644 index 49edbf3..0000000 --- a/source/engine/util.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "util.h" - -unsigned int powof2(unsigned int num) -{ - if (num != 0) { - num--; - num |= (num >> 1); - num |= (num >> 2); - num |= (num >> 4); - num |= (num >> 8); - num |= (num >> 16); - num++; - } - - return num; -} - -int ispow2(int num) -{ - return (num && !(num & (num - 1))); -} \ No newline at end of file diff --git a/source/engine/util.h b/source/engine/util.h deleted file mode 100644 index a217d33..0000000 --- a/source/engine/util.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef UTIL_H -#define UTIL_H - -unsigned int powof2(unsigned int num); -int ispow2(int num); - -#endif \ No newline at end of file diff --git a/source/engine/vec.c b/source/engine/vec.c deleted file mode 100755 index f50e60d..0000000 --- a/source/engine/vec.c +++ /dev/null @@ -1,150 +0,0 @@ -#include "vec.h" -#include "log.h" -#include -#include - -struct vec vec_init(size_t width, int size) -{ - struct vec v; - v.size = size; - v.width = width; - v.len = 0; - v.data = calloc(v.size, v.width); - return v; -} - -struct vec *vec_make(size_t width, int size) -{ - struct vec *new = calloc(1, sizeof(struct vec)); - new->size = size; - new->width = width; - new->data = calloc(new->size, new->width); - return new; -} - -void *vec_get(struct vec *vec, int n) -{ - if (n < vec->len) - return (char *) vec->data + (n * vec->width); - - YughLog(0, 4,"Attempted to access element %d of a vec with %d elements.", n, vec->len); - return NULL; -} - -void vec_walk(struct vec *vec, void (*fn)(void *data)) -{ - for(int i = 0; i < vec->len; i++) - fn((char*)vec->data + (i * vec->width)); -} - -void vec_delete(struct vec *vec, int n) -{ - if (n < vec->len) { - vec->len--; - memcpy(vec_p(vec, n), vec_p(vec, vec->len), vec->width); - } - -} - -void vec_del_order(struct vec *vec, int n) -{ - if (n < vec->len) { - vec->len--; - memmove(vec_p(vec, n), vec_p(vec, n + 1), - vec->width * (vec->len - n)); - } -} - -void *vec_add(struct vec *vec, const void *data) -{ - if (vec->size == vec->len) - vec_expand(vec); - - if (data) - memcpy(vec_p(vec, vec->len), data, vec->width); - else - memset(vec_p(vec, vec->len), 0, vec->width); - - vec->len++; - return vec_get(vec, vec->len - 1); -} - -void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b)) -{ - if(vec->size == vec->len) - vec_expand(vec); - - int n = vec->len - 1; - while (sort(vec_p(vec, n), data)) - n--; - - return vec_insert(vec, data, n); -} - -void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data) -{ - for (int i = 0; i < v->len; i++) { - if (valid(vec_p(v, i), data)) - return vec_p(v, i); - } - - return NULL; -} - -void *vec_insert(struct vec *vec, void *data, int n) -{ - if (vec->size == vec->len) - vec_expand(vec); - - memmove(vec_p(vec, n), vec_p(vec, n + 1), vec->width * (vec->len - n)); - vec->len++; - memcpy(vec_p(vec, n), data, vec->width); - return vec_get(vec, n); -} - -char *vec_p(struct vec *vec, int n) -{ - return ((char *) vec->data + (vec->width * n)); -} - -void *vec_set(struct vec *vec, int n, void *data) -{ - if (vec->len >= n) - return NULL; - - memcpy(vec_p(vec, n), data, vec->width); - return vec_get(vec, n); -} - -void vec_expand(struct vec *vec) -{ - vec->size *= 2; - vec->data = realloc(vec->data, vec->size * vec->width); -} - -void vec_store(struct vec *vec, FILE * f) -{ - fwrite(&vec->len, sizeof(vec->len), 1, f); - fwrite(&vec->size, sizeof(vec->size), 1, f); - fwrite(&vec->width, sizeof(vec->width), 1, f); - fwrite(vec->data, vec->width, vec->len, f); -} - -void vec_load(struct vec *vec, FILE * f) -{ - fread(&vec->len, sizeof(vec->len), 1, f); - fread(&vec->size, sizeof(vec->size), 1, f); - fread(&vec->width, sizeof(vec->width), 1, f); - fread(vec->data, vec->width, vec->len, f); -} - -void vec_clear(struct vec *vec) -{ - vec->len = 0; -} - -void vec_fit(struct vec *vec) -{ - vec->size = vec->len; - vec->data = realloc(vec->data, vec->size * vec->width); -} diff --git a/source/engine/vec.h b/source/engine/vec.h deleted file mode 100755 index 3b95630..0000000 --- a/source/engine/vec.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef VEC_H -#define VEC_H - -#include - -struct vec { - int len; // How many elements are in the vec - int size; // The capacity of the vec - size_t width; // The size in bytes of an element of the vector - void *data; -}; - -struct vec vec_init(size_t width, int size); -struct vec *vec_make(size_t width, int size); -void *vec_get(struct vec *vec, int n); -void vec_walk(struct vec *vec, void (*fn)(void *data)); -void vec_clear(struct vec *vec); -void *vec_add(struct vec *vec, const void *data); - -/* sort returns 0 for a<=b, 1 for a>b */ -void *vec_add_sort(struct vec *vec, void *data, int (*sort)(void *a, void *b)); -void *vec_insert(struct vec *vec, void *data, int n); -void *vec_set(struct vec *vec, int n, void *data); -void *vec_find(struct vec *v, int (*valid)(void *a, void *data), void *data); -void vec_delete(struct vec *vec, int n); -void vec_del_order(struct vec *vec, int n); -void vec_store(struct vec *vec, FILE *f); -void vec_load(struct vec *vec, FILE *f); -char *vec_p(struct vec *vec, int n); -void vec_expand(struct vec *vec); -void vec_fit(struct vec *vec); - -#endif