diff --git a/cig.h b/cig.h index 40fb13f..4b12ed7 100644 --- a/cig.h +++ b/cig.h @@ -9,7 +9,8 @@ #include typedef union any_align { char c; int i; long l; long long ll; float f; double d; void *p; long double ld; } any_align_t; -#define MAX_ALIGN ((size_t) sizeof(any_align_t)) +#define ALIGN_OF(TYPE) ((size_t)(&((struct{char c; TYPE t;}*) 0)->t)) +#define MAX_ALIGN (ALIGN_OF(any_align_t)) #define KB (1024) #define MB (KB * KB) #define GB (KB * KB * KB) diff --git a/hashmap.c b/hashmap.c index 57dcc42..0469efa 100644 --- a/hashmap.c +++ b/hashmap.c @@ -1,28 +1,34 @@ #include "cig.h" +static inline int mapping_cap(int capacity) { + return capacity * 2; +} + void *hashmap_create_func( hashmap_create_func_args_t args ) { - size_t size_of_index_arr = sizeof(int) * args.initial_capacity * 2; - size_t INC = sizeof(any_align_t); - size_t SIZE = args.itemsize * args.initial_capacity; - size_t size_of_items_arr = (SIZE + INC - 1) / INC; - size_t bytes = sizeof(hashmap_header_t) + size_of_items_arr + size_of_index_arr; - hashmap_header_t *header = allocator_alloc_func( - args.allocator, - bytes, - args.file, - args.line - ); + size_t size_of_index_arr = sizeof(int) * mapping_cap(args.initial_capacity); + size_t INC = ALIGN_OF(int); + size_t SIZE = args.itemsize * args.initial_capacity; + size_t size_of_items_arr = (SIZE + INC - 1) / INC; + size_t bytes = + sizeof(hashmap_header_t) + size_of_items_arr + size_of_index_arr; - header->n_items = 0; - header->capacity = args.initial_capacity; - header->itemsize = args.itemsize; - header->keysize = args.keysize; - header->allocator = args.allocator; - header->hash = args.hash; - header->equals = args.equals; + hashmap_header_t *header = + allocator_alloc_func(args.allocator, bytes, args.file, args.line); - return header->bytes; + header->n_items = 0; + header->capacity = args.initial_capacity; + header->itemsize = args.itemsize; + header->keysize = args.keysize; + header->allocator = args.allocator; + header->hash = args.hash; + header->equals = args.equals; + + return header->bytes; +} + +int *hashmap_get_mapping_arr(hashmap_header_t *this) { + // TODO }