From 2afa2131ee799439ca692c840798f110be97258c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 09:42:11 -0500 Subject: [PATCH] feat(hash table): added hash table structure and implemented initialize and destroy --- Include/Fledasty/Core/DoublyLinkedList.h | 9 ++-- Include/Fledasty/Core/HashTable.h | 48 +++++++++++++++++++++ Include/Fledasty/Core/LinkedList.h | 4 +- Src/Core/DoublyLinkedList.c | 1 - Src/Core/HashTable.c | 55 ++++++++++++++++++++++++ Tests/Main.c | 8 ++++ 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 Include/Fledasty/Core/HashTable.h create mode 100644 Src/Core/HashTable.c diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 5a0575b..7013824 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -12,11 +12,12 @@ * limitations under the License. * * ----------------------------------------------------------------------------- - * File: DoublyLinkedList.c + * File: DoublyLinkedList.h * Description: - * This file contains the functions for modifying the Doubly Linked List. It - * includes functions to append, Insert before, Insert after, Insert at index, - * Get, Remove element, Remove at index, Check if has element and Check if is empty. + * This file contains the Doubly Linked List structure and the functions for + * modifying it. It includes functions to append, Insert before, Insert after, + * Insert at index, Get, Remove element, Remove at index, Check if has element and + * Check if is empty. * * Author: Mineplay * ----------------------------------------------------------------------------- diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h new file mode 100644 index 0000000..684c8bb --- /dev/null +++ b/Include/Fledasty/Core/HashTable.h @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: HashTable.h + * Description: + * This file contains the HashTable structure and the functions for modifying it. + * It includes functions to get, Insert, Remove, check if has key and check if + * empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_HASH_TABLE +#define FLEDASTY_HASH_TABLE + +#include +#include + +#include "../Utils/Error.h" + +typedef struct { + void *key, *value; +} FledastyHashTablePair; + +typedef struct { + size_t size, capacity; + + size_t element_byte_size; + FledastyHashTablePair **Table; + + size_t (*hash_function)(void *key); +} FledastyHashtable; + +FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)); +FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table); + +#endif \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 718d3fe..7089340 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -14,8 +14,8 @@ * ----------------------------------------------------------------------------- * File: LinkedList.h * Description: - * This file contains the functions for modifying the Linked List. It includes - * functions to append, Insert before, Insert after, Insert at index, + * This file contains the Linked List structure and the functions for modifying it. + * It includes functions to append, Insert before, Insert after, Insert at index, * Get, Remove element, Remove at index, Check if has element and Check if is empty. * * Author: Mineplay diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index c1bdb2f..62bf88d 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -22,7 +22,6 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/DoublyLinkedList.h" -#include "Fledasty/Utils/Error.h" #include #include diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c new file mode 100644 index 0000000..ff46de1 --- /dev/null +++ b/Src/Core/HashTable.c @@ -0,0 +1,55 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: HashTable.c + * Description: + * This file contains the functions for modifying the Hash Table. It includes functions + * to get, Insert, Remove, check if has key and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/HashTable.h" + +#include +#include +#include + +FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)) { + if (new_hash_table == NULL || hash_function == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_hash_table->element_byte_size = element_byte_size; + new_hash_table->hash_function = hash_function; + + new_hash_table->size = 0; + new_hash_table->capacity = 1024; + new_hash_table->Table = (FledastyHashTablePair**)hallocy_calloc(sizeof(FledastyHashTablePair*), new_hash_table->capacity); + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table) { + if (current_hash_table == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + for (size_t i = 0; i < current_hash_table->capacity; i += 1) { + hallocy_free(current_hash_table->Table[i]); + } + + hallocy_free(current_hash_table->Table); + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index f474731..b335fe4 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,6 +25,9 @@ #include #include #include +#include + +static inline size_t integer_hash_function(void *key) { return *(size_t*)key; } int main() { FledastyQueue test_queue; @@ -179,6 +182,11 @@ int main() { } fledasty_doubly_list_destroy(&test_doubly_linked_list); + + FledastyHashtable test_hash_table; + fledasty_hash_table_initialize(&test_hash_table, sizeof(int), integer_hash_function); + + fledasty_hash_table_destroy(&test_hash_table); printf("Done\n"); return 0; } \ No newline at end of file