1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-26 05:42:07 +00: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,16 +2,28 @@
#include <string.h> #include <string.h>
#include <mem/heap.h> #include <mem/heap.h>
Vector_t newVec(u32 typesz, u32 preallocate) Vector_t newVec(u8 typesz, u32 preallocate) {
{ if (preallocate) {
Vector_t res = { Vector_t res = {
.data = calloc(preallocate, typesz), .data = calloc(preallocate, typesz),
.capacity = preallocate * typesz, .capacity = preallocate * typesz,
.count = 0, .count = 0,
.elemSz = typesz .elemSz = typesz
}; };
// check .data != null; // check .data != null;
return res; 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 vecFromArray(void* array, u32 count, u32 typesz)
@ -25,18 +37,21 @@ Vector_t vecFromArray(void* array, u32 count, u32 typesz)
return res; return res;
} }
bool vecAdd(Vector_t* v, void* elem, u32 sz) int vecAdd(Vector_t* v, void* elem, u8 sz) {
{
if (!v || !elem || v->elemSz != sz) if (!v || !elem || v->elemSz != sz)
return false; return 0;
if (v->data == NULL) {
v->data = calloc(1, v->elemSz);
}
u32 usedbytes = v->count * sz; u32 usedbytes = v->count * sz;
if (usedbytes >= v->capacity) if (usedbytes >= v->capacity)
{ {
v->capacity *= 2; v->capacity *= 2;
void *buff = malloc(v->capacity); void* buff = malloc(v->capacity);
if (!buff) if (!buff)
return false; return 0;
memcpy(buff, v->data, v->capacity / 2); memcpy(buff, v->data, v->capacity / 2);
free(v->data); free(v->data);
v->data = buff; v->data = buff;
@ -44,7 +59,7 @@ bool vecAdd(Vector_t* v, void* elem, u32 sz)
memcpy((char*)v->data + usedbytes, elem, sz); memcpy((char*)v->data + usedbytes, elem, sz);
v->count++; v->count++;
return true; return 1;
} }
Vector_t vecCopyOffset(Vector_t* orig, u32 offset) { Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
@ -58,3 +73,33 @@ 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; void* data;
u32 capacity; u32 capacity;
u32 count; u32 count;
u32 elemSz; u8 elemSz;
// u32 typeTag; // u32 typeTag;
} Vector_t; } Vector_t;
@ -23,8 +23,14 @@ typedef struct {
#define vecGetArrayPtr(vec, type) (type)((vec)->data) #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); 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 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);