Skip to content

Commit 182332c

Browse files
committed
fixed finite hashmap's compare function pointer and hashes in copy function
1 parent 9de6028 commit 182332c

11 files changed

Lines changed: 795 additions & 759 deletions

File tree

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,108 @@
1-
#ifndef FHASH_TABLE_H
2-
#define FHASH_TABLE_H
1+
#ifndef FHASH_MAP_H
2+
#define FHASH_MAP_H
33

44
#include <cerpec.h>
55

66
/// @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;
910
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;
1213
memory_s const * allocator;
13-
} fhash_table_s;
14+
} fhash_map_s;
1415

1516
/// @brief Creates an empty structure.
1617
/// @param key_size Size of a single key.
1718
/// @param value_size Size of a single value.
1819
/// @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.
2022
/// @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);
2224

2325
/// @brief Creates an empty structure.
2426
/// @param key_size Size of a single key.
2527
/// @param value_size Size of a single value.
2628
/// @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.
2831
/// @param allocator Custom allocator structure.
2932
/// @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);
3134

3235
/// @brief Destroys a structure, and its elements and makes it unusable.
33-
/// @param table Structure to destroy.
36+
/// @param map Structure to destroy.
3437
/// @param destroy_key Function pointer to destroy a single key.
3538
/// @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);
3740

3841
/// @brief Clears a structure, and destroys its elements, but remains usable.
39-
/// @param table Structure to destroy.
42+
/// @param map Structure to destroy.
4043
/// @param destroy_key Function pointer to destroy a single key.
4144
/// @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);
4346

4447
/// @brief Creates a copy of a structure and all its elements.
45-
/// @param table Structure to copy.
48+
/// @param map Structure to copy.
4649
/// @param copy_key Function pointer to create a deep/shallow copy of a single key.
4750
/// @param copy_value Function pointer to create a deep/shallow copy of a single value.
4851
/// @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);
5053

5154
/// @brief Checks if structure is empty.
52-
/// @param table Structure to check.
55+
/// @param map Structure to check.
5356
/// @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);
5558

5659
/// @brief Checks if structure is full.
57-
/// @param table Structure to check.
60+
/// @param map Structure to check.
5861
/// @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);
6063

6164
/// @brief Inserts unique key/value pair into structure.
62-
/// @param table Structure to insert element into.
65+
/// @param map Structure to insert element into.
6366
/// @param key Key to insert.
6467
/// @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);
6669

6770
/// @brief Removes unique key/value pair from structure.
68-
/// @param table Structure to remove element into.
71+
/// @param map Structure to remove element into.
6972
/// @param key Key to remove.
7073
/// @param key_buffer Key buffer to save removed key.
7174
/// @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);
7376

7477
/// @brief Checks if structure contains key.
75-
/// @param table Structure to check.
78+
/// @param map Structure to check.
7679
/// @param key Key to check.
7780
/// @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);
7982

8083
/// @brief Gets value based on key.
81-
/// @param table Structure to get value.
84+
/// @param map Structure to get value.
8285
/// @param key Key to value.
8386
/// @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);
8588

8689
/// @brief Sets value based on key.
87-
/// @param table Structure to set value.
90+
/// @param map Structure to set value.
8891
/// @param key Key to value.
8992
/// @param value New value to insert.
9093
/// @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);
9295

9396
/// @brief Iterates over each key in structure.
94-
/// @param table Structure to iterate over.
97+
/// @param map Structure to iterate over.
9598
/// @param handle Function pointer to handle each element reference using generic arguments.
9699
/// @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);
98101

99102
/// @brief Iterates over each value in structure.
100-
/// @param table Structure to iterate over.
103+
/// @param map Structure to iterate over.
101104
/// @param handle Function pointer to handle each element reference using generic arguments.
102105
/// @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);
104107

105-
#endif // FHASH_TABLE_H
108+
#endif // FHASH_MAP_H

source/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ add_library(${PROJECT_NAME} cerpec.c
1313
graph/imatrix_graph.c
1414

1515
misc/ihash_map.c misc/ibinary_heap.c
16-
misc/fbinary_heap.c misc/fhash_table.c
16+
misc/fbinary_heap.c misc/fhash_map.c
1717
set/frb_set.c
1818
)

0 commit comments

Comments
 (0)