|
| 1 | +#ifndef HASH_TABLES_H |
| 2 | +#define HASH_TABLES_H |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +/** |
| 9 | + * struct hash_node_s - Node of a hash table |
| 10 | + * |
| 11 | + * @key: The key, string |
| 12 | + * The key is unique in the HashTable |
| 13 | + * @value: The value corresponding to a key |
| 14 | + * @next: A pointer to the next node of the List |
| 15 | + */ |
| 16 | +typedef struct hash_node_s |
| 17 | +{ |
| 18 | +char *key; |
| 19 | +char *value; |
| 20 | +struct hash_node_s *next; |
| 21 | +} hash_node_t; |
| 22 | + |
| 23 | +/** |
| 24 | + * struct hash_table_s - Hash table data structure |
| 25 | + * |
| 26 | + * @size: The size of the array |
| 27 | + * @array: An array of size @size |
| 28 | + * Each cell of this array is a pointer to the first node of a linked list, |
| 29 | + * because we want our HashTable to use a Chaining collision handling |
| 30 | + */ |
| 31 | +typedef struct hash_table_s |
| 32 | +{ |
| 33 | +unsigned long int size; |
| 34 | +hash_node_t **array; |
| 35 | +} hash_table_t; |
| 36 | + |
| 37 | +hash_table_t *hash_table_create(unsigned long int size); |
| 38 | +unsigned long int hash_djb2(const unsigned char *str); |
| 39 | +unsigned long int key_index(const unsigned char *key, unsigned long int size); |
| 40 | +int hash_table_set(hash_table_t *ht, const char *key, const char *value); |
| 41 | +char *hash_table_get(const hash_table_t *ht, const char *key); |
| 42 | +void hash_table_print(const hash_table_t *ht); |
| 43 | +void hash_table_delete(hash_table_t *ht); |
| 44 | + |
| 45 | +/** |
| 46 | + * struct shash_node_s - Node of a sorted hash table |
| 47 | + * |
| 48 | + * @key: The key, string |
| 49 | + * The key is unique in the HashTable |
| 50 | + * @value: The value corresponding to a key |
| 51 | + * @next: A pointer to the next node of the List |
| 52 | + * @sprev: A pointer to the previous element of the sorted linked list |
| 53 | + * @snext: A pointer to the next element of the sorted linked list |
| 54 | + */ |
| 55 | +typedef struct shash_node_s |
| 56 | +{ |
| 57 | +char *key; |
| 58 | +char *value; |
| 59 | +struct shash_node_s *next; |
| 60 | +struct shash_node_s *sprev; |
| 61 | +struct shash_node_s *snext; |
| 62 | +} shash_node_t; |
| 63 | + |
| 64 | +/** |
| 65 | + * struct shash_table_s - Sorted hash table data structure |
| 66 | + * |
| 67 | + * @size: The size of the array |
| 68 | + * @array: An array of size @size |
| 69 | + * Each cell of this array is a pointer to the first node of a linked list, |
| 70 | + * because we want our HashTable to use a Chaining collision handling |
| 71 | + * @shead: A pointer to the first element of the sorted linked list |
| 72 | + * @stail: A pointer to the last element of the sorted linked list |
| 73 | + */ |
| 74 | +typedef struct shash_table_s |
| 75 | +{ |
| 76 | +unsigned long int size; |
| 77 | +shash_node_t **array; |
| 78 | +shash_node_t *shead; |
| 79 | +shash_node_t *stail; |
| 80 | +} shash_table_t; |
| 81 | + |
| 82 | +shash_table_t *shash_table_create(unsigned long int size); |
| 83 | +int shash_table_set(shash_table_t *ht, const char *key, const char *value); |
| 84 | +char *shash_table_get(const shash_table_t *ht, const char *key); |
| 85 | +void shash_table_print(const shash_table_t *ht); |
| 86 | +void shash_table_print_rev(const shash_table_t *ht); |
| 87 | +void shash_table_delete(shash_table_t *ht); |
| 88 | + |
| 89 | +#endif /* HASH_TABLES */ |
0 commit comments