C Container Collection (CCC)
Loading...
Searching...
No Matches
flat_hash_map.h
Go to the documentation of this file.
1
24#ifndef CCC_FLAT_HASH_MAP_H
25#define CCC_FLAT_HASH_MAP_H
26
28#include <stddef.h>
31#include "impl/impl_flat_hash_map.h"
32#include "types.h"
33
43typedef struct ccc_fhmap_ ccc_flat_hash_map;
44
49typedef struct ccc_fhmap_elem_ ccc_fhmap_elem;
50
55typedef union ccc_fhmap_entry_ ccc_fhmap_entry;
56
78#define ccc_fhm_init(memory_ptr, capacity, fhash_elem_field, key_field, \
79 alloc_fn, hash_fn, key_eq_fn, aux_data) \
80 ccc_impl_fhm_init(memory_ptr, capacity, fhash_elem_field, key_field, \
81 alloc_fn, hash_fn, key_eq_fn, aux_data)
82
168 ccc_alloc_fn *fn);
169
180[[nodiscard]] bool ccc_fhm_contains(ccc_flat_hash_map *h, void const *key);
181
186[[nodiscard]] void *ccc_fhm_get_key_val(ccc_flat_hash_map *h, void const *key);
187
206 ccc_fhmap_elem *out_handle);
207
219#define ccc_fhm_insert_r(flat_hash_map_ptr, out_handle_ptr) \
220 &(ccc_entry) \
221 { \
222 ccc_fhm_insert((flat_hash_map_ptr), (out_handle_ptr)).impl_ \
223 }
224
236 ccc_fhmap_elem *out_handle);
237
248#define ccc_fhm_remove_r(flat_hash_map_ptr, out_handle_ptr) \
249 &(ccc_entry) \
250 { \
251 ccc_fhm_remove((flat_hash_map_ptr), (out_handle_ptr)).impl_ \
252 }
253
264 ccc_fhmap_elem *key_val_handle);
265
276#define ccc_fhm_try_insert_r(flat_hash_map_ptr, key_val_handle_ptr) \
277 &(ccc_entry) \
278 { \
279 ccc_fhm_try_insert((flat_hash_map_ptr), (key_val_handle_ptr)).impl_ \
280 }
281
297#define ccc_fhm_try_insert_w(flat_hash_map_ptr, key, lazy_value...) \
298 &(ccc_entry) \
299 { \
300 ccc_impl_fhm_try_insert_w(flat_hash_map_ptr, key, lazy_value) \
301 }
302
311[[nodiscard]] ccc_entry
313
322#define ccc_fhm_insert_or_assign_r(flat_hash_map_ptr, key_val_handle_ptr) \
323 &(ccc_entry) \
324 { \
325 ccc_fhm_insert_or_assign((flat_hash_map_ptr), (key_val_handle)).impl_ \
326 }
327
340#define ccc_fhm_insert_or_assign_w(flat_hash_map_ptr, key, lazy_value...) \
341 &(ccc_entry) \
342 { \
343 ccc_impl_fhm_insert_or_assign_w(flat_hash_map_ptr, key, lazy_value) \
344 }
345
362 void const *key);
363
380#define ccc_fhm_entry_r(flat_hash_map_ptr, key_ptr) \
381 &(ccc_fhmap_entry) \
382 { \
383 ccc_fhm_entry((flat_hash_map_ptr), (key_ptr)).impl_ \
384 }
385
395 ccc_update_fn *fn);
396
405[[nodiscard]] ccc_fhmap_entry *
407
434#define ccc_fhm_and_modify_w(flat_hash_map_entry_ptr, type_name, \
435 closure_over_T...) \
436 &(ccc_fhmap_entry) \
437 { \
438 ccc_impl_fhm_and_modify_w(flat_hash_map_entry_ptr, type_name, \
439 closure_over_T) \
440 }
441
451[[nodiscard]] void *ccc_fhm_or_insert(ccc_fhmap_entry const *e,
452 ccc_fhmap_elem *elem);
453
465#define ccc_fhm_or_insert_w(flat_hash_map_entry_ptr, lazy_key_value...) \
466 ccc_impl_fhm_or_insert_w(flat_hash_map_entry_ptr, lazy_key_value)
467
479[[nodiscard]] void *ccc_fhm_insert_entry(ccc_fhmap_entry const *e,
480 ccc_fhmap_elem *elem);
481
487#define ccc_fhm_insert_entry_w(flat_hash_map_entry_ptr, lazy_key_value...) \
488 ccc_impl_fhm_insert_entry_w(flat_hash_map_entry_ptr, lazy_key_value)
489
495
500#define ccc_fhm_remove_entry_r(flat_hash_map_entry_ptr) \
501 &(ccc_entry) \
502 { \
503 ccc_fhm_remove_entry((flat_hash_map_entry_ptr)).impl_ \
504 }
505
509[[nodiscard]] void *ccc_fhm_unwrap(ccc_fhmap_entry const *e);
510
514[[nodiscard]] bool ccc_fhm_occupied(ccc_fhmap_entry const *e);
515
531[[nodiscard]] bool ccc_fhm_insert_error(ccc_fhmap_entry const *e);
532
543
558
568
583[[nodiscard]] void *ccc_fhm_begin(ccc_flat_hash_map const *h);
584
591[[nodiscard]] void *ccc_fhm_next(ccc_flat_hash_map const *h,
592 ccc_fhmap_elem const *iter);
593
598[[nodiscard]] void *ccc_fhm_end(ccc_flat_hash_map const *h);
599
609[[nodiscard]] bool ccc_fhm_is_empty(ccc_flat_hash_map const *h);
610
614[[nodiscard]] size_t ccc_fhm_size(ccc_flat_hash_map const *h);
615
624[[nodiscard]] size_t ccc_fhm_next_prime(size_t n);
625
629[[nodiscard]] size_t ccc_fhm_capacity(ccc_flat_hash_map const *h);
630
638[[nodiscard]] void *ccc_fhm_data(ccc_flat_hash_map const *h);
639
643[[nodiscard]] bool ccc_fhm_validate(ccc_flat_hash_map const *h);
644
649#ifdef FLAT_HASH_MAP_USING_NAMESPACE_CCC
650typedef ccc_fhmap_elem fhmap_elem;
651typedef ccc_flat_hash_map flat_hash_map;
652typedef ccc_fhmap_entry fhmap_entry;
653# define fhm_init(args...) ccc_fhm_init(args)
654# define fhm_static_init(args...) ccc_fhm_static_init(args)
655# define fhm_zero_init(args...) ccc_fhm_zero_init(args)
656# define fhm_copy(args...) ccc_fhm_copy(args)
657# define fhm_and_modify_w(args...) ccc_fhm_and_modify_w(args)
658# define fhm_or_insert_w(args...) ccc_fhm_or_insert_w(args)
659# define fhm_insert_entry_w(args...) ccc_fhm_insert_entry_w(args)
660# define fhm_try_insert_w(args...) ccc_fhm_try_insert_w(args)
661# define fhm_insert_or_assign_w(args...) ccc_fhm_insert_or_assign_w(args)
662# define fhm_contains(args...) ccc_fhm_contains(args)
663# define fhm_get_key_val(args...) ccc_fhm_get_key_val(args)
664# define fhm_remove_r(args...) ccc_fhm_remove_r(args)
665# define fhm_insert_r(args...) ccc_fhm_insert_r(args)
666# define fhm_try_insert_r(args...) ccc_fhm_try_insert_r(args)
667# define fhm_insert_or_assign_r(args...) ccc_fhm_insert_or_assign_r(args)
668# define fhm_remove_entry_r(args...) ccc_fhm_remove_entry_r(args)
669# define fhm_remove(args...) ccc_fhm_remove(args)
670# define fhm_insert(args...) ccc_fhm_insert(args)
671# define fhm_try_insert(args...) ccc_fhm_try_insert(args)
672# define fhm_insert_or_assign(args...) ccc_fhm_insert_or_assign(args)
673# define fhm_remove_entry(args...) ccc_fhm_remove_entry(args)
674# define fhm_entry_r(args...) ccc_fhm_entry_r(args)
675# define fhm_entry(args...) ccc_fhm_entry(args)
676# define fhm_and_modify(args...) ccc_fhm_and_modify(args)
677# define fhm_and_modify_aux(args...) ccc_fhm_and_modify_aux(args)
678# define fhm_or_insert(args...) ccc_fhm_or_insert(args)
679# define fhm_insert_entry(args...) ccc_fhm_insert_entry(args)
680# define fhm_unwrap(args...) ccc_fhm_unwrap(args)
681# define fhm_occupied(args...) ccc_fhm_occupied(args)
682# define fhm_insert_error(args...) ccc_fhm_insert_error(args)
683# define fhm_begin(args...) ccc_fhm_begin(args)
684# define fhm_next(args...) ccc_fhm_next(args)
685# define fhm_end(args...) ccc_fhm_end(args)
686# define fhm_data(args...) ccc_fhm_data(args)
687# define fhm_is_empty(args...) ccc_fhm_is_empty(args)
688# define fhm_size(args...) ccc_fhm_size(args)
689# define fhm_clear(args...) ccc_fhm_clear(args)
690# define fhm_clear_and_free(args...) ccc_fhm_clear_and_free(args)
691# define fhm_next_prime(args...) ccc_fhm_next_prime(args)
692# define fhm_capacity(args...) ccc_fhm_capacity(args)
693# define fhm_validate(args...) ccc_fhm_validate(args)
694#endif
695
696#endif /* CCC_FLAT_HASH_MAP_H */
void * ccc_fhm_next(ccc_flat_hash_map const *h, ccc_fhmap_elem const *iter)
Advances the iterator to the next occupied table slot.
size_t ccc_fhm_next_prime(size_t n)
Helper to find a prime number if needed.
ccc_entry ccc_fhm_insert_or_assign(ccc_flat_hash_map *h, ccc_fhmap_elem *key_val_handle)
Invariantly inserts or overwrites a user struct into the table.
bool ccc_fhm_contains(ccc_flat_hash_map *h, void const *key)
Searches the table for the presence of key.
ccc_fhmap_entry * ccc_fhm_and_modify_aux(ccc_fhmap_entry *e, ccc_update_fn *fn, void *aux)
Modifies the provided entry if it is Occupied.
void * ccc_fhm_begin(ccc_flat_hash_map const *h)
Obtains a pointer to the first element in the table.
ccc_entry ccc_fhm_insert(ccc_flat_hash_map *h, ccc_fhmap_elem *out_handle)
Invariantly inserts the key value wrapping out_handle.
bool ccc_fhm_occupied(ccc_fhmap_entry const *e)
Returns the Vacant or Occupied status of the entry.
size_t ccc_fhm_size(ccc_flat_hash_map const *h)
Returns the size of the table.
ccc_result ccc_fhm_clear_and_free(ccc_flat_hash_map *h, ccc_destructor_fn *fn)
Frees all slots in the table and frees the underlying buffer.
void * ccc_fhm_insert_entry(ccc_fhmap_entry const *e, ccc_fhmap_elem *elem)
Inserts the provided entry invariantly.
ccc_entry_status ccc_fhm_entry_status(ccc_fhmap_entry const *e)
Obtain the entry status from a container entry.
ccc_fhmap_entry * ccc_fhm_and_modify(ccc_fhmap_entry *e, ccc_update_fn *fn)
Modifies the provided entry if it is Occupied.
bool ccc_fhm_is_empty(ccc_flat_hash_map const *h)
Returns the size status of the table.
ccc_result ccc_fhm_copy(ccc_flat_hash_map *dst, ccc_flat_hash_map const *src, ccc_alloc_fn *fn)
Copy the map at source to destination.
ccc_result ccc_fhm_clear(ccc_flat_hash_map *h, ccc_destructor_fn *fn)
Frees all slots in the table for use without affecting capacity.
void * ccc_fhm_get_key_val(ccc_flat_hash_map *h, void const *key)
Returns a reference into the table at entry key.
ccc_entry ccc_fhm_try_insert(ccc_flat_hash_map *h, ccc_fhmap_elem *key_val_handle)
Attempts to insert the key value wrapping key_val_handle.
bool ccc_fhm_insert_error(ccc_fhmap_entry const *e)
Provides the status of the entry should an insertion follow.
ccc_entry ccc_fhm_remove_entry(ccc_fhmap_entry const *e)
Remove the entry from the table if Occupied.
struct ccc_fhmap_elem_ ccc_fhmap_elem
An intrusive element for a user provided type.
Definition: flat_hash_map.h:49
ccc_fhmap_entry ccc_fhm_entry(ccc_flat_hash_map *h, void const *key)
Obtains an entry for the provided key in the table for future use.
void * ccc_fhm_or_insert(ccc_fhmap_entry const *e, ccc_fhmap_elem *elem)
Inserts the struct with handle elem if the entry is Vacant.
void * ccc_fhm_end(ccc_flat_hash_map const *h)
Check the current iterator against the end for loop termination.
void * ccc_fhm_unwrap(ccc_fhmap_entry const *e)
Unwraps the provided entry to obtain a view into the table element.
bool ccc_fhm_validate(ccc_flat_hash_map const *h)
Validation of invariants for the hash table.
union ccc_fhmap_entry_ ccc_fhmap_entry
A container specific entry used to implement the Entry Interface.
Definition: flat_hash_map.h:55
size_t ccc_fhm_capacity(ccc_flat_hash_map const *h)
Return the full capacity of the backing storage.
struct ccc_fhmap_ ccc_flat_hash_map
A container for storing key-value structures defined by the user in a contiguous buffer.
Definition: flat_hash_map.h:43
void * ccc_fhm_data(ccc_flat_hash_map const *h)
Return a reference to the base of backing array. O(1).
ccc_entry ccc_fhm_remove(ccc_flat_hash_map *h, ccc_fhmap_elem *out_handle)
Removes the key value in the map storing the old value, if present, in the struct containing out_hand...
The C Container Collection Fundamental Types.
ccc_result
A result of actions on containers.
Definition: types.h:65
void ccc_update_fn(ccc_user_type)
A callback function for modifying an element in the container.
Definition: types.h:216
union ccc_entry_ ccc_entry
An Occupied or Vacant position in a searchable container.
Definition: types.h:47
void * ccc_alloc_fn(void *ptr, size_t size, void *aux)
An allocation function at the core of all containers.
Definition: types.h:199
enum ccc_entry_status_ ccc_entry_status
The status monitoring and entry state once it is obtained.
Definition: types.h:57
void ccc_destructor_fn(ccc_user_type)
A callback function for destroying an element in the container.
Definition: types.h:234