C Container Collection (CCC)
Loading...
Searching...
No Matches
handle_hash_map.h
Go to the documentation of this file.
1
45#ifndef CCC_HANDLE_HASH_MAP_H
46#define CCC_HANDLE_HASH_MAP_H
47
49#include <stddef.h>
52#include "impl/impl_handle_hash_map.h"
53#include "types.h"
54
64typedef struct ccc_hhmap_ ccc_handle_hash_map;
65
70typedef struct ccc_hhmap_elem_ ccc_hhmap_elem;
71
76typedef union ccc_hhmap_handle_ ccc_hhmap_handle;
77
99#define ccc_hhm_init(memory_ptr, hhash_elem_field, key_field, hash_fn, \
100 key_eq_fn, alloc_fn, aux_data, capacity) \
101 ccc_impl_hhm_init(memory_ptr, hhash_elem_field, key_field, hash_fn, \
102 key_eq_fn, alloc_fn, aux_data, capacity)
103
196 ccc_handle_hash_map const *src, ccc_alloc_fn *fn);
197
214[[nodiscard]] void *ccc_hhm_at(ccc_handle_hash_map const *h, ccc_handle_i i);
215
222#define ccc_hhm_as(handle_hash_map_ptr, type_name, handle_i...) \
223 ccc_impl_hhm_as(handle_hash_map_ptr, type_name, handle_i)
224
231 void const *key);
232
238 void const *key);
239
261 ccc_hhmap_elem *out_handle);
262
273#define ccc_hhm_swap_handle_r(handle_hash_map_ptr, out_handle_ptr) \
274 &(ccc_handle) \
275 { \
276 ccc_hhm_swap_handle((handle_hash_map_ptr), (out_handle_ptr)).impl_ \
277 }
278
290 ccc_hhmap_elem *out_handle);
291
303#define ccc_hhm_remove_r(handle_hash_map_ptr, out_handle_ptr) \
304 &(ccc_handle) \
305 { \
306 ccc_hhm_remove((handle_hash_map_ptr), (out_handle_ptr)).impl_ \
307 }
308
317 ccc_hhmap_elem *key_val_handle);
318
327#define ccc_hhm_try_insert_r(handle_hash_map_ptr, key_val_handle_ptr) \
328 &(ccc_handle) \
329 { \
330 ccc_hhm_try_insert((handle_hash_map_ptr), (key_val_handle_ptr)).impl_ \
331 }
332
348#define ccc_hhm_try_insert_w(handle_hash_map_ptr, key, lazy_value...) \
349 &(ccc_handle) \
350 { \
351 ccc_impl_hhm_try_insert_w(handle_hash_map_ptr, key, lazy_value) \
352 }
353
362[[nodiscard]] ccc_handle
364 ccc_hhmap_elem *key_val_handle);
365
375#define ccc_hhm_insert_or_assign_r(handle_hash_map_ptr, key_val_handle_ptr) \
376 &(ccc_handle) \
377 { \
378 ccc_hhm_insert_or_assign((handle_hash_map_ptr), (key_val_handle)) \
379 .impl_ \
380 }
381
394#define ccc_hhm_insert_or_assign_w(handle_hash_map_ptr, key, lazy_value...) \
395 &(ccc_handle) \
396 { \
397 ccc_impl_hhm_insert_or_assign_w(handle_hash_map_ptr, key, lazy_value) \
398 }
399
416 void const *key);
417
434#define ccc_hhm_handle_r(handle_hash_map_ptr, key_ptr) \
435 &(ccc_hhmap_handle) \
436 { \
437 ccc_hhm_handle((handle_hash_map_ptr), (key_ptr)).impl_ \
438 }
439
449 ccc_update_fn *fn);
450
459[[nodiscard]] ccc_hhmap_handle *
461
488#define ccc_hhm_and_modify_w(handle_hash_map_handle_ptr, type_name, \
489 closure_over_T...) \
490 &(ccc_hhmap_handle) \
491 { \
492 ccc_impl_hhm_and_modify_w(handle_hash_map_handle_ptr, type_name, \
493 closure_over_T) \
494 }
495
507 ccc_hhmap_elem *elem);
508
520#define ccc_hhm_or_insert_w(handle_hash_map_handle_ptr, lazy_key_value...) \
521 ccc_impl_hhm_or_insert_w(handle_hash_map_handle_ptr, lazy_key_value)
522
535 ccc_hhmap_elem *elem);
536
542#define ccc_hhm_insert_handle_w(handle_hash_map_handle_ptr, lazy_key_value...) \
543 ccc_impl_hhm_insert_handle_w(handle_hash_map_handle_ptr, lazy_key_value)
544
552
559#define ccc_hhm_remove_handle_r(handle_hash_map_handle_ptr) \
560 &(ccc_handle) \
561 { \
562 ccc_hhm_remove_handle((handle_hash_map_handle_ptr)).impl_ \
563 }
564
570
575
592
602[[nodiscard]] ccc_handle_status
604
619
630
646
654
662[[nodiscard]] ccc_tribool ccc_hhm_end(ccc_hhmap_handle const *iter);
663
674
679
688[[nodiscard]] ccc_ucount ccc_hhm_next_prime(size_t n);
689
694
702[[nodiscard]] void *ccc_hhm_data(ccc_handle_hash_map const *h);
703
709
714#ifdef HANDLE_HASH_MAP_USING_NAMESPACE_CCC
715typedef ccc_handle_hash_map handle_hash_map;
716typedef ccc_hhmap_elem hhmap_elem;
717typedef ccc_hhmap_handle hhmap_handle;
718# define hhm_init(args...) ccc_hhm_init(args)
719# define hhm_copy(args...) ccc_hhm_copy(args)
720# define hhm_contains(args...) ccc_hhm_contains(args)
721# define hhm_get_key_val(args...) ccc_hhm_get_key_val(args)
722# define hhm_at(args...) ccc_hhm_at(args)
723# define hhm_as(args...) ccc_hhm_as(args)
724# define hhm_swap_entry(args...) ccc_hhm_swap_entry(args)
725# define hhm_swap_entry_r(args...) ccc_hhm_swap_entry_r(args)
726# define hhm_remove(args...) ccc_hhm_remove(args)
727# define hhm_remove_r(args...) ccc_hhm_remove_r(args)
728# define hhm_try_insert(args...) ccc_hhm_try_insert(args)
729# define hhm_try_insert_r(args...) ccc_hhm_try_insert_r(args)
730# define hhm_try_insert_w(args...) ccc_hhm_try_insert_w(args)
731# define hhm_insert_or_assign(args...) ccc_hhm_insert_or_assign(args)
732# define hhm_insert_or_assign_r(args...) ccc_hhm_insert_or_assign_r(args)
733# define hhm_insert_or_assign_w(args...) ccc_hhm_insert_or_assign_w(args)
734# define hhm_handle(args...) ccc_hhm_handle(args)
735# define hhm_handle_r(args...) ccc_hhm_handle_r(args)
736# define hhm_and_modify(args...) ccc_hhm_and_modify(args)
737# define hhm_and_modify_aux(args...) ccc_hhm_and_modify_aux(args)
738# define hhm_and_modify_w(args...) ccc_hhm_and_modify_w(args)
739# define hhm_or_insert(args...) ccc_hhm_or_insert(args)
740# define hhm_or_insert_w(args...) ccc_hhm_or_insert_w(args)
741# define hhm_insert_handle(args...) ccc_hhm_insert_handle(args)
742# define hhm_insert_handle_w(args...) ccc_hhm_insert_handle_w(args)
743# define hhm_remove_handle(args...) ccc_hhm_remove_handle(args)
744# define hhm_remove_handle_r(args...) ccc_hhm_remove_handle_r(args)
745# define hhm_unwrap(args...) ccc_hhm_unwrap(args)
746# define hhm_occupied(args...) ccc_hhm_occupied(args)
747# define hhm_insert_error(args...) ccc_hhm_insert_error(args)
748# define hhm_handle_status(args...) ccc_hhm_handle_status(args)
749# define hhm_clear(args...) ccc_hhm_clear(args)
750# define hhm_clear_and_free(args...) ccc_hhm_clear_and_free(args)
751# define hhm_begin(args...) ccc_hhm_begin(args)
752# define hhm_next(args...) ccc_hhm_next(args)
753# define hhm_end(args...) ccc_hhm_end(args)
754# define hhm_is_empty(args...) ccc_hhm_is_empty(args)
755# define hhm_size(args...) ccc_hhm_size(args)
756# define hhm_next_prime(args...) ccc_hhm_next_prime(args)
757# define hhm_capacity(args...) ccc_hhm_capacity(args)
758# define hhm_data(args...) ccc_hhm_data(args)
759# define hhm_validate(args...) ccc_hhm_validate(args)
760#endif /* HANDLE_HASH_MAP_USING_NAMESPACE_CCC */
761
762#endif /* CCC_HANDLE_HASH_MAP_H */
ccc_handle_i ccc_hhm_insert_handle(ccc_hhmap_handle const *e, ccc_hhmap_elem *elem)
Inserts the provided handle invariantly.
ccc_handle ccc_hhm_try_insert(ccc_handle_hash_map *h, ccc_hhmap_elem *key_val_handle)
Attempts to insert the key value wrapping key_val_handle.
ccc_result ccc_hhm_next(ccc_hhmap_handle *iter)
Advances the iterator to the next occupied table handle.
ccc_handle ccc_hhm_remove_handle(ccc_hhmap_handle const *e)
Remove the handle from the table if Occupied.
ccc_handle ccc_hhm_remove(ccc_handle_hash_map *h, ccc_hhmap_elem *out_handle)
Removes the key value in the map storing the old value, if present, in the struct containing out_hand...
struct ccc_hhmap_ ccc_handle_hash_map
A container for storing key-value structures defined by the user in a contiguous buffer.
Definition: handle_hash_map.h:64
ccc_handle_i ccc_hhm_or_insert(ccc_hhmap_handle const *e, ccc_hhmap_elem *elem)
Inserts the struct with handle elem if the handle is Vacant.
ccc_hhmap_handle * ccc_hhm_and_modify(ccc_hhmap_handle *e, ccc_update_fn *fn)
Modifies the provided handle if it is Occupied.
struct ccc_hhmap_elem_ ccc_hhmap_elem
An intrusive element for a user provided type.
Definition: handle_hash_map.h:70
ccc_ucount ccc_hhm_size(ccc_handle_hash_map const *h)
Returns the size of the table representing active slots.
ccc_handle_status ccc_hhm_handle_status(ccc_hhmap_handle const *e)
Obtain the handle status from a container handle.
ccc_handle ccc_hhm_swap_handle(ccc_handle_hash_map *h, ccc_hhmap_elem *out_handle)
Invariantly inserts the key value wrapping out_handle.
ccc_tribool ccc_hhm_contains(ccc_handle_hash_map *h, void const *key)
Searches the table for the presence of key.
ccc_result ccc_hhm_clear(ccc_handle_hash_map *h, ccc_destructor_fn *fn)
Frees all slots in the table for use without affecting capacity.
void * ccc_hhm_data(ccc_handle_hash_map const *h)
Return a reference to the base of backing array. O(1).
ccc_tribool ccc_hhm_end(ccc_hhmap_handle const *iter)
Check if the current handle iterator has reached the end.
void * ccc_hhm_at(ccc_handle_hash_map const *h, ccc_handle_i i)
Returns a reference to the user data at the provided handle.
ccc_ucount ccc_hhm_capacity(ccc_handle_hash_map const *h)
Return the full capacity of the backing storage.
ccc_result ccc_hhm_clear_and_free(ccc_handle_hash_map *h, ccc_destructor_fn *fn)
Frees all slots in the table and frees the underlying buffer.
ccc_tribool ccc_hhm_occupied(ccc_hhmap_handle const *e)
Returns the Vacant or Occupied status of the handle.
ccc_handle_i ccc_hhm_unwrap(ccc_hhmap_handle const *e)
Unwraps the provided handle to obtain a handle index.
ccc_ucount ccc_hhm_next_prime(size_t n)
Helper to find a prime number if needed.
ccc_handle_i ccc_hhm_get_key_val(ccc_handle_hash_map *h, void const *key)
Returns a handle to the element stored at key if present.
ccc_handle ccc_hhm_insert_or_assign(ccc_handle_hash_map *h, ccc_hhmap_elem *key_val_handle)
Invariantly inserts or overwrites a user struct into the table.
ccc_hhmap_handle ccc_hhm_begin(ccc_handle_hash_map const *h)
Obtains a handle to the first element in the table.
ccc_hhmap_handle * ccc_hhm_and_modify_aux(ccc_hhmap_handle *e, ccc_update_fn *fn, void *aux)
Modifies the provided handle if it is Occupied.
ccc_hhmap_handle ccc_hhm_handle(ccc_handle_hash_map *h, void const *key)
Obtains a handle for the provided key in the table for future use.
ccc_tribool ccc_hhm_validate(ccc_handle_hash_map const *h)
Validation of invariants for the hash table.
ccc_tribool ccc_hhm_is_empty(ccc_handle_hash_map const *h)
Returns the size status of the table.
ccc_tribool ccc_hhm_insert_error(ccc_hhmap_handle const *e)
Provides the status of the handle should an insertion follow.
ccc_result ccc_hhm_copy(ccc_handle_hash_map *dst, ccc_handle_hash_map const *src, ccc_alloc_fn *fn)
Copy the map at source to destination.
union ccc_hhmap_handle_ ccc_hhmap_handle
A container specific handle used to implement the Handle Interface.
Definition: handle_hash_map.h:76
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
union ccc_handle_ ccc_handle
An Occupied or Vacant handle to a flat searchable container entry.
Definition: types.h:66
size_t ccc_handle_i
A stable index to user data in a container that uses a flat array as the underlying storage method.
Definition: types.h:78
ccc_result
A result of actions on containers.
Definition: types.h:117
enum ccc_entry_status_ ccc_handle_status
The status monitoring and handle state once it is obtained.
Definition: types.h:88
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
void ccc_destructor_fn(ccc_user_type)
A callback function for destroying an element in the container.
Definition: types.h:318