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
110#define ccc_fhm_init(memory_ptr, fhash_elem_field, key_field, hash_fn, \
111 key_eq_fn, alloc_fn, aux_data, capacity) \
112 ccc_impl_fhm_init(memory_ptr, fhash_elem_field, key_field, hash_fn, \
113 key_eq_fn, alloc_fn, aux_data, capacity)
114
204 ccc_alloc_fn *fn);
205
218 void const *key);
219
224[[nodiscard]] void *ccc_fhm_get_key_val(ccc_flat_hash_map *h, void const *key);
225
244 ccc_fhmap_elem *out_handle);
245
257#define ccc_fhm_swap_entry_r(flat_hash_map_ptr, out_handle_ptr) \
258 &(ccc_entry) \
259 { \
260 ccc_fhm_swap_entry((flat_hash_map_ptr), (out_handle_ptr)).impl_ \
261 }
262
274 ccc_fhmap_elem *out_handle);
275
286#define ccc_fhm_remove_r(flat_hash_map_ptr, out_handle_ptr) \
287 &(ccc_entry) \
288 { \
289 ccc_fhm_remove((flat_hash_map_ptr), (out_handle_ptr)).impl_ \
290 }
291
302 ccc_fhmap_elem *key_val_handle);
303
314#define ccc_fhm_try_insert_r(flat_hash_map_ptr, key_val_handle_ptr) \
315 &(ccc_entry) \
316 { \
317 ccc_fhm_try_insert((flat_hash_map_ptr), (key_val_handle_ptr)).impl_ \
318 }
319
335#define ccc_fhm_try_insert_w(flat_hash_map_ptr, key, lazy_value...) \
336 &(ccc_entry) \
337 { \
338 ccc_impl_fhm_try_insert_w(flat_hash_map_ptr, key, lazy_value) \
339 }
340
349[[nodiscard]] ccc_entry
351
360#define ccc_fhm_insert_or_assign_r(flat_hash_map_ptr, key_val_handle_ptr) \
361 &(ccc_entry) \
362 { \
363 ccc_fhm_insert_or_assign((flat_hash_map_ptr), (key_val_handle)).impl_ \
364 }
365
378#define ccc_fhm_insert_or_assign_w(flat_hash_map_ptr, key, lazy_value...) \
379 &(ccc_entry) \
380 { \
381 ccc_impl_fhm_insert_or_assign_w(flat_hash_map_ptr, key, lazy_value) \
382 }
383
400 void const *key);
401
418#define ccc_fhm_entry_r(flat_hash_map_ptr, key_ptr) \
419 &(ccc_fhmap_entry) \
420 { \
421 ccc_fhm_entry((flat_hash_map_ptr), (key_ptr)).impl_ \
422 }
423
433 ccc_update_fn *fn);
434
443[[nodiscard]] ccc_fhmap_entry *
445
472#define ccc_fhm_and_modify_w(flat_hash_map_entry_ptr, type_name, \
473 closure_over_T...) \
474 &(ccc_fhmap_entry) \
475 { \
476 ccc_impl_fhm_and_modify_w(flat_hash_map_entry_ptr, type_name, \
477 closure_over_T) \
478 }
479
489[[nodiscard]] void *ccc_fhm_or_insert(ccc_fhmap_entry const *e,
490 ccc_fhmap_elem *elem);
491
503#define ccc_fhm_or_insert_w(flat_hash_map_entry_ptr, lazy_key_value...) \
504 ccc_impl_fhm_or_insert_w(flat_hash_map_entry_ptr, lazy_key_value)
505
517[[nodiscard]] void *ccc_fhm_insert_entry(ccc_fhmap_entry const *e,
518 ccc_fhmap_elem *elem);
519
525#define ccc_fhm_insert_entry_w(flat_hash_map_entry_ptr, lazy_key_value...) \
526 ccc_impl_fhm_insert_entry_w(flat_hash_map_entry_ptr, lazy_key_value)
527
533
538#define ccc_fhm_remove_entry_r(flat_hash_map_entry_ptr) \
539 &(ccc_entry) \
540 { \
541 ccc_fhm_remove_entry((flat_hash_map_entry_ptr)).impl_ \
542 }
543
547[[nodiscard]] void *ccc_fhm_unwrap(ccc_fhmap_entry const *e);
548
553
570
581
596
606
621[[nodiscard]] void *ccc_fhm_begin(ccc_flat_hash_map const *h);
622
629[[nodiscard]] void *ccc_fhm_next(ccc_flat_hash_map const *h,
630 ccc_fhmap_elem const *iter);
631
636[[nodiscard]] void *ccc_fhm_end(ccc_flat_hash_map const *h);
637
648
653
662[[nodiscard]] ccc_ucount ccc_fhm_next_prime(size_t n);
663
668
676[[nodiscard]] void *ccc_fhm_data(ccc_flat_hash_map const *h);
677
683
688#ifdef FLAT_HASH_MAP_USING_NAMESPACE_CCC
689typedef ccc_fhmap_elem fhmap_elem;
690typedef ccc_flat_hash_map flat_hash_map;
691typedef ccc_fhmap_entry fhmap_entry;
692# define fhm_init(args...) ccc_fhm_init(args)
693# define fhm_static_init(args...) ccc_fhm_static_init(args)
694# define fhm_zero_init(args...) ccc_fhm_zero_init(args)
695# define fhm_copy(args...) ccc_fhm_copy(args)
696# define fhm_and_modify_w(args...) ccc_fhm_and_modify_w(args)
697# define fhm_or_insert_w(args...) ccc_fhm_or_insert_w(args)
698# define fhm_insert_entry_w(args...) ccc_fhm_insert_entry_w(args)
699# define fhm_try_insert_w(args...) ccc_fhm_try_insert_w(args)
700# define fhm_insert_or_assign_w(args...) ccc_fhm_insert_or_assign_w(args)
701# define fhm_contains(args...) ccc_fhm_contains(args)
702# define fhm_get_key_val(args...) ccc_fhm_get_key_val(args)
703# define fhm_remove_r(args...) ccc_fhm_remove_r(args)
704# define fhm_swap_entry_r(args...) ccc_fhm_swap_entry_r(args)
705# define fhm_try_insert_r(args...) ccc_fhm_try_insert_r(args)
706# define fhm_insert_or_assign_r(args...) ccc_fhm_insert_or_assign_r(args)
707# define fhm_remove_entry_r(args...) ccc_fhm_remove_entry_r(args)
708# define fhm_remove(args...) ccc_fhm_remove(args)
709# define fhm_swap_entry(args...) ccc_fhm_swap_entry(args)
710# define fhm_try_insert(args...) ccc_fhm_try_insert(args)
711# define fhm_insert_or_assign(args...) ccc_fhm_insert_or_assign(args)
712# define fhm_remove_entry(args...) ccc_fhm_remove_entry(args)
713# define fhm_entry_r(args...) ccc_fhm_entry_r(args)
714# define fhm_entry(args...) ccc_fhm_entry(args)
715# define fhm_and_modify(args...) ccc_fhm_and_modify(args)
716# define fhm_and_modify_aux(args...) ccc_fhm_and_modify_aux(args)
717# define fhm_or_insert(args...) ccc_fhm_or_insert(args)
718# define fhm_insert_entry(args...) ccc_fhm_insert_entry(args)
719# define fhm_unwrap(args...) ccc_fhm_unwrap(args)
720# define fhm_occupied(args...) ccc_fhm_occupied(args)
721# define fhm_insert_error(args...) ccc_fhm_insert_error(args)
722# define fhm_begin(args...) ccc_fhm_begin(args)
723# define fhm_next(args...) ccc_fhm_next(args)
724# define fhm_end(args...) ccc_fhm_end(args)
725# define fhm_data(args...) ccc_fhm_data(args)
726# define fhm_is_empty(args...) ccc_fhm_is_empty(args)
727# define fhm_size(args...) ccc_fhm_size(args)
728# define fhm_clear(args...) ccc_fhm_clear(args)
729# define fhm_clear_and_free(args...) ccc_fhm_clear_and_free(args)
730# define fhm_next_prime(args...) ccc_fhm_next_prime(args)
731# define fhm_capacity(args...) ccc_fhm_capacity(args)
732# define fhm_validate(args...) ccc_fhm_validate(args)
733#endif
734
735#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.
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.
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.
ccc_ucount ccc_fhm_next_prime(size_t n)
Helper to find a prime number if needed.
void * ccc_fhm_begin(ccc_flat_hash_map const *h)
Obtains a pointer to the first element in 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.
ccc_tribool ccc_fhm_is_empty(ccc_flat_hash_map const *h)
Returns the size status of the table.
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.
ccc_entry ccc_fhm_swap_entry(ccc_flat_hash_map *h, ccc_fhmap_elem *out_handle)
Invariantly inserts the key value wrapping out_handle.
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_ucount ccc_fhm_size(ccc_flat_hash_map const *h)
Returns the size of the table representing active slots.
ccc_tribool ccc_fhm_insert_error(ccc_fhmap_entry const *e)
Provides the status of the entry should an insertion follow.
ccc_tribool ccc_fhm_validate(ccc_flat_hash_map const *h)
Validation of invariants for the hash table.
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.
ccc_ucount ccc_fhm_capacity(ccc_flat_hash_map const *h)
Return the full capacity of the backing storage.
ccc_tribool ccc_fhm_occupied(ccc_fhmap_entry const *e)
Returns the Vacant or Occupied status of the entry.
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.
union ccc_fhmap_entry_ ccc_fhmap_entry
A container specific entry used to implement the Entry Interface.
Definition: flat_hash_map.h:55
ccc_tribool ccc_fhm_contains(ccc_flat_hash_map *h, void const *key)
Searches the table for the presence of key.
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...
A type for returning an unsigned integer from a container for counting. Intended to count sizes,...
Definition: types.h:172
The C Container Collection Fundamental Types.
void ccc_update_fn(ccc_user_type)
A callback function for modifying an element in the container.
Definition: types.h:300
ccc_result
A result of actions on containers.
Definition: types.h:117
union ccc_entry_ ccc_entry
An Occupied or Vacant position in a searchable container.
Definition: types.h:46
ccc_tribool
A three state boolean to allow for an error state. Error is -1, False is 0, and True is 1.
Definition: types.h:102
void * ccc_alloc_fn(void *ptr, size_t size, void *aux)
An allocation function at the core of all containers.
Definition: types.h:283
enum ccc_entry_status_ ccc_entry_status
The status monitoring and entry state once it is obtained.
Definition: types.h:56
void ccc_destructor_fn(ccc_user_type)
A callback function for destroying an element in the container.
Definition: types.h:318