refactoring server and add myclib
This commit is contained in:
188
include/myclib/hashmap/myhashmap.c
Normal file
188
include/myclib/hashmap/myhashmap.c
Normal file
@@ -0,0 +1,188 @@
|
||||
#include "myhashmap.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static size_t mcl_get_bucket_index(mcl_hashmap *hashmap, void *key) {
|
||||
unsigned int hash = hashmap->hash_fn(key);
|
||||
return hash % MYCLIB_HASHMAP_SIZE;
|
||||
}
|
||||
|
||||
static void mcl_free_bucket_content(mcl_hashmap *hashmap, mcl_bucket *bucket) {
|
||||
if (bucket == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Free key if free function is provided */
|
||||
if (hashmap->free_key_fn != NULL && bucket->key != NULL) {
|
||||
hashmap->free_key_fn(bucket->key);
|
||||
}
|
||||
|
||||
/* Free value if free function is provided */
|
||||
if (hashmap->free_value_fn != NULL && bucket->value != NULL) {
|
||||
hashmap->free_value_fn(bucket->value);
|
||||
}
|
||||
}
|
||||
|
||||
static mcl_bucket *mcl_find_bucket(mcl_hashmap *hashmap, void *key, mcl_bucket **prev) {
|
||||
size_t index = mcl_get_bucket_index(hashmap, key);
|
||||
mcl_bucket *bucket = &hashmap->map[index];
|
||||
|
||||
*prev = NULL;
|
||||
|
||||
/* Return NULL if first bucket is empty */
|
||||
if (bucket->key == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Search through the collision chain */
|
||||
while (bucket != NULL) {
|
||||
if (hashmap->equal_fn(bucket->key, key)) {
|
||||
return bucket;
|
||||
}
|
||||
*prev = bucket;
|
||||
bucket = bucket->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn) {
|
||||
mcl_hashmap *hashmap = malloc(sizeof(mcl_hashmap));
|
||||
if (hashmap == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Initialize hash map with given parameters */
|
||||
hashmap->hash_fn = hash_fn;
|
||||
hashmap->equal_fn = equal_fn;
|
||||
hashmap->free_key_fn = free_key_fn;
|
||||
hashmap->free_value_fn = free_value_fn;
|
||||
|
||||
/* Clear all buckets in the map */
|
||||
memset(hashmap->map, 0, sizeof(hashmap->map));
|
||||
|
||||
return hashmap;
|
||||
}
|
||||
|
||||
void mcl_hm_free(mcl_hashmap *hashmap) {
|
||||
if (hashmap == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Iterate through all buckets in the hash map */
|
||||
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
|
||||
mcl_bucket *bucket = &hashmap->map[i];
|
||||
|
||||
/* Free the first bucket if it contains data */
|
||||
if (bucket->key != NULL) {
|
||||
mcl_free_bucket_content(hashmap, bucket);
|
||||
}
|
||||
|
||||
/* Free all chained buckets */
|
||||
bucket = bucket->next;
|
||||
while (bucket != NULL) {
|
||||
mcl_bucket *next = bucket->next;
|
||||
mcl_free_bucket_content(hashmap, bucket);
|
||||
free(bucket);
|
||||
bucket = next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Free the hash map structure itself */
|
||||
free(hashmap);
|
||||
}
|
||||
|
||||
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
|
||||
if (hashmap == NULL || key == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Try to find existing bucket */
|
||||
mcl_bucket *prev;
|
||||
mcl_bucket *existing = mcl_find_bucket(hashmap, key, &prev);
|
||||
|
||||
if (existing != NULL) {
|
||||
/* Key exists, update value */
|
||||
if (hashmap->free_value_fn != NULL && existing->value != NULL) {
|
||||
hashmap->free_value_fn(existing->value);
|
||||
}
|
||||
existing->value = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Key doesn't exist, need to insert new bucket */
|
||||
size_t index = mcl_get_bucket_index(hashmap, key);
|
||||
mcl_bucket *bucket = &hashmap->map[index];
|
||||
|
||||
/* If first bucket is empty, use it */
|
||||
if (bucket->key == NULL) {
|
||||
bucket->key = key;
|
||||
bucket->value = value;
|
||||
bucket->next = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Create new bucket and insert at head of collision chain */
|
||||
mcl_bucket *new_bucket = malloc(sizeof(mcl_bucket));
|
||||
if (new_bucket == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
new_bucket->key = key;
|
||||
new_bucket->value = value;
|
||||
new_bucket->next = bucket->next;
|
||||
bucket->next = new_bucket;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key) {
|
||||
if (hashmap == NULL || key == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mcl_bucket *prev;
|
||||
return mcl_find_bucket(hashmap, key, &prev);
|
||||
}
|
||||
|
||||
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key) {
|
||||
if (hashmap == NULL || key == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mcl_bucket *prev;
|
||||
mcl_bucket *to_remove = mcl_find_bucket(hashmap, key, &prev);
|
||||
|
||||
if (to_remove == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Free the content of the bucket */
|
||||
mcl_free_bucket_content(hashmap, to_remove);
|
||||
|
||||
/* Handle removal based on position in chain */
|
||||
if (prev == NULL) {
|
||||
/* Removing first bucket in chain */
|
||||
if (to_remove->next != NULL) {
|
||||
/* Move next bucket's content to first bucket and free the next bucket */
|
||||
mcl_bucket *next_bucket = to_remove->next;
|
||||
to_remove->key = next_bucket->key;
|
||||
to_remove->value = next_bucket->value;
|
||||
to_remove->next = next_bucket->next;
|
||||
free(next_bucket);
|
||||
} else {
|
||||
/* No next bucket, mark first bucket as empty */
|
||||
to_remove->key = NULL;
|
||||
to_remove->value = NULL;
|
||||
to_remove->next = NULL;
|
||||
}
|
||||
} else {
|
||||
/* Removing bucket from middle/end of chain */
|
||||
prev->next = to_remove->next;
|
||||
free(to_remove);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
127
include/myclib/hashmap/myhashmap.h
Normal file
127
include/myclib/hashmap/myhashmap.h
Normal file
@@ -0,0 +1,127 @@
|
||||
#ifndef MYCLIB_HASHMAP_H
|
||||
#define MYCLIB_HASHMAP_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define MYCLIB_HASHMAP_SIZE 1024 /**< Number of buckets in the hash map */
|
||||
|
||||
/**
|
||||
* @brief A single bucket in the hash map
|
||||
*
|
||||
* Each bucket can hold one key-value pair and points to the next bucket
|
||||
* in case of hash collisions (separate chaining).
|
||||
*/
|
||||
typedef struct mcl_bucket_t {
|
||||
void *key; /**< Pointer to the key */
|
||||
void *value; /**< Pointer to the value */
|
||||
struct mcl_bucket_t *next; /**< Pointer to the next bucket in case of collision */
|
||||
} mcl_bucket;
|
||||
|
||||
/**
|
||||
* @brief Function pointer type for a hash function
|
||||
*
|
||||
* @param[in] key Pointer to the key to hash
|
||||
* @return The computed hash as an unsigned integer
|
||||
*/
|
||||
typedef unsigned int mcl_hash_fn(const void *key);
|
||||
|
||||
/**
|
||||
* @brief Function pointer type for a key comparison function
|
||||
*
|
||||
* @param[in] key_a Pointer to the first key
|
||||
* @param[in] key_b Pointer to the second key
|
||||
* @return true if the keys are considered equal, false otherwise
|
||||
*/
|
||||
typedef bool mcl_equal_fn(const void *key_a, const void *key_b);
|
||||
|
||||
/**
|
||||
* @brief Function pointer type for freeing a key
|
||||
*
|
||||
* @param[in] key Pointer to the key to free
|
||||
*/
|
||||
typedef void mcl_free_key_fn(void *key);
|
||||
|
||||
/**
|
||||
* @brief Function pointer type for freeing a value
|
||||
*
|
||||
* @param[in] value Pointer to the value to free
|
||||
*/
|
||||
typedef void mcl_free_value_fn(void *value);
|
||||
|
||||
/**
|
||||
* @brief Main structure representing the hash map
|
||||
*
|
||||
* Contains function pointers for hash computation, key comparison,
|
||||
* and memory management, along with the bucket array.
|
||||
*/
|
||||
typedef struct mcl_hashmap_t {
|
||||
mcl_hash_fn *hash_fn; /**< Hash function */
|
||||
mcl_equal_fn *equal_fn; /**< Equality comparison function */
|
||||
mcl_free_key_fn *free_key_fn; /**< Key deallocation function (optional) */
|
||||
mcl_free_value_fn *free_value_fn; /**< Value deallocation function (optional) */
|
||||
mcl_bucket map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */
|
||||
} mcl_hashmap;
|
||||
|
||||
/**
|
||||
* @brief Initialize a new hash map with user-defined behavior functions
|
||||
*
|
||||
* Creates a new hash map and initializes it with the provided function pointers.
|
||||
* The free functions can be NULL if no automatic memory management is needed.
|
||||
*
|
||||
* @param[in] hash_fn Function used to hash keys (required)
|
||||
* @param[in] equal_fn Function used to compare keys (required)
|
||||
* @param[in] free_key_fn Function used to free keys (optional, can be NULL)
|
||||
* @param[in] free_value_fn Function used to free values (optional, can be NULL)
|
||||
* @return A pointer to the newly initialized hash map, or NULL on failure
|
||||
*/
|
||||
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn);
|
||||
|
||||
/**
|
||||
* @brief Free all resources used by the hash map
|
||||
*
|
||||
* Iterates through all buckets, frees keys and values using the provided
|
||||
* free functions (if not NULL), and deallocates the hash map structure.
|
||||
*
|
||||
* @param[in] hashmap Pointer to the hash map to free
|
||||
*/
|
||||
void mcl_hm_free(mcl_hashmap *hashmap);
|
||||
|
||||
/**
|
||||
* @brief Insert or update a key-value pair in the hash map
|
||||
*
|
||||
* If the key already exists, the old value is freed (if free_value_fn is provided)
|
||||
* and replaced with the new value. If the key doesn't exist, a new entry is created.
|
||||
*
|
||||
* @param[in] hashmap Pointer to the hash map
|
||||
* @param[in] key Pointer to the key to insert (must not be NULL)
|
||||
* @param[in] value Pointer to the value to insert (can be NULL)
|
||||
* @return true if the operation succeeded, false on failure (NULL hashmap/key or memory allocation failure)
|
||||
*/
|
||||
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief Retrieve a bucket by key
|
||||
*
|
||||
* Searches for the given key in the hash map and returns the bucket containing it.
|
||||
* The caller can then access both the key and value from the returned bucket.
|
||||
*
|
||||
* @param[in] hashmap Pointer to the hash map
|
||||
* @param[in] key Pointer to the key to search for
|
||||
* @return Pointer to the found bucket, or NULL if not found or on invalid input
|
||||
*/
|
||||
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key);
|
||||
|
||||
/**
|
||||
* @brief Remove a key-value pair from the hash map
|
||||
*
|
||||
* Searches for the given key and removes it from the hash map. Both the key
|
||||
* and value are freed using the provided free functions (if not NULL).
|
||||
*
|
||||
* @param[in] hashmap Pointer to the hash map
|
||||
* @param[in] key Pointer to the key to remove
|
||||
* @return true if the key was found and removed, false if not found or on invalid input
|
||||
*/
|
||||
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key);
|
||||
|
||||
#endif /* MYCLIB_HASHMAP_H */
|
||||
119
include/myclib/string/mystring.c
Normal file
119
include/myclib/string/mystring.c
Normal file
@@ -0,0 +1,119 @@
|
||||
#include "mystring.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
mcl_string *mcl_string_new(const char *text, long initial_capacity) {
|
||||
if (!text) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate string struct */
|
||||
mcl_string *str = malloc(sizeof(mcl_string));
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Calculate size and capacity */
|
||||
str->size = strlen(text);
|
||||
size_t capacity = initial_capacity;
|
||||
|
||||
if (capacity != -1 && capacity - 1 < str->size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (capacity == -1) {
|
||||
capacity = (unsigned long)pow(2, (unsigned)log2(str->size) + 1);
|
||||
}
|
||||
|
||||
str->capacity = capacity;
|
||||
|
||||
/* Allocate data buffer */
|
||||
str->data = malloc(sizeof(char) * str->capacity);
|
||||
if (!str->data) {
|
||||
free(str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Copy the text and ensure null termination */
|
||||
memset(str->data, 0, str->capacity);
|
||||
memcpy(str->data, text, str->size);
|
||||
str->data[str->size] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
int mcl_string_append(mcl_string *string, const char *text) {
|
||||
if (!string || !text) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Handle empty case */
|
||||
size_t text_len = strlen(text);
|
||||
if (text_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t new_size = text_len + string->size;
|
||||
|
||||
/* Check if we need to resize */
|
||||
if (new_size + 1 > string->capacity) {
|
||||
size_t new_capacity = (unsigned long)pow(2, (unsigned)log2(new_size) + 1);
|
||||
/* Reallocate the buffer */
|
||||
void *new_data = realloc(string->data, sizeof(char) * new_capacity);
|
||||
if (!new_data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
string->data = new_data;
|
||||
string->capacity = new_capacity;
|
||||
|
||||
/* Init to 0 the new capacity */
|
||||
memset(string->data + string->size, 0, string->capacity - string->size);
|
||||
}
|
||||
|
||||
/* Append text */
|
||||
memcpy(string->data + string->size, text, text_len);
|
||||
string->size = new_size;
|
||||
string->data[string->size] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mcl_string_free(mcl_string *string) {
|
||||
if (!string) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (string->data) {
|
||||
free(string->data);
|
||||
}
|
||||
|
||||
free(string);
|
||||
}
|
||||
|
||||
size_t mcl_string_length(const mcl_string *string) {
|
||||
if (!string) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return string->size;
|
||||
}
|
||||
|
||||
size_t mcl_string_capacity(const mcl_string *string) {
|
||||
if (!string) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return string->capacity;
|
||||
}
|
||||
|
||||
const char *mcl_string_cstr(const mcl_string *string) {
|
||||
if (!string || !string->data) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return string->data;
|
||||
}
|
||||
73
include/myclib/string/mystring.h
Normal file
73
include/myclib/string/mystring.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#ifndef MYCLIB_STRING_H
|
||||
#define MYCLIB_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* @brief String structure
|
||||
*/
|
||||
typedef struct mcl_string_t {
|
||||
size_t size; /**< Length of the string (excluding null terminator) */
|
||||
size_t capacity; /**< Total allocated capacity */
|
||||
char *data; /**< Pointer to the string data */
|
||||
} mcl_string;
|
||||
|
||||
/**
|
||||
* @brief Create a new string
|
||||
*
|
||||
* @param text The text to initialize from
|
||||
* @param initial_capacity The initial capacity, pass -1 to retrieve it from the text
|
||||
* @return Pointer to the new string, or NULL on failure
|
||||
*
|
||||
* @note The caller is responsible for freeing the returned string with mcl_string_free() and to pass the right inital_capacity
|
||||
*/
|
||||
mcl_string *mcl_string_new(const char *text, long initial_capacity);
|
||||
|
||||
/**
|
||||
* @brief Append text to an existing string
|
||||
*
|
||||
* @param string The string to append to
|
||||
* @param text The string to append (can be empty)
|
||||
* @return 0 on success, -1 on failure
|
||||
*
|
||||
* @note If it fails, the original string remains unchanged
|
||||
*/
|
||||
int mcl_string_append(mcl_string *string, const char *text);
|
||||
|
||||
/**
|
||||
* @brief Free a string
|
||||
*
|
||||
* @param string The string to free
|
||||
*
|
||||
* @note This function is safe to call with NULL pointers
|
||||
*/
|
||||
void mcl_string_free(mcl_string *string);
|
||||
|
||||
/**
|
||||
* @brief Get the current length of the string
|
||||
*
|
||||
* @param string The string to query
|
||||
* @return The length of the string, or 0 if string is NULL
|
||||
*/
|
||||
size_t mcl_string_length(const mcl_string *string);
|
||||
|
||||
/**
|
||||
* @brief Get the current capacity of the string
|
||||
*
|
||||
* @param string The string to query
|
||||
* @return The capacity of the string buffer, or 0 if string is NULL
|
||||
*/
|
||||
size_t mcl_string_capacity(const mcl_string *string);
|
||||
|
||||
/**
|
||||
* @brief Get a read-only string representation
|
||||
*
|
||||
* @param string The string to access
|
||||
* @return Pointer to null-terminated string data, or empty string "" if string is NULL
|
||||
*
|
||||
* @warning The returned pointer should not be modified directly and may become
|
||||
* invalid after any modification operation on the string
|
||||
*/
|
||||
const char *mcl_string_cstr(const mcl_string *string);
|
||||
|
||||
#endif /* MYCLIB_STRING_H */
|
||||
Reference in New Issue
Block a user