1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-26 05:42:07 +00:00
TegraExplorer/source/utils/vector.c

104 lines
2.1 KiB
C
Raw Normal View History

#include "vector.h"
2021-07-09 21:56:13 +01:00
#include "../gfx/gfx.h"
#include <string.h>
#include <mem/heap.h>
2021-06-24 12:01:33 +01:00
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)
{
2021-06-24 12:01:33 +01:00
Vector_t res = {
.data = array,
.capacity = count * typesz,
.count = count,
.elemSz = typesz
};
return res;
}
2021-06-24 20:26:36 +01:00
int _vecAdd(Vector_t* v, void* elem, u8 sz) {
2021-06-24 12:01:33 +01:00
if (!v || !elem || v->elemSz != sz)
return 0;
if (v->data == NULL) {
v->data = calloc(1, v->elemSz);
}
2021-07-09 21:56:13 +01:00
u32 usedbytes = v->count * (u32)v->elemSz;
2021-06-24 12:01:33 +01:00
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;
}
2021-07-09 21:56:13 +01:00
memcpy(((u8*)v->data) + usedbytes, elem, v->elemSz);
2021-06-24 12:01:33 +01:00
v->count++;
return 1;
2020-12-28 13:51:59 +00:00
}
Vector_t vecCopyOffset(Vector_t* orig, u32 offset) {
Vector_t dst = newVec(orig->elemSz, orig->count - offset);
memcpy(dst.data, ((u8*)orig->data + orig->elemSz * offset), (orig->count - offset) * orig->elemSz);
dst.count = orig->count - offset;
2020-12-28 13:51:59 +00:00
return dst;
}
Vector_t vecCopy(Vector_t* orig) {
2021-06-24 12:01:33 +01:00
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;
}
2021-06-24 12:01:33 +01:00
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--;
}