update stuff

This commit is contained in:
Maix0 2024-05-20 00:35:39 +02:00
parent 5973022688
commit 544ed8b045
194 changed files with 2060 additions and 1464 deletions

View file

@ -25,16 +25,25 @@ C__TYPEHEADER__
#include <stdlib.h>
#include <string.h>
/// @struct A key-value pair for the hashmap
typedef struct s_kv_C__PREFIX__
{
C__KEYTYPE__ key;
C__VALTYPE__ val;
} t_kv_C__PREFIX__;
/// @typedef A function that hashes a key
typedef void (*t_hash_C__PREFIX___fn)(t_hasher *hasher, C__KEYTYPE__ *key);
/// @typedef A function that drops a key-value pair
typedef void (*t_drop_C__PREFIX___fn)(t_kv_C__PREFIX__ val);
/// @typedef A function that compares two keys and returns true if they are equal
typedef bool (*t_eq_C__PREFIX___fn)(C__KEYTYPE__ *lhs, C__KEYTYPE__ *rhs);
/// @struct A single entry in the hashmap
/// @var hash_id The hash of the key
/// @var kv The key-value pair
/// @var next The next entry in the bucket
typedef struct s_entry_C__PREFIX__
{
t_usize hash_id;
@ -42,6 +51,13 @@ typedef struct s_entry_C__PREFIX__
struct s_entry_C__PREFIX__ *next;
} t_entry_C__PREFIX__;
/// @struct A hashmap of keys C__KEYTYPE__ and values C__VALTYPE__
/// @var buckets The array of buckets
/// @var num_buckets The number of buckets
/// @var hasher The hasher function
/// @var hfunc The hash function
/// @var cfunc The comparison function
/// @var drop The drop function
typedef struct s_hashmap_C__PREFIX__
{
t_entry_C__PREFIX__ **buckets;
@ -52,31 +68,56 @@ typedef struct s_hashmap_C__PREFIX__
t_drop_C__PREFIX___fn drop;
} t_hashmap_C__PREFIX__;
t_hashmap_C__PREFIX__ *new_hashmap_C__PREFIX__(t_hash_C__PREFIX___fn hash,
t_eq_C__PREFIX___fn cmp,
t_drop_C__PREFIX___fn drop);
t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp,
t_drop_C__PREFIX___fn drop, size_t cap);
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
/// @param hash The hash function
/// @param cmp The comparison function
/// @param drop The drop function
/// @return A new hashmap
t_hashmap_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop);
void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap);
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
/// @param hash The hash function
/// @param cmp The comparison function
/// @param drop The drop function
/// @param cap The number of buckets
/// @return A new hashmap
t_hashmap_C__PREFIX__ *hmap_new_with_buckets_C__PREFIX__(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop, size_t cap);
void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
C__VALTYPE__ value);
/// @brief Drops the hashmap and all of its entries
/// @param hmap The hashmap to drop
void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap);
C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key);
void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
/// @brief Inserts a key-value pair into the hashmap
/// @param hmap The hashmap
/// @param key The key
/// @param value The value
void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key, C__VALTYPE__ value);
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
t_usize hash,
C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev);
/// @brief Gets the value associated with the key
/// @param hmap The hashmap
/// @param key The key
/// @return The value associated with the key, or NULL if the key is not in the hashmap
C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
/// @brief Removes the key-value pair from the hashmap
/// @param hmap The hashmap
/// @param key The key
void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key);
t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
t_error (*func)(t_usize idx,
const C__KEYTYPE__ *key,
C__VALTYPE__ *val, void *ctx),
void *ctx);
/// @brief Get an entry from the hashmap
/// @param hmap The hashmap
/// @param hash The hash of the key
/// @param key The key
/// @param prev The previous entry in the bucket
/// @return The entry, or NULL if the key is not in the hashmap
/// @note this is an internal function
t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, t_usize hash, C__KEYTYPE__ *key, t_entry_C__PREFIX__ **prev);
/// @brief Iterates over the hashmap and calls the given function for each key-value pair
/// @param self The hashmap
/// @param func The function to call
/// @param ctx The context to pass to the function
/// @return An error code
/// @note The iteration can be stopped by returning an error code from the function
t_error hmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self, t_error (*func)(t_usize idx, const C__KEYTYPE__ *key, C__VALTYPE__ *val, void *ctx), void *ctx);
#endif

View file

@ -16,9 +16,12 @@
C__TYPEHEADER__
#include "me/types.h"
/// @brief A function that takes two C__TYPENAME__ and compare them
typedef bool (*t_vec_C__PREFIX___sort_fn)(C__TYPENAME__ *, C__TYPENAME__ *);
/// @brief A function that free an C__TYPENAME__
typedef void (*t_free_C__PREFIX___item)(C__TYPENAME__);
/// @brief A dynamic array of C__TYPENAME__
typedef struct s_vec_C__PREFIX__
{
t_free_C__PREFIX___item free_func;
@ -27,32 +30,94 @@ typedef struct s_vec_C__PREFIX__
C__TYPENAME__ *buffer;
} t_vec_C__PREFIX__;
t_vec_C__PREFIX__ vec_C__PREFIX___new(t_usize capacity,
t_free_C__PREFIX___item free_function);
/// @brief Create a new vec_C__PREFIX__ with a given capacity
/// @param capacity The capacity of the new vec_C__PREFIX__ (in terms of elements)
/// @param free_function The function that will be used to free the elements of the vec_C__PREFIX__
t_vec_C__PREFIX__ vec_C__PREFIX___new(t_usize capacity, t_free_C__PREFIX___item free_function);
/// @brief Push an element to the last position of the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to push the element to
/// @param element The element to push
t_error vec_C__PREFIX___push(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element);
t_error vec_C__PREFIX___push_front(t_vec_C__PREFIX__ *vec,
C__TYPENAME__ element);
/// @brief Push an element to the first position of the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to push the element to
/// @param element The element to push
/// @note This operation is O(n)
t_error vec_C__PREFIX___push_front(t_vec_C__PREFIX__ *vec, C__TYPENAME__ element);
/// @brief Get the last element from the vec_C__PREFIX__, and remove it from the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to get the element from
/// @param[out] out The last element of the vec_C__PREFIX__
/// @return true if the operation failed, false otherwise
t_error vec_C__PREFIX___pop(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value);
/// @brief Get the first element from the vec_C__PREFIX__, and remove it from the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to get the element from
/// @param[out] out The first element of the vec_C__PREFIX__
/// @return true if the operation failed, false otherwise
/// @note This operation is O(n)
t_error vec_C__PREFIX___pop_front(t_vec_C__PREFIX__ *vec, C__TYPENAME__ *value);
/// @brief Free the vector and all its elements
/// @param vec The vec_C__PREFIX__ to free
void vec_C__PREFIX___free(t_vec_C__PREFIX__ vec);
t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec,
t_usize wanted_capacity);
t_error vec_C__PREFIX___find(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), t_usize *index);
t_error vec_C__PREFIX___find_starting(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *),
t_usize starting_index, t_usize *index);
t_error vec_C__PREFIX___all(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result);
t_error vec_C__PREFIX___any(t_vec_C__PREFIX__ *vec,
bool (*fn)(const C__TYPENAME__ *), bool *result);
void vec_C__PREFIX___iter(t_vec_C__PREFIX__ *vec,
void (*fn)(t_usize index, C__TYPENAME__ *value,
void *state),
void *state);
/// @brief Make the vec_C__PREFIX__ at least the given capacity
/// @param vec The vec_C__PREFIX__ to reserve
/// @param wanted_capacity The minimum capacity to reserve
/// @return true if the operation failed, false otherwise
t_error vec_C__PREFIX___reserve(t_vec_C__PREFIX__ *vec, t_usize wanted_capacity);
/// @brief Run the function and returns the index of the first element that returns true
/// @param vec The vec_C__PREFIX__ to search in
/// @param fn The function to run on each element
/// @param[out] index The index of the first element that returns true
t_error vec_C__PREFIX___find(t_vec_C__PREFIX__ *vec, bool (*fn)(const C__TYPENAME__ *), t_usize *index);
/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index
/// @param vec The vec_C__PREFIX__ to search in
/// @param fn The function to run on each element
/// @param starting_index The index to start the search from
/// @param[out] index The index of the first element that returns true
t_error vec_C__PREFIX___find_starting(t_vec_C__PREFIX__ *vec, bool (*fn)(const C__TYPENAME__ *), t_usize starting_index, t_usize *index);
/// @brief Run the function on every element of the vec_C__PREFIX__ and returns if all elements returned true
/// @param vec The vec_C__PREFIX__ to search in
/// @param fn The function to run on each element
/// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise
/// @note If the vec_C__PREFIX__ is empty, result will be true
t_error vec_C__PREFIX___all(t_vec_C__PREFIX__ *vec, bool (*fn)(const C__TYPENAME__ *), bool *result);
/// @brief Run the function on every element of the vec_C__PREFIX__ and returns if any element returned true
/// @param vec The vec_C__PREFIX__ to search in
/// @param fn The function to run on each element
/// @param[out] result The result of the operation
/// @return true if the operation failed, false otherwise
/// @note If the vec_C__PREFIX__ is empty, result will be false
t_error vec_C__PREFIX___any(t_vec_C__PREFIX__ *vec, bool (*fn)(const C__TYPENAME__ *), bool *result);
/// @brief Run the function on every element of the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to iterate over
/// @param fn The function to run on each element
/// @param state The state to pass to the function
void vec_C__PREFIX___iter(t_vec_C__PREFIX__ *vec, void (*fn)(t_usize index, C__TYPENAME__ *value, void *state), void *state);
/// @brief Reverse the order of the elements in the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to reverse
void vec_C__PREFIX___reverse(t_vec_C__PREFIX__ *vec);
void vec_C__PREFIX___sort(t_vec_C__PREFIX__ *vec,
t_vec_C__PREFIX___sort_fn is_sorted);
/// @brief Sort the elements of the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to sort
/// @param is_sorted The function to use to compare the elements
void vec_C__PREFIX___sort(t_vec_C__PREFIX__ *vec, t_vec_C__PREFIX___sort_fn is_sorted);
/// @brief Get a pointer to the last element of the vec_C__PREFIX__
/// @param vec The vec_C__PREFIX__ to get the element from
/// @param[out] out A pointer to the last element of the vec_C__PREFIX__
/// @return true if the operation failed, false otherwise
t_error vec_C__PREFIX___back(t_vec_C__PREFIX__ *vec, C__TYPENAME__ **out);
#endif

View file

@ -10,9 +10,9 @@
/* */
/* ************************************************************************** */
#include "me/char/isalpha.h"
#include "me/char/char.h"
#include "me/convert/str_to_numbers.h"
#include "me/string/str_len.h"
#include "me/str/str.h"
#include "me/types.h"
#define OP_ADD 0b0001

View file

@ -11,9 +11,9 @@
/* ************************************************************************** */
#include "me/char/isalpha.h"
#include "me/char/char.h"
#include "me/convert/str_to_numbers.h"
#include "me/string/str_len.h"
#include "me/str/str.h"
#include "me/types.h"
#include "me/printf/printf.h"

View file

@ -14,19 +14,19 @@
#include "me/hash/sip.h"
#include "me/hashmap/hashmap_C__PREFIX__.h"
#include "me/mem/mem.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <stdlib.h>
t_hashmap_C__PREFIX__ *new_hashmap_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
t_hashmap_C__PREFIX__ *hmap_new_C__PREFIX__(t_hash_C__PREFIX___fn hfunc,
t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop)
{
return (new_hashmap_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
return (hmap_new_with_buckets_C__PREFIX__(hfunc, cfunc, drop,
DEFAULT_BUCKETS));
}
t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
t_hashmap_C__PREFIX__ *hmap_new_with_buckets_C__PREFIX__(
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop, t_usize buckets)
{
@ -46,7 +46,7 @@ t_hashmap_C__PREFIX__ *new_hashmap_with_buckets_C__PREFIX__(
return (hmap);
}
void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
void hmap_free_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
{
t_usize index;
@ -65,7 +65,7 @@ void drop_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap)
mem_free(hmap);
}
t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
t_entry_C__PREFIX__ *hmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
t_usize hashed_key,
C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev)
@ -88,7 +88,7 @@ t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
return (NULL);
}
void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
void hmap_insert_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
C__VALTYPE__ value)
{
t_usize hashed_key;
@ -98,7 +98,7 @@ void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
hmap->hfunc(&hmap->hasher, &key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
prev = NULL;
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
if (entry == NULL)
{
entry = mem_alloc(sizeof(t_entry_C__PREFIX__));

View file

@ -12,7 +12,7 @@
#include "me/hashmap/hashmap_C__PREFIX__.h"
t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
t_error hmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self,
t_error (*func)(t_usize idx,
const C__KEYTYPE__ *key,
C__VALTYPE__ *val, void *ctx),

View file

@ -13,11 +13,11 @@
#include "me/hash/sip.h"
#include "me/hashmap/hashmap_C__PREFIX__.h"
#include "me/mem/mem.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include <stdlib.h>
C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__VALTYPE__ *hmap_get_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key)
{
t_usize hashed_key;
@ -26,13 +26,13 @@ C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
hmap->hfunc(&hmap->hasher, key);
hashed_key = hasher_reset_and_finish(&hmap->hasher);
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
if (entry == NULL)
return (NULL);
return (&entry->kv.val);
}
void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
void hmap_remove_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
{
t_usize hashed_key;
t_entry_C__PREFIX__ *prev;
@ -42,7 +42,7 @@ void remove_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
hashed_key = hasher_reset_and_finish(&hmap->hasher);
hmap->hasher = hasher_sip13_new();
prev = NULL;
entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
entry = hmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev);
if (entry == NULL)
return;
if (prev == NULL)

View file

@ -10,9 +10,9 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/mem/mem.h"
#include "me/vec/vec_C__PREFIX__.h"

View file

@ -10,9 +10,9 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>

View file

@ -10,9 +10,9 @@
/* */
/* ************************************************************************** */
#include "me/mem/mem_alloc_array.h"
#include "me/mem/mem_copy.h"
#include "me/mem/mem_set_zero.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/types.h"
#include "me/vec/vec_C__PREFIX__.h"
#include <stdlib.h>