-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.h
90 lines (69 loc) · 2.89 KB
/
hashmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef HASHMAP_H
#define HASHMAP_H
#include <stdbool.h>
#include "linked_list.h"
/* A hash function is needed, this function should take a pointer in
* parameter and return an int. */
typedef int (*hash_function)(const void *);
/* A function is needed in order to compare keys, this function must
* return true if the two pointers design the same key, and false if
* they're not identical. */
typedef bool (*equals_function)(const void *, const void *);
struct hashmap{
hash_function h_f;
equals_function e_f;
int nb_elements;
int map_size;
linked_list_pointer * map;
int actual_indice;
};
typedef struct hashmap * hashmap_pointer;
/* Create a new hashmap with the specified hash_function and equals_function
*/
hashmap_pointer new_hashmap(hash_function h_f, equals_function e_f);
/* Get an element with the specified key, return NULL in two cases
* - The key can't be found int the hashmap
* - The key is found but is associated with the data NULL
*/
void * hashmap_get(hashmap_pointer hm, const void * key);
/* Return true if the specified key can be found in the map,
* false otherwise. */
bool hashmap_exists(hashmap_pointer hm, const void * key);
/* Add the specified value with the specified key in the hashmap.
* If the key is already in the specified map, NULL is returned,
* If everything worked properly, returns the inserted value.
*/
void * hashmap_add(hashmap_pointer hm, const void * key, void * value);
/* Remove the entry with the specified key.
* If no entry with the specified key can be found, nothing is done
* If the option free_key is specified :
* - The key of the object found will be erased.
* (If it contains pointers of pointers, better use your own free)
* If the option free_data is specified :
* - The data of the object found will be erased.
* (If it contains pointers of pointers, better use your own free)
*/
void hashmap_remove(hashmap_pointer hm,
const void * key,
bool free_key,
bool free_data);
/* Return the number of elements contained in the hashmap. */
int hashmap_size(hashmap_pointer hm);
/* Remove all the entries and destroy the hashmap.
* If the option free_key is specified :
* - The key of the object found will be erased.
* (If it contains pointers of pointers, better use your own free)
* If the option free_data is specified :
* - The data of the object found will be erased.
* (If it contains pointers of pointers, better use your own free)
*/
void hashmap_destroy(hashmap_pointer hm,
bool free_keys,
bool free_data);
void hashmap_start_iteration(hashmap_pointer hm);
const void * hashmap_get_current_key(hashmap_pointer hm);
void * hashmap_get_current_value(hashmap_pointer hm);
void hashmap_next_element(hashmap_pointer hm);
/* return true if actual is after last_element */
bool hashmap_is_ended_iteration(hashmap_pointer hm);
#endif