api largely finished, polish and comments still needed.

This commit is contained in:
2026-05-17 02:38:56 -05:00
parent 11c2ad914c
commit 2440359748
166 changed files with 17690 additions and 16 deletions

View File

@@ -1,27 +1,32 @@
#ifndef DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_H
#ifndef MARIGOLD_DYNAMIC_ARRAY_H
#define MARIGOLD_DYNAMIC_ARRAY_H
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct dynamic_array
{
size_t item_size;
size_t starting_capacity;
size_t current_capacity;
size_t current_size;
void* memory_block_pointer;
unsigned int starting_capacity;
unsigned int current_capacity;
unsigned int current_size;
int (*comparator)(const void*, const void*);
pthread_mutex_t* mutex_lock;
unsigned short owner_count;
unsigned short growth_steps;
unsigned char growth_factor;
bool is_multithread_safe;
bool is_read_only;
bool is_certainly_sorted;
} dynamic_array;
/**
* @brief Create and initialize a new dynamic array.
* @brief Create a new dynamic array, it then internally does all the initalization.
*
* Creates a dynamic array with the specified item size and starting capacity.
* The growth_factor determines how the array expands when capacity is exceeded.
@@ -36,8 +41,18 @@ typedef struct dynamic_array
dynamic_array* dynamic_array_create(size_t item_size,
unsigned int starting_capacity,
unsigned char growth_factor,
int (*comparator)(const void*, const void*),
bool is_multithread_safe);
/**
* @brief This function checks to make sure the owner count 1 or 0
* then deallocates what needs to be deallocated, before destorying it.
*
* @param array_to_destroy is the dynamic array being deallocated/destroyed.
* @return true on successful destory, false on failure.
*/
bool dynamic_array_destroy(dynamic_array* array_to_destroy);
/**
* @brief Get the number of elements currently in the array.
*
@@ -61,8 +76,8 @@ unsigned int dynamic_array_get_capacity(const dynamic_array* array);
* @param index Index of the element to access.
* @return Pointer to the element, or NULL if index is out of bounds.
*/
void* dynamic_array_get_pointer_to_index(dynamic_array* array,
unsigned int index);
void* dynamic_array_get_pointer_to_index(const dynamic_array* array,
const unsigned int index);
/**
* @brief Get a const pointer to the element at the specified index.
@@ -72,7 +87,7 @@ void* dynamic_array_get_pointer_to_index(dynamic_array* array,
* @return Const pointer to the element, or NULL if index is out of bounds.
*/
const void* dynamic_array_get_const_pointer_to_index(const dynamic_array* array,
unsigned int index);
const unsigned int index);
/**
* @brief Append an element to the end of the array.
@@ -106,7 +121,7 @@ bool dynamic_array_pop(dynamic_array* array);
* @return true on success, false if index is out of bounds.
*/
bool dynamic_array_remove(dynamic_array* array,
unsigned int index);
const unsigned int index);
/**
* @brief Clear all elements from the array.
@@ -133,7 +148,7 @@ bool dynamic_array_is_empty(const dynamic_array* array);
* @return true on success, false on allocation failure.
*/
bool dynamic_array_increase_capacity(dynamic_array* array,
unsigned int new_capacity);
const unsigned int new_capacity);
/**
* @brief Increment the owner count for shared ownership.
@@ -172,9 +187,62 @@ bool dynamic_array_is_thread_safe(const dynamic_array* array);
/**
* @brief Creates a deep copy of the array.
*
* @param array Pointer to the copy being copied, and one to copy it onto.
* @return true if creation is successful. False if it fails for any reason.
* @param array_to_copy Pointer to the array being copied.
*
* @return returns the new deep copy as a pointer.
*/
bool dynamic_array_clone(const dynamic_array* original_array, dynamic_array* new_array);
dynamic_array* dynamic_array_clone(const dynamic_array* array_to_copy);
#endif /* DYNAMIC_ARRAY_H */
/**
* @brief Creates a deep copy of a slice of an array
*
* @param array_to_copy_and_slice Pointer to the array being copied/sliced.
* @param starting_point Index to start the slice at.
* @param ending_point Index to end the slice at.
* @param is_start_inclusive Boolean indicator of if the starting index should be
* included in the slice.
* @param is_end_inclusive Boolean indicator of if the ending index should be
* included in the slice.
*
* @return returns the new deep copy of the array as a pointer to a new array.
*/
dynamic_array* dynamic_array_clone_slice(const dynamic_array* array_to_copy_and_slice,
const size_t starting_point,
const size_t ending_point,
const bool is_start_inclusive,
const bool is_end_inclusive);
/**
* @brief Modifies the list so that it is sorted, and can invert the sorting function.
*
* @param array_to_sort Pointer to the array to be sorted.
* @param should_reverse_sort If true, inverts the order of the array.
*/
void dynamic_array_sort_in_place(dynamic_array* array_to_sort,
const bool should_reverse_sort);
void dynamic_array_sort_slice(dynamic_array* array_to_sort,
const size_t starting_point,
const size_t ending_point,
const bool is_start_inclusive,
const bool is_end_inclusive,
const bool should_reverse_sort);
dynamic_array* dynamic_array_get_sorted_copy(dynamic_array* array_to_sort,
const bool should_reverse_sort);
void dynamic_array_reverse(dynamic_array* array_to_reverse);
bool dynamic_array_contains(dynamic_array* array_to_search,
void* pointer_of_value_to_find);
size_t dynamic_array_find_first_index_of(dynamic_array* array_to_search,
void* pointer_of_value_to_find);
size_t dynamic_array_find_final_index_of(dynamic_array* array_to_search,
void* pointer_of_value_to_find);
void dynamic_array_set_comparator_function(dynamic_array* array,
int (*comparator)(const void*, const void*));
#endif /* MARIGOLD_DYNAMIC_ARRAY_H */