Skip to content

Commit

Permalink
[Update] - Algorithm Hashtable / Time with pit
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaucoul committed Jul 23, 2024
1 parent 9ae2969 commit f85d2c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
22 changes: 12 additions & 10 deletions Hephaistos/libs/algorithms/hashtable/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 16:49:31 by vvaucoul #+# #+# */
/* Updated: 2024/07/23 11:38:20 by vvaucoul ### ########.fr */
/* Updated: 2024/07/23 15:02:40 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,22 +17,24 @@
*
* @return A pointer to the newly created hashtable
*/
Hashtable *hashtable_create(void) {
Hashtable *hashtable_create(uint8_t size) {
Hashtable *table = (Hashtable *)kmalloc(sizeof(Hashtable));
if (!table) {
return NULL;
}

table->buckets = (HashEntry **)kmalloc(sizeof(HashEntry *) * HASHTABLE_SIZE);
table->buckets = (HashEntry **)kmalloc(sizeof(HashEntry *) * size);
if (!table->buckets) {
kfree(table);
return NULL;
}

for (uint32_t i = 0; i < HASHTABLE_SIZE; ++i) {
for (uint32_t i = 0; i < size; ++i) {
table->buckets[i] = NULL;
}

table->size = size;

return table;
}

Expand All @@ -55,7 +57,7 @@ static void hashtable_free_entry(HashEntry *entry) {
*/
void hashtable_delete(Hashtable *table) {
if (table) {
for (uint32_t i = 0; i < HASHTABLE_SIZE; ++i) {
for (uint32_t i = 0; i < table->size; ++i) {
HashEntry *entry = table->buckets[i];
while (entry) {
HashEntry *next = entry->next;
Expand All @@ -74,12 +76,12 @@ void hashtable_delete(Hashtable *table) {
* @param key The key to hash
* @return The index in the hashtable
*/
uint32_t hashtable_fn(const char *key) {
uint32_t hashtable_fn(const char *key, uint8_t size) {
uint32_t hash = 0;
while (*key) {
hash = (hash << 5) + *key++;
}
return hash % HASHTABLE_SIZE;
return hash % size;
}

/**
Expand All @@ -90,7 +92,7 @@ uint32_t hashtable_fn(const char *key) {
* @param value The value to insert
*/
void hashtable_insert(Hashtable *table, const char *key, void *value) {
uint32_t index = hashtable_fn(key);
uint32_t index = hashtable_fn(key, table->size);
HashEntry *entry = table->buckets[index];

while (entry) {
Expand Down Expand Up @@ -124,7 +126,7 @@ void hashtable_insert(Hashtable *table, const char *key, void *value) {
* @return The value associated with the key, or NULL if not found
*/
void *hashtable_get(Hashtable *table, const char *key) {
uint32_t index = hashtable_fn(key);
uint32_t index = hashtable_fn(key, table->size);
HashEntry *entry = table->buckets[index];

while (entry) {
Expand All @@ -143,7 +145,7 @@ void *hashtable_get(Hashtable *table, const char *key) {
* @param key The key to remove
*/
void hashtable_remove(Hashtable *table, const char *key) {
uint32_t index = hashtable_fn(key);
uint32_t index = hashtable_fn(key, table->size);
HashEntry *entry = table->buckets[index];
HashEntry *prev = NULL;

Expand Down
10 changes: 4 additions & 6 deletions Hephaistos/libs/algorithms/hashtable/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 16:46:01 by vvaucoul #+# #+# */
/* Updated: 2024/07/23 11:39:30 by vvaucoul ### ########.fr */
/* Updated: 2024/07/23 15:03:01 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,9 +15,6 @@

#include <libs/stddef/stddef.h>

// Todo: Temporary define size to 128, default: 1024 -> memory crash, must be fixed
#define HASHTABLE_SIZE 128 // Define the size of the hashtable

// Define the structure for hashtable entries
typedef struct HashEntry {
char *key;
Expand All @@ -28,12 +25,13 @@ typedef struct HashEntry {
// Define the hashtable structure
typedef struct {
HashEntry **buckets; // Array of pointers to HashEntry
uint8_t size; // Hashtable max size
} Hashtable;

// Function prototypes
Hashtable *hashtable_create(void);
Hashtable *hashtable_create(uint8_t size);
void hashtable_delete(Hashtable *table);
uint32_t hashtable_fn(const char *key);
uint32_t hashtable_fn(const char *key, uint8_t size);
void hashtable_insert(Hashtable *table, const char *key, void *value);
void *hashtable_get(Hashtable *table, const char *key);
void hashtable_remove(Hashtable *table, const char *key);
Expand Down
6 changes: 3 additions & 3 deletions Hephaistos/libs/time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 14:11:09 by vvaucoul #+# #+# */
/* Updated: 2024/07/22 20:57:53 by vvaucoul ### ########.fr */
/* Updated: 2024/07/23 13:53:53 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@
*/
void counter_start(counter_t *counter) {
assert(counter != NULL);
counter->start = timer_ticks;
counter->start = pit_get_ticks();
}

/**
Expand All @@ -30,7 +30,7 @@ void counter_start(counter_t *counter) {
*/
void counter_stop(counter_t *counter) {
assert(counter != NULL);
counter->end = timer_ticks;
counter->end = pit_get_ticks();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions Hephaistos/workflows/tests/workflow_algorithm_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 14:54:21 by vvaucoul #+# #+# */
/* Updated: 2024/07/22 11:46:08 by vvaucoul ### ########.fr */
/* Updated: 2024/07/23 15:04:12 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@ void print_hashtable(Hashtable *table) {
return;
}

for (int i = 0; i < HASHTABLE_SIZE; i++) {
for (int i = 0; i < table->size; i++) {
HashEntry *entry = table->buckets[i];

while (entry != NULL) {
Expand All @@ -32,14 +32,14 @@ void print_hashtable(Hashtable *table) {

// Test function for hashtable_create
void test_hashtable_create() {
Hashtable *table = hashtable_create();
Hashtable *table = hashtable_create(1);
assert_msg(table != NULL, "Hashtable should not be NULL");
hashtable_delete(table);
}

// Test function for hashtable_insert and hashtable_get
void test_hashtable_insert_get() {
Hashtable *table = hashtable_create();
Hashtable *table = hashtable_create(2);
const char *key1 = "key1";
const char *key2 = "key2";
const char *value1 = "value1";
Expand All @@ -59,7 +59,7 @@ void test_hashtable_insert_get() {

// Test function for hashtable_remove
void test_hashtable_remove() {
Hashtable *table = hashtable_create();
Hashtable *table = hashtable_create(2);
const char *key1 = "key1";
const char *key2 = "key2";
const char *value1 = "value1";
Expand All @@ -77,19 +77,19 @@ void test_hashtable_remove() {

// Test function for hashtable_delete
void test_hashtable_delete() {
Hashtable *table = hashtable_create();
Hashtable *table = hashtable_create(1);
hashtable_insert(table, "key1", "value1");
hashtable_delete(table);
}

// Test function for hashtable_fn
void test_hashtable_fn() {
assert_msg(hashtable_fn("test") > 0, "Hashtable function should return a positive value for a non-empty string");
assert_msg(hashtable_fn("test", 1) > 0, "Hashtable function should return a positive value for a non-empty string");
}

// Test function for hashtable_contains_key
void test_hashtable_contains_key() {
Hashtable *table = hashtable_create();
Hashtable *table = hashtable_create(2);
const char *key1 = "key1";
const char *key2 = "key2";
const char *value1 = "value1";
Expand Down

0 comments on commit f85d2c2

Please sign in to comment.