libhl/tests/vector.c

135 lines
3.0 KiB
C
Raw Normal View History

2023-08-23 19:03:32 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char * data;
size_t element_size;
size_t element_count;
} vector_t;
typedef struct {
int a;
int b;
char * c;
} example_t;
2023-08-23 19:32:40 -04:00
extern void vector_init(vector_t * vector,
size_t element_size,
size_t element_count);
2023-08-23 19:03:32 -04:00
extern void vector_push(vector_t * vector,
void * data);
extern void vector_pop(vector_t * vector);
extern void * vector_get(vector_t * vector,
size_t element);
2023-08-23 19:32:40 -04:00
extern void vector_set(vector_t * vector,
void * data,
size_t element);
2023-08-23 19:03:32 -04:00
extern void vector_free(vector_t * vector);
extern void example_print(example_t * example);
2023-08-23 19:32:40 -04:00
void vector_init(vector_t * vector,
size_t element_size,
size_t element_count) {
vector->data = NULL;
vector->element_size = element_size;
vector->element_count = element_count;
vector->data = malloc(vector->element_size * vector->element_count);
if ((! vector->data) && (vector->element_count)) {
// Handle or output error somehow?
puts("vector_init");
exit(EXIT_FAILURE);
}
memset(vector->data,
0,
vector->element_size * vector->element_count);
}
2023-08-23 19:03:32 -04:00
void vector_push(vector_t * vector,
void * data) {
++vector->element_count;
vector->data = realloc(vector->data,
vector->element_size * vector->element_count);
2023-08-23 19:32:40 -04:00
if (! vector->data) {
2023-08-23 19:03:32 -04:00
// Handle or output error somehow?
puts("vector_push");
exit(EXIT_FAILURE);
}
memcpy(&vector->data[(vector->element_count - 1) * vector->element_size],
data,
vector->element_size);
}
void vector_pop(vector_t * vector) {
(void) vector;
}
void * vector_get(vector_t * vector,
size_t element) {
if (element >= vector->element_count) {
// Handle or output error somehow?
puts("vector_get");
exit(EXIT_FAILURE);
}
return &vector->data[vector->element_size * element];
}
2023-08-23 19:32:40 -04:00
void vector_set(vector_t * vector,
void * data,
size_t element) {
if (element >= vector->element_count) {
// Handle or output error somehow?
puts("vector_set");
exit(EXIT_FAILURE);
}
memcpy(&vector->data[vector->element_size * element],
data,
vector->element_size);
}
2023-08-23 19:03:32 -04:00
void vector_free(vector_t * vector) {
free(vector->data);
}
void example_print(example_t * example) {
printf("a : %+i\n", example->a);
printf("b : %+i\n", example->b);
printf("c : %2s\n", example->c);
}
int main(void) {
vector_t vector;
example_t x = { 1, -1, "A" },
y = { 2, -2, "B" },
z = { 3, -3, "C" };
2023-08-23 19:32:40 -04:00
vector_init(&vector, sizeof(example_t), 10);
2023-08-23 19:03:32 -04:00
2023-08-23 19:32:40 -04:00
vector_set(&vector, &x, 0);
vector_set(&vector, &y, 1);
vector_set(&vector, &z, 2);
2023-08-23 19:03:32 -04:00
example_print(vector_get(&vector, 0));
example_print(vector_get(&vector, 1));
example_print(vector_get(&vector, 2));
vector_free(&vector);
return (EXIT_SUCCESS);
}