Compare commits
3 commits
2bfd6ddb4c
...
dfb24b1716
| Author | SHA1 | Date | |
|---|---|---|---|
| dfb24b1716 | |||
| 352281790d | |||
| 25e7e915f4 |
7 changed files with 499 additions and 674 deletions
|
|
@ -26,34 +26,225 @@
|
|||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
#define FLEDASTY_DYNAMIC_ARRAY_DEFINE(type, name) \
|
||||
typedef struct { \
|
||||
size_t size, capacity; \
|
||||
type *buffer; \
|
||||
} FledastyDynamicArray_##name; \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_free(FledastyDynamicArray_##name *current_dynamic_array); \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_append(FledastyDynamicArray_##name *current_dynamic_array, type value); \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_at_index(FledastyDynamicArray_##name *current_dynamic_array, const size_t index, type value); \
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_before_value(FledastyDynamicArray_##name *current_dynamic_array, type before_value, type value); \
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_after_value(FledastyDynamicArray_##name *current_dynamic_array, type after_value, type value); \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_remove_at_index(FledastyDynamicArray_##name *current_dynamic_array, const size_t index); \
|
||||
FledastyError fledasty_dynamic_array_##name##_remove_value(FledastyDynamicArray_##name *current_dynamic_array, type value); \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_clear(FledastyDynamicArray_##name *current_dynamic_array); \
|
||||
FledastyError fledasty_dynamic_array_##name##_shrink_to_fit(FledastyDynamicArray_##name *current_dynamic_array); \
|
||||
\
|
||||
bool fledasty_dynamic_array_##name##_has_value(const FledastyDynamicArray_##name *current_dynamic_array, type value); \
|
||||
inline static bool fledasty_dynamic_array_##name##_is_empty(const FledastyDynamicArray_##name *current_dynamic_array) { return current_dynamic_array == NULL || current_dynamic_array->size == 0; }
|
||||
|
||||
size_t element_byte_size;
|
||||
unsigned char *buffer;
|
||||
} FledastyDynamicArray;
|
||||
|
||||
FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size);
|
||||
FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array);
|
||||
|
||||
FledastyError fledasty_dynamic_array_append(FledastyDynamicArray *current_dynamic_array, void *value);
|
||||
|
||||
FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index, void *value);
|
||||
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value);
|
||||
FledastyError fledasty_dynamic_array_insert_after_value(FledastyDynamicArray *current_dynamic_array, void *after_value, void *value);
|
||||
|
||||
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index);
|
||||
|
||||
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);
|
||||
FledastyError fledasty_dynamic_array_shrink_to_fit(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 == NULL || current_dynamic_array->size == 0; }
|
||||
#define FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(type, name, compare_function) \
|
||||
FledastyError fledasty_dynamic_array_##name##_free(FledastyDynamicArray_##name *current_dynamic_array) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (hallocy_free(current_dynamic_array->buffer) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_dynamic_array->size = 0; \
|
||||
current_dynamic_array->capacity = 0; \
|
||||
current_dynamic_array->buffer = NULL; \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_append(FledastyDynamicArray_##name *current_dynamic_array, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) { \
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity ? current_dynamic_array->capacity : 16; \
|
||||
current_dynamic_array->buffer = (type*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * sizeof(type)); \
|
||||
\
|
||||
if (current_dynamic_array->buffer == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
current_dynamic_array->buffer[current_dynamic_array->size++] = value; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_at_index(FledastyDynamicArray_##name *current_dynamic_array, const size_t index, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
if (index >= current_dynamic_array->size) { \
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
|
||||
} \
|
||||
\
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) { \
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity ? current_dynamic_array->capacity : 16; \
|
||||
current_dynamic_array->buffer = (type*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * sizeof(type)); \
|
||||
\
|
||||
if (current_dynamic_array->buffer == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
type *insert_pointer = current_dynamic_array->buffer + index; \
|
||||
hallocy_move_memory(insert_pointer + 1, insert_pointer, (current_dynamic_array->size - index) * sizeof(type)); \
|
||||
*insert_pointer = value; \
|
||||
\
|
||||
current_dynamic_array->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_before_value(FledastyDynamicArray_##name *current_dynamic_array, type before_value, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < current_dynamic_array->size; index += 1) { \
|
||||
if (compare_function(current_dynamic_array->buffer[index], before_value)) { \
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) { \
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity ? current_dynamic_array->capacity : 16; \
|
||||
current_dynamic_array->buffer = (type*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * sizeof(type)); \
|
||||
\
|
||||
if (current_dynamic_array->buffer == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
type *insert_pointer = current_dynamic_array->buffer + index; \
|
||||
hallocy_move_memory(insert_pointer + 1, insert_pointer, (current_dynamic_array->size - index) * sizeof(type)); \
|
||||
*insert_pointer = value; \
|
||||
\
|
||||
current_dynamic_array->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_insert_after_value(FledastyDynamicArray_##name *current_dynamic_array, type after_value, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < current_dynamic_array->size; index += 1) { \
|
||||
if (compare_function(current_dynamic_array->buffer[index], after_value)) { \
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) { \
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity ? current_dynamic_array->capacity : 16; \
|
||||
current_dynamic_array->buffer = (type*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * sizeof(type)); \
|
||||
\
|
||||
if (current_dynamic_array->buffer == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
type *insert_pointer = current_dynamic_array->buffer + index + 1; \
|
||||
hallocy_move_memory(insert_pointer + 1, insert_pointer, (current_dynamic_array->size - index) * sizeof(type)); \
|
||||
*insert_pointer = value; \
|
||||
\
|
||||
current_dynamic_array->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_remove_at_index(FledastyDynamicArray_##name *current_dynamic_array, const size_t index) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (index >= current_dynamic_array->size) { \
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
|
||||
} \
|
||||
\
|
||||
type *remove_pointer = current_dynamic_array->buffer + index; \
|
||||
hallocy_copy_memory(remove_pointer, remove_pointer + 1, (current_dynamic_array->size - index + 1) * sizeof(type)); \
|
||||
current_dynamic_array->size -= 1; \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_remove_value(FledastyDynamicArray_##name *current_dynamic_array, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < current_dynamic_array->size; index += 1) { \
|
||||
if (compare_function(current_dynamic_array->buffer[index], value)) { \
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + index, current_dynamic_array->buffer + index + 1, (current_dynamic_array->size - index) * sizeof(type)); \
|
||||
current_dynamic_array->size -= 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_clear(FledastyDynamicArray_##name *current_dynamic_array) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
current_dynamic_array->size = 0; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_dynamic_array_##name##_shrink_to_fit(FledastyDynamicArray_##name *current_dynamic_array) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
current_dynamic_array->capacity = current_dynamic_array->size; \
|
||||
type *shrinked_buffer = (type*)hallocy_malloc(current_dynamic_array->capacity * sizeof(type)); \
|
||||
if (shrinked_buffer == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
hallocy_copy_memory(shrinked_buffer, current_dynamic_array->buffer, (current_dynamic_array->size - 1) * sizeof(type)); \
|
||||
if (hallocy_free(current_dynamic_array->buffer) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
current_dynamic_array->buffer = shrinked_buffer; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
bool fledasty_dynamic_array_##name##_has_value(const FledastyDynamicArray_##name *current_dynamic_array, type value) { \
|
||||
if (current_dynamic_array == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < current_dynamic_array->size; index += 1) { \
|
||||
if (compare_function(current_dynamic_array->buffer[index], value)) { \
|
||||
return true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -26,34 +26,220 @@
|
|||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
#include "DynamicArray.h"
|
||||
|
||||
typedef struct {
|
||||
void *key, *value;
|
||||
} FledastyHashTablePair;
|
||||
#define FLEDASTY_HASH_TABLE_DEFINE(key_type, value_type, name, compare_key_function) \
|
||||
typedef struct { \
|
||||
key_type key; \
|
||||
value_type value; \
|
||||
} FledastyHashTablePair_##name; \
|
||||
\
|
||||
FLEDASTY_DYNAMIC_ARRAY_DEFINE(FledastyHashTablePair_##name, fledasty_internal_pair) \
|
||||
\
|
||||
typedef struct { \
|
||||
size_t size, capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *Table; \
|
||||
\
|
||||
size_t (*hash_function)(const key_type key); \
|
||||
} FledastyHashTable_##name; \
|
||||
\
|
||||
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##_remove(FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *current_hash_table); \
|
||||
FledastyError fledasty_hash_table_##name##_shrink_to_fit(FledastyHashTable_##name *current_hash_table); \
|
||||
\
|
||||
bool fledasty_hash_table_##name##_has_key(const FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
static inline bool fledasty_hash_table_##name##_is_empty(const FledastyHashTable_##name *current_hash_table) { return current_hash_table == NULL || current_hash_table->size == 0; } \
|
||||
static inline bool fledasty_hash_table_##name##_compare_pair(const FledastyHashTablePair_##name first_pair, const FledastyHashTablePair_##name second_pair) { return compare_key_function(first_pair.key, second_pair.key); }
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
|
||||
size_t key_byte_size, value_byte_size;
|
||||
FledastyDynamicArray *Table;
|
||||
|
||||
size_t (*hash_function)(const void *key);
|
||||
} FledastyHashTable;
|
||||
|
||||
FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, const size_t key_byte_size, const size_t value_byte_size, size_t (*hash_function)(const 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);
|
||||
FledastyError fledasty_hash_table_shrink_to_fit(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 == NULL || current_hash_table->size == 0; }
|
||||
#define FLEDASTY_HASH_TABLE_IMPLEMENT(key_type, value_type, name, compare_key_function) \
|
||||
FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(FledastyHashTablePair_##name, fledasty_internal_pair, fledasty_hash_table_##name##_compare_pair) \
|
||||
static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75; \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_free(FledastyHashTable_##name *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) { \
|
||||
FledastyError result = fledasty_dynamic_array_fledasty_internal_pair_free(¤t_hash_table->Table[i]); \
|
||||
if (result != FLEDASTY_ERROR_NONE) { \
|
||||
return result; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
if (hallocy_free(current_hash_table->Table) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *current_hash_table, key_type key, value_type value) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) { \
|
||||
const size_t old_capacity = current_hash_table->capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *previous_table = current_hash_table->Table; \
|
||||
\
|
||||
current_hash_table->capacity += current_hash_table->capacity ? current_hash_table->capacity : 1024; \
|
||||
current_hash_table->Table = (FledastyDynamicArray_fledasty_internal_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_fledasty_internal_pair), current_hash_table->capacity); \
|
||||
if (current_hash_table->Table == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < old_capacity; index += 1) { \
|
||||
FledastyDynamicArray_fledasty_internal_pair *current_dynamic_array = previous_table + index; \
|
||||
if (current_dynamic_array->buffer != NULL) { \
|
||||
for (size_t list_index = 0; list_index < current_dynamic_array->size; list_index += 1) { \
|
||||
FledastyHashTablePair_##name pair = current_dynamic_array->buffer[list_index]; \
|
||||
\
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(current_hash_table->Table + key_index, pair); \
|
||||
} \
|
||||
\
|
||||
fledasty_dynamic_array_fledasty_internal_pair_free(current_dynamic_array); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
hallocy_free(previous_table); \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
\
|
||||
FledastyHashTablePair_##name pair = { .key = key, .value = value }; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(¤t_hash_table->Table[index], pair); \
|
||||
\
|
||||
current_hash_table->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
if (current_hash_table->Table[index].buffer == NULL) { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
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; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
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 FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \
|
||||
FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \
|
||||
if (compare_key_function(value.key, key)) { \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_remove_at_index(¤t_hash_table->Table[index], list_index); \
|
||||
current_hash_table->size -= 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *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) { \
|
||||
FledastyError result = fledasty_dynamic_array_fledasty_internal_pair_free(¤t_hash_table->Table[index]); \
|
||||
if (result != FLEDASTY_ERROR_NONE) { \
|
||||
return result; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
current_hash_table->size = 0; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_shrink_to_fit(FledastyHashTable_##name *current_hash_table) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
const size_t old_capacity = current_hash_table->capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *previous_table = current_hash_table->Table; \
|
||||
\
|
||||
if (current_hash_table->size == 0) { \
|
||||
current_hash_table->capacity = 1024; \
|
||||
} else { \
|
||||
current_hash_table->capacity = (current_hash_table->size * 100) / FLEDASTY_HASH_TABLE_SIZE_THRESHOLD; \
|
||||
} \
|
||||
\
|
||||
current_hash_table->Table = (FledastyDynamicArray_fledasty_internal_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_fledasty_internal_pair), current_hash_table->capacity); \
|
||||
if (current_hash_table->Table == NULL) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
for (size_t index = 0; index < old_capacity; index += 1) { \
|
||||
FledastyDynamicArray_fledasty_internal_pair *current_dynamic_array = previous_table + index; \
|
||||
if (current_dynamic_array->buffer != NULL) { \
|
||||
for (size_t list_index = 0; list_index < current_dynamic_array->size; list_index += 1) { \
|
||||
FledastyHashTablePair_##name pair = current_dynamic_array->buffer[list_index]; \
|
||||
\
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(current_hash_table->Table + key_index, pair); \
|
||||
} \
|
||||
\
|
||||
fledasty_dynamic_array_fledasty_internal_pair_free(current_dynamic_array); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
hallocy_free(previous_table); \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
bool fledasty_hash_table_##name##_has_key(const FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
if (current_hash_table->Table[index].buffer == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
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 true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -31,26 +31,26 @@
|
|||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
#define FLEDASTY_QUEUE_DEFINE(type) \
|
||||
#define FLEDASTY_QUEUE_DEFINE(type, name) \
|
||||
typedef struct { \
|
||||
size_t size, capacity; \
|
||||
size_t head, tail; \
|
||||
type *buffer; \
|
||||
} FledastyQueue_##type; \
|
||||
} FledastyQueue_##name; \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue); \
|
||||
FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *current_queue); \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue, type value); \
|
||||
type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue); \
|
||||
FledastyError fledasty_queue_##name##_push(FledastyQueue_##name *current_queue, type value); \
|
||||
type fledasty_queue_##name##_pop(FledastyQueue_##name *current_queue); \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue); \
|
||||
FledastyError fledasty_queue_##type##_shrink_to_fit(FledastyQueue_##type *current_queue); \
|
||||
FledastyError fledasty_queue_##name##_clear(FledastyQueue_##name *current_queue); \
|
||||
FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue); \
|
||||
\
|
||||
static inline type fledasty_queue_##type##_peek(const FledastyQueue_##type *current_queue) { return (current_queue != NULL && current_queue->size > 0) ? current_queue->buffer[current_queue->size - 1] : 0; } \
|
||||
static inline bool fledasty_queue_##type##_is_empty(const FledastyQueue_##type *current_queue) { return current_queue == NULL || current_queue->size == 0; }
|
||||
static inline type fledasty_queue_##name##_peek(const FledastyQueue_##name *current_queue) { return (current_queue != NULL && current_queue->size > 0) ? current_queue->buffer[current_queue->head] : 0; } \
|
||||
static inline bool fledasty_queue_##name##_is_empty(const FledastyQueue_##name *current_queue) { return current_queue == NULL || current_queue->size == 0; }
|
||||
|
||||
#define FLEDASTY_QUEUE_IMPLEMENT(type) \
|
||||
FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue) { \
|
||||
#define FLEDASTY_QUEUE_IMPLEMENT(type, name) \
|
||||
FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *current_queue) { \
|
||||
if (current_queue == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -66,7 +66,7 @@ FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue)
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue, type value) { \
|
||||
FledastyError fledasty_queue_##name##_push(FledastyQueue_##name *current_queue, type value) { \
|
||||
if (current_queue == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -101,9 +101,9 @@ FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue,
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue) { \
|
||||
type fledasty_queue_##name##_pop(FledastyQueue_##name *current_queue) { \
|
||||
if (current_queue == NULL || current_queue->size == 0) { \
|
||||
return 0; \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
current_queue->size -= 1; \
|
||||
|
|
@ -115,7 +115,7 @@ type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue) {
|
|||
return current_queue->buffer[current_queue->head - 1]; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue) { \
|
||||
FledastyError fledasty_queue_##name##_clear(FledastyQueue_##name *current_queue) { \
|
||||
if (current_queue == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -124,7 +124,7 @@ FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue)
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_queue_##type##_shrink_to_fit(FledastyQueue_##type *current_queue) { \
|
||||
FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue) { \
|
||||
if (current_queue == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
|
|||
|
|
@ -31,25 +31,25 @@
|
|||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
#define FLEDASTY_STACK_DEFINE(type) \
|
||||
#define FLEDASTY_STACK_DEFINE(type, name) \
|
||||
typedef struct { \
|
||||
size_t size, capacity; \
|
||||
type *buffer; \
|
||||
} FledastyStack_##type; \
|
||||
} FledastyStack_##name; \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack); \
|
||||
FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack); \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack, type value); \
|
||||
FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value); \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack); \
|
||||
FledastyError fledasty_stack_##type##_shrink_to_fit(FledastyStack_##type *current_stack); \
|
||||
FledastyError fledasty_stack_##name##_clear(FledastyStack_##name *current_stack); \
|
||||
FledastyError fledasty_stack_##name##_shrink_to_fit(FledastyStack_##name *current_stack); \
|
||||
\
|
||||
static inline type fledasty_stack_##type##_pop(FledastyStack_##type *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[--current_stack->size] : 0; } \
|
||||
static inline type fledasty_stack_##type##_peek(const FledastyStack_##type *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[current_stack->size - 1] : 0; } \
|
||||
static inline bool fledasty_stack_##type##_is_empty(const FledastyStack_##type *current_stack) { return current_stack == NULL || current_stack->size == 0; }
|
||||
static inline type fledasty_stack_##name##_pop(FledastyStack_##name *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[--current_stack->size] : 0; } \
|
||||
static inline type fledasty_stack_##name##_peek(const FledastyStack_##name *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[current_stack->size - 1] : 0; } \
|
||||
static inline bool fledasty_stack_##name##_is_empty(const FledastyStack_##name *current_stack) { return current_stack == NULL || current_stack->size == 0; }
|
||||
|
||||
#define FLEDASTY_STACK_IMPLEMENT(type) \
|
||||
FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack) { \
|
||||
#define FLEDASTY_STACK_IMPLEMENT(type, name) \
|
||||
FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack) { \
|
||||
if (current_stack == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -65,7 +65,7 @@ FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack)
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack, type value) { \
|
||||
FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value) { \
|
||||
if (current_stack == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -83,7 +83,7 @@ FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack,
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack) { \
|
||||
FledastyError fledasty_stack_##name##_clear(FledastyStack_##name *current_stack) { \
|
||||
if (current_stack == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
@ -92,7 +92,7 @@ FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack)
|
|||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_stack_##type##_shrink_to_fit(FledastyStack_##type *current_stack) { \
|
||||
FledastyError fledasty_stack_##name##_shrink_to_fit(FledastyStack_##name *current_stack) { \
|
||||
if (current_stack == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
|
|
|
|||
|
|
@ -1,267 +0,0 @@
|
|||
/*
|
||||
* 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: DynamicArray.c
|
||||
* Description:
|
||||
* This file contains the functions for modifying the Dynamic Array. 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
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Core/DynamicArray.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size) {
|
||||
if (new_dynamic_array == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
new_dynamic_array->element_byte_size = element_byte_size;
|
||||
if (values == NULL || values_size == 0) {
|
||||
new_dynamic_array->size = 0;
|
||||
new_dynamic_array->capacity = 10;
|
||||
|
||||
new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size);
|
||||
if (new_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
} else {
|
||||
new_dynamic_array->size = values_size;
|
||||
new_dynamic_array->capacity = new_dynamic_array->size + new_dynamic_array->size;
|
||||
|
||||
new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size);
|
||||
if (new_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(new_dynamic_array->buffer, values, values_size * element_byte_size);
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (hallocy_free(current_dynamic_array->buffer) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
current_dynamic_array->buffer = NULL;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_append(FledastyDynamicArray *current_dynamic_array, void *value) {
|
||||
if (current_dynamic_array == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + (current_dynamic_array->size * current_dynamic_array->element_byte_size), value, current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index, void *value) {
|
||||
if (current_dynamic_array == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (index >= current_dynamic_array->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *insert_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
|
||||
hallocy_move_memory(insert_pointer + current_dynamic_array->element_byte_size, insert_pointer, (current_dynamic_array->size - index) * current_dynamic_array->element_byte_size);
|
||||
hallocy_copy_memory(insert_pointer, value, current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value) {
|
||||
if (current_dynamic_array == NULL || before_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
size_t byte_index = 0;
|
||||
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
|
||||
while (byte_index < byte_size) {
|
||||
if (hallocy_compare_memory(current_dynamic_array->buffer + byte_index, before_value, current_dynamic_array->element_byte_size)) {
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_move_memory(current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), current_dynamic_array->buffer + byte_index, byte_size - byte_index);
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
byte_index += current_dynamic_array->element_byte_size;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_insert_after_value(FledastyDynamicArray *current_dynamic_array, void *after_value, void *value) {
|
||||
if (current_dynamic_array == NULL || after_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
size_t byte_index = 0;
|
||||
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
|
||||
while (byte_index < byte_size) {
|
||||
if (hallocy_compare_memory(current_dynamic_array->buffer + byte_index, after_value, current_dynamic_array->element_byte_size)) {
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
byte_index += current_dynamic_array->element_byte_size;
|
||||
hallocy_move_memory(current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), current_dynamic_array->buffer + byte_index, byte_size - byte_index);
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
byte_index += current_dynamic_array->element_byte_size;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index) {
|
||||
if (current_dynamic_array == NULL || index >= current_dynamic_array->size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
if (index >= current_dynamic_array->size) {
|
||||
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
unsigned char *index_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
|
||||
hallocy_copy_memory(index_pointer, index_pointer + current_dynamic_array->element_byte_size, (current_dynamic_array->size + index) * current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size -= 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_dynamic_array, void *value) {
|
||||
if (current_dynamic_array == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
size_t byte_index = 0;
|
||||
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
|
||||
while (byte_index < byte_size) {
|
||||
if (hallocy_compare_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size)) {
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), byte_size - (byte_index + current_dynamic_array->element_byte_size));
|
||||
current_dynamic_array->size -= 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
byte_index += current_dynamic_array->element_byte_size;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
FledastyError fledasty_dynamic_array_shrink_to_fit(FledastyDynamicArray *current_dynamic_array) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
current_dynamic_array->capacity = (current_dynamic_array->size == 0) ? 10 : current_dynamic_array->size;
|
||||
unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
if (shrinked_buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(shrinked_buffer, current_dynamic_array->buffer, current_dynamic_array->size * current_dynamic_array->element_byte_size);
|
||||
if (hallocy_free(current_dynamic_array->buffer) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
current_dynamic_array->buffer = shrinked_buffer;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value) {
|
||||
if (current_dynamic_array == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t byte_index = 0; byte_index < current_dynamic_array->size * current_dynamic_array->element_byte_size; byte_index += current_dynamic_array->element_byte_size) {
|
||||
if (hallocy_compare_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -1,276 +0,0 @@
|
|||
/*
|
||||
* 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, const size_t key_byte_size, const size_t value_byte_size, size_t (*hash_function)(const 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) {
|
||||
FledastyError result = fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]);
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (hallocy_free(current_hash_table->Table) != 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;
|
||||
}
|
||||
|
||||
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) {
|
||||
const size_t old_capacity = current_hash_table->capacity;
|
||||
FledastyDynamicArray *previous_table = current_hash_table->Table;
|
||||
|
||||
current_hash_table->capacity += current_hash_table->capacity;
|
||||
current_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), current_hash_table->capacity);
|
||||
if (current_hash_table->Table == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < old_capacity; index += 1) {
|
||||
FledastyDynamicArray *current_dynamic_array = previous_table + index;
|
||||
if (current_dynamic_array->buffer != NULL) {
|
||||
for (size_t list_index = 0; list_index < current_dynamic_array->size; list_index += 1) {
|
||||
FledastyHashTablePair *pair = (FledastyHashTablePair*)fledasty_dynamic_array_get(current_dynamic_array, list_index);
|
||||
|
||||
const size_t key_index = current_hash_table->hash_function(pair->key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[key_index].buffer == NULL) {
|
||||
FledastyError result = fledasty_dynamic_array_initialize(current_hash_table->Table + key_index, NULL, 0, sizeof(FledastyHashTablePair));
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_append(current_hash_table->Table + key_index, pair);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_destroy(current_dynamic_array);
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_free(previous_table);
|
||||
}
|
||||
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[index].buffer == NULL) {
|
||||
FledastyError result = fledasty_dynamic_array_initialize(¤t_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair));
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
current_hash_table->size += 1;
|
||||
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;
|
||||
}
|
||||
|
||||
const 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) {
|
||||
const 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;
|
||||
}
|
||||
|
||||
const 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)) {
|
||||
if (hallocy_free(value->key) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(value->value) != 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) {
|
||||
FledastyError result = fledasty_dynamic_array_destroy(¤t_hash_table->Table[index]);
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_hash_table->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_hash_table_shrink_to_fit(FledastyHashTable *current_hash_table) {
|
||||
if (current_hash_table == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
const size_t old_capacity = current_hash_table->capacity;
|
||||
FledastyDynamicArray *previous_table = current_hash_table->Table;
|
||||
|
||||
if (current_hash_table->size == 0) {
|
||||
current_hash_table->capacity = 1024;
|
||||
} else {
|
||||
current_hash_table->capacity = (current_hash_table->size * 100) / FLEDASTY_HASH_TABLE_SIZE_THRESHOLD;
|
||||
}
|
||||
|
||||
current_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), current_hash_table->capacity);
|
||||
if (current_hash_table->Table == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < old_capacity; index += 1) {
|
||||
FledastyDynamicArray *current_dynamic_array = previous_table + index;
|
||||
if (current_dynamic_array->buffer != NULL) {
|
||||
for (size_t list_index = 0; list_index < current_dynamic_array->size; list_index += 1) {
|
||||
FledastyHashTablePair *pair = (FledastyHashTablePair*)fledasty_dynamic_array_get(current_dynamic_array, list_index);
|
||||
|
||||
const size_t key_index = current_hash_table->hash_function(pair->key) % current_hash_table->capacity;
|
||||
if (current_hash_table->Table[key_index].buffer == NULL) {
|
||||
FledastyError result = fledasty_dynamic_array_initialize(current_hash_table->Table + key_index, NULL, 0, sizeof(FledastyHashTablePair));
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_append(current_hash_table->Table + key_index, pair);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_destroy(current_dynamic_array);
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_free(previous_table);
|
||||
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;
|
||||
}
|
||||
|
||||
const 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) {
|
||||
const 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;
|
||||
}
|
||||
95
Tests/Main.c
95
Tests/Main.c
|
|
@ -30,13 +30,20 @@
|
|||
#include <Fledasty/Strings/UTF8String.h>
|
||||
#include <Fledasty/Algorithms/Hashing.h>
|
||||
|
||||
FLEDASTY_STACK_DEFINE(int)
|
||||
FLEDASTY_STACK_IMPLEMENT(int)
|
||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||
static inline size_t integer_hash_function(const int key) { return key; }
|
||||
|
||||
FLEDASTY_QUEUE_DEFINE(int)
|
||||
FLEDASTY_QUEUE_IMPLEMENT(int)
|
||||
FLEDASTY_STACK_DEFINE(int, int)
|
||||
FLEDASTY_STACK_IMPLEMENT(int, int)
|
||||
|
||||
static inline size_t integer_hash_function(const void *key) { return *(int*)key; }
|
||||
FLEDASTY_QUEUE_DEFINE(int, int)
|
||||
FLEDASTY_QUEUE_IMPLEMENT(int, int)
|
||||
|
||||
FLEDASTY_DYNAMIC_ARRAY_DEFINE(int, int)
|
||||
FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(int, int, compare_integers)
|
||||
|
||||
FLEDASTY_HASH_TABLE_DEFINE(int, int, int_int, compare_integers)
|
||||
FLEDASTY_HASH_TABLE_IMPLEMENT(int, int, int_int, compare_integers)
|
||||
|
||||
int main() {
|
||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||
|
|
@ -46,7 +53,7 @@ int main() {
|
|||
|
||||
fledasty_queue_int_shrink_to_fit(&test_queue);
|
||||
printf("Queue schrinked to fit %s\n", (test_queue.capacity == test_queue.size) ? "succeeded" : "failed");
|
||||
fledasty_queue_int_push(&test_queue, 0);
|
||||
fledasty_queue_int_push(&test_queue, 10);
|
||||
printf("Queue peeked: %d\n", fledasty_queue_int_peek(&test_queue));
|
||||
|
||||
for (int i = test_queue.size; i > 0; i -= 1) {
|
||||
|
|
@ -80,44 +87,35 @@ int main() {
|
|||
|
||||
fledasty_stack_int_free(&test_stack);
|
||||
|
||||
FledastyDynamicArray test_dynamic_array;
|
||||
fledasty_dynamic_array_initialize(&test_dynamic_array, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
||||
FledastyDynamicArray_int test_dynamic_array = { 0, 0, NULL };
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
fledasty_dynamic_array_append(&test_dynamic_array, &i);
|
||||
fledasty_dynamic_array_int_append(&test_dynamic_array, i);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_shrink_to_fit(&test_dynamic_array);
|
||||
fledasty_dynamic_array_int_shrink_to_fit(&test_dynamic_array);
|
||||
printf("Dynamic array schrinked to fit %s\n", (test_dynamic_array.capacity == test_dynamic_array.size) ? "succeeded" : "failed");
|
||||
|
||||
int insert_value = 18;
|
||||
fledasty_dynamic_array_insert_at_index(&test_dynamic_array, 1, &insert_value);
|
||||
fledasty_dynamic_array_int_insert_at_index(&test_dynamic_array, 1, 18);
|
||||
fledasty_dynamic_array_int_insert_before_value(&test_dynamic_array, 1, 35);
|
||||
fledasty_dynamic_array_int_insert_after_value(&test_dynamic_array, 35, 90);
|
||||
|
||||
insert_value = 35;
|
||||
int insert_at_value = 11;
|
||||
fledasty_dynamic_array_insert_before_value(&test_dynamic_array, &insert_at_value, &insert_value);
|
||||
insert_value = 90;
|
||||
insert_at_value = 35;
|
||||
fledasty_dynamic_array_insert_after_value(&test_dynamic_array, &insert_at_value, &insert_value);
|
||||
|
||||
int remove_value = 15;
|
||||
fledasty_dynamic_array_remove_value(&test_dynamic_array, &remove_value);
|
||||
fledasty_dynamic_array_remove_at_index(&test_dynamic_array, test_dynamic_array.size - 2);
|
||||
fledasty_dynamic_array_int_remove_value(&test_dynamic_array, 35);
|
||||
fledasty_dynamic_array_int_remove_at_index(&test_dynamic_array, test_dynamic_array.size - 2);
|
||||
|
||||
for (int i = 0; i < test_dynamic_array.size; i += 1) {
|
||||
int *dynamic_array_data = (int*)fledasty_dynamic_array_get(&test_dynamic_array, i);
|
||||
printf("Dynamic array get: %d\n", *dynamic_array_data);
|
||||
printf("Dynamic array get: %d\n", test_dynamic_array.buffer[i]);
|
||||
}
|
||||
|
||||
if (fledasty_dynamic_array_has_value(&test_dynamic_array, &insert_value)) {
|
||||
printf("Dynamic array contains %d\n", insert_value);
|
||||
if (fledasty_dynamic_array_int_has_value(&test_dynamic_array, 90)) {
|
||||
printf("Dynamic array contains 90\n");
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_clear(&test_dynamic_array);
|
||||
if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) {
|
||||
fledasty_dynamic_array_int_clear(&test_dynamic_array);
|
||||
if (fledasty_dynamic_array_int_is_empty(&test_dynamic_array)) {
|
||||
printf("Dynamic array is empty\n");
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_destroy(&test_dynamic_array);
|
||||
fledasty_dynamic_array_int_free(&test_dynamic_array);
|
||||
|
||||
FledastyLinkedList test_linked_list;
|
||||
fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
||||
|
|
@ -126,16 +124,16 @@ int main() {
|
|||
fledasty_linked_list_append(&test_linked_list, &i);
|
||||
}
|
||||
|
||||
insert_value = 35;
|
||||
int insert_value = 35;
|
||||
fledasty_linked_list_insert_at_index(&test_linked_list, 1, &insert_value);
|
||||
insert_value = 28;
|
||||
insert_at_value = 35;
|
||||
int insert_at_value = 35;
|
||||
fledasty_linked_list_insert_before_value(&test_linked_list, &insert_at_value, &insert_value);
|
||||
insert_value = 90;
|
||||
fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value);
|
||||
|
||||
fledasty_linked_list_remove_at_index(&test_linked_list, 2);
|
||||
remove_value = 0;
|
||||
int remove_value = 0;
|
||||
fledasty_linked_list_remove_value(&test_linked_list, &remove_value);
|
||||
|
||||
FledastyLinkedListNode *test_linked_list_node = test_linked_list.start;
|
||||
|
|
@ -196,42 +194,35 @@ int main() {
|
|||
|
||||
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);
|
||||
|
||||
FledastyHashTable_int_int test_hash_table = { .size = 0, .capacity = 0, .Table = NULL, .hash_function = (size_t(*)(const 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);
|
||||
fledasty_hash_table_int_int_insert(&test_hash_table, i * i, i);
|
||||
}
|
||||
|
||||
fledasty_hash_table_shrink_to_fit(&test_hash_table);
|
||||
fledasty_hash_table_int_int_shrink_to_fit(&test_hash_table);
|
||||
|
||||
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);
|
||||
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_get(&test_hash_table, &hash_table_remove) != NULL) {
|
||||
if (fledasty_hash_table_int_int_get(&test_hash_table, 2) != 0) {
|
||||
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);
|
||||
printf("Hash table get: %d\n", fledasty_hash_table_int_int_get(&test_hash_table, i * i));
|
||||
}
|
||||
|
||||
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);
|
||||
if (fledasty_hash_table_int_int_has_key(&test_hash_table, 4)) {
|
||||
printf("Hash table contains key 4\n");
|
||||
}
|
||||
|
||||
fledasty_hash_table_clear(&test_hash_table);
|
||||
if (fledasty_hash_table_is_empty(&test_hash_table)) {
|
||||
fledasty_hash_table_int_int_clear(&test_hash_table);
|
||||
if (fledasty_hash_table_int_int_is_empty(&test_hash_table)) {
|
||||
printf("Hash table is empty\n");
|
||||
}
|
||||
|
||||
fledasty_hash_table_destroy(&test_hash_table);
|
||||
fledasty_hash_table_int_int_free(&test_hash_table);
|
||||
|
||||
FledastyUtf8String test_utf8_string;
|
||||
unsigned char *test_string = (unsigned char*)"😀€Testing";
|
||||
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 14);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue