Dynamic array implemented in C
DataType dtype = INT;
DArray *arr_int = NULL;
arr_int = darray_create(1024, dtype);
darray_destory(arr_int);
int v = 123;
darray_push(arr_int, v);
int src[] = {1, 2, 3, 4, 5};
int vec_size = 5;
darray_vec_push(arr_int, src, vec_size);
int index = 10;
PDataType pres;
arr_int->get(arr_int, index, &pres);
printf("%d\n", *(pres.pint));
typedef enum _dataType {
CHAR,
INT,
LONG,
FLOAT,
DOUBLE,
SIZET
} DataType;
typedef union _pdatatype {
char *pchar;
int *pint;
long *plong;
float *pfloat;
double *pdouble;
size_t *psizet;
} PDataType;
#define _darray_type_declare(T) \
struct _darray_struct_##T { \
const size_t element_size; \
size_t capacity; \
size_t length; \
T *content; \
void (* const get)(DArray *, size_t, PDataType *); \
void (* const set)(DArray *, size_t, T *); \
}\
typedef struct _d_array DArray;
struct _d_array {
/* data */
const size_t element_size;
size_t capacity;
size_t length;
void *content;
void (* const get)(DArray *, size_t, PDataType *);
void (* const set)(DArray *, size_t, void *);
};
typedef _darray_type_declare(char) DArray_CHAR;
typedef _darray_type_declare(int) DArray_INT;
typedef _darray_type_declare(long) DArray_LONG;
typedef _darray_type_declare(float) DArray_FLOAT;
typedef _darray_type_declare(double) DArray_DOUBLE;
typedef _darray_type_declare(size_t) DArray_SIZET;