hamnet70/impl/src/var_array.c

49 lines
931 B
C
Raw Normal View History

#include <stdio.h>
#include "var_array.h"
bool var_array_cf_init(var_array_cf_t *array)
{
array->used = 0;
array->allocated = 64;
array->symbols = reallocarray(NULL, array->allocated, sizeof(float complex));
return (array->symbols != NULL);
}
void var_array_cf_free(var_array_cf_t *array)
{
if(array->symbols) {
free(array->symbols);
array->symbols = NULL;
}
array->used = 0;
array->allocated = 0;
}
void var_array_cf_clear(var_array_cf_t *array)
{
array->used = 0;
}
bool var_array_cf_append(var_array_cf_t *array, float complex symbol)
{
if(array->used == array->allocated) {
size_t new_size = 2 * array->allocated;
float complex *new_symbols = reallocarray(array->symbols, new_size, sizeof(float complex));
if(!new_symbols) {
perror("reallocarray");
return false;
}
array->allocated = new_size;
array->symbols = new_symbols;
}
array->symbols[array->used++] = symbol;
return true;
}