Compare commits

...

5 commits

13 changed files with 143 additions and 26 deletions

View file

@ -52,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; }

View file

@ -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; }

View file

@ -47,12 +47,12 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table,
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(FledastyHashTable *current_hash_table, void *key);
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(FledastyHashTable *current_hash_table, void *key);
static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
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

View file

@ -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; }

View file

@ -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

View file

@ -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

View file

@ -71,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;
}
@ -312,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;

View file

@ -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;

View file

@ -21,8 +21,6 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/HashTable.h"
#include "Fledasty/Core/DynamicArray.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -42,6 +40,9 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table,
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;
}
@ -55,7 +56,11 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table)
fledasty_dynamic_array_destroy(&current_hash_table->Table[i]);
}
hallocy_free(current_hash_table->Table);
HallocyError result = hallocy_free(current_hash_table->Table);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
return FLEDASTY_ERROR_NONE;
}
@ -84,9 +89,17 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
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(&current_hash_table->Table[index], &pair);
@ -94,7 +107,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
return FLEDASTY_ERROR_NONE;
}
void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) {
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) {
if (current_hash_table == NULL || key == NULL) {
return NULL;
}
@ -131,8 +144,15 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table,
while (list_index < current_hash_table->Table[index].size) {
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(&current_hash_table->Table[index], list_index);
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
hallocy_free(value->key);
hallocy_free(value->value);
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(&current_hash_table->Table[index], list_index);
current_hash_table->size -= 1;
@ -161,7 +181,7 @@ FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) {
return FLEDASTY_ERROR_NONE;
}
bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) {
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) {
if (current_hash_table == NULL || key == NULL) {
return false;
}

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -45,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");
}
@ -66,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");
}
@ -101,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");
}
@ -136,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);
}
@ -177,6 +180,7 @@ 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");
}