hashmap init seems to be done

missing any testing though
This commit is contained in:
2026-02-28 00:05:18 +01:00
parent 24dd5250f8
commit 9140019767
2 changed files with 30 additions and 34 deletions
+2 -14
View File
@@ -238,26 +238,16 @@ void arr_qsort(void *this, dyn_array_cmp_fn cmp_fn);
typedef int (*hash_func_t)(void *key);
typedef int (*equals_func_t)(void *key1, void *key2);
// TODO: after this comes the actual hash map. it maps keys to indexes into the
// key-value pair array. (which does not actually need values, only a key
// field.)
// TODO: This means that when allocating memory for the array of key-value
// pairs, we actually need to allocate it in terms of the max_align type, and
// increase in size by using that size as the smallest increment. Which will
// keep the hash array aligned properly in memory.
typedef struct hashmap_header {
int n_items, capacity, itemsize, keysize;
int n_items, capacity, itemsize, keysize, mapping_capacity;
allocator_t allocator;
hash_func_t hash;
equals_func_t equals;
int *mapping_arr;
union {
uint8_t bytes[1];
any_align_t _[1];
};
// The above union is actually a different size. Then comes an array of
// integers which is double the size of n_items * sizeof(int). int
// indexes[...];
} hashmap_header_t;
typedef struct hashmap_create_func_args {
@@ -283,8 +273,6 @@ void *hashmap_create_func(hashmap_create_func_args_t args);
}); \
} while(0);
// TODO this shite
// CLI /////////////////////////////////////////////////////////////////////////
#define CLI_UNIQUE1 __macro_internal_34bba35b8b9b20a75f9881e3795630e25d36e620d9c9741e2e9141ba82ec6ef7__
+19 -11
View File
@@ -8,15 +8,21 @@ void *hashmap_create_func(
hashmap_create_func_args_t args
) {
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;
const size_t cap_of_index_arr =
sizeof(int) * mapping_cap(args.initial_capacity);
const size_t INC = ALIGN_OF(int);
const size_t SIZE = args.itemsize * args.initial_capacity;
const size_t cap_of_items_arr = (SIZE + INC - 1) / INC;
const size_t bytes =
sizeof(hashmap_header_t) + cap_of_items_arr + cap_of_index_arr;
hashmap_header_t *header =
allocator_alloc_func(args.allocator, bytes, args.file, args.line);
allocator_alloc_func(
args.allocator,
bytes,
args.file,
args.line
);
header->n_items = 0;
header->capacity = args.initial_capacity;
@@ -25,10 +31,12 @@ void *hashmap_create_func(
header->allocator = args.allocator;
header->hash = args.hash;
header->equals = args.equals;
header->mapping_arr = (int*) &header->bytes[cap_of_items_arr];
header->mapping_capacity = mapping_cap(args.initial_capacity);
for ( int i = 0; i < header->mapping_capacity; i++ ) {
header->mapping_arr[i] = -1;
}
return header->bytes;
}
int *hashmap_get_mapping_arr(hashmap_header_t *this) {
// TODO
}