Merge pull request 'f17-improvement-consistency-performance' (#25) from f17-improvement-consistency-performance into main

Reviewed-on: #25
This commit is contained in:
Mineplay 2025-07-10 16:57:04 -05:00
commit 4bf374e3b1
18 changed files with 1675 additions and 1690 deletions

View file

@ -0,0 +1,36 @@
/*
* 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: Hashing.h
* Description:
* This file contains the functions for hashing common datastructures and types
* for hash tables.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef FLEDASTY_HASHING
#define FLEDASTY_HASHING
#include <stddef.h>
#include <stdint.h>
typedef struct {
uint64_t low, high;
} FledastyHash128;
uint32_t fledasty_mur_mur_3_hash_x32(const void *bytes, const size_t size, const uint32_t seed);
FledastyHash128 fledasty_mur_mur_3_hash_x64_128(const void *bytes, const size_t size, const uint32_t seed);
#endif

View file

@ -27,34 +27,328 @@
#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 FledastyDoublyLinkedListNode {
void *value;
struct FledastyDoublyLinkedListNode *previous, *next;
} FledastyDoublyLinkedListNode;
#define FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(type, name) \
typedef struct FledastyDoublyLinkedListNode_##name { \
type value; \
struct FledastyDoublyLinkedListNode_##name *previous, *next; \
} FledastyDoublyLinkedListNode_##name; \
\
typedef struct { \
size_t size; \
FledastyDoublyLinkedListNode_##name *start, *end; \
} FledastyDoublyLinkedList_##name; \
\
FledastyError fledasty_doubly_list_##name##_free(FledastyDoublyLinkedList_##name *current_doubly_linked_list); \
\
FledastyError fledasty_doubly_linked_list_##name##_append(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
\
FledastyError fledasty_doubly_linked_list_##name##_insert_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index, type value); \
FledastyError fledasty_doubly_linked_list_##name##_insert_before_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type before_value, type value); \
FledastyError fledasty_doubly_linked_list_##name##_insert_after_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type after_value, type value); \
\
FledastyError fledasty_doubly_linked_list_##name##_remove_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index); \
FledastyError fledasty_doubly_linked_list_##name##_remove_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
\
FledastyError fledasty_doubly_linked_list_##name##_clear(FledastyDoublyLinkedList_##name *current_doubly_linked_list); \
\
bool fledasty_doubly_linked_list_##name##_has_value(const FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value); \
static inline bool fledasty_doubly_linked_list_##name##_is_empty(const FledastyDoublyLinkedList_##name *current_doubly_linked_list) { return current_doubly_linked_list == NULL || current_doubly_linked_list->size == 0; }
typedef struct {
size_t size, element_byte_size;
FledastyDoublyLinkedListNode *start, *end;
} FledastyDoublyLinkedList;
#define FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(type, name, compare_function) \
FledastyError fledasty_doubly_list_##name##_free(FledastyDoublyLinkedList_##name *current_doubly_linked_list) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *previous_node = NULL; \
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
previous_node = current_node; \
current_node = current_node->next; \
\
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
} \
\
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_append(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
\
new_node->previous = current_doubly_linked_list->end; \
new_node->next = NULL; \
\
if (current_doubly_linked_list->start == NULL) { \
current_doubly_linked_list->start = new_node; \
} else { \
current_doubly_linked_list->end->next = new_node; \
} \
\
current_doubly_linked_list->end = new_node; \
current_doubly_linked_list->size += 1; \
\
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_insert_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index, type value) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (index > current_doubly_linked_list->size) { \
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
} \
\
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (index == 0) { \
new_node->previous = NULL; \
new_node->next = current_doubly_linked_list->start; \
\
current_doubly_linked_list->start->previous = new_node; \
current_doubly_linked_list->start = new_node; \
} else if (index == current_doubly_linked_list->size) { \
new_node->previous = current_doubly_linked_list->end; \
new_node->next = NULL; \
\
current_doubly_linked_list->end->next = new_node; \
current_doubly_linked_list->end = new_node; \
} else { \
FledastyDoublyLinkedListNode_##name *current_node = NULL; \
if (index < current_doubly_linked_list->size / 2) { \
current_node = current_doubly_linked_list->start; \
for (size_t node = 0; node < index - 1; node += 1) { \
current_node = current_node->next; \
} \
} else { \
current_node = current_doubly_linked_list->end; \
for (size_t node = current_doubly_linked_list->size - 1; node > index - 1; node -= 1) { \
current_node = current_node->previous; \
} \
} \
\
new_node->previous = current_node; \
new_node->next = current_node->next; \
\
current_node->next->previous = new_node; \
current_node->next = new_node; \
} \
\
current_doubly_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_insert_before_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type before_value, type value) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, before_value)) { \
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (current_node->previous == NULL) { \
new_node->previous = NULL; \
new_node->next = current_doubly_linked_list->start; \
\
current_doubly_linked_list->start->previous = new_node; \
current_doubly_linked_list->start = new_node; \
} else { \
new_node->previous = current_node->previous; \
new_node->next = current_node; \
\
current_node->previous->next = new_node; \
current_node->previous = new_node; \
} \
\
current_doubly_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_insert_after_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type after_value, type value) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, after_value)) { \
FledastyDoublyLinkedListNode_##name *new_node = (FledastyDoublyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (current_node == current_doubly_linked_list->end) { \
new_node->next = NULL; \
new_node->previous = current_doubly_linked_list->end; \
\
current_doubly_linked_list->end->next = new_node; \
current_doubly_linked_list->end = new_node; \
} else { \
new_node->previous = current_node; \
new_node->next = current_node->next; \
\
current_node->next->previous = new_node; \
current_node->next = new_node; \
} \
\
current_doubly_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_remove_at_index(FledastyDoublyLinkedList_##name *current_doubly_linked_list, const size_t index) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (index >= current_doubly_linked_list->size) { \
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
} \
\
FledastyDoublyLinkedListNode_##name *current_node = NULL; \
if (index < current_doubly_linked_list->size / 2) { \
current_node = current_doubly_linked_list->start; \
for (size_t node = 0; node < index; node += 1) { \
current_node = current_node->next; \
} \
} else { \
current_node = current_doubly_linked_list->end; \
for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) { \
current_node = current_node->previous; \
} \
} \
\
if (current_node == current_doubly_linked_list->start) { \
current_doubly_linked_list->start = current_node->next; \
} else { \
current_node->previous->next = current_node->next; \
} \
\
if (current_node == current_doubly_linked_list->end) { \
current_doubly_linked_list->end = current_node->previous; \
} else { \
current_node->next->previous = current_node->previous; \
} \
\
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_doubly_linked_list->size -= 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_remove_value(FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, value)) { \
if (current_node == current_doubly_linked_list->start) { \
current_doubly_linked_list->start = current_node->next; \
} else { \
current_node->previous->next = current_node->next; \
} \
\
if (current_node == current_doubly_linked_list->end) { \
current_doubly_linked_list->end = current_node->previous; \
} else { \
current_node->next->previous = current_node->previous; \
} \
\
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_doubly_linked_list->size -= 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_doubly_linked_list_##name##_clear(FledastyDoublyLinkedList_##name *current_doubly_linked_list) { \
if (current_doubly_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyDoublyLinkedListNode_##name *previous_node = NULL; \
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
previous_node = current_node; \
current_node = current_node->next; \
\
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
} \
\
current_doubly_linked_list->start = NULL; \
current_doubly_linked_list->end = NULL; \
\
current_doubly_linked_list->size = 0; \
return FLEDASTY_ERROR_NONE; \
} \
\
bool fledasty_doubly_linked_list_##name##_has_value(const FledastyDoublyLinkedList_##name *current_doubly_linked_list, type value) { \
if (current_doubly_linked_list == NULL) { \
return false; \
} \
\
FledastyDoublyLinkedListNode_##name *current_node = current_doubly_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, value)) { \
return true; \
} \
\
current_node = current_node->next; \
} \
\
return false; \
}
FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size);
FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_doubly_linked_list);
FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value);
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value);
FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value);
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value);
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 == NULL || current_doubly_linked_list->size == 0; }
#endif
#endif

View file

@ -26,33 +26,227 @@
#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;
static const size_t FLEDASTY_DYNAMIC_ARRAY_DEFAULT_ALLOCATION_SIZE = 16;
size_t element_byte_size;
unsigned char *buffer;
} FledastyDynamicArray;
#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; }
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);
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 : FLEDASTY_DYNAMIC_ARRAY_DEFAULT_ALLOCATION_SIZE; \
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 : FLEDASTY_DYNAMIC_ARRAY_DEFAULT_ALLOCATION_SIZE; \
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 : FLEDASTY_DYNAMIC_ARRAY_DEFAULT_ALLOCATION_SIZE; \
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 : FLEDASTY_DYNAMIC_ARRAY_DEFAULT_ALLOCATION_SIZE; \
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

View file

@ -26,33 +26,223 @@
#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;
static const int FLEDASTY_HASH_TABLE_TOTAL_SIZE_PERCENTAGE = 100;
static const int FLEDASTY_HASH_TABLE_THRESHOLD_SIZE_PERCENTAGE = 75;
static const size_t FLEDASTY_HASH_TABLE_DEFAULT_ALLOCATION_SIZE = 1024;
typedef struct {
size_t size, capacity;
#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); }
size_t key_byte_size, value_byte_size;
FledastyDynamicArray *Table;
#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) \
\
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(&current_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_THRESHOLD_SIZE_PERCENTAGE) / FLEDASTY_HASH_TABLE_TOTAL_SIZE_PERCENTAGE) { \
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 : FLEDASTY_HASH_TABLE_DEFAULT_ALLOCATION_SIZE; \
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(&current_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(&current_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(&current_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 = FLEDASTY_HASH_TABLE_DEFAULT_ALLOCATION_SIZE; \
} else { \
current_hash_table->capacity = (current_hash_table->size * FLEDASTY_HASH_TABLE_TOTAL_SIZE_PERCENTAGE) / FLEDASTY_HASH_TABLE_THRESHOLD_SIZE_PERCENTAGE; \
} \
\
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; \
}
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 == NULL || current_hash_table->size == 0; }
#endif
#endif

View file

@ -26,34 +26,294 @@
#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 FledastyLinkedListNode{
void *value;
struct FledastyLinkedListNode *next;
} FledastyLinkedListNode;
#define FLEDASTY_LINKED_LIST_DEFINE(type, name) \
typedef struct FledastyLinkedListNode_##name { \
type value; \
struct FledastyLinkedListNode_##name *next; \
} FledastyLinkedListNode_##name; \
\
typedef struct { \
size_t size; \
FledastyLinkedListNode_##name *start, *end; \
} FledastyLinkedList_##name; \
\
FledastyError fledasty_linked_list_##name##_free(FledastyLinkedList_##name *current_linked_list); \
\
FledastyError fledasty_linked_list_##name##_append(FledastyLinkedList_##name *current_linked_list, type value); \
\
FledastyError fledasty_linked_list_##name##_insert_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index, type value); \
FledastyError fledasty_linked_list_##name##_insert_before_value(FledastyLinkedList_##name *current_linked_list, type before_value, type value); \
FledastyError fledasty_linked_list_##name##_insert_after_value(FledastyLinkedList_##name *current_linked_list, type after_value, type value); \
\
FledastyError fledasty_linked_list_##name##_remove_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index); \
FledastyError fledasty_linked_list_##name##_remove_value(FledastyLinkedList_##name *current_linked_list, type value); \
\
FledastyError fledasty_linked_list_##name##_clear(FledastyLinkedList_##name *current_linked_list); \
\
bool fledasty_linked_list_##name##_has_value(const FledastyLinkedList_##name *current_linked_list, type value); \
static inline bool fledasty_linked_list_##name##_is_empty(const FledastyLinkedList_##name *current_linked_list) { return current_linked_list == NULL || current_linked_list->size == 0; }
typedef struct {
size_t size, element_byte_size;
FledastyLinkedListNode *start, *end;
} FledastyLinkedList;
FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size);
FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_list);
FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value);
FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_linked_list, const size_t index, void *value);
FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value);
FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value);
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 == NULL || current_linked_list->size == 0; }
#define FLEDASTY_LINKED_LIST_IMPLEMENT(type, name, compare_function) \
FledastyError fledasty_linked_list_##name##_free(FledastyLinkedList_##name *current_linked_list) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *previous_node = NULL; \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
previous_node = current_node; \
current_node = current_node->next; \
\
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
} \
\
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_linked_list_##name##_append(FledastyLinkedList_##name *current_linked_list, type value) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
new_node->next = NULL; \
\
if (current_linked_list->start == NULL) { \
current_linked_list->start = new_node; \
current_linked_list->end = new_node; \
} else { \
current_linked_list->end->next = new_node; \
current_linked_list->end = new_node; \
} \
\
current_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_linked_list_##name##_insert_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index, type value) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (index > current_linked_list->size) { \
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
} \
\
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (index == 0) { \
new_node->next = current_linked_list->start; \
current_linked_list->start = new_node; \
} else if (index == current_linked_list->size) { \
new_node->next = NULL; \
current_linked_list->end->next = new_node; \
current_linked_list->end = new_node; \
} else { \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
for (size_t node = 0; node < index - 1; node += 1) { \
current_node = current_node->next; \
} \
\
new_node->next = current_node->next; \
current_node->next = new_node; \
} \
\
current_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_linked_list_##name##_insert_before_value(FledastyLinkedList_##name *current_linked_list, type before_value, type value) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *previous_node = NULL; \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, before_value)) { \
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (previous_node == NULL) { \
new_node->next = current_linked_list->start; \
current_linked_list->start = new_node; \
} else { \
new_node->next = current_node; \
previous_node->next = new_node; \
} \
\
current_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
previous_node = current_node; \
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_linked_list_##name##_insert_after_value(FledastyLinkedList_##name *current_linked_list, type after_value, type value) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, after_value)) { \
FledastyLinkedListNode_##name *new_node = (FledastyLinkedListNode_##name*)hallocy_malloc(sizeof(FledastyLinkedListNode_##name)); \
if (new_node == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
new_node->value = value; \
if (current_node == current_linked_list->end) { \
new_node->next = NULL; \
\
current_linked_list->end->next = new_node; \
current_linked_list->end = new_node; \
} else { \
new_node->next = current_node->next; \
current_node->next = new_node; \
} \
\
current_linked_list->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_linked_list_##name##_remove_at_index(FledastyLinkedList_##name *current_linked_list, const size_t index) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (index >= current_linked_list->size) { \
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; \
} \
\
FledastyLinkedListNode_##name *previous_node = NULL; \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
for (size_t node = 0; node < index; node += 1) { \
previous_node = current_node; \
current_node = current_node->next; \
} \
\
if (current_node == current_linked_list->end) { \
current_linked_list->end = previous_node; \
} \
\
if (previous_node == NULL) { \
current_linked_list->start = current_node->next; \
} else { \
previous_node->next = current_node->next; \
} \
\
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_linked_list->size -= 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_linked_list_##name##_remove_value(FledastyLinkedList_##name *current_linked_list, type value) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *previous_node = NULL; \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, value)) { \
if (current_node == current_linked_list->end) { \
current_linked_list->end = previous_node; \
} \
\
if (previous_node == NULL) { \
current_linked_list->start = current_node->next; \
} else { \
previous_node->next = current_node->next; \
} \
\
if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_linked_list->size -= 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
previous_node = current_node; \
current_node = current_node->next; \
} \
\
return FLEDASTY_ERROR_VALUE_NOT_FOUND; \
} \
\
FledastyError fledasty_linked_list_##name##_clear(FledastyLinkedList_##name *current_linked_list) { \
if (current_linked_list == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
FledastyLinkedListNode_##name *previous_node = NULL; \
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
previous_node = current_node; \
current_node = current_node->next; \
\
if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
} \
\
current_linked_list->start = NULL; \
current_linked_list->end = NULL; \
\
current_linked_list->size = 0; \
return FLEDASTY_ERROR_NONE; \
} \
\
bool fledasty_linked_list_##name##_has_value(const FledastyLinkedList_##name *current_linked_list, type value) { \
if (current_linked_list == NULL) { \
return false; \
} \
\
FledastyLinkedListNode_##name *current_node = current_linked_list->start; \
while (current_node != NULL) { \
if (compare_function(current_node->value, value)) { \
return true; \
} \
\
current_node = current_node->next; \
} \
\
return false; \
}
#endif

View file

@ -25,26 +25,137 @@
#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;
size_t head, tail;
static const size_t FLEDASTY_QUEUE_DEFAULT_ALLOCATION_SIZE = 16;
size_t element_byte_size;
unsigned char *buffer;
} FledastyQueue;
#define FLEDASTY_QUEUE_DEFINE(type, name) \
typedef struct { \
size_t size, capacity; \
size_t head, tail; \
type *buffer; \
} FledastyQueue_##name; \
\
FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *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_##name##_clear(FledastyQueue_##name *current_queue); \
FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue); \
\
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; }
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, const size_t element_byte_size);
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue);
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 == NULL || current_queue->size == 0; }
#define FLEDASTY_QUEUE_IMPLEMENT(type, name) \
FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *current_queue) { \
if (current_queue == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (hallocy_free(current_queue->buffer) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_queue->size = 0; \
current_queue->capacity = 0; \
current_queue->buffer = NULL; \
\
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_queue_##name##_push(FledastyQueue_##name *current_queue, type value) { \
if (current_queue == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (current_queue->size == current_queue->capacity) { \
current_queue->capacity += current_queue->capacity ? current_queue->capacity : FLEDASTY_QUEUE_DEFAULT_ALLOCATION_SIZE; \
current_queue->buffer = (type*)hallocy_realloc(current_queue->buffer, current_queue->capacity * sizeof(type)); \
\
if (current_queue->buffer == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
if (current_queue->tail < current_queue->head) { \
size_t head_size = current_queue->size - current_queue->tail; \
\
hallocy_move_memory(current_queue->buffer, current_queue->buffer + current_queue->head, head_size * sizeof(type)); \
hallocy_move_memory(current_queue->buffer + head_size, current_queue->buffer, current_queue->tail * sizeof(type)); \
} else { \
hallocy_move_memory(current_queue->buffer, current_queue->buffer + current_queue->head, current_queue->size * sizeof(type)); \
} \
\
current_queue->head = 0; \
current_queue->tail = current_queue->size; \
} \
\
current_queue->buffer[current_queue->tail++] = value; \
if (current_queue->tail >= current_queue->capacity) { \
current_queue->tail = 0; \
} \
\
current_queue->size += 1; \
return FLEDASTY_ERROR_NONE; \
} \
\
type fledasty_queue_##name##_pop(FledastyQueue_##name *current_queue) { \
if (current_queue == NULL || current_queue->size == 0) { \
return 0; \
} \
\
current_queue->size -= 1; \
current_queue->head += 1; \
if (current_queue->head == current_queue->capacity) { \
current_queue->head = 0; \
} \
\
return current_queue->buffer[current_queue->head - 1]; \
} \
\
FledastyError fledasty_queue_##name##_clear(FledastyQueue_##name *current_queue) { \
if (current_queue == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
current_queue->size = 0; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue) { \
if (current_queue == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
size_t new_capacity = (current_queue->size == 0) ? FLEDASTY_QUEUE_DEFAULT_ALLOCATION_SIZE : current_queue->size; \
type *shrinked_buffer = (type*)hallocy_malloc(new_capacity * sizeof(type)); \
if (shrinked_buffer == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
if (current_queue->tail < current_queue->head) { \
size_t head_size = current_queue->size - current_queue->tail; \
\
hallocy_copy_memory(shrinked_buffer, current_queue->buffer + current_queue->head, head_size * sizeof(type)); \
hallocy_copy_memory(shrinked_buffer + head_size, current_queue->buffer, current_queue->tail * sizeof(type)); \
} else { \
hallocy_copy_memory(shrinked_buffer, current_queue->buffer + current_queue->head, current_queue->size * sizeof(type)); \
} \
\
if (hallocy_free(current_queue->buffer) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_queue->head = 0; \
current_queue->tail = current_queue->size; \
current_queue->capacity = new_capacity; \
current_queue->buffer = shrinked_buffer; \
\
return FLEDASTY_ERROR_NONE; \
}
#endif

View file

@ -25,25 +25,93 @@
#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;
static const size_t FLEDASTY_STACK_DEFAULT_ALLOCATION_SIZE = 16;
size_t element_byte_size;
unsigned char *buffer;
} FledastyStack;
#define FLEDASTY_STACK_DEFINE(type, name) \
typedef struct { \
size_t size, capacity; \
type *buffer; \
} FledastyStack_##name; \
\
FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack); \
\
FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value); \
\
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_##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; }
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, const size_t element_byte_size);
FledastyError fledasty_stack_destroy(FledastyStack *current_stack);
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 == NULL || current_stack->size == 0; }
#define FLEDASTY_STACK_IMPLEMENT(type, name) \
FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack) { \
if (current_stack == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_stack->size = 0; \
current_stack->capacity = 0; \
current_stack->buffer = NULL; \
\
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value) { \
if (current_stack == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
if (current_stack->size == current_stack->capacity) { \
current_stack->capacity += current_stack->capacity ? current_stack->capacity : FLEDASTY_STACK_DEFAULT_ALLOCATION_SIZE; \
current_stack->buffer = (type*)hallocy_realloc(current_stack->buffer, current_stack->capacity * sizeof(type)); \
\
if (current_stack->buffer == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
} \
\
current_stack->buffer[current_stack->size++] = value; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_stack_##name##_clear(FledastyStack_##name *current_stack) { \
if (current_stack == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
current_stack->size = 0; \
return FLEDASTY_ERROR_NONE; \
} \
\
FledastyError fledasty_stack_##name##_shrink_to_fit(FledastyStack_##name *current_stack) { \
if (current_stack == NULL) { \
return FLEDASTY_ERROR_INVALID_POINTER; \
} \
\
current_stack->capacity = (current_stack->size == 0) ? FLEDASTY_STACK_DEFAULT_ALLOCATION_SIZE : current_stack->size; \
type *shrinked_buffer = (type*)hallocy_malloc(current_stack->capacity * sizeof(type)); \
if (shrinked_buffer == NULL) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size * sizeof(type)); \
if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) { \
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
} \
\
current_stack->buffer = shrinked_buffer; \
return FLEDASTY_ERROR_NONE; \
}
#endif

View file

@ -23,6 +23,9 @@
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef FLEDASTY_UTF8_STRING
#define FLEDASTY_UTF8_STRING
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
@ -45,10 +48,12 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index);
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_string, unsigned char *replace_character_string, const size_t replace_character_string_size, unsigned char *character_string, const size_t character_string_size);
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string);
FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string);
bool fledasty_utf8_string_has_string(const FledastyUtf8String *current_string, unsigned char *character_string, const size_t character_string_size);
static inline bool fledasty_utf8_string_is_empty(const FledastyUtf8String *current_string) { return current_string == NULL || current_string->size == 0; }
@ -56,4 +61,6 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
uint32_t *fledasty_utf8_string_decode(const FledastyUtf8String *current_string, size_t *unicode_string_size);
bool fledasty_utf8_string_validate(unsigned char *character_string, const size_t character_string_size);
size_t fledasty_utf8_string_get_size(const unsigned char *character_string);
size_t fledasty_utf8_string_get_size(const unsigned char *character_string);
#endif

View file

@ -33,4 +33,4 @@ typedef enum {
FLEDASTY_ERROR_INVALID_VALUE = 6,
} FledastyError;
#endif
#endif

187
Src/Algorithms/Hashing.c Normal file
View file

@ -0,0 +1,187 @@
/*
* 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: Hashing.c
* Description:
* This file contains the functions for hashing common datastructures and types
* for hash tables.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Algorithms/Hashing.h"
#include <Hallocy/Core/Memory.h>
static inline uint32_t fledasty_rolt_32(const uint32_t key, const uint32_t rotation) { return (key << rotation) | (key >> (32 - rotation)); }
static inline uint64_t fledasty_rolt_64(uint64_t key, const uint64_t rotation) { return (key << rotation) | (key >> (64 - rotation)); }
uint32_t fledasty_mur_mur_3_hash_x32(const void *bytes, const size_t size, const uint32_t seed) {
unsigned char *hashing_bytes = (unsigned char*)bytes;
uint32_t hash = seed;
uint32_t key = 0;
for (size_t index = size >> 2; index; index -= 1) {
hallocy_copy_memory(&key, hashing_bytes, sizeof(uint32_t));
hashing_bytes += sizeof(uint32_t);
key *= 0xcc9e2d51;
key = fledasty_rolt_32(key, 15);
key *= 0x1b873593;
hash ^= key;
hash = fledasty_rolt_32(hash, 13);
hash = (hash * 5) + 0xe6546b64;
}
for (size_t index = size & 3; index; index -= 1) {
key = 0;
for (size_t i = (size & 3); i > 0; i -= 1) {
key ^= hashing_bytes[i] << (8 * (i & 3));
}
key *= 0xcc9e2d51;
key = fledasty_rolt_32(key, 13);
key *= 0x1b873593;
hash ^= key;
}
hash ^= size;
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
FledastyHash128 fledasty_mur_mur_3_hash_x64_128(const void *bytes, const size_t size, const uint32_t seed) {
unsigned char *hashing_bytes = (unsigned char *)bytes;
FledastyHash128 hash = { .low = seed, .high = seed };
size_t low_key = 0;
size_t high_key = 0;
for (size_t index = size >> 4; index; index -= 1) {
hallocy_copy_memory(&low_key, hashing_bytes, sizeof(uint64_t));
hashing_bytes += sizeof(uint64_t);
low_key *= 0x87c37b91114253d5ULL;
low_key = fledasty_rolt_64(low_key, 31);
low_key *= 0x4cf5ad432745937fULL;
hash.low ^= low_key;
hash.low = fledasty_rolt_64(hash.low, 27);
hash.low += hash.high;
hash.low = hash.low * 5 + 0x52dce729;
hallocy_copy_memory(&high_key, hashing_bytes, sizeof(uint64_t));
hashing_bytes += sizeof(uint64_t);
high_key *= 0x4cf5ad432745937fULL;
high_key = fledasty_rolt_64(high_key, 33);
high_key *= 0x87c37b91114253d5ULL;
hash.high ^= high_key;
hash.high = fledasty_rolt_64(hash.high, 31);
hash.high += hash.low;
hash.high = hash.high * 5 + 0x38495ab5;
}
low_key = 0;
high_key = 0;
switch(size & 15) {
case 15:
high_key ^= ((uint64_t)hashing_bytes[14]) << 48;
case 14:
high_key ^= ((uint64_t)hashing_bytes[13]) << 40;
case 13:
high_key ^= ((uint64_t)hashing_bytes[12]) << 32;
case 12:
high_key ^= ((uint64_t)hashing_bytes[11]) << 24;
case 11:
high_key ^= ((uint64_t)hashing_bytes[10]) << 16;
case 10:
high_key ^= ((uint64_t)hashing_bytes[9]) << 8;
case 9:
high_key ^= (uint64_t)hashing_bytes[8];
case 8:
low_key ^= ((uint64_t)hashing_bytes[7]) << 56;
case 7:
low_key ^= ((uint64_t)hashing_bytes[6]) << 48;
case 6:
low_key ^= ((uint64_t)hashing_bytes[5]) << 40;
case 5:
low_key ^= ((uint64_t)hashing_bytes[4]) << 32;
case 4:
low_key ^= ((uint64_t)hashing_bytes[3]) << 24;
case 3:
low_key ^= ((uint64_t)hashing_bytes[2]) << 16;
case 2:
low_key ^= ((uint64_t)hashing_bytes[1]) << 8;
case 1:
low_key ^= ((uint64_t)hashing_bytes[0]);
}
if (low_key != 0) {
low_key *= 0x87c37b91114253d5ULL;
low_key = fledasty_rolt_64(low_key, 31);
low_key *= 0x4cf5ad432745937fULL;
hash.low ^= low_key;
}
hash.low = fledasty_rolt_64(hash.low, 27);
hash.low += hash.high;
hash.low = hash.low * 5 + 0x52dce729;
if (high_key != 0) {
high_key *= 0x4cf5ad432745937fULL;
high_key = fledasty_rolt_64(high_key, 33);
high_key *= 0x87c37b91114253d5ULL;
hash.high ^= high_key;
}
hash.high = fledasty_rolt_64(hash.high, 31);
hash.high += hash.low;
hash.high = hash.high * 5 + 0x38495ab5;
hash.low ^= size;
hash.high ^= size;
hash.low += hash.high;
hash.high += hash.low;
hash.low ^= hash.low >> 33;
hash.low *= 0xff51afd7ed558ccdULL;
hash.low ^= hash.low >> 33;
hash.low *= 0xc4ceb9fe1a85ec53ULL;
hash.low ^= hash.low >> 33;
hash.high ^= hash.high >> 33;
hash.high *= 0xff51afd7ed558ccdULL;
hash.high ^= hash.high >> 33;
hash.high *= 0xc4ceb9fe1a85ec53ULL;
hash.high ^= hash.high >> 33;
hash.low += hash.high;
hash.high += hash.low;
return hash;
}

View file

@ -1,363 +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: DoublyLinkedList.c
* 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.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/DoublyLinkedList.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size) {
if (new_doubly_linked_list == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_doubly_linked_list->element_byte_size = element_byte_size;
if (values == NULL || values_size == 0) {
new_doubly_linked_list->size = 0;
new_doubly_linked_list->start = NULL;
new_doubly_linked_list->end = NULL;
} else {
new_doubly_linked_list->size = values_size;
new_doubly_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_doubly_linked_list->start->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_doubly_linked_list->start->value, values, new_doubly_linked_list->element_byte_size);
new_doubly_linked_list->start->previous = NULL;
new_doubly_linked_list->start->next = NULL;
FledastyDoublyLinkedListNode *last_node = new_doubly_linked_list->start;
for (size_t index = new_doubly_linked_list->element_byte_size; index < new_doubly_linked_list->size * new_doubly_linked_list->element_byte_size; index += new_doubly_linked_list->element_byte_size) {
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_node->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_doubly_linked_list->element_byte_size);
new_node->previous = last_node;
new_node->next = NULL;
last_node->next = new_node;
last_node = new_node;
}
new_doubly_linked_list->end = last_node;
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_list_destroy(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;
}
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
if (current_doubly_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
new_node->previous = current_doubly_linked_list->end;
new_node->next = NULL;
current_doubly_linked_list->end->next = new_node;
current_doubly_linked_list->end = new_node;
current_doubly_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value) {
if (current_doubly_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index > current_doubly_linked_list->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (index == 0) {
new_node->previous = NULL;
new_node->next = current_doubly_linked_list->start;
current_doubly_linked_list->start->previous = new_node;
current_doubly_linked_list->start = new_node;
} else if (index == current_doubly_linked_list->size) {
new_node->previous = current_doubly_linked_list->end;
new_node->next = NULL;
current_doubly_linked_list->end->next = new_node;
current_doubly_linked_list->end = new_node;
} else {
FledastyDoublyLinkedListNode *current_node = NULL;
if (index < current_doubly_linked_list->size / 2) {
current_node = current_doubly_linked_list->start;
for (size_t node = 0; node < index - 1; node += 1) {
current_node = current_node->next;
}
} else {
current_node = current_doubly_linked_list->end;
for (size_t node = current_doubly_linked_list->size - 1; node > index - 1; node -= 1) {
current_node = current_node->previous;
}
}
new_node->previous = current_node;
new_node->next = current_node->next;
current_node->next->previous = new_node;
current_node->next = new_node;
}
current_doubly_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value) {
if (current_doubly_linked_list == NULL || before_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_doubly_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (current_node->previous == NULL) {
new_node->previous = NULL;
new_node->next = current_doubly_linked_list->start;
current_doubly_linked_list->start->previous = new_node;
current_doubly_linked_list->start = new_node;
} else {
new_node->previous = current_node->previous;
new_node->next = current_node;
current_node->previous->next = new_node;
current_node->previous = new_node;
}
current_doubly_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value) {
if (current_doubly_linked_list == NULL || after_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_doubly_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (current_node == current_doubly_linked_list->end) {
new_node->next = NULL;
new_node->previous = current_doubly_linked_list->end;
current_doubly_linked_list->end->next = new_node;
current_doubly_linked_list->end = new_node;
} else {
new_node->previous = current_node;
new_node->next = current_node->next;
current_node->next->previous = new_node;
current_node->next = new_node;
}
current_doubly_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) {
if (current_doubly_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index >= current_doubly_linked_list->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
FledastyDoublyLinkedListNode *current_node = NULL;
if (index < current_doubly_linked_list->size / 2) {
current_node = current_doubly_linked_list->start;
for (size_t node = 0; node < index; node += 1) {
current_node = current_node->next;
}
} else {
current_node = current_doubly_linked_list->end;
for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) {
current_node = current_node->previous;
}
}
if (current_node->previous == NULL) {
current_doubly_linked_list->start = current_node->next;
current_doubly_linked_list->start->previous = NULL;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
}
} else {
current_node->previous->next = current_node->next;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
} else {
current_node->next->previous = current_node->previous;
}
}
hallocy_free(current_node->value);
hallocy_free(current_node);
current_doubly_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) {
if (current_doubly_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_node->previous == NULL) {
current_doubly_linked_list->start = current_node->next;
current_doubly_linked_list->start->previous = NULL;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
}
} else {
current_node->previous->next = current_node->next;
if (current_node->next == NULL) {
current_doubly_linked_list->end = current_node->previous;
} else {
current_node->next->previous = current_node->previous;
}
}
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;
}
FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start;
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) {
return true;
}
current_node = current_node->next;
}
return false;
}

View file

@ -1,245 +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 "Fledasty/Utils/Error.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;
}
HallocyError result = hallocy_free(current_dynamic_array->buffer);
if (result != 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);
}
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 && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, before_value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
}
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 && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, after_value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
}
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 && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size)) {
byte_index += current_dynamic_array->element_byte_size;
}
if (byte_index == byte_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
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;
}
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;
}
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;
}

View file

@ -1,205 +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, 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(&current_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(&current_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(&current_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(&current_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(&current_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(&current_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(&current_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(&current_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;
}

View file

@ -1,319 +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: LinkedList.c
* Description:
* This file contains the functions for modifying the 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.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/LinkedList.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size) {
if (new_linked_list == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_linked_list->element_byte_size = element_byte_size;
if (values == NULL || values_size == 0) {
new_linked_list->size = 0;
new_linked_list->start = NULL;
new_linked_list->end = NULL;
} else {
new_linked_list->size = values_size;
new_linked_list->start = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size);
hallocy_copy_memory(new_linked_list->start->value, values, new_linked_list->element_byte_size);
new_linked_list->start->next = NULL;
FledastyLinkedListNode *last_node = new_linked_list->start;
for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_node->value = hallocy_malloc(new_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_linked_list->element_byte_size);
new_node->next = NULL;
last_node->next = new_node;
last_node = new_node;
}
new_linked_list->end = last_node;
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_destroy(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;
}
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value) {
if (current_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size);
new_node->next = NULL;
current_linked_list->end->next = new_node;
current_linked_list->end = new_node;
current_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_linked_list, const size_t index, void *value) {
if (current_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index > current_linked_list->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size);
if (index == 0) {
new_node->next = current_linked_list->start;
current_linked_list->start = new_node;
} else if (index == current_linked_list->size) {
new_node->next = NULL;
current_linked_list->end->next = new_node;
current_linked_list->end = new_node;
} else {
FledastyLinkedListNode *current_node = current_linked_list->start;
for (size_t node = 0; node < index - 1; node += 1) {
current_node = current_node->next;
}
new_node->next = current_node->next;
current_node->next = new_node;
}
current_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value) {
if (current_linked_list == NULL || before_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, before_value, current_linked_list->element_byte_size)) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size);
if (previous_node == NULL) {
new_node->next = current_linked_list->start;
current_linked_list->start = new_node;
} else {
new_node->next = current_node;
previous_node->next = new_node;
}
current_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value) {
if (current_linked_list == NULL || after_value == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, after_value, current_linked_list->element_byte_size)) {
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
new_node->value = hallocy_malloc(current_linked_list->element_byte_size);
hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size);
if (current_node == current_linked_list->end) {
new_node->next = NULL;
current_linked_list->end->next = new_node;
current_linked_list->end = new_node;
} else {
new_node->next = current_node->next;
current_node->next = new_node;
}
current_linked_list->size += 1;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index) {
if (current_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index >= current_linked_list->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
for (size_t node = 0; node < index; node += 1) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node->next == NULL) {
current_linked_list->end = previous_node;
}
if (previous_node == NULL) {
current_linked_list->start = current_node->next;
} else {
previous_node->next = 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_remove_value(FledastyLinkedList *current_linked_list, void *value) {
if (current_linked_list == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_node->next == NULL) {
current_linked_list->end = previous_node;
}
if (previous_node == NULL) {
current_linked_list->start = current_node->next;
} else {
previous_node->next = 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;
}
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL) {
if (hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) {
return true;
}
current_node = current_node->next;
}
return false;
}

View file

@ -1,136 +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: Queue.c
* Description:
* This file contains the functions for modifying the queue. It includes functions
* to Push, Pop, Peek and check if empty.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/Queue.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, const size_t element_byte_size) {
if (new_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
new_queue->head = 0;
new_queue->element_byte_size = element_byte_size;
if (values == NULL || values_size == 0) {
new_queue->size = 0;
new_queue->capacity = 10;
new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
if (new_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
} else {
new_queue->size = values_size;
new_queue->capacity = new_queue->size + new_queue->size;
new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
if (new_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_queue->buffer, values, values_size * element_byte_size);
}
new_queue->tail = new_queue->size;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_queue->buffer);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_queue->buffer = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
if (current_queue == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_queue->size == current_queue->capacity) {
current_queue->capacity += current_queue->capacity;
current_queue->buffer = (unsigned char*)hallocy_realloc(current_queue->buffer, current_queue->capacity * current_queue->element_byte_size);
if (current_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
size_t head_length = current_queue->size - current_queue->head;
size_t new_head = current_queue->capacity - head_length;
hallocy_move_memory(current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_length * current_queue->element_byte_size);
current_queue->head = new_head;
}
hallocy_copy_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), value, current_queue->element_byte_size);
current_queue->size += 1;
current_queue->tail += 1;
if (current_queue->tail >= current_queue->capacity) {
current_queue->tail = 0;
}
return FLEDASTY_ERROR_NONE;
}
void *fledasty_queue_peek(const FledastyQueue *current_queue) {
if (current_queue == NULL) {
return NULL;
}
return current_queue->buffer + (current_queue->head * current_queue->element_byte_size);
}
void *fledasty_queue_pop(FledastyQueue *current_queue) {
if (current_queue == NULL || current_queue->size == 0) {
return NULL;
}
void *value_address = current_queue->buffer + (current_queue->head * current_queue->element_byte_size);
current_queue->size -= 1;
current_queue->head += 1;
if (current_queue->head == current_queue->capacity) {
current_queue->head = 0;
}
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

@ -1,118 +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: Stack.c
* Description:
* This file contains the functions for modifying the stack. It includes functions
* to Push, Pop, Peek and check if empty.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/Stack.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, const size_t element_byte_size) {
if (new_stack == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
new_stack->element_byte_size = element_byte_size;
if (values == NULL || values_size == 0) {
new_stack->size = 0;
new_stack->capacity = 10;
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
if (new_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
} else {
new_stack->size = values_size;
new_stack->capacity = new_stack->size + new_stack->size;
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
if (new_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_stack->buffer, values, values_size * element_byte_size);
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_stack_destroy(FledastyStack *current_stack) {
if (current_stack == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_stack->buffer);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_stack->buffer = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
if (current_stack == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_stack->size == current_stack->capacity) {
current_stack->capacity += current_stack->capacity;
current_stack->buffer = (unsigned char*)hallocy_realloc(current_stack->buffer, current_stack->capacity);
if (current_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_copy_memory(current_stack->buffer + (current_stack->size * current_stack->element_byte_size), value, current_stack->element_byte_size);
current_stack->size += 1;
return FLEDASTY_ERROR_NONE;
}
void *fledasty_stack_peek(const FledastyStack *current_stack) {
if (current_stack == NULL) {
return NULL;
}
return current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
}
void *fledasty_stack_pop(FledastyStack *current_stack) {
if (current_stack == NULL) {
return NULL;
}
void *value_address = current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
current_stack->size -= 1;
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

@ -61,8 +61,7 @@ FledastyError fledasty_utf8_string_destroy(FledastyUtf8String *current_string) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_string->character_string);
if (result != HALLOCY_ERROR_NONE) {
if (hallocy_free(current_string->character_string) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -80,11 +79,11 @@ FledastyError fledasty_utf8_string_append(FledastyUtf8String *current_string, un
}
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_copy_memory(current_string->character_string + (current_string->size - 1), character_string, character_string_size);
hallocy_copy_memory(current_string->character_string + current_string->size, character_string, character_string_size);
current_string->size += character_string_size;
current_string->character_string[current_string->size] = '\0';
@ -97,7 +96,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (index >= current_string->size) {
if (index > current_string->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
@ -114,6 +113,7 @@ FledastyError fledasty_utf8_string_insert_at_index(FledastyUtf8String *current_s
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -128,23 +128,23 @@ FledastyError fledasty_utf8_string_insert_before_string(FledastyUtf8String *curr
size_t index = 0;
while (index < (current_string->size - before_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, before_character_string, before_character_string_size)) {
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - before_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *current_string, unsigned char *after_character_string, const size_t after_character_string_size, unsigned char *character_string, const size_t character_string_size) {
@ -157,25 +157,25 @@ FledastyError fledasty_utf8_string_insert_after_string(FledastyUtf8String *curre
}
size_t index = 0;
while (index < (current_string->size - after_character_string_size) && !hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) {
while (index < (current_string->size - after_character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, after_character_string, after_character_string_size)) {
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size + 1;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
index += after_character_string_size;
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index + 1);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - after_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
if (current_string->capacity <= current_string->size + character_string_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
index += after_character_string_size;
hallocy_move_memory(current_string->character_string + (index + character_string_size), current_string->character_string + index, current_string->size - index);
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size += character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) {
@ -193,7 +193,7 @@ FledastyError fledasty_utf8_string_pop(FledastyUtf8String *current_string) {
current_string->size -= 1;
}
current_string->character_string[current_string->size - 1] = '\0';
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -208,18 +208,19 @@ FledastyError fledasty_utf8_string_remove(FledastyUtf8String *current_string, un
}
size_t index = 0;
while (index < (current_string->size - character_string_size) && !hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
while (index < (current_string->size - character_string_size)) {
if (hallocy_compare_memory(current_string->character_string + index, character_string, character_string_size)) {
hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size));
current_string->size -= character_string_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
hallocy_move_memory(current_string->character_string + index, current_string->character_string + index + character_string_size, current_string->size - (index + character_string_size));
current_string->size -= character_string_size;
return FLEDASTY_ERROR_NONE;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_string, const size_t start_index, const size_t end_index) {
@ -227,24 +228,14 @@ FledastyError fledasty_utf8_string_remove_range(FledastyUtf8String *current_stri
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (start_index > end_index || end_index > current_string->size) {
if (start_index >= end_index || end_index >= current_string->size) {
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
hallocy_move_memory(current_string->character_string + start_index, current_string->character_string + end_index, current_string->size - end_index);
current_string->size -= end_index - start_index;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
current_string->size = 0;
current_string->character_string[0] = '\0';
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
@ -258,18 +249,51 @@ FledastyError fledasty_utf8_string_replace_string(FledastyUtf8String *current_st
}
size_t index = 0;
while (index < current_string->size - replace_character_string_size && !hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) {
while (index < current_string->size - replace_character_string_size) {
if (hallocy_compare_memory(current_string->character_string + index, replace_character_string, replace_character_string_size)) {
const size_t new_size = current_string->size + (character_string_size - replace_character_string_size);
if (current_string->capacity <= new_size) {
current_string->capacity += (current_string->capacity > character_string_size) ? current_string->capacity : character_string_size;
current_string->character_string = (unsigned char*)hallocy_realloc(current_string->character_string, current_string->capacity * sizeof(unsigned char));
}
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size = new_size;
current_string->character_string[current_string->size] = '\0';
return FLEDASTY_ERROR_NONE;
}
index += 1;
}
if (index == current_string->size - replace_character_string_size) {
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
}
FledastyError fledasty_utf8_string_clear(FledastyUtf8String *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
hallocy_move_memory(current_string->character_string + index + character_string_size, current_string->character_string + index + replace_character_string_size, current_string->size - (index + replace_character_string_size));
hallocy_copy_memory(current_string->character_string + index, character_string, character_string_size);
current_string->size = 0;
current_string->character_string[0] = '\0';
current_string->size += character_string_size - replace_character_string_size;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_utf8_string_shrink_to_fit(FledastyUtf8String *current_string) {
if (current_string == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
unsigned char *previous_string = current_string->character_string;
current_string->capacity = current_string->size + 1;
current_string->character_string = (unsigned char*)hallocy_malloc(current_string->capacity);
hallocy_copy_memory(current_string->character_string, previous_string, current_string->size);
current_string->character_string[current_string->size] = '\0';
hallocy_free(previous_string);
return FLEDASTY_ERROR_NONE;
}
@ -346,7 +370,7 @@ FledastyUtf8String fledasty_utf8_string_encode(const uint32_t *unicode, const si
}
utf8_string.size = string_index;
if (utf8_string.capacity <= utf8_string.size + 1) {
if (utf8_string.capacity <= utf8_string.size) {
utf8_string.capacity += utf8_string.capacity;
utf8_string.character_string = (unsigned char*)hallocy_realloc(utf8_string.character_string, utf8_string.capacity);
}

View file

@ -28,204 +28,197 @@
#include <Fledasty/Core/DoublyLinkedList.h>
#include <Fledasty/Core/HashTable.h>
#include <Fledasty/Strings/UTF8String.h>
#include <Fledasty/Algorithms/Hashing.h>
static inline size_t integer_hash_function(void *key) { return *(size_t*)key; }
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_STACK_DEFINE(int, int)
FLEDASTY_STACK_IMPLEMENT(int, int)
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)
FLEDASTY_LINKED_LIST_DEFINE(int, int)
FLEDASTY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int)
FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers)
int main() {
FledastyQueue test_queue;
fledasty_queue_initialize(&test_queue, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
for (int i = 0; i < 10; i += 1) {
fledasty_queue_push(&test_queue, &i);
fledasty_queue_int_push(&test_queue, i);
}
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
printf("Queue peeked: %d\n", *peeked_queue_data);
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, 10);
printf("Queue peeked: %d\n", fledasty_queue_int_peek(&test_queue));
for (int i = test_queue.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
printf("Queue popped: %d\n", *popped_data);
printf("Queue popped: %d\n", fledasty_queue_int_pop(&test_queue));
}
fledasty_queue_clear(&test_queue);
if (fledasty_queue_is_empty(&test_queue)) {
fledasty_queue_int_clear(&test_queue);
if (fledasty_queue_int_is_empty(&test_queue)) {
printf("Queue is empty\n");
}
fledasty_queue_destroy(&test_queue);
FledastyStack test_stack;
fledasty_stack_initialize(&test_stack, (int[]){ 11, 12, 13, 14, 15 }, 5, sizeof(int));
fledasty_queue_int_free(&test_queue);
FledastyStack_int test_stack = { 0, 0, NULL };
for (int i = 0; i < 10; i += 1) {
fledasty_stack_push(&test_stack, &i);
fledasty_stack_int_push(&test_stack, i);
}
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
printf("Stack peeked: %d\n", *peeked_stack_data);
fledasty_stack_int_shrink_to_fit(&test_stack);
printf("Stack schrinked to fit %s\n", (test_stack.capacity == test_stack.size) ? "succeeded" : "failed");
printf("Stack peeked: %d\n", fledasty_stack_int_peek(&test_stack));
for (int i = test_stack.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_stack_pop(&test_stack);
printf("Stack popped: %d\n", *popped_data);
printf("Stack popped: %d\n", fledasty_stack_int_pop(&test_stack));
}
fledasty_stack_clear(&test_stack);
if (fledasty_stack_is_empty(&test_stack)) {
fledasty_stack_int_clear(&test_stack);
if (fledasty_stack_int_is_empty(&test_stack)) {
printf("Stack is empty\n");
}
fledasty_stack_destroy(&test_stack);
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);
}
int insert_value = 18;
fledasty_dynamic_array_insert_at_index(&test_dynamic_array, 1, &insert_value);
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");
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);
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);
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);
FledastyLinkedList test_linked_list;
fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
fledasty_dynamic_array_int_free(&test_dynamic_array);
FledastyLinkedList_int test_linked_list = { 0, NULL, NULL };
for (int i = 0; i < 10; i += 1) {
fledasty_linked_list_append(&test_linked_list, &i);
fledasty_linked_list_int_append(&test_linked_list, i);
}
insert_value = 35;
fledasty_linked_list_insert_at_index(&test_linked_list, 1, &insert_value);
insert_value = 28;
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_int_insert_at_index(&test_linked_list, 1, 35);
fledasty_linked_list_int_insert_before_value(&test_linked_list, 35, 28);
fledasty_linked_list_int_insert_after_value(&test_linked_list, 35, 90);
fledasty_linked_list_remove_at_index(&test_linked_list, 2);
remove_value = 0;
fledasty_linked_list_remove_value(&test_linked_list, &remove_value);
fledasty_linked_list_int_remove_at_index(&test_linked_list, 2);
fledasty_linked_list_int_remove_value(&test_linked_list, 0);
FledastyLinkedListNode *test_linked_list_node = test_linked_list.start;
FledastyLinkedListNode_int *test_linked_list_node = test_linked_list.start;
for (int i = 0; i < test_linked_list.size; i += 1) {
printf("Linked list get: %d\n", *(int*)test_linked_list_node->value);
printf("Linked list get: %d\n", test_linked_list_node->value);
test_linked_list_node = test_linked_list_node->next;
}
if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) {
printf("Linked list contains %d\n", insert_value);
if (fledasty_linked_list_int_has_value(&test_linked_list, 90)) {
printf("Linked list contains 90\n");
}
fledasty_linked_list_clear(&test_linked_list);
if (fledasty_linked_list_is_empty(&test_linked_list)) {
fledasty_linked_list_int_clear(&test_linked_list);
if (fledasty_linked_list_int_is_empty(&test_linked_list)) {
printf("Linked list is empty\n");
}
fledasty_linked_list_destroy(&test_linked_list);
fledasty_linked_list_int_free(&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));
FledastyDoublyLinkedList_int test_doubly_linked_list = { 0, NULL , NULL };
for (int i = 0; i < 10; i += 1) {
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
fledasty_doubly_linked_list_int_append(&test_doubly_linked_list, i);
}
insert_value = 35;
fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &insert_value);
insert_value = 28;
insert_at_value = 35;
fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
insert_value = 90;
fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
fledasty_doubly_linked_list_int_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, 35);
fledasty_doubly_linked_list_int_insert_before_value(&test_doubly_linked_list, 35, 28);
fledasty_doubly_linked_list_int_insert_after_value(&test_doubly_linked_list, 35, 90);
fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2);
remove_value = 0;
fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value);
fledasty_doubly_linked_list_int_remove_at_index(&test_doubly_linked_list, 2);
fledasty_doubly_linked_list_int_remove_value(&test_doubly_linked_list, 0);
FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start;
FledastyDoublyLinkedListNode_int *test_doubly_linked_list_node = test_doubly_linked_list.start;
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
printf("Doubly linked list get: %d\n", *(int*)test_doubly_linked_list_node->value);
printf("Doubly linked list get: %d\n", test_doubly_linked_list_node->value);
test_doubly_linked_list_node = test_doubly_linked_list_node->next;
}
test_doubly_linked_list_node = test_doubly_linked_list.end;
for (int i = 0; i < test_doubly_linked_list.size; i += 1) {
printf("Doubly linked list get backwards: %d\n", *(int*)test_doubly_linked_list_node->value);
printf("Doubly linked list get backwards: %d\n", test_doubly_linked_list_node->value);
test_doubly_linked_list_node = test_doubly_linked_list_node->previous;
}
if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) {
printf("Doubly linked list contains %d\n", insert_value);
if (fledasty_doubly_linked_list_int_has_value(&test_doubly_linked_list, 35)) {
printf("Doubly linked list contains 35\n");
}
fledasty_doubly_linked_list_clear(&test_doubly_linked_list);
if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) {
fledasty_doubly_linked_list_int_clear(&test_doubly_linked_list);
if (fledasty_doubly_linked_list_int_is_empty(&test_doubly_linked_list)) {
printf("Doubly linked list is empty\n");
}
fledasty_doubly_list_destroy(&test_doubly_linked_list);
fledasty_doubly_list_int_free(&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);
}
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_shrink_to_fit(&test_hash_table);
if (fledasty_hash_table_get(&test_hash_table, &hash_table_remove) != NULL) {
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) {
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, 15);
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 14);
printf("%s\n", test_string);
printf("%s\n", test_utf8_string.character_string);
@ -235,7 +228,7 @@ int main() {
printf("Insert Before: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_after_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"Bye", 3);
printf("Insert After: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size - 1, (unsigned char*)"index", 5);
fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size, (unsigned char*)"index", 5);
printf("Insert at Index: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_replace_string(&test_utf8_string, (unsigned char*)"😀", 4, (unsigned char*)"𓃶 ", 5);
printf("Replace: %s\n", test_utf8_string.character_string);
@ -243,13 +236,20 @@ int main() {
printf("Pop: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_remove(&test_utf8_string, (unsigned char*)"𓃶 ", 5);
printf("Remove: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 5);
fledasty_utf8_string_remove_range(&test_utf8_string, 0, 6);
printf("Remove range: %s\n", test_utf8_string.character_string);
fledasty_utf8_string_shrink_to_fit(&test_utf8_string);
if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"😀", 4)) {
printf("String contains 😀!\n");
}
uint32_t hash_x32 = fledasty_mur_mur_3_hash_x32(test_utf8_string.character_string, test_utf8_string.size, 0);
printf("UTF-8 String hash using murmur 32 is: %u\n", hash_x32);
FledastyHash128 hash_x64 = fledasty_mur_mur_3_hash_x64_128(test_utf8_string.character_string, test_utf8_string.size, 0);
printf("UTF-8 String hash using murmur 64 is: low = %lu, high = %lu\n", hash_x64.low, hash_x64.high);
size_t unicode_length = 0;
uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length);
FledastyUtf8String encoded_string = fledasty_utf8_string_encode(unicode, unicode_length);