Fumofumotris/source/fumoengine/include/vector.c

47 lines
901 B
C
Raw Normal View History

2024-05-07 22:10:15 +00:00
#include "vector.h"
#include <stdlib.h>
#include <string.h>
2024-05-15 22:10:47 +00:00
bool CreateVector(struct Vector *vec, usize value_size)
2024-05-07 22:10:15 +00:00
{
2024-05-15 22:10:47 +00:00
void *array = malloc(16 * value_size);
2024-05-07 22:10:15 +00:00
if (array == nullptr)
return false;
2024-05-15 22:10:47 +00:00
vec->value_size = value_size;
2024-05-15 05:44:13 +00:00
vec->len = 0;
vec->capacity = 16;
vec->array = array;
2024-05-07 22:10:15 +00:00
return true;
}
void FreeVector(struct Vector *vec)
{
free(vec->array);
}
2024-05-15 22:10:47 +00:00
void *VectorGet(struct Vector *vec, usize i)
2024-05-07 22:10:15 +00:00
{
2024-05-15 22:10:47 +00:00
return (u8 *)vec->array + i * vec->value_size;
2024-05-07 22:10:15 +00:00
}
2024-05-15 22:10:47 +00:00
bool VectorAdd(struct Vector *vec, void *item)
2024-05-07 22:10:15 +00:00
{
if (vec->len == vec->capacity) {
usize new_capacity = vec->capacity * 2;
2024-05-15 22:10:47 +00:00
void *new_array = realloc(vec->array, new_capacity * vec->value_size);
2024-05-07 22:10:15 +00:00
if (new_array == nullptr)
return false;
vec->capacity = new_capacity;
}
2024-05-15 22:10:47 +00:00
memcpy(VectorGet(vec, vec->len++), item, vec->value_size);
2024-05-07 22:10:15 +00:00
return true;
}