Merge pull request 'f3-basic-hash-tabel' (#21) from f3-basic-hash-tabel into main
Reviewed-on: #21
This commit is contained in:
commit
b9e4104468
14 changed files with 422 additions and 23 deletions
|
|
@ -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
|
||||
* -----------------------------------------------------------------------------
|
||||
|
|
@ -51,6 +52,8 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke
|
|||
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index);
|
||||
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list);
|
||||
|
||||
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
|
||||
static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; }
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,8 @@ void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_arr
|
|||
FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index);
|
||||
FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_dynamic_array, void *value);
|
||||
|
||||
FledastyError fledasty_dynamic_array_clear(FledastyDynamicArray *current_dynamic_array);
|
||||
|
||||
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value);
|
||||
inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array->size == 0; }
|
||||
|
||||
|
|
|
|||
58
Include/Fledasty/Core/HashTable.h
Normal file
58
Include/Fledasty/Core/HashTable.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* 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 <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
#include "DynamicArray.h"
|
||||
|
||||
typedef struct {
|
||||
void *key, *value;
|
||||
} FledastyHashTablePair;
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
|
||||
size_t key_byte_size, value_byte_size;
|
||||
FledastyDynamicArray *Table;
|
||||
|
||||
size_t (*hash_function)(void *key);
|
||||
} FledastyHashTable;
|
||||
|
||||
FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key));
|
||||
FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table);
|
||||
|
||||
FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value);
|
||||
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key);
|
||||
FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key);
|
||||
|
||||
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table);
|
||||
|
||||
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key);
|
||||
static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -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
|
||||
|
|
@ -51,6 +51,8 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren
|
|||
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index);
|
||||
FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value);
|
||||
|
||||
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list);
|
||||
|
||||
bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value);
|
||||
static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; }
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,8 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
|||
void *fledasty_queue_peek(const FledastyQueue *current_queue);
|
||||
void *fledasty_queue_pop(FledastyQueue *current_queue);
|
||||
|
||||
FledastyError fledasty_queue_clear(FledastyQueue *current_queue);
|
||||
|
||||
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);
|
|||
void *fledasty_stack_peek(const FledastyStack *current_stack);
|
||||
void *fledasty_stack_pop(FledastyStack *current_stack);
|
||||
|
||||
FledastyError fledasty_stack_clear(FledastyStack *current_stack);
|
||||
|
||||
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ typedef enum {
|
|||
FLEDASTY_ERROR_INVALID_POINTER = 2,
|
||||
FLEDASTY_ERROR_INDEX_OUT_OF_RANGE = 3,
|
||||
FLEDASTY_ERROR_VALUE_NOT_FOUND = 4,
|
||||
FLEDASTY_ERROR_KEY_NOT_FOUND = 5,
|
||||
} FledastyError;
|
||||
|
||||
#endif
|
||||
|
|
@ -22,7 +22,6 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Core/DoublyLinkedList.h"
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
|
|
@ -72,26 +71,23 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou
|
|||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *previous_node = NULL;
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node->next != NULL) {
|
||||
while (current_node != NULL) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
|
||||
HallocyError result = hallocy_free(current_node->previous->value);
|
||||
if (result == HALLOCY_ERROR_NONE) {
|
||||
HallocyError result = hallocy_free(previous_node->value);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
result = hallocy_free(current_node->previous);
|
||||
if (result == HALLOCY_ERROR_NONE) {
|
||||
result = hallocy_free(previous_node);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_node != NULL) {
|
||||
hallocy_free(current_node->value);
|
||||
hallocy_free(current_node);
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
|
|
@ -313,13 +309,42 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
|
|||
}
|
||||
}
|
||||
|
||||
hallocy_free(current_node->next);
|
||||
hallocy_free(current_node->value);
|
||||
hallocy_free(current_node);
|
||||
|
||||
current_doubly_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) {
|
||||
if (current_doubly_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyDoublyLinkedListNode *previous_node = NULL;
|
||||
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
|
||||
HallocyError result = hallocy_free(previous_node->value);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
result = hallocy_free(previous_node);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
current_doubly_linked_list->size = 0;
|
||||
current_doubly_linked_list->start = NULL;
|
||||
current_doubly_linked_list->end = NULL;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
|
||||
if (current_doubly_linked_list == NULL || value == NULL) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Core/DynamicArray.h"
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
|
|
@ -220,6 +221,15 @@ FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_
|
|||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_clear(FledastyDynamicArray *current_dynamic_array) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_dynamic_array->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return false;
|
||||
|
|
|
|||
205
Src/Core/HashTable.c
Normal file
205
Src/Core/HashTable.c
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
/*
|
||||
* 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 <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75;
|
||||
|
||||
FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key)) {
|
||||
if (new_hash_table == NULL || hash_function == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
new_hash_table->key_byte_size = key_byte_size;
|
||||
new_hash_table->value_byte_size = value_byte_size;
|
||||
new_hash_table->hash_function = hash_function;
|
||||
|
||||
new_hash_table->size = 0;
|
||||
new_hash_table->capacity = 1024;
|
||||
new_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), new_hash_table->capacity);
|
||||
if (new_hash_table->Table == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
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) {
|
||||
fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]);
|
||||
}
|
||||
|
||||
HallocyError result = hallocy_free(current_hash_table->Table);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value) {
|
||||
if (current_hash_table == NULL || key == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_hash_table->size += 1;
|
||||
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) {
|
||||
current_hash_table->capacity += current_hash_table->capacity;
|
||||
|
||||
FledastyDynamicArray *previous_table = current_hash_table->Table;
|
||||
current_hash_table->Table = (FledastyDynamicArray*)hallocy_realloc(current_hash_table->Table, current_hash_table->capacity * sizeof(FledastyDynamicArray));
|
||||
|
||||
if (previous_table != current_hash_table->Table) {
|
||||
hallocy_set_memory(current_hash_table->Table + (current_hash_table->size - 2), 0, current_hash_table->capacity);
|
||||
}
|
||||
}
|
||||
|
||||
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[index].buffer == NULL) {
|
||||
fledasty_dynamic_array_initialize(¤t_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair));
|
||||
}
|
||||
|
||||
FledastyHashTablePair pair;
|
||||
|
||||
pair.key = hallocy_malloc(current_hash_table->key_byte_size);
|
||||
if (pair.key == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(pair.key, key, current_hash_table->key_byte_size);
|
||||
|
||||
pair.value = hallocy_malloc(current_hash_table->value_byte_size);
|
||||
if (pair.value == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size);
|
||||
|
||||
fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair);
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) {
|
||||
if (current_hash_table == NULL || key == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[index].buffer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t list_index = 0;
|
||||
while (list_index < current_hash_table->Table[index].size) {
|
||||
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index);
|
||||
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
|
||||
return value->value;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key) {
|
||||
if (current_hash_table == NULL || key == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[index].buffer == NULL) {
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND;
|
||||
}
|
||||
|
||||
size_t list_index = 0;
|
||||
while (list_index < current_hash_table->Table[index].size) {
|
||||
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index);
|
||||
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
|
||||
HallocyError result = hallocy_free(value->key);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
result = hallocy_free(value->value);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_remove_at_index(¤t_hash_table->Table[index], list_index);
|
||||
current_hash_table->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) {
|
||||
if (current_hash_table == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < current_hash_table->capacity; index += 1) {
|
||||
if (current_hash_table->Table[index].buffer != NULL) {
|
||||
fledasty_dynamic_array_destroy(¤t_hash_table->Table[index]);
|
||||
}
|
||||
}
|
||||
|
||||
current_hash_table->size = 0;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) {
|
||||
if (current_hash_table == NULL || key == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[index].buffer == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t list_index = 0;
|
||||
while (list_index < current_hash_table->Table[index].size) {
|
||||
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index);
|
||||
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -74,12 +74,12 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li
|
|||
current_node = current_node->next;
|
||||
|
||||
HallocyError result = hallocy_free(previous_node->value);
|
||||
if (result == HALLOCY_ERROR_NONE) {
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
result = hallocy_free(previous_node);
|
||||
if (result == HALLOCY_ERROR_NONE) {
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
|
@ -265,13 +265,42 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link
|
|||
previous_node->next = current_node->next;
|
||||
}
|
||||
|
||||
hallocy_free(current_node->next);
|
||||
hallocy_free(current_node->value);
|
||||
hallocy_free(current_node);
|
||||
|
||||
current_linked_list->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list) {
|
||||
if (current_linked_list == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
FledastyLinkedListNode *previous_node = NULL;
|
||||
FledastyLinkedListNode *current_node = current_linked_list->start;
|
||||
while (current_node != NULL) {
|
||||
previous_node = current_node;
|
||||
current_node = current_node->next;
|
||||
|
||||
HallocyError result = hallocy_free(previous_node->value);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
result = hallocy_free(previous_node);
|
||||
if (result != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
current_linked_list->size = 0;
|
||||
current_linked_list->start = NULL;
|
||||
current_linked_list->end = NULL;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value) {
|
||||
if (current_linked_list == NULL || value == NULL) {
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -125,3 +125,12 @@ void *fledasty_queue_pop(FledastyQueue *current_queue) {
|
|||
|
||||
return value_address;
|
||||
}
|
||||
|
||||
FledastyError fledasty_queue_clear(FledastyQueue *current_queue) {
|
||||
if (current_queue == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_queue->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,3 +107,12 @@ void *fledasty_stack_pop(FledastyStack *current_stack) {
|
|||
|
||||
return value_address;
|
||||
}
|
||||
|
||||
FledastyError fledasty_stack_clear(FledastyStack *current_stack) {
|
||||
if (current_stack == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_stack->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
46
Tests/Main.c
46
Tests/Main.c
|
|
@ -25,6 +25,9 @@
|
|||
#include <Fledasty/Core/DynamicArray.h>
|
||||
#include <Fledasty/Core/LinkedList.h>
|
||||
#include <Fledasty/Core/DoublyLinkedList.h>
|
||||
#include <Fledasty/Core/HashTable.h>
|
||||
|
||||
static inline size_t integer_hash_function(void *key) { return *(size_t*)key; }
|
||||
|
||||
int main() {
|
||||
FledastyQueue test_queue;
|
||||
|
|
@ -42,6 +45,7 @@ int main() {
|
|||
printf("Queue popped: %d\n", *popped_data);
|
||||
}
|
||||
|
||||
fledasty_queue_clear(&test_queue);
|
||||
if (fledasty_queue_is_empty(&test_queue)) {
|
||||
printf("Queue is empty\n");
|
||||
}
|
||||
|
|
@ -63,6 +67,7 @@ int main() {
|
|||
printf("Stack popped: %d\n", *popped_data);
|
||||
}
|
||||
|
||||
fledasty_stack_clear(&test_stack);
|
||||
if (fledasty_stack_is_empty(&test_stack)) {
|
||||
printf("Stack is empty\n");
|
||||
}
|
||||
|
|
@ -98,6 +103,7 @@ int main() {
|
|||
printf("Dynamic array contains %d\n", insert_value);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_clear(&test_dynamic_array);
|
||||
if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) {
|
||||
printf("Dynamic array is empty\n");
|
||||
}
|
||||
|
|
@ -133,15 +139,15 @@ int main() {
|
|||
printf("Linked list contains %d\n", insert_value);
|
||||
}
|
||||
|
||||
fledasty_linked_list_clear(&test_linked_list);
|
||||
if (fledasty_linked_list_is_empty(&test_linked_list)) {
|
||||
printf("Linked list is empty\n");
|
||||
}
|
||||
|
||||
fledasty_linked_list_destroy(&test_linked_list);
|
||||
|
||||
|
||||
FledastyDoublyLinkedList test_doubly_linked_list;
|
||||
fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
||||
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
|
||||
}
|
||||
|
|
@ -174,11 +180,47 @@ int main() {
|
|||
printf("Doubly linked list contains %d\n", insert_value);
|
||||
}
|
||||
|
||||
fledasty_doubly_linked_list_clear(&test_doubly_linked_list);
|
||||
if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) {
|
||||
printf("Doubly linked list is empty\n");
|
||||
}
|
||||
|
||||
fledasty_doubly_list_destroy(&test_doubly_linked_list);
|
||||
|
||||
FledastyHashTable test_hash_table;
|
||||
fledasty_hash_table_initialize(&test_hash_table, sizeof(int), sizeof(int), integer_hash_function);
|
||||
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
int pow_value = i * i;
|
||||
fledasty_hash_table_insert(&test_hash_table, &pow_value, &i);
|
||||
}
|
||||
|
||||
int hash_table_value = 5;
|
||||
int hash_table_remove = 2;
|
||||
fledasty_hash_table_insert(&test_hash_table, &hash_table_remove, &hash_table_value);
|
||||
fledasty_hash_table_remove(&test_hash_table, &hash_table_remove);
|
||||
|
||||
if (fledasty_hash_table_get(&test_hash_table, &hash_table_remove) != NULL) {
|
||||
printf("Value not removed from hash table\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
int pow_value = i * i;
|
||||
int *hash_table_value = (int*)fledasty_hash_table_get(&test_hash_table, &pow_value);
|
||||
printf("Hash table get: %d\n", *hash_table_value);
|
||||
}
|
||||
|
||||
hash_table_value = 4;
|
||||
if (fledasty_hash_table_has_key(&test_hash_table, &hash_table_value)) {
|
||||
printf("Hash table contains key %d\n", hash_table_value);
|
||||
}
|
||||
|
||||
fledasty_hash_table_clear(&test_hash_table);
|
||||
if (fledasty_hash_table_is_empty(&test_hash_table)) {
|
||||
printf("Hash table is empty\n");
|
||||
}
|
||||
|
||||
fledasty_hash_table_destroy(&test_hash_table);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue