diff options
Diffstat (limited to 'include/container.h')
-rw-r--r-- | include/container.h | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/include/container.h b/include/container.h index 5f40e4a..0234abe 100644 --- a/include/container.h +++ b/include/container.h @@ -1,6 +1,12 @@ +#ifndef CONTAINER_DISABLE_ALL #ifndef JUSTANOTHERCATGIRL_HEADERS_CONTAINER #define JUSTANOTHERCATGIRL_HEADERS_CONTAINER +#if defined(CONTAINER_DISABLE_ARRAY) || defined(CONTAINER_DISABLE_ARRAY) || defined(CONTAINER_DISABLE_HASH) +# define CONTAINER_DISABLE_HMAP +# define CONTAINER_DISABLE_HSET +#endif + #include <stdint.h> #include <stdarg.h> #include <stdio.h> @@ -10,6 +16,7 @@ /* ----------------------------------------------------------------- */ /* -----------------UTILITY HEADER---------------------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_UTILITY #define stringify(val) _stringify_helper(val) #define _stringify_helper(val) #val #define string_concat_separator(first, ...) stringify(first) ";" string_concat_separator(__VA_ARGS__) @@ -68,7 +75,7 @@ typedef void*(*memcpy_t)(void* restrict, const void*, size_t); #else # define _CONTAINER_STATIC static #endif /* CONTAINER_EXPOSE_HELPERS */ - +#endif /* CONTAINER_DISABLE_UTILITY */ /* ----------------------------------------------------------------- */ /* -------------------------ARRAY HEADER---------------------------- */ @@ -87,7 +94,7 @@ typedef void*(*memcpy_t)(void* restrict, const void*, size_t); /* TODO: implement in operations that change the array size */ /* #define SHINK_RESIZING_ARRAY */ - +#ifndef CONTAINER_DISABLE_ARRAY /* size of the array header. Should not be used directly, unless you know what you are doing */ #define DYNARRAY_HEADER_SIZE sizeof(struct _dynarray_header) @@ -154,11 +161,13 @@ void* array_copy(void* old); /* Returns bool. Assumes that the array is sorted. */ char array_binary_search(void* array, void* element, qsort_cmp_t cmp); struct linked_list array_to_ll(void* array); +#endif /* CONTAINER_DISABLE_ARRAY */ /* ----------------------------------------------------------------- */ /* ----------------------LINKED LIST HEADER------------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_LINKED_LIST typedef void(*free_t)(void*); struct __linked_list_meta { size_t element_size; @@ -292,11 +301,13 @@ void ll_set_free(struct linked_list* list, free_t new_free); /* Creates a deep copy of the list, meaning that it must be free'd as any other list */ /* Uses `cpy` function to copy data to new location. If NULL, memcpy is used. */ struct linked_list ll_deep_copy(const struct linked_list* list, memcpy_t cpy); +#endif /* CONTAINER_DISABLE_LINKED_LIST */ /* ----------------------------------------------------------------- */ /* ------------------------HASH MAP HEADER-------------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_HMAP #ifndef HMAP_MAX_BUCKET_SIZE # define HMAP_MAX_BUCKET_SIZE 8 #endif @@ -378,11 +389,13 @@ char hmapi_le(const struct hash_map_iter *a, const struct hash_map_iter* b); char hmapi_ne(const struct hash_map_iter *a, const struct hash_map_iter* b); /* 1 if `iter` is an end iterator, 0 otherwise */ char hmapi_end(const struct hash_map_iter* iter); +#endif /* CONTAINER_DISABLE_HMAP */ /* ----------------------------------------------------------------- */ /* ------------------------HASH SET HEADER-------------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_HSET #ifndef HSET_MAX_BUCKET_SIZE # define HSET_MAX_BUCKET_SIZE 8 #endif @@ -454,12 +467,14 @@ char hseti_le(const struct hash_set_iter *a, const struct hash_set_iter* b); char hseti_ne(const struct hash_set_iter *a, const struct hash_set_iter* b); /* 1 if `iter` is an end iterator, 0 otherwise */ char hseti_end(const struct hash_set_iter* iter); +#endif /* CONTAINER_DISABLE_HSET */ /* ------------------------------------------------------------------------- */ /* ------From now on, the rest of the header is implementation details------ */ /* -------------------the API and documentation end here-------------------- */ /* ------------------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_ARRAY enum _dynarray_header_idx { _dah_idx_member_size = 0, _dah_idx_capacity = 1, @@ -481,6 +496,7 @@ void *_memshrink_array(void *dynarray); void *_insert_to_index_dynarray(void *const dynarray, const void *const element, size_t el_size, size_t index); void* _array_extend(void* array, void* buffer, size_t len); void* _array_pop_at(void* array, size_t idx); +#endif /* CONTAINER_DISABLE_ARRAY */ /* uncomment in dev mode so that LSP highlights the code */ /* #define CONTAINER_IMPLEMENTATION */ @@ -490,6 +506,7 @@ void* _array_pop_at(void* array, size_t idx); /* ---------------------UTILITY IMPLEMENTATION---------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_UTILITY #ifndef __GNUC__ unsigned long __bit_scan_32(int32_t number) { # ifdef _MSC_VER @@ -572,12 +589,13 @@ const qsort_cmp_t __qsort_cmps[64] = { 0, 0, 0, __default_long_long_cmp, }; #endif /* __GNUC__ */ - +#endif /* CONTAINER_DISABLE_UTILITY */ /* ----------------------------------------------------------------- */ /* ----------------------ARRAY IMPLEMENTATION----------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_ARRAY void *_alloc_dynarray(size_t el_size, size_t len) { byte *data = (byte *)malloc(el_size * len + DYNARRAY_HEADER_SIZE); @@ -699,10 +717,12 @@ struct linked_list array_to_ll(void* array) { struct linked_list ret = ll_create_from_buffer(array_element_size(array), array, array_size(array)); return ret; } +#endif /* CONTAINER_DISABLE_ARRAY */ /* ----------------------------------------------------------------- */ /* ------------------LINKED LIST IMPLEMENTATION--------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_LINKED_LIST struct linked_list ll_create(size_t memb_size) { struct __linked_list_meta meta = { .assumed_size = 0, @@ -1057,12 +1077,13 @@ struct linked_list ll_deep_copy(const struct linked_list* list, memcpy_t cpy) { } return ret; } - +#endif /* CONTAINER_DISABLE_LINKED_LIST */ /* ----------------------------------------------------------------- */ /* ---------------------HASH MAP IMPLEMENTATION--------------------- */ /* ----------------------------------------------------------------- */ +#ifndef CONTAINER_DISABLE_HMAP void __hmap_ll_custom_free(void* data) { struct hmap_pair *pair = data; free(pair->key); @@ -1252,12 +1273,13 @@ char hmapi_ne(const struct hash_map_iter *a, const struct hash_map_iter* b) { char hmapi_end(const struct hash_map_iter* iter) { return iter->current_node == NULL || iter->bucket_pos == SIZE_MAX; } +#endif /* CONTAINER_DISABLE_HMAP */ /* ----------------------------------------------------------------- */ /* ---------------------HASH SET IMPLEMENTATION--------------------- */ /* ----------------------------------------------------------------- */ - +#ifndef CONTAINER_DISABLE_HSET struct hash_set hset_new(const size_t el_size, hset_equal_fn eq, hset_hash_fn hash) { struct hash_set ret = { .buckets = array_new(struct linked_list, HMAP_INIT_SIZE), @@ -1416,8 +1438,9 @@ char hseti_ne(const struct hash_set_iter *a, const struct hash_set_iter* b) { char hseti_end(const struct hash_set_iter* iter) { return iter->current_node == NULL || iter->bucket_pos == SIZE_MAX; } +#endif /* CONTAINER_DISABLE_HSET */ #endif /* CONTAINER_IMPLEMENTATION */ #endif /* JUSTANOTHERCATGIRL_HEADERS_CONTAINER */ - +#endif /* CONTAINER_DISABLE_ALL */ /* vim: set ts=8 noet: */ |