Moved utilities out

This commit is contained in:
John Alanbrook 2022-07-23 01:47:44 +00:00
parent c8d83cc733
commit 3dd60ae92f
6 changed files with 0 additions and 284 deletions

View file

@ -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++)];
}

View file

@ -1,20 +0,0 @@
#ifndef CIRCBUF_H
#define CIRCBUF_H
#include <stdio.h>
#include <stdint.h>
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

View file

@ -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)));
}

View file

@ -1,7 +0,0 @@
#ifndef UTIL_H
#define UTIL_H
unsigned int powof2(unsigned int num);
int ispow2(int num);
#endif

View file

@ -1,150 +0,0 @@
#include "vec.h"
#include "log.h"
#include <string.h>
#include <stdlib.h>
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);
}

View file

@ -1,33 +0,0 @@
#ifndef VEC_H
#define VEC_H
#include <stdio.h>
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