|
| 1 | +#include "hash_tables.h" |
| 2 | + |
| 3 | +/** |
| 4 | + * shash_table_create - creates a hash table |
| 5 | + * |
| 6 | + * @size: the size of the array |
| 7 | + * |
| 8 | + * Return: the newly created hash_table, or NULL on failure |
| 9 | + */ |
| 10 | + |
| 11 | +shash_table_t *shash_table_create(unsigned long int size) |
| 12 | +{ |
| 13 | + shash_table_t *new_sht = NULL; |
| 14 | + unsigned long int index = 0; |
| 15 | + |
| 16 | + new_sht = malloc(sizeof(shash_table_t)); |
| 17 | + if (new_sht) |
| 18 | + { |
| 19 | + new_sht->size = size; |
| 20 | + new_sht->array = malloc(sizeof(shash_node_t *) * size); |
| 21 | + if (!new_sht->array) |
| 22 | + { |
| 23 | + free(new_sht); |
| 24 | + return (NULL); |
| 25 | + } |
| 26 | + |
| 27 | + while (index < size) |
| 28 | + { |
| 29 | + new_sht->array[index] = NULL; |
| 30 | + index++; |
| 31 | + } |
| 32 | + new_sht->shead = NULL; |
| 33 | + new_sht->stail = NULL; |
| 34 | + } |
| 35 | + |
| 36 | + return (new_sht); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * insert_node - handles the add, addend and insert of nodes in linked list |
| 41 | + * |
| 42 | + * @ht: the hash table to add the key/value to |
| 43 | + * @new_node: newly created node to insert |
| 44 | + */ |
| 45 | + |
| 46 | +void insert_node(shash_table_t *ht, shash_node_t *new_node) |
| 47 | +{ |
| 48 | + shash_node_t *tmp_node = NULL, *head = NULL; |
| 49 | + |
| 50 | + new_node->snext = NULL; |
| 51 | + new_node->sprev = NULL; |
| 52 | + if (!ht->shead && !ht->stail) /* if the linked list is empty */ |
| 53 | + { |
| 54 | + ht->shead = new_node; |
| 55 | + ht->stail = new_node; |
| 56 | + } |
| 57 | + else if (strcmp(new_node->key, ht->shead->key) <= 0) /* if is <= first */ |
| 58 | + { |
| 59 | + new_node->snext = ht->shead; |
| 60 | + new_node->snext->sprev = new_node; |
| 61 | + ht->shead = new_node; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + head = ht->shead; |
| 66 | + while (head->snext && strcmp(new_node->key, head->snext->key) > 0) |
| 67 | + head = head->snext; |
| 68 | + new_node->snext = head->snext; |
| 69 | + new_node->sprev = head; |
| 70 | + tmp_node = head->snext; |
| 71 | + head->snext = new_node; |
| 72 | + if (!tmp_node) |
| 73 | + ht->stail = new_node; |
| 74 | + else |
| 75 | + tmp_node->sprev = new_node; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * shash_table_add - adds element to the @ht hash table when it doesn't exist |
| 81 | + * |
| 82 | + * @ht: the hash table to add the key/value to |
| 83 | + * @key: the key (cannot be empty) |
| 84 | + * @value: duplicated value associated with the key |
| 85 | + * |
| 86 | + * Return: the new node, NULL if fails |
| 87 | + */ |
| 88 | + |
| 89 | +shash_node_t *shash_table_add(shash_table_t *ht, const char *key, char *value) |
| 90 | +{ |
| 91 | + shash_node_t *new_node = NULL; |
| 92 | + |
| 93 | + new_node = malloc(sizeof(shash_node_t)); |
| 94 | + if (!new_node) |
| 95 | + { |
| 96 | + free(value); |
| 97 | + return (NULL); |
| 98 | + } |
| 99 | + |
| 100 | + new_node->key = strdup(key); |
| 101 | + new_node->value = value; |
| 102 | + if (!new_node->key || !new_node->value) |
| 103 | + { |
| 104 | + if (new_node->key) |
| 105 | + free(new_node->key); |
| 106 | + free(value); |
| 107 | + return (NULL); |
| 108 | + } |
| 109 | + |
| 110 | + insert_node(ht, new_node); |
| 111 | + return (new_node); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * shash_table_set - adds or sets an element to the @ht hash table |
| 116 | + * |
| 117 | + * @ht: the hash table to add or update the key/value to |
| 118 | + * @key: the key (cannot be empty) |
| 119 | + * @value: value associated with the key |
| 120 | + * |
| 121 | + * Return: 1 if it succeeded, 0 otherwise |
| 122 | + */ |
| 123 | + |
| 124 | +int shash_table_set(shash_table_t *ht, const char *key, const char *value) |
| 125 | +{ |
| 126 | + unsigned long int index; |
| 127 | + shash_node_t *new_node = NULL, *tmp_node = NULL; |
| 128 | + char *new_value = NULL; |
| 129 | + |
| 130 | + if (!ht || !key || !value || strlen(key) == 0) |
| 131 | + return (0); |
| 132 | + |
| 133 | + index = key_index((const unsigned char *)key, ht->size); |
| 134 | + tmp_node = ht->array[index]; |
| 135 | + |
| 136 | + new_value = strdup(value); |
| 137 | + if (!new_value) |
| 138 | + return (0); |
| 139 | + |
| 140 | + while (tmp_node) |
| 141 | + { |
| 142 | + if (strcmp(tmp_node->key, key) == 0) |
| 143 | + { |
| 144 | + free(tmp_node->value); |
| 145 | + tmp_node->value = new_value; |
| 146 | + break; |
| 147 | + } |
| 148 | + tmp_node = tmp_node->next; |
| 149 | + } |
| 150 | + |
| 151 | + if (!tmp_node) |
| 152 | + { |
| 153 | + new_node = shash_table_add(ht, key, new_value); |
| 154 | + if (!new_node) |
| 155 | + return (0); |
| 156 | + new_node->next = ht->array[index]; |
| 157 | + ht->array[index] = new_node; |
| 158 | + } |
| 159 | + |
| 160 | + return (1); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * shash_table_get - retrieves a value associated with a key |
| 165 | + * |
| 166 | + * @ht: the hash table to look into |
| 167 | + * @key: the key to looking for |
| 168 | + * |
| 169 | + * Return: the value associated with the element, or NULL if key not found |
| 170 | + */ |
| 171 | + |
| 172 | +char *shash_table_get(const shash_table_t *ht, const char *key) |
| 173 | +{ |
| 174 | + unsigned long int index; |
| 175 | + shash_node_t *tmp_node = NULL; |
| 176 | + |
| 177 | + if (!ht || !key || strlen(key) == 0) |
| 178 | + return (NULL); |
| 179 | + |
| 180 | + index = key_index((const unsigned char *)key, ht->size); |
| 181 | + tmp_node = ht->array[index]; |
| 182 | + |
| 183 | + while (tmp_node) |
| 184 | + { |
| 185 | + if (strcmp(tmp_node->key, key) == 0) |
| 186 | + return (tmp_node->value); |
| 187 | + tmp_node = tmp_node->next; |
| 188 | + } |
| 189 | + |
| 190 | + return (NULL); |
| 191 | +} |
| 192 | + |
| 193 | +/** |
| 194 | + * shash_table_print - prints a hash table |
| 195 | + * |
| 196 | + * @ht: the hash table to print |
| 197 | + */ |
| 198 | + |
| 199 | +void shash_table_print(const shash_table_t *ht) |
| 200 | +{ |
| 201 | + shash_node_t *tmp_node = NULL; |
| 202 | + char *comma = ""; |
| 203 | + |
| 204 | + if (ht) |
| 205 | + { |
| 206 | + printf("{"); |
| 207 | + tmp_node = ht->shead; |
| 208 | + |
| 209 | + while (tmp_node) |
| 210 | + { |
| 211 | + printf("%s", comma); |
| 212 | + comma = ", "; |
| 213 | + |
| 214 | + if (tmp_node->key) |
| 215 | + printf("'%s': '%s'", tmp_node->key, tmp_node->value); |
| 216 | + |
| 217 | + tmp_node = tmp_node->snext; |
| 218 | + } |
| 219 | + printf("}\n"); |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | + * shash_table_print_rev - rev prints a hash table |
| 225 | + * |
| 226 | + * @ht: the hash table to print |
| 227 | + */ |
| 228 | + |
| 229 | +void shash_table_print_rev(const shash_table_t *ht) |
| 230 | +{ |
| 231 | + shash_node_t *tmp_node = NULL; |
| 232 | + char *comma = ""; |
| 233 | + |
| 234 | + if (ht) |
| 235 | + { |
| 236 | + printf("{"); |
| 237 | + tmp_node = ht->stail; |
| 238 | + |
| 239 | + while (tmp_node) |
| 240 | + { |
| 241 | + printf("%s", comma); |
| 242 | + comma = ", "; |
| 243 | + |
| 244 | + if (tmp_node->key) |
| 245 | + printf("'%s': '%s'", tmp_node->key, tmp_node->value); |
| 246 | + |
| 247 | + tmp_node = tmp_node->sprev; |
| 248 | + } |
| 249 | + printf("}\n"); |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +/** |
| 254 | + * shash_table_delete - deletes a hash table |
| 255 | + * |
| 256 | + * @ht: the hash table to delete |
| 257 | + */ |
| 258 | + |
| 259 | +void shash_table_delete(shash_table_t *ht) |
| 260 | +{ |
| 261 | + shash_node_t *tmp_node = NULL; |
| 262 | + unsigned long int index = 0; |
| 263 | + |
| 264 | + if (ht) |
| 265 | + { |
| 266 | + while (index < ht->size) |
| 267 | + { |
| 268 | + while (ht->array[index]) |
| 269 | + { |
| 270 | + tmp_node = ht->array[index]; |
| 271 | + ht->array[index] = ht->array[index]->next; |
| 272 | + |
| 273 | + if (tmp_node->value) |
| 274 | + free(tmp_node->value); |
| 275 | + if (tmp_node->key) |
| 276 | + free(tmp_node->key); |
| 277 | + free(tmp_node); |
| 278 | + } |
| 279 | + free(ht->array[index]); |
| 280 | + index++; |
| 281 | + } |
| 282 | + free(ht->array); |
| 283 | + free((void *)ht); |
| 284 | + } |
| 285 | +} |
0 commit comments