-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.h
149 lines (122 loc) · 3.47 KB
/
hashtable.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once
/**
* @file hashtable.h
* @brief Local hash-table implementation.
*
* @author Valérian Rousset
*/
#include <stddef.h> // for size_t
#include <stdint.h> // for int32_t (weeks 04 to 06)
#include "error.h" // for error_code
/*
* Definition of type for local hash-tables keys
*/
typedef const char* pps_key_t;
/*
* Definition of type for local hash-tables values
*/
typedef const char* pps_value_t;
/*
* key-value pair
*/
typedef struct
{
pps_key_t key;
pps_value_t value;
} kv_pair_t;
/*
* Definition of type for local hash-tables buckets
*/
typedef struct bucket_t bucket_t;
/*
* Definition of local hash-table type
*/
#define HTABLE_SIZE 256
struct Htable_t{
bucket_t* elements;
size_t size;
};
typedef struct Htable_t* Htable_t;
// Only from week 07
#define NO_HTABLE ((Htable_t) NULL)
typedef struct
{
kv_pair_t* elems;
size_t size;
size_t allocated;
} kv_list_t;
/**
* @brief create a kv list
* @return the kv list
*/
kv_list_t* create_kv_list();
/**
* @brief get all the pairs from the table
* @param list the list to complete
* @return an error code of an error occured
*/
error_code get_kv_pair(kv_list_t* list, Htable_t table);
/**
* @brief construct a hash-table of the given size.
* Note: does NOTHING until week 07.
* @param size number of buckets in the new hash-table
* @return the newly allocated hash-table
*/
Htable_t construct_Htable(size_t size);
/**
* @brief delete the given hash-table
* Note: does NOTHING until week 07.
* @param table the hash-table to free (passed by reference)
*/
void delete_Htable_and_content(Htable_t* table);
/**
* @brief add a key:value pair to hash-table
* @param table the table where to add
* @param key the key to which the value shall be associated
* @param value the value to be added
* @return 0 on success; error code on errror (see error.h)
*/
error_code add_Htable_value(Htable_t table, pps_key_t key, pps_value_t value);
/**
* @brief get a value for a given in the given hash-table
* @param table the table where to get
* @param key the key associated to the wanted value
* @return the associated value
*/
pps_value_t get_Htable_value(Htable_t table, pps_key_t key);
/**
* @brief compute the hash for the given key and size of hash-table.
* Note: although this is a local function, it is exposed here
* for test/grading purposes.
* @param key the key onto compute the hash
* @param table_size size of the containing table
* @return a hash value in range [0..table_size-1], or SIZE_MAX if error
*/
size_t hash_function(pps_key_t key, size_t table_size);
/**
* @brief get the content of the table
* Note: does NOTHING until week 08.
* @param table table to read from
* @return the list of key values from the given table, NULL if a failure occured
*/
kv_list_t *get_Htable_content(Htable_t table);
/**
* @brief delete a key:value pair from the hash-table
* Note: does NOTHING until week 10.
* @param table the table where to delete
* @param key the key which is to be delete
* @return 0 on success; error code on errror (see error.h)
*/
error_code del_Htable_key(Htable_t table, pps_key_t key);
/**
* @brief free a key-value pair content (both of them)
* Note: does NOTHING until week 07.
* @param kv the key-value pair to free
*/
void kv_pair_free(kv_pair_t *kv);
/**
* @brief free a kv_list and it's content
* Note: does NOTHING until week 08.
* @param list the list to free
*/
void kv_list_free(kv_list_t *list);