1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-09-18 21:13:24 +01:00

upgrade vector.c

This commit is contained in:
suchmememanyskill 2021-06-24 13:01:33 +02:00
parent a1ae52e8ac
commit 24a90ebc99
2 changed files with 91 additions and 40 deletions

View file

@ -2,49 +2,64 @@
#include <string.h>
#include <mem/heap.h>
Vector_t newVec(u32 typesz, u32 preallocate)
{
Vector_t res = {
.data = calloc(preallocate, typesz),
.capacity = preallocate * typesz,
.count = 0,
.elemSz = typesz
};
// check .data != null;
return res;
Vector_t newVec(u8 typesz, u32 preallocate) {
if (preallocate) {
Vector_t res = {
.data = calloc(preallocate, typesz),
.capacity = preallocate * typesz,
.count = 0,
.elemSz = typesz
};
// check .data != null;
return res;
}
else {
Vector_t res = {
.data = NULL,
.capacity = 1 * typesz,
.count = 0,
.elemSz = typesz
};
return res;
}
}
Vector_t vecFromArray(void* array, u32 count, u32 typesz)
{
Vector_t res = {
.data = array,
.capacity = count * typesz,
.count = count,
.elemSz = typesz
};
return res;
Vector_t res = {
.data = array,
.capacity = count * typesz,
.count = count,
.elemSz = typesz
};
return res;
}
bool vecAdd(Vector_t* v, void* elem, u32 sz)
{
if (!v || !elem || v->elemSz != sz)
return false;
int vecAdd(Vector_t* v, void* elem, u8 sz) {
if (!v || !elem || v->elemSz != sz)
return 0;
u32 usedbytes = v->count * sz;
if (usedbytes >= v->capacity)
{
v->capacity *= 2;
void *buff = malloc(v->capacity);
if (!buff)
return false;
memcpy(buff, v->data, v->capacity / 2);
free(v->data);
v->data = buff;
}
if (v->data == NULL) {
v->data = calloc(1, v->elemSz);
}
memcpy((char*)v->data + usedbytes, elem, sz);
v->count++;
return true;
u32 usedbytes = v->count * sz;
if (usedbytes >= v->capacity)
{
v->capacity *= 2;
void* buff = malloc(v->capacity);
if (!buff)
return 0;
memcpy(buff, v->data, v->capacity / 2);
free(v->data);
v->data = buff;
}
memcpy((char*)v->data + usedbytes, elem, sz);
v->count++;
return 1;
}
Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
@ -55,6 +70,36 @@ Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
}
Vector_t vecCopy(Vector_t* orig) {
return vecCopyOffset(orig, 0);
return vecCopyOffset(orig, 0);
}
void* getStackEntry(Vector_t *stack) {
if (stack->count <= 0)
return NULL;
return ((u8*)stack->data + (stack->elemSz * (stack->count - 1)));
}
// This will stay valid until the queue is modified
void* popStackEntry(Vector_t* stack) {
if (stack->count <= 0)
return NULL;
void* a = getStackEntry(stack);
stack->count--;
return a;
}
void vecRem(Vector_t *vec, int idx) {
if (vec->count <= 0 || idx >= vec->count)
return;
if (idx == (vec->count - 1)) {
vec->count--;
return;
}
memcpy((u8*)vec->data + (vec->elemSz * idx), (u8*)vec->data + (vec->elemSz * (idx + 1)), (vec->count - idx - 1) * vec->elemSz);
vec->count--;
}

View file

@ -5,7 +5,7 @@ typedef struct {
void* data;
u32 capacity;
u32 count;
u32 elemSz;
u8 elemSz;
// u32 typeTag;
} Vector_t;
@ -23,8 +23,14 @@ typedef struct {
#define vecGetArrayPtr(vec, type) (type)((vec)->data)
Vector_t newVec(u32 typesz, u32 preallocate);
#define vecForEach(type, varname, vecPtr) for (type varname = vecPtr->data; ((u8*)varname - (u8*)vecPtr->data) < (vecPtr->count * vecPtr->elemSz); varname++)
Vector_t newVec(u8 typesz, u32 preallocate);
Vector_t vecFromArray(void* array, u32 count, u32 typesz);
bool vecAdd(Vector_t* v, void* elem, u32 sz);
bool vecAdd(Vector_t* v, void* elem, u8 sz);
Vector_t vecCopy(Vector_t* orig);
Vector_t vecCopyOffset(Vector_t* orig, u32 offset);
Vector_t vecCopyOffset(Vector_t* orig, u32 offset);
void* getStackEntry(Vector_t* stack);
void* popStackEntry(Vector_t* stack);
void vecRem(Vector_t * vec, int idx);