Skip to content

Commit

Permalink
Hashtable (#5)
Browse files Browse the repository at this point in the history
* linkedlist_finished

* fineshed_hashtable

* added missing function from to shell.h

* modification to shell.h
  • Loading branch information
h01nait authored May 16, 2023
1 parent 1771a31 commit 9ac6a9a
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 6 deletions.
18 changes: 18 additions & 0 deletions _clear_entry.c
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);
}
20 changes: 20 additions & 0 deletions _clear_map.c
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);
}
21 changes: 21 additions & 0 deletions _get_hash_code.c
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);
}
29 changes: 29 additions & 0 deletions _get_keys.c
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);
}
27 changes: 27 additions & 0 deletions _get_value.c
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);
}
20 changes: 20 additions & 0 deletions _init_map.c
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);
}
38 changes: 38 additions & 0 deletions _set_value.c
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);
}
27 changes: 27 additions & 0 deletions _strdup.c
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);
}
45 changes: 39 additions & 6 deletions shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,45 @@
#include <stdlib.h>

#define BUFFER_SIZE 1024
#define BACKET_SIZE 64

/**
* struct list_s - node of linkedlist
*
* @data: linkedlist content
* @next: next node
*/
*struct entry_s - struct that hold key and value
* of hash table
*
*@key: the key used to retrieve values
*@value: the respective data for the key
*/
typedef struct entry_s
{
char *key;
char *value;
} entry_t;


/**
* struct list_s - node of linkedlist
*
* @data: linkedlist content
* @next: next node
*/
typedef struct list_s
{
void *data;
struct list_s *next;
} list_t;

/**
* struct map_s - defines a structure for a hash table
*
* @backets: an array of linkedlist where
* our entry data will be stored
*/
typedef struct map_s
{
list_t *backets[BACKET_SIZE];
} map_t;

char *_copy(char *dest, char *src, size_t size);
void *_realloc(void *old_buffer, size_t old_size, size_t new_size);
ssize_t _getline(char **line);
Expand All @@ -27,8 +53,15 @@ int _parsing_error_handler(char *line);
size_t _strlen(char *s);
void _free_split(char ***backets);
char **_split(char *line, char *diameter);
void _free_split(char ***backets);
list_t *add_to_list(list_t **lst, void *data);
void *pop_from_list(list_t **list);
void free_list(list_t *list, void (*free_content)(void *data));
char *_strdup(const char *str);
int _get_hash_code(const char *key);
map_t *_init_map(void);
int _set_value(map_t *map, const char *key, const char *value);
char *_get_value(const map_t *map, const char *key);
void _clear_entry(void *data);
void _clear_map(map_t *map);
list_t *_get_keys(const map_t *map);
#endif

0 comments on commit 9ac6a9a

Please sign in to comment.