-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
489e445
commit 46827ab
Showing
3 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
#include "hash_tables.h" | ||
|
||
shash_table_t *shash_table_create(unsigned long int size); | ||
int shash_table_set(shash_table_t *ht, const char *key, const char *value); | ||
char *shash_table_get(const shash_table_t *ht, const char *key); | ||
void shash_table_print(const shash_table_t *ht); | ||
void shash_table_print_rev(const shash_table_t *ht); | ||
void shash_table_delete(shash_table_t *ht); | ||
|
||
/** | ||
* shash_table_create - Creates a sorted hash table. | ||
* @size: The size of new sorted hash table. | ||
* | ||
* Return: If an error occurs - NULL. | ||
* Otherwise - a pointer to the new sorted hash table. | ||
*/ | ||
shash_table_t *shash_table_create(unsigned long int size) | ||
{ | ||
shash_table_t *ht; | ||
unsigned long int i; | ||
|
||
ht = malloc(sizeof(shash_table_t)); | ||
if (ht == NULL) | ||
return (NULL); | ||
|
||
ht->size = size; | ||
ht->array = malloc(sizeof(shash_node_t *) * size); | ||
if (ht->array == NULL) | ||
return (NULL); | ||
for (i = 0; i < size; i++) | ||
ht->array[i] = NULL; | ||
ht->shead = NULL; | ||
ht->stail = NULL; | ||
|
||
return (ht); | ||
} | ||
|
||
/** | ||
* shash_table_set - Adds an element to a sorted hash table. | ||
* @ht: A pointer to the sorted hash table. | ||
* @key: The key to add - cannot be an empty string. | ||
* @value: The value associated with key. | ||
* | ||
* Return: Upon failure - 0. | ||
* Otherwise - 1. | ||
*/ | ||
int shash_table_set(shash_table_t *ht, const char *key, const char *value) | ||
{ | ||
shash_node_t *new, *tmp; | ||
char *value_copy; | ||
unsigned long int index; | ||
|
||
if (ht == NULL || key == NULL || *key == '\0' || value == NULL) | ||
return (0); | ||
|
||
value_copy = strdup(value); | ||
if (value_copy == NULL) | ||
return (0); | ||
|
||
index = key_index((const unsigned char *)key, ht->size); | ||
tmp = ht->shead; | ||
while (tmp) | ||
{ | ||
if (strcmp(tmp->key, key) == 0) | ||
{ | ||
free(tmp->value); | ||
tmp->value = value_copy; | ||
return (1); | ||
} | ||
tmp = tmp->snext; | ||
} | ||
|
||
new = malloc(sizeof(shash_node_t)); | ||
if (new == NULL) | ||
{ | ||
free(value_copy); | ||
return (0); | ||
} | ||
new->key = strdup(key); | ||
if (new->key == NULL) | ||
{ | ||
free(value_copy); | ||
free(new); | ||
return (0); | ||
} | ||
new->value = value_copy; | ||
new->next = ht->array[index]; | ||
ht->array[index] = new; | ||
|
||
if (ht->shead == NULL) | ||
{ | ||
new->sprev = NULL; | ||
new->snext = NULL; | ||
ht->shead = new; | ||
ht->stail = new; | ||
} | ||
else if (strcmp(ht->shead->key, key) > 0) | ||
{ | ||
new->sprev = NULL; | ||
new->snext = ht->shead; | ||
ht->shead->sprev = new; | ||
ht->shead = new; | ||
} | ||
else | ||
{ | ||
tmp = ht->shead; | ||
while (tmp->snext != NULL && strcmp(tmp->snext->key, key) < 0) | ||
tmp = tmp->snext; | ||
new->sprev = tmp; | ||
new->snext = tmp->snext; | ||
if (tmp->snext == NULL) | ||
ht->stail = new; | ||
else | ||
tmp->snext->sprev = new; | ||
tmp->snext = new; | ||
} | ||
|
||
return (1); | ||
} | ||
|
||
/** | ||
* shash_table_get - Retrieve the value associated with | ||
* a key in a sorted hash table. | ||
* @ht: A pointer to the sorted hash table. | ||
* @key: The key to get the value of. | ||
* | ||
* Return: If the key cannot be matched - NULL. | ||
* Otherwise - the value associated with key in ht. | ||
*/ | ||
char *shash_table_get(const shash_table_t *ht, const char *key) | ||
{ | ||
shash_node_t *node; | ||
unsigned long int index; | ||
|
||
if (ht == NULL || key == NULL || *key == '\0') | ||
return (NULL); | ||
|
||
index = key_index((const unsigned char *)key, ht->size); | ||
if (index >= ht->size) | ||
return (NULL); | ||
|
||
node = ht->shead; | ||
while (node != NULL && strcmp(node->key, key) != 0) | ||
node = node->snext; | ||
|
||
return ((node == NULL) ? NULL : node->value); | ||
} | ||
|
||
/** | ||
* shash_table_print - Prints a sorted hash table in order. | ||
* @ht: A pointer to the sorted hash table. | ||
*/ | ||
void shash_table_print(const shash_table_t *ht) | ||
{ | ||
shash_node_t *node; | ||
|
||
if (ht == NULL) | ||
return; | ||
|
||
node = ht->shead; | ||
printf("{"); | ||
while (node != NULL) | ||
{ | ||
printf("'%s': '%s'", node->key, node->value); | ||
node = node->snext; | ||
if (node != NULL) | ||
printf(", "); | ||
} | ||
printf("}\n"); | ||
} | ||
|
||
/** | ||
* shash_table_print_rev - Prints a sorted hash table in reverse order. | ||
* @ht: A pointer to the sorted hash table to print. | ||
*/ | ||
void shash_table_print_rev(const shash_table_t *ht) | ||
{ | ||
shash_node_t *node; | ||
|
||
if (ht == NULL) | ||
return; | ||
|
||
node = ht->stail; | ||
printf("{"); | ||
while (node != NULL) | ||
{ | ||
printf("'%s': '%s'", node->key, node->value); | ||
node = node->sprev; | ||
if (node != NULL) | ||
printf(", "); | ||
} | ||
printf("}\n"); | ||
} | ||
|
||
/** | ||
* shash_table_delete - Deletes a sorted hash table. | ||
* @ht: A pointer to the sorted hash table. | ||
*/ | ||
void shash_table_delete(shash_table_t *ht) | ||
{ | ||
shash_table_t *head = ht; | ||
shash_node_t *node, *tmp; | ||
|
||
if (ht == NULL) | ||
return; | ||
|
||
node = ht->shead; | ||
while (node) | ||
{ | ||
tmp = node->snext; | ||
free(node->key); | ||
free(node->value); | ||
free(node); | ||
node = tmp; | ||
} | ||
|
||
free(head->array); | ||
free(head); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "hash_tables.h" | ||
|
||
/** | ||
* hash_table_print - Prints a hash table. | ||
* @ht: A pointer to the hash table. | ||
**/ | ||
void hash_table_print(const hash_table_t *ht) | ||
{ | ||
unsigned long int i; | ||
hash_node_t *node; | ||
char *sep = ""; | ||
|
||
if (ht == NULL) | ||
return; | ||
|
||
printf("{"); | ||
for (i = 0; i < ht->size; i++) | ||
{ | ||
node = ht->array[i]; | ||
while (node != NULL) | ||
{ | ||
printf("%s'%s': '%s'", sep, node->key, node->value); | ||
sep = ", "; | ||
node = node->next; | ||
} | ||
} | ||
printf("}\n"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "hash_tables.h" | ||
|
||
/** | ||
* hash_table_delete - Deletes a hash table. | ||
* @ht: A pointer to the hash table. | ||
**/ | ||
void hash_table_delete(hash_table_t *ht) | ||
{ | ||
unsigned long int i; | ||
hash_node_t *node; | ||
hash_node_t *temp; | ||
|
||
/* check for invalid input param */ | ||
if (ht == NULL) | ||
return; | ||
|
||
/* interate through each index in the hash table */ | ||
for (i = 0; i < ht->size; i++) | ||
{ | ||
node = ht->array[i]; | ||
/* frees the nodes in the linked list */ | ||
while (node != NULL) | ||
{ | ||
temp = node; | ||
node = node->next; | ||
free(temp->key); | ||
free(temp->value); | ||
free(temp); | ||
} | ||
} | ||
/* free the array and the hash table struct */ | ||
free(ht->array); | ||
free(ht); | ||
} | ||
|