-
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.
* linkedlist_finished * fineshed_hashtable * added missing function from to shell.h * modification to shell.h
- Loading branch information
Showing
9 changed files
with
239 additions
and
6 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,18 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _clear_entry - frees the memory allocated for an entry in the hash table. | ||
* | ||
* @data : entry data | ||
* | ||
* Return: nothing | ||
*/ | ||
void _clear_entry(void *data) | ||
{ | ||
entry_t *entry; | ||
|
||
entry = data; | ||
free(entry->key); | ||
free(entry->value); | ||
free(data); | ||
} |
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,20 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _clear_map - frees the memory allocated for an entry in the hash table. | ||
* | ||
*@map: map to be freed | ||
* Return: nothing | ||
*/ | ||
void _clear_map(map_t *map) | ||
{ | ||
int iterator; | ||
|
||
iterator = 0; | ||
while (iterator < BACKET_SIZE) | ||
{ | ||
free_list(map->backets[iterator], _clear_entry); | ||
iterator++; | ||
} | ||
free(map); | ||
} |
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,21 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _get_hash_code - calculates the hash code for a given key. | ||
* | ||
* @key: key to be hash | ||
* | ||
* Return: return an integer signify hashed code of the given key | ||
*/ | ||
int _get_hash_code(const char *key) | ||
{ | ||
int hash = 0; | ||
int i; | ||
|
||
for (i = 0; key[i] != '\0'; i++) | ||
{ | ||
hash += key[i]; | ||
} | ||
|
||
return (hash % BACKET_SIZE); | ||
} |
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 "shell.h" | ||
|
||
/** | ||
* _get_keys - retrieves a linked list of all the keys in the hash table. | ||
* | ||
*@map: map to retrieve | ||
* Return: list of keys | ||
*/ | ||
list_t *_get_keys(const map_t *map) | ||
{ | ||
list_t *list, *iterator; | ||
entry_t *entry; | ||
int iter; | ||
|
||
list = NULL; | ||
iter = 0; | ||
while (iter < BACKET_SIZE) | ||
{ | ||
iterator = map->backets[iter]; | ||
while (iterator) | ||
{ | ||
entry = iterator->data; | ||
add_to_list(&list, _strdup(entry->key)); | ||
iterator = iterator->next; | ||
} | ||
iter++; | ||
} | ||
return (list); | ||
} |
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,27 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _get_value - retrieves the value associated | ||
* with the given key from the hash table. | ||
* | ||
*@map: map to get the value from | ||
*@key: key to retieve the associated value | ||
* Return: pointer to the value or NULL in error | ||
*/ | ||
char *_get_value(const map_t *map, const char *key) | ||
{ | ||
int backet_index; | ||
list_t *backet; | ||
entry_t *entry; | ||
|
||
backet_index = _get_hash_code(key); | ||
backet = map->backets[backet_index]; | ||
while (backet) | ||
{ | ||
entry = backet->data; | ||
if (_strcmp(entry->key, key)) | ||
return (entry->value); | ||
backet = backet->next; | ||
} | ||
return (NULL); | ||
} |
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,20 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _init_map - initializes a new hash table and returns a pointer to it. | ||
* | ||
* Return: a pointer two inititialized map | ||
*/ | ||
map_t *_init_map(void) | ||
{ | ||
map_t *map = (map_t *)malloc(sizeof(map_t)); | ||
int iterator; | ||
|
||
iterator = 0; | ||
while (iterator < BACKET_SIZE) | ||
{ | ||
map->backets[iterator] = NULL; | ||
iterator++; | ||
} | ||
return (map); | ||
} |
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,38 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _set_value - adds or updates a new entry | ||
* to the hash table with the given key and value. | ||
* | ||
*@map: map to add new entry to it | ||
*@key: string implies to key of the entry | ||
*@value: string implies to value of the entry | ||
*Return: 0 on error, 1 success | ||
*/ | ||
int _set_value(map_t *map, const char *key, const char *value) | ||
{ | ||
int backet_index; | ||
list_t *iterator; | ||
entry_t *entry; | ||
|
||
backet_index = _get_hash_code(key); | ||
iterator = map->backets[backet_index]; | ||
while (iterator) | ||
{ | ||
entry = iterator->data; | ||
if (_strcmp(entry->key, key)) | ||
{ | ||
free(entry->value); | ||
entry->value = _strdup(value); | ||
return (1); | ||
} | ||
iterator = iterator->next; | ||
} | ||
entry = malloc(sizeof(entry_t)); | ||
if (!entry) | ||
return (0); | ||
entry->key = _strdup(key); | ||
entry->value = _strdup(value); | ||
add_to_list(&map->backets[backet_index], entry); | ||
return (1); | ||
} |
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,27 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _strdup - function that takes a string and duplicates | ||
* it and return new pointer of duplicated string | ||
* | ||
* @str: string to be duplicated | ||
* Return: new char pointer to the new string | ||
*/ | ||
char *_strdup(const char *str) | ||
{ | ||
char *new_str; | ||
size_t len, iterator; | ||
|
||
len = _strlen(str); | ||
new_str = malloc(sizeof(char) * (len + 1)); | ||
if (!new_str) | ||
return (NULL); | ||
new_str[len] = 0; | ||
iterator = 0; | ||
while (iterator < len) | ||
{ | ||
new_str[iterator] = str[iterator]; | ||
iterator++; | ||
} | ||
return (new_str); | ||
} |
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