|
1 | | -#ifndef FHASH_TABLE_H |
2 | | -#define FHASH_TABLE_H |
| 1 | +#ifndef FHASH_MAP_H |
| 2 | +#define FHASH_MAP_H |
3 | 3 |
|
4 | 4 | #include <cerpec.h> |
5 | 5 |
|
6 | 6 | /// @brief Finite hash table data structure. |
7 | | -typedef struct finite_hash_table { |
8 | | - hash_fn hash; |
| 7 | +typedef struct finite_hash_map { |
| 8 | + hash_fn hash_key; |
| 9 | + compare_fn compare_key; |
9 | 10 | char * keys, * values; |
10 | | - size_t * next, * head; |
11 | | - size_t key_size, value_size, empty, length, max; |
| 11 | + size_t * next, * prev, * head, * hashes; |
| 12 | + size_t key_size, value_size, length, max; |
12 | 13 | memory_s const * allocator; |
13 | | -} fhash_table_s; |
| 14 | +} fhash_map_s; |
14 | 15 |
|
15 | 16 | /// @brief Creates an empty structure. |
16 | 17 | /// @param key_size Size of a single key. |
17 | 18 | /// @param value_size Size of a single value. |
18 | 19 | /// @param max Maximum length of structure. |
19 | | -/// @param hash Function pointer to hash element into value. |
| 20 | +/// @param compare_key Function pointer to compare keys. |
| 21 | +/// @param hash_key Function pointer to hash element into value. |
20 | 22 | /// @return Table structure. |
21 | | -fhash_table_s create_fhash_table(size_t const key_size, size_t const value_size, size_t const max, hash_fn const hash); |
| 23 | +fhash_map_s create_fhash_map(size_t const key_size, size_t const value_size, size_t const max, hash_fn const hash_key, compare_fn const compare_key); |
22 | 24 |
|
23 | 25 | /// @brief Creates an empty structure. |
24 | 26 | /// @param key_size Size of a single key. |
25 | 27 | /// @param value_size Size of a single value. |
26 | 28 | /// @param max Maximum length of structure. |
27 | | -/// @param hash Function pointer to hash element into value. |
| 29 | +/// @param hash_key Function pointer to hash element into value. |
| 30 | +/// @param compare_key Function pointer to compare keys. |
28 | 31 | /// @param allocator Custom allocator structure. |
29 | 32 | /// @return Table structure. |
30 | | -fhash_table_s make_fhash_table(size_t const key_size, size_t const value_size, size_t const max, hash_fn const hash, memory_s const * const allocator); |
| 33 | +fhash_map_s make_fhash_map(size_t const key_size, size_t const value_size, size_t const max, hash_fn const hash_key, compare_fn const compare_key, memory_s const * const allocator); |
31 | 34 |
|
32 | 35 | /// @brief Destroys a structure, and its elements and makes it unusable. |
33 | | -/// @param table Structure to destroy. |
| 36 | +/// @param map Structure to destroy. |
34 | 37 | /// @param destroy_key Function pointer to destroy a single key. |
35 | 38 | /// @param destroy_value Function pointer to destroy a single value. |
36 | | -void destroy_fhash_table(fhash_table_s * const table, set_fn const destroy_key, set_fn const destroy_value); |
| 39 | +void destroy_fhash_map(fhash_map_s * const map, set_fn const destroy_key, set_fn const destroy_value); |
37 | 40 |
|
38 | 41 | /// @brief Clears a structure, and destroys its elements, but remains usable. |
39 | | -/// @param table Structure to destroy. |
| 42 | +/// @param map Structure to destroy. |
40 | 43 | /// @param destroy_key Function pointer to destroy a single key. |
41 | 44 | /// @param destroy_value Function pointer to destroy a single value. |
42 | | -void clear_fhash_table(fhash_table_s * table, const set_fn destroy_key, const set_fn destroy_value); |
| 45 | +void clear_fhash_map(fhash_map_s * map, const set_fn destroy_key, const set_fn destroy_value); |
43 | 46 |
|
44 | 47 | /// @brief Creates a copy of a structure and all its elements. |
45 | | -/// @param table Structure to copy. |
| 48 | +/// @param map Structure to copy. |
46 | 49 | /// @param copy_key Function pointer to create a deep/shallow copy of a single key. |
47 | 50 | /// @param copy_value Function pointer to create a deep/shallow copy of a single value. |
48 | 51 | /// @return Stack structure. |
49 | | -fhash_table_s copy_fhash_table(fhash_table_s const * const table, copy_fn const copy_key, copy_fn const copy_value); |
| 52 | +fhash_map_s copy_fhash_map(fhash_map_s const * const map, copy_fn const copy_key, copy_fn const copy_value); |
50 | 53 |
|
51 | 54 | /// @brief Checks if structure is empty. |
52 | | -/// @param table Structure to check. |
| 55 | +/// @param map Structure to check. |
53 | 56 | /// @return 'true' if empty, 'false' if not. |
54 | | -bool is_empty_fhash_table(fhash_table_s const * const table); |
| 57 | +bool is_empty_fhash_map(fhash_map_s const * const map); |
55 | 58 |
|
56 | 59 | /// @brief Checks if structure is full. |
57 | | -/// @param table Structure to check. |
| 60 | +/// @param map Structure to check. |
58 | 61 | /// @return 'true' if full, 'false' if not. |
59 | | -bool is_full_fhash_table(fhash_table_s const * const table); |
| 62 | +bool is_full_fhash_map(fhash_map_s const * const map); |
60 | 63 |
|
61 | 64 | /// @brief Inserts unique key/value pair into structure. |
62 | | -/// @param table Structure to insert element into. |
| 65 | +/// @param map Structure to insert element into. |
63 | 66 | /// @param key Key to insert. |
64 | 67 | /// @param value Value to insert. |
65 | | -void insert_fhash_table(fhash_table_s * const table, void const * const key, void const * const value); |
| 68 | +void insert_fhash_map(fhash_map_s * const map, void const * const key, void const * const value); |
66 | 69 |
|
67 | 70 | /// @brief Removes unique key/value pair from structure. |
68 | | -/// @param table Structure to remove element into. |
| 71 | +/// @param map Structure to remove element into. |
69 | 72 | /// @param key Key to remove. |
70 | 73 | /// @param key_buffer Key buffer to save removed key. |
71 | 74 | /// @param value_buffer Value buffer to save removed value. |
72 | | -void remove_fhash_table(fhash_table_s * const table, void const * const key, void * const key_buffer, void * const value_buffer); |
| 75 | +void remove_fhash_map(fhash_map_s * const map, void const * const key, void * const key_buffer, void * const value_buffer); |
73 | 76 |
|
74 | 77 | /// @brief Checks if structure contains key. |
75 | | -/// @param table Structure to check. |
| 78 | +/// @param map Structure to check. |
76 | 79 | /// @param key Key to check. |
77 | 80 | /// @return 'true' if contained, 'false' otherwise. |
78 | | -bool contains_key_fhash_table(fhash_table_s const * const table, void const * const key); |
| 81 | +bool contains_key_fhash_map(fhash_map_s const * const map, void const * const key); |
79 | 82 |
|
80 | 83 | /// @brief Gets value based on key. |
81 | | -/// @param table Structure to get value. |
| 84 | +/// @param map Structure to get value. |
82 | 85 | /// @param key Key to value. |
83 | 86 | /// @param value_buffer Value buffer to save retrieved value. |
84 | | -void get_value_fhash_table(fhash_table_s const * const table, void const * const key, void * const value_buffer); |
| 87 | +void get_value_fhash_map(fhash_map_s const * const map, void const * const key, void * const value_buffer); |
85 | 88 |
|
86 | 89 | /// @brief Sets value based on key. |
87 | | -/// @param table Structure to set value. |
| 90 | +/// @param map Structure to set value. |
88 | 91 | /// @param key Key to value. |
89 | 92 | /// @param value New value to insert. |
90 | 93 | /// @param value_buffer Old value buffer to save replaced value. |
91 | | -void set_value_fhash_table(fhash_table_s const * const table, void const * const key, void const * const value, void * const value_buffer); |
| 94 | +void set_value_fhash_map(fhash_map_s const * const map, void const * const key, void const * const value, void * const value_buffer); |
92 | 95 |
|
93 | 96 | /// @brief Iterates over each key in structure. |
94 | | -/// @param table Structure to iterate over. |
| 97 | +/// @param map Structure to iterate over. |
95 | 98 | /// @param handle Function pointer to handle each element reference using generic arguments. |
96 | 99 | /// @param arguments Generic arguments to use in function pointer. |
97 | | -void map_key_fhash_table(fhash_table_s const * const table, handle_fn const handle, void * const arguments); |
| 100 | +void map_key_fhash_map(fhash_map_s const * const map, handle_fn const handle, void * const arguments); |
98 | 101 |
|
99 | 102 | /// @brief Iterates over each value in structure. |
100 | | -/// @param table Structure to iterate over. |
| 103 | +/// @param map Structure to iterate over. |
101 | 104 | /// @param handle Function pointer to handle each element reference using generic arguments. |
102 | 105 | /// @param arguments Generic arguments to use in function pointer. |
103 | | -void map_value_fhash_table(fhash_table_s const * const table, handle_fn const handle, void * const arguments); |
| 106 | +void map_value_fhash_map(fhash_map_s const * const map, handle_fn const handle, void * const arguments); |
104 | 107 |
|
105 | | -#endif // FHASH_TABLE_H |
| 108 | +#endif // FHASH_MAP_H |
0 commit comments