feat(dynamic array): implemented clear function
This commit is contained in:
parent
5556948e2d
commit
f0c224d879
4 changed files with 38 additions and 5 deletions
|
|
@ -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; }
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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(¤t_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(¤t_hash_table->Table[index], &pair);
|
||||
|
|
@ -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(¤t_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(¤t_hash_table->Table[index], list_index);
|
||||
current_hash_table->size -= 1;
|
||||
|
|
|
|||
|
|
@ -101,6 +101,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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue