Skip to content

Commit

Permalink
fix flat hash map to be consistent with initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
agl-alexglopez committed Dec 16, 2024
1 parent de3a1e3 commit d336856
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 76 deletions.
6 changes: 3 additions & 3 deletions ccc/flat_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ May be NULL if the user provides a allocation function. The buffer will be
interpreted in units of type size that the user intends to store.
@param [in] capacity the starting capacity of the provided buffer or 0 if no
buffer is provided and an allocation function is given.
@param [in] key_field the field of the struct used for key storage.
@param [in] fhash_elem_field the name of the fhmap_elem field.
@param [in] key_field the field of the struct used for key storage.
@param [in] alloc_fn the allocation function for resizing or NULL if no
resizing is allowed.
@param [in] hash_fn the ccc_hash_fn function the user desires for the table.
@param [in] key_eq_fn the ccc_key_eq_fn the user intends to use.
@param [in] aux_data auxiliary data that is needed for hashing or comparison.
@return the flat hash map directly initialized on the right hand side of the
equality operator (i.e. ccc_flat_hash_map fh = ccc_fhm_init(...);) */
#define ccc_fhm_init(memory_ptr, capacity, key_field, fhash_elem_field, \
#define ccc_fhm_init(memory_ptr, capacity, fhash_elem_field, key_field, \
alloc_fn, hash_fn, key_eq_fn, aux_data) \
ccc_impl_fhm_init(memory_ptr, capacity, key_field, fhash_elem_field, \
ccc_impl_fhm_init(memory_ptr, capacity, fhash_elem_field, key_field, \
alloc_fn, hash_fn, key_eq_fn, aux_data)

/** @brief Copy the map at source to destination.
Expand Down
24 changes: 1 addition & 23 deletions ccc/impl/impl_flat_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ union ccc_fhmap_entry_
struct ccc_fhash_entry_ impl_;
};

#define ccc_impl_fhm_init(memory_ptr, capacity, key_field, fhash_elem_field, \
#define ccc_impl_fhm_init(memory_ptr, capacity, fhash_elem_field, key_field, \
alloc_fn, hash_fn, key_eq_fn, aux) \
{ \
.buf_ \
Expand All @@ -60,28 +60,6 @@ union ccc_fhmap_entry_
= offsetof(typeof(*(memory_ptr)), fhash_elem_field), \
}

#define ccc_impl_fhm_zero_init(type_name, key_field, fhash_elem_field, \
alloc_fn, hash_fn, key_eq_fn, aux) \
{ \
.buf_ = ccc_buf_init((type_name *)NULL, alloc_fn, aux, 0), \
.hash_fn_ = (hash_fn), \
.eq_fn_ = (key_eq_fn), \
.key_offset_ = (offsetof(type_name, key_field)), \
.hash_elem_offset_ = offsetof(type_name, fhash_elem_field), \
}

#define ccc_impl_fhm_static_init(memory_ptr, key_field, fhash_elem_field, \
hash_fn, key_eq_fn, aux) \
{ \
.buf_ = ccc_buf_init(memory_ptr, NULL, aux, \
sizeof(memory_ptr) / sizeof((memory_ptr)[0])), \
.hash_fn_ = (hash_fn), \
.eq_fn_ = (key_eq_fn), \
.key_offset_ = (offsetof(typeof(*(memory_ptr)), key_field)), \
.hash_elem_offset_ \
= offsetof(typeof(*(memory_ptr)), fhash_elem_field), \
}

struct ccc_ent_ ccc_impl_fhm_find(struct ccc_fhmap_ const *, void const *key,
uint64_t hash);
void ccc_impl_fhm_insert(struct ccc_fhmap_ *h, void const *e, uint64_t hash,
Expand Down
2 changes: 1 addition & 1 deletion samples/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ static bool
found_dst(struct graph *const graph, struct vertex *const src)
{
flat_hash_map parent_map
= fhm_init((struct path_backtrack_cell *)NULL, 0, current, elem,
= fhm_init((struct path_backtrack_cell *)NULL, 0, elem, current,
std_alloc, hash_parent_cells, eq_parent_cells, NULL);
flat_double_ended_queue bfs
= fdeq_init((struct point *)NULL, std_alloc, NULL, 0);
Expand Down
26 changes: 13 additions & 13 deletions tests/fhmap/test_fhmap_construct.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ gen(int *to_affect)

static struct val s_vals[10];
static flat_hash_map static_fh
= fhm_init(s_vals, sizeof(s_vals) / sizeof(s_vals[0]), key, e, NULL,
= fhm_init(s_vals, sizeof(s_vals) / sizeof(s_vals[0]), e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);

CHECK_BEGIN_STATIC_FN(fhmap_test_static_init)
Expand Down Expand Up @@ -84,9 +84,9 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_static_init)

CHECK_BEGIN_STATIC_FN(fhmap_test_copy_no_alloc)
{
flat_hash_map src = fhm_init((struct val[11]){}, 11, key, e, NULL,
flat_hash_map src = fhm_init((struct val[11]){}, 11, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
flat_hash_map dst = fhm_init((struct val[13]){}, 13, key, e, NULL,
flat_hash_map dst = fhm_init((struct val[13]){}, 13, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
(void)insert(&src, &(struct val){.key = 0}.e);
(void)insert(&src, &(struct val){.key = 1, .val = 1}.e);
Expand All @@ -109,9 +109,9 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_copy_no_alloc)

CHECK_BEGIN_STATIC_FN(fhmap_test_copy_no_alloc_fail)
{
flat_hash_map src = fhm_init((struct val[11]){}, 11, key, e, NULL,
flat_hash_map src = fhm_init((struct val[11]){}, 11, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
flat_hash_map dst = fhm_init((struct val[7]){}, 7, key, e, NULL,
flat_hash_map dst = fhm_init((struct val[7]){}, 7, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
(void)insert(&src, &(struct val){.key = 0}.e);
(void)insert(&src, &(struct val){.key = 1, .val = 1}.e);
Expand All @@ -125,9 +125,9 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_copy_no_alloc_fail)

CHECK_BEGIN_STATIC_FN(fhmap_test_copy_alloc)
{
flat_hash_map src = fhm_init((struct val *)NULL, 0, key, e, std_alloc,
flat_hash_map src = fhm_init((struct val *)NULL, 0, e, key, std_alloc,
fhmap_int_zero, fhmap_id_eq, NULL);
flat_hash_map dst = fhm_init((struct val *)NULL, 0, key, e, std_alloc,
flat_hash_map dst = fhm_init((struct val *)NULL, 0, e, key, std_alloc,
fhmap_int_zero, fhmap_id_eq, NULL);
(void)insert(&src, &(struct val){.key = 0}.e);
(void)insert(&src, &(struct val){.key = 1, .val = 1}.e);
Expand All @@ -153,9 +153,9 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_copy_alloc)

CHECK_BEGIN_STATIC_FN(fhmap_test_copy_alloc_fail)
{
flat_hash_map src = fhm_init((struct val *)NULL, 0, key, e, std_alloc,
flat_hash_map src = fhm_init((struct val *)NULL, 0, e, key, std_alloc,
fhmap_int_zero, fhmap_id_eq, NULL);
flat_hash_map dst = fhm_init((struct val *)NULL, 0, key, e, std_alloc,
flat_hash_map dst = fhm_init((struct val *)NULL, 0, e, key, std_alloc,
fhmap_int_zero, fhmap_id_eq, NULL);
(void)insert(&src, &(struct val){.key = 0}.e);
(void)insert(&src, &(struct val){.key = 1, .val = 1}.e);
Expand All @@ -178,7 +178,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_empty)

CHECK_BEGIN_STATIC_FN(fhmap_test_entry_functional)
{
flat_hash_map fh = fhm_init((struct val[5]){}, 5, key, e, NULL,
flat_hash_map fh = fhm_init((struct val[5]){}, 5, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
CHECK(is_empty(&fh), true);
struct val def = {.key = 137, .val = 0};
Expand All @@ -199,7 +199,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_functional)

CHECK_BEGIN_STATIC_FN(fhmap_test_entry_macros)
{
flat_hash_map fh = fhm_init((struct val[5]){}, 5, key, e, NULL,
flat_hash_map fh = fhm_init((struct val[5]){}, 5, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
CHECK(is_empty(&fh), true);
CHECK(get_key_val(&fh, &(int){137}) == NULL, true);
Expand All @@ -223,7 +223,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_macros)

CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_functional)
{
flat_hash_map fh = fhm_init((struct val[5]){}, 5, key, e, NULL,
flat_hash_map fh = fhm_init((struct val[5]){}, 5, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
CHECK(is_empty(&fh), true);
struct val def = {.key = 137, .val = 0};
Expand Down Expand Up @@ -261,7 +261,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_functional)

CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_macros)
{
flat_hash_map fh = fhm_init((struct val[5]){}, 5, key, e, NULL,
flat_hash_map fh = fhm_init((struct val[5]){}, 5, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);
CHECK(is_empty(&fh), true);

Expand Down
30 changes: 15 additions & 15 deletions tests/fhmap/test_fhmap_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CHECK_BEGIN_STATIC_FN(fill_n, ccc_flat_hash_map *const fh, size_t const n,
the user on insert. Leave this test here to always catch this. */
CHECK_BEGIN_STATIC_FN(fhmap_test_validate)
{
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);

ccc_entry ent = insert(&fh, &(struct val){.key = -1, .val = -1}.e);
Expand All @@ -86,7 +86,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_validate)
CHECK_BEGIN_STATIC_FN(fhmap_test_insert)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry ent = insert(&fh, &(struct val){.key = -1, .val = -1}.e);
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -143,7 +143,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_insert)
CHECK_BEGIN_STATIC_FN(fhmap_test_remove)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry ent = remove(&fh, &(struct val){.key = -1, .val = -1}.e);
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -212,7 +212,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_remove)
CHECK_BEGIN_STATIC_FN(fhmap_test_try_insert)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry ent = try_insert(&fh, &(struct val){.key = -1, .val = -1}.e);
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -268,7 +268,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_try_insert)
CHECK_BEGIN_STATIC_FN(fhmap_test_try_insert_with)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry *ent = fhm_try_insert_w(&fh, -1, val(-1));
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -325,7 +325,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_try_insert_with)
CHECK_BEGIN_STATIC_FN(fhmap_test_insert_or_assign)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry ent
= insert_or_assign(&fh, &(struct val){.key = -1, .val = -1}.e);
Expand Down Expand Up @@ -382,7 +382,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_insert_or_assign)
CHECK_BEGIN_STATIC_FN(fhmap_test_insert_or_assign_with)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_entry *ent = fhm_insert_or_assign_w(&fh, -1, val(-1));
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -438,7 +438,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_insert_or_assign_with)
CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_fhmap_entry *ent = entry_r(&fh, &(int){-1});
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -507,7 +507,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify)
CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_aux)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
int aux = 1;
ccc_fhmap_entry *ent = entry_r(&fh, &(int){-1});
Expand Down Expand Up @@ -573,7 +573,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_aux)
CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_with)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
ccc_fhmap_entry *ent = entry_r(&fh, &(int){-1});
ent = fhm_and_modify_w(ent, struct val, { T->val++; });
Expand Down Expand Up @@ -638,7 +638,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_entry_and_modify_with)
CHECK_BEGIN_STATIC_FN(fhmap_test_or_insert)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
struct val *v = or_insert(entry_r(&fh, &(int){-1}),
&(struct val){.key = -1, .val = -1}.e);
Expand Down Expand Up @@ -691,7 +691,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_or_insert)
CHECK_BEGIN_STATIC_FN(fhmap_test_or_insert_with)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
struct val *v = fhm_or_insert_w(entry_r(&fh, &(int){-1}), idval(-1, -1));
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -742,7 +742,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_or_insert_with)
CHECK_BEGIN_STATIC_FN(fhmap_test_insert_entry)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
struct val *v = insert_entry(entry_r(&fh, &(int){-1}),
&(struct val){.key = -1, .val = -1}.e);
Expand Down Expand Up @@ -795,7 +795,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_insert_entry)
CHECK_BEGIN_STATIC_FN(fhmap_test_insert_entry_with)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
struct val *v = fhm_insert_entry_w(entry_r(&fh, &(int){-1}), idval(-1, -1));
CHECK(validate(&fh), true);
Expand Down Expand Up @@ -846,7 +846,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_insert_entry_with)
CHECK_BEGIN_STATIC_FN(fhmap_test_remove_entry)
{
int size = 30;
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[50]){}, 50, e, key, NULL,
fhmap_int_to_u64, fhmap_id_eq, NULL);
struct val *v = or_insert(entry_r(&fh, &(int){-1}),
&(struct val){.key = -1, .val = -1}.e);
Expand Down
4 changes: 2 additions & 2 deletions tests/fhmap/test_fhmap_erase.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

CHECK_BEGIN_STATIC_FN(fhmap_test_erase)
{
ccc_flat_hash_map fh = fhm_init((struct val[10]){}, 10, key, e, NULL,
ccc_flat_hash_map fh = fhm_init((struct val[10]){}, 10, e, key, NULL,
fhmap_int_zero, fhmap_id_eq, NULL);

struct val query = {.key = 137, .val = 99};
Expand Down Expand Up @@ -43,7 +43,7 @@ CHECK_BEGIN_STATIC_FN(fhmap_test_erase)

CHECK_BEGIN_STATIC_FN(fhmap_test_shuffle_insert_erase)
{
ccc_flat_hash_map h = fhm_init((struct val *)NULL, 0, key, e, std_alloc,
ccc_flat_hash_map h = fhm_init((struct val *)NULL, 0, e, key, std_alloc,
fhmap_int_to_u64, fhmap_id_eq, NULL);

int const to_insert = 100;
Expand Down
Loading

0 comments on commit d336856

Please sign in to comment.