Remove complex arrays
This can be done with structs in an array_t
This commit is contained in:
209
src/safe_array.c
209
src/safe_array.c
@@ -240,212 +240,3 @@ void array_2d_free(array_2d_t *x)
|
||||
}
|
||||
free(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function generates a complex safe array
|
||||
*
|
||||
* @param rows The number of rows in the array
|
||||
* @param size The element size (sizeof(type))
|
||||
* @return array_c_s_t* The pointer to the complex array struct
|
||||
*/
|
||||
array_c_s_t *array_c_s_init(size_t rows, size_t size)
|
||||
{
|
||||
array_c_s_t *x = calloc(sizeof(array_c_s_t) + (2 * rows * size) - 1, 1);
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to allocate memory for array", __func__);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
x->rows = rows;
|
||||
x->size = size;
|
||||
x->ptr = x->data;
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets the number of rows in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @return size_t The number of rows
|
||||
*/
|
||||
size_t array_c_s_get_rows(array_c_s_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x->rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets an imaginary element from the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to get
|
||||
* @return void* The ptr to the imaginary element
|
||||
*/
|
||||
void* array_c_get_imag(array_c_s_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + (2 * row * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets a real element from the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to get
|
||||
* @return void* The ptr to the real element
|
||||
*/
|
||||
void* array_c_get_real(array_c_s_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + ((2 * row + 1) * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets an element in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to set
|
||||
* @param real The ptr to the real value of the element
|
||||
* @param imaginary The ptr to the imaginary value of the element
|
||||
*/
|
||||
int array_c_s_set(array_c_s_t *x, size_t row, void *real, void *imaginary)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy((char *)x->ptr + ((2 * row + 1) * x->size), real, x->size);
|
||||
memcpy((char *)x->ptr + ( 2 * row * x->size), imaginary, x->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
*/
|
||||
void array_c_s_free(array_c_s_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
}
|
||||
|
||||
free(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function generates a complex safe array in polar form
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param rows The number of complex elements in the array
|
||||
* @param init_val The initial value of the array
|
||||
* @return int The number of elements in the array
|
||||
*/
|
||||
array_c_p_t *array_c_p_init(size_t rows, size_t size)
|
||||
{
|
||||
array_c_p_t* x = calloc(sizeof(array_c_p_t) + (2 * rows * size) - 1, 1);
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: Failed to allocate memory for array", __func__);
|
||||
return NULL;
|
||||
}
|
||||
x->size = size;
|
||||
x->rows = rows;
|
||||
x->ptr = x->data;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function gets the number of rows in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @return size_t The number of rows
|
||||
*/
|
||||
size_t array_c_p_get_rows(array_c_p_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x->rows;
|
||||
}
|
||||
|
||||
void* array_c_get_mag(array_c_p_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + (2 * row * x->size);
|
||||
}
|
||||
|
||||
void* array_c_get_phase(array_c_p_t *x, size_t row)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (char *)x->ptr + ((2 * row + 1) * x->size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function sets an element in the complex safe array
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
* @param row The row index of the element to set
|
||||
* @param magnitude The magnitude value of the element
|
||||
* @param phase The phase value of the element
|
||||
*/
|
||||
int array_c_p_set(array_c_p_t *x, size_t row, void* magnitude, void* phase)
|
||||
{
|
||||
if (row >= x->rows)
|
||||
{
|
||||
fprintf(stderr, "%s: index out of bounds: %d > %d\n", __func__, row, x->rows);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy((char *)x->ptr + (2 * row * x->size), magnitude, x->size);
|
||||
memcpy((char *)x->ptr + ((2 * row + 1) * x->size), phase, x->size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function frees the complex safe array, sets the pointer to NULL and the number of rows to 0
|
||||
*
|
||||
* @param x The pointer to the complex array struct
|
||||
*/
|
||||
void array_c_p_free(array_c_p_t *x)
|
||||
{
|
||||
if (!x)
|
||||
{
|
||||
fprintf(stderr, "%s: ptr dereferences to NULL\n", __func__);
|
||||
}
|
||||
|
||||
free(x);
|
||||
}
|
||||
Reference in New Issue
Block a user