refactor(error handling): changed get functions for tree and hash table to improve error handling
This commit is contained in:
parent
ecbffdc112
commit
4e57a165fd
3 changed files with 23 additions and 14 deletions
|
|
@ -55,7 +55,7 @@ typedef struct {
|
|||
FledastyError fledasty_hash_table_##name##_free(FledastyHashTable_##name *current_hash_table); \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *current_hash_table, key_type key, value_type value); \
|
||||
value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out); \
|
||||
FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *current_hash_table); \
|
||||
|
|
@ -137,24 +137,25 @@ FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *curr
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
FledastyError fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key, value_type *out) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return 0; \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
if (current_hash_table->Table[index].buffer == NULL) { \
|
||||
return 0; \
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \
|
||||
const FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \
|
||||
if (compare_key_function(value.key, key)) { \
|
||||
return value.value; \
|
||||
*out = value.value; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return 0; \
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ typedef struct {
|
|||
FledastyError fledasty_binary_search_tree_##name##_free(FledastyBinarySearchTree_##name *current_binary_search_tree); \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type value); \
|
||||
FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \
|
||||
FledastyError fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type *out); \
|
||||
FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key); \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name **fledasty_binary_search_tree_##name##_in_order_traversal(FledastyBinarySearchTree_##name *current_binary_search_tree);
|
||||
|
|
@ -139,9 +139,9 @@ FledastyError fledasty_binary_search_tree_##name##_insert(FledastyBinarySearchTr
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \
|
||||
FledastyError fledasty_binary_search_tree_##name##_get(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key, value_type *out) { \
|
||||
if (current_binary_search_tree == NULL) { \
|
||||
return NULL; \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
FledastyBinarySearchTreeNode_##name *current_node = current_binary_search_tree->top; \
|
||||
|
|
@ -153,7 +153,12 @@ FledastyBinarySearchTreeNode_##name *fledasty_binary_search_tree_##name##_get(Fl
|
|||
} \
|
||||
} \
|
||||
\
|
||||
return current_node; \
|
||||
if (current_node == NULL) { \
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
*out = current_node->value; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_binary_search_tree_##name##_remove(FledastyBinarySearchTree_##name *current_binary_search_tree, key_type key) { \
|
||||
|
|
|
|||
11
Tests/Main.c
11
Tests/Main.c
|
|
@ -19,6 +19,7 @@
|
|||
* Author: Mineplay
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
#include <stdio.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Fledasty/Core/Queue.h>
|
||||
|
|
@ -203,12 +204,14 @@ int main() {
|
|||
fledasty_hash_table_int_int_insert(&test_hash_table, 2, 5);
|
||||
fledasty_hash_table_int_int_remove(&test_hash_table, 2);
|
||||
|
||||
if (fledasty_hash_table_int_int_get(&test_hash_table, 2) != 0) {
|
||||
int get_out = 0;
|
||||
if (fledasty_hash_table_int_int_get(&test_hash_table, 2, &get_out) != FLEDASTY_ERROR_KEY_NOT_FOUND) {
|
||||
printf("Value not removed from hash table\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
printf("Hash table get: %d\n", fledasty_hash_table_int_int_get(&test_hash_table, i * i));
|
||||
fledasty_hash_table_int_int_get(&test_hash_table, i * i, &get_out);
|
||||
printf("Hash table get: %d\n", get_out);
|
||||
}
|
||||
|
||||
if (fledasty_hash_table_int_int_has_key(&test_hash_table, 4)) {
|
||||
|
|
@ -344,8 +347,8 @@ int main() {
|
|||
|
||||
hallocy_free(in_order_result);
|
||||
|
||||
FledastyBinarySearchTreeNode_int_int *node = fledasty_binary_search_tree_int_int_get(&test_search_tree, 7);
|
||||
printf("Search tree get result: %d\n", node->value);
|
||||
fledasty_binary_search_tree_int_int_get(&test_search_tree, 7, &get_out);
|
||||
printf("Search tree get result: %d\n", get_out);
|
||||
|
||||
fledasty_binary_search_tree_int_int_free(&test_search_tree);
|
||||
printf("Done\n");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue