diff --git a/src/containers/array/array.h b/src/containers/array/array.h new file mode 100644 index 0000000..cf9b187 --- /dev/null +++ b/src/containers/array/array.h @@ -0,0 +1,52 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include "../../common/aliases/aliases.h" + +#ifdef __cplusplus +BEGIN_C_LINKAGE +#endif // !__cplusplus + +typedef struct int_array IntArray; +struct int_array { + int *items; + u64 count; + u64 capacity; +}; + +#define U64_RSHIFT_OR_1(X) (((u64)X) | (((u64)X) >> 1)) +#define U64_RSHIFT_OR_2(X) (((u64)X) | (((u64)X) >> 2)) +#define U64_RSHIFT_OR_4(X) (((u64)X) | (((u64)X) >> 4)) +#define U64_RSHIFT_OR_8(X) (((u64)X) | (((u64)X) >> 8)) +#define U64_RSHIFT_OR_16(X) (((u64)X) | (((u64)X) >> 16)) +#define U64_RSHIFT_OR_32(X) (((u64)X) | (((u64)X) >> 32)) +#define U64_ROUND_UP_POW2(X) ( \ + ( \ + U64_RSHIFT_OR_32( \ + U64_RSHIFT_OR_16( \ + U64_RSHIFT_OR_8( \ + U64_RSHIFT_OR_4( \ + U64_RSHIFT_OR_2( \ + U64_RSHIFT_OR_1(X - 1) \ + ) \ + ) \ + ) \ + ) \ + ) \ + ) + 1 \ +) + +#define INT_ARRAY_COUNT_ARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int)) + +#define wapp_int_array(...) ((IntArray){ \ + .items = (int[U64_ROUND_UP_POW2(INT_ARRAY_COUNT_ARGS(__VA_ARGS__) * 2)]){__VA_ARGS__}, \ + .count = INT_ARRAY_COUNT_ARGS(__VA_ARGS__), \ + .capacity = U64_ROUND_UP_POW2(INT_ARRAY_COUNT_ARGS(__VA_ARGS__) * 2) \ +}) +#define wapp_int_array_with_capacity(CAPACITY) ((IntArray){.items = (int[CAPACITY]){0}, .count = 0, .capacity = CAPACITY}) + +#ifdef __cplusplus +END_C_LINKAGE +#endif // !__cplusplus + +#endif // !ARRAY_H diff --git a/src/containers/wapp_containers.h b/src/containers/wapp_containers.h index 0c48a9f..eacb239 100644 --- a/src/containers/wapp_containers.h +++ b/src/containers/wapp_containers.h @@ -2,6 +2,7 @@ #define WAPP_CONTAINERS_H #include "dbl_list/dbl_list.h" +#include "array/array.h" #include "../common/wapp_common.h" #endif // !WAPP_CONTAINERS_H