|
| 1 | + |
| 2 | +#include "hash_tables.h" |
| 3 | + |
| 4 | +shash_table_t *shash_table_create(unsigned long int size); |
| 5 | +int shash_table_set(shash_table_t *ht, const char *key, const char *value); |
| 6 | +char *shash_table_get(const shash_table_t *ht, const char *key); |
| 7 | +void shash_table_print(const shash_table_t *ht); |
| 8 | +void shash_table_print_rev(const shash_table_t *ht); |
| 9 | +void shash_table_delete(shash_table_t *ht); |
| 10 | + |
| 11 | +/** |
| 12 | + * shash_table_create - Creates a sorted hash table. |
| 13 | + * @size: The size of new sorted hash table. |
| 14 | + * |
| 15 | + * Return: If an error occurs - NULL. |
| 16 | + * Otherwise - a pointer to the new sorted hash table. |
| 17 | + */ |
| 18 | +shash_table_t *shash_table_create(unsigned long int size) |
| 19 | +{ |
| 20 | + shash_table_t *ht; |
| 21 | + unsigned long int i; |
| 22 | + |
| 23 | + ht = malloc(sizeof(shash_table_t)); |
| 24 | + if (ht == NULL) |
| 25 | + return (NULL); |
| 26 | + |
| 27 | + ht->size = size; |
| 28 | + ht->array = malloc(sizeof(shash_node_t *) * size); |
| 29 | + if (ht->array == NULL) |
| 30 | + return (NULL); |
| 31 | + for (i = 0; i < size; i++) |
| 32 | + ht->array[i] = NULL; |
| 33 | + ht->shead = NULL; |
| 34 | + ht->stail = NULL; |
| 35 | + |
| 36 | + return (ht); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * shash_table_set - Adds an element to a sorted hash table. |
| 41 | + * @ht: A pointer to the sorted hash table. |
| 42 | + * @key: The key to add - cannot be an empty string. |
| 43 | + * @value: The value associated with key. |
| 44 | + * |
| 45 | + * Return: Upon failure - 0. |
| 46 | + * Otherwise - 1. |
| 47 | + */ |
| 48 | +int shash_table_set(shash_table_t *ht, const char *key, const char *value) |
| 49 | +{ |
| 50 | + shash_node_t *new, *tmp; |
| 51 | + char *value_copy; |
| 52 | + unsigned long int index; |
| 53 | + |
| 54 | + if (ht == NULL || key == NULL || *key == '\0' || value == NULL) |
| 55 | + return (0); |
| 56 | + |
| 57 | + value_copy = strdup(value); |
| 58 | + if (value_copy == NULL) |
| 59 | + return (0); |
| 60 | + |
| 61 | + index = key_index((const unsigned char *)key, ht->size); |
| 62 | + tmp = ht->shead; |
| 63 | + while (tmp) |
| 64 | + { |
| 65 | + if (strcmp(tmp->key, key) == 0) |
| 66 | + { |
| 67 | + free(tmp->value); |
| 68 | + tmp->value = value_copy; |
| 69 | + return (1); |
| 70 | + } |
| 71 | + tmp = tmp->snext; |
| 72 | + } |
| 73 | + |
| 74 | + new = malloc(sizeof(shash_node_t)); |
| 75 | + if (new == NULL) |
| 76 | + { |
| 77 | + free(value_copy); |
| 78 | + return (0); |
| 79 | + } |
| 80 | + new->key = strdup(key); |
| 81 | + if (new->key == NULL) |
| 82 | + { |
| 83 | + free(value_copy); |
| 84 | + free(new); |
| 85 | + return (0); |
| 86 | + } |
| 87 | + new->value = value_copy; |
| 88 | + new->next = ht->array[index]; |
| 89 | + ht->array[index] = new; |
| 90 | + |
| 91 | + if (ht->shead == NULL) |
| 92 | + { |
| 93 | + new->sprev = NULL; |
| 94 | + new->snext = NULL; |
| 95 | + ht->shead = new; |
| 96 | + ht->stail = new; |
| 97 | + } |
| 98 | + else if (strcmp(ht->shead->key, key) > 0) |
| 99 | + { |
| 100 | + new->sprev = NULL; |
| 101 | + new->snext = ht->shead; |
| 102 | + ht->shead->sprev = new; |
| 103 | + ht->shead = new; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + tmp = ht->shead; |
| 108 | + while (tmp->snext != NULL && strcmp(tmp->snext->key, key) < 0) |
| 109 | + tmp = tmp->snext; |
| 110 | + new->sprev = tmp; |
| 111 | + new->snext = tmp->snext; |
| 112 | + if (tmp->snext == NULL) |
| 113 | + ht->stail = new; |
| 114 | + else |
| 115 | + tmp->snext->sprev = new; |
| 116 | + tmp->snext = new; |
| 117 | + } |
| 118 | + |
| 119 | + return (1); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * shash_table_get - Retrieve the value associated with |
| 124 | + * a key in a sorted hash table. |
| 125 | + * @ht: A pointer to the sorted hash table. |
| 126 | + * @key: The key to get the value of. |
| 127 | + * |
| 128 | + * Return: If the key cannot be matched - NULL. |
| 129 | + * Otherwise - the value associated with key in ht. |
| 130 | + */ |
| 131 | +char *shash_table_get(const shash_table_t *ht, const char *key) |
| 132 | +{ |
| 133 | + shash_node_t *node; |
| 134 | + unsigned long int index; |
| 135 | + |
| 136 | + if (ht == NULL || key == NULL || *key == '\0') |
| 137 | + return (NULL); |
| 138 | + |
| 139 | + index = key_index((const unsigned char *)key, ht->size); |
| 140 | + if (index >= ht->size) |
| 141 | + return (NULL); |
| 142 | + |
| 143 | + node = ht->shead; |
| 144 | + while (node != NULL && strcmp(node->key, key) != 0) |
| 145 | + node = node->snext; |
| 146 | + |
| 147 | + return ((node == NULL) ? NULL : node->value); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * shash_table_print - Prints a sorted hash table in order. |
| 152 | + * @ht: A pointer to the sorted hash table. |
| 153 | + */ |
| 154 | +void shash_table_print(const shash_table_t *ht) |
| 155 | +{ |
| 156 | + shash_node_t *node; |
| 157 | + |
| 158 | + if (ht == NULL) |
| 159 | + return; |
| 160 | + |
| 161 | + node = ht->shead; |
| 162 | + printf("{"); |
| 163 | + while (node != NULL) |
| 164 | + { |
| 165 | + printf("'%s': '%s'", node->key, node->value); |
| 166 | + node = node->snext; |
| 167 | + if (node != NULL) |
| 168 | + printf(", "); |
| 169 | + } |
| 170 | + printf("}\n"); |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * shash_table_print_rev - Prints a sorted hash table in reverse order. |
| 175 | + * @ht: A pointer to the sorted hash table to print. |
| 176 | + */ |
| 177 | +void shash_table_print_rev(const shash_table_t *ht) |
| 178 | +{ |
| 179 | + shash_node_t *node; |
| 180 | + |
| 181 | + if (ht == NULL) |
| 182 | + return; |
| 183 | + |
| 184 | + node = ht->stail; |
| 185 | + printf("{"); |
| 186 | + while (node != NULL) |
| 187 | + { |
| 188 | + printf("'%s': '%s'", node->key, node->value); |
| 189 | + node = node->sprev; |
| 190 | + if (node != NULL) |
| 191 | + printf(", "); |
| 192 | + } |
| 193 | + printf("}\n"); |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * shash_table_delete - Deletes a sorted hash table. |
| 198 | + * @ht: A pointer to the sorted hash table. |
| 199 | + */ |
| 200 | +void shash_table_delete(shash_table_t *ht) |
| 201 | +{ |
| 202 | + shash_table_t *head = ht; |
| 203 | + shash_node_t *node, *tmp; |
| 204 | + |
| 205 | + if (ht == NULL) |
| 206 | + return; |
| 207 | + |
| 208 | + node = ht->shead; |
| 209 | + while (node) |
| 210 | + { |
| 211 | + tmp = node->snext; |
| 212 | + free(node->key); |
| 213 | + free(node->value); |
| 214 | + free(node); |
| 215 | + node = tmp; |
| 216 | + } |
| 217 | + |
| 218 | + free(head->array); |
| 219 | + free(head); |
| 220 | +} |
0 commit comments