2023-08-24 06:24:46 -04:00
|
|
|
/* vector.c
|
|
|
|
* Copyright 2023 Anon Anonson, Ognjen 'xolatile' Milan Robovic, Emil Williams
|
|
|
|
* SPDX Identifier: GPL-3.0-only / NO WARRANTY / NO GUARANTEE */
|
|
|
|
|
2023-08-23 21:47:09 -04:00
|
|
|
#include "vector.h"
|
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
void vector_init(vector_t * vector,
|
|
|
|
size_t element_size,
|
2023-08-23 21:47:09 -04:00
|
|
|
size_t element_count) {
|
2023-08-25 15:20:55 -04:00
|
|
|
assert(element_size);
|
2023-08-25 13:44:07 -04:00
|
|
|
|
2023-08-23 21:47:09 -04:00
|
|
|
vector->data = NULL;
|
|
|
|
vector->element_size = element_size;
|
|
|
|
vector->element_count = element_count;
|
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
vector->data = calloc(vector->element_count, vector->element_size);
|
2023-08-23 21:47:09 -04:00
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
assert(vector->data);
|
2023-08-23 21:47:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vector_push(vector_t * vector,
|
2023-08-25 13:44:07 -04:00
|
|
|
void * data) {
|
|
|
|
assert(vector);
|
|
|
|
|
|
|
|
vector->element_count += 1;
|
2023-08-23 21:47:09 -04:00
|
|
|
|
|
|
|
vector->data = realloc(vector->data,
|
|
|
|
vector->element_size * vector->element_count);
|
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
assert(vector->data);
|
2023-08-23 21:47:09 -04:00
|
|
|
|
|
|
|
memcpy(&vector->data[(vector->element_count - 1) * vector->element_size],
|
|
|
|
data,
|
|
|
|
vector->element_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vector_pop(vector_t * vector) {
|
2023-08-25 13:44:07 -04:00
|
|
|
assert(vector); // UNUSED
|
2023-08-23 21:47:09 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
void * vector_get(const vector_t * const vector,
|
2023-08-23 21:47:09 -04:00
|
|
|
const size_t element) {
|
2023-08-25 13:44:07 -04:00
|
|
|
assert(vector);
|
|
|
|
assert(element >= vector->element_count);
|
2023-08-23 21:47:09 -04:00
|
|
|
|
|
|
|
return &vector->data[vector->element_size * element];
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:44:07 -04:00
|
|
|
void vector_set(vector_t * vector,
|
|
|
|
void * data,
|
2023-08-23 21:47:09 -04:00
|
|
|
size_t element) {
|
2023-08-25 13:44:07 -04:00
|
|
|
assert(vector);
|
|
|
|
assert(element >= vector->element_count);
|
2023-08-23 21:47:09 -04:00
|
|
|
|
|
|
|
memcpy(&vector->data[vector->element_size * element],
|
|
|
|
data,
|
|
|
|
vector->element_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vector_free(vector_t * vector) {
|
|
|
|
free(vector->data);
|
|
|
|
}
|
|
|
|
|