diff --git a/hashmap/myhashmap.c b/hashmap/myhashmap.c index 3f220c5..074f0a3 100644 --- a/hashmap/myhashmap.c +++ b/hashmap/myhashmap.c @@ -4,14 +4,14 @@ #include #include -static size_t mcl_get_mutex(mcl_hashmap_s *hashmap, size_t hash) { return hash % hashmap->num_locks; } +static size_t get_mutex(hashmap_s *hashmap, size_t hash) { return hash % hashmap->num_locks; } -static size_t mcl_get_bucket_index(mcl_hashmap_s *hashmap, void *key) { +static size_t get_bucket_index(hashmap_s *hashmap, void *key) { unsigned int hash = hashmap->hash_fn(key); return hash % MYCLIB_HASHMAP_SIZE; } -static void mcl_free_bucket_content(mcl_hashmap_s *hashmap, mcl_bucket_s *bucket) { +static void free_bucket_content(hashmap_s *hashmap, bucket_s *bucket) { if (bucket == NULL) { return; } @@ -27,9 +27,9 @@ static void mcl_free_bucket_content(mcl_hashmap_s *hashmap, mcl_bucket_s *bucket } } -static mcl_bucket_s *mcl_find_bucket(mcl_hashmap_s *hashmap, void *key, mcl_bucket_s **prev) { - size_t index = mcl_get_bucket_index(hashmap, key); - mcl_bucket_s *bucket = &hashmap->map[index]; +static bucket_s *find_bucket(hashmap_s *hashmap, void *key, bucket_s **prev) { + size_t index = get_bucket_index(hashmap, key); + bucket_s *bucket = &hashmap->map[index]; *prev = NULL; @@ -50,8 +50,8 @@ static mcl_bucket_s *mcl_find_bucket(mcl_hashmap_s *hashmap, void *key, mcl_buck return NULL; } -mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) { - mcl_hashmap_s *hashmap = malloc(sizeof(mcl_hashmap_s)); +hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) { + hashmap_s *hashmap = malloc(sizeof(hashmap_s)); if (hashmap == NULL) { return NULL; } @@ -90,25 +90,25 @@ mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_k return hashmap; } -void mcl_hm_free(mcl_hashmap_s *hashmap) { +void hm_free(hashmap_s *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_s *bucket = &hashmap->map[i]; + bucket_s *bucket = &hashmap->map[i]; /* Free the first bucket if it contains data */ if (bucket->key != NULL) { - mcl_free_bucket_content(hashmap, bucket); + free_bucket_content(hashmap, bucket); } /* Free all chained buckets */ bucket = bucket->next; while (bucket != NULL) { - mcl_bucket_s *next = bucket->next; - mcl_free_bucket_content(hashmap, bucket); + bucket_s *next = bucket->next; + free_bucket_content(hashmap, bucket); free(bucket); bucket = next; } @@ -124,7 +124,7 @@ void mcl_hm_free(mcl_hashmap_s *hashmap) { free(hashmap); } -void mcl_hm_free_bucket(mcl_bucket_s *bucket) { +void hm_free_bucket(bucket_s *bucket) { if (bucket == NULL) { return; } @@ -134,17 +134,17 @@ void mcl_hm_free_bucket(mcl_bucket_s *bucket) { free(bucket); } -bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value) { +bool hm_set(hashmap_s *hashmap, void *key, void *value) { if (hashmap == NULL || key == NULL || value == NULL) { return false; } - size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); + size_t mutex_id = get_mutex(hashmap, hashmap->hash_fn(key)); mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_lock(mutex); - mcl_bucket_s *prev; - mcl_bucket_s *existing = mcl_find_bucket(hashmap, key, &prev); + bucket_s *prev; + bucket_s *existing = find_bucket(hashmap, key, &prev); if (existing != NULL) { /* Key exists, update value */ @@ -166,8 +166,8 @@ bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value) { } /* Key doesn't exist, need to insert new bucket */ - size_t index = mcl_get_bucket_index(hashmap, key); - mcl_bucket_s *bucket = &hashmap->map[index]; + size_t index = get_bucket_index(hashmap, key); + bucket_s *bucket = &hashmap->map[index]; if (bucket->key == NULL) { /* First bucket is empty, use it */ @@ -196,7 +196,7 @@ bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value) { } /* Create new bucket and insert at head of collision chain */ - mcl_bucket_s *new_bucket = malloc(sizeof(mcl_bucket_s)); + bucket_s *new_bucket = malloc(sizeof(bucket_s)); if (new_bucket == NULL) { mtx_unlock(mutex); @@ -230,12 +230,12 @@ bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value) { return true; } -static mcl_bucket_s *mcl_get_bucket_copy(mcl_bucket_s *from, size_t key_size, size_t value_size) { - mcl_bucket_s *copy = malloc(sizeof(mcl_bucket_s)); +static bucket_s *get_bucket_copy(bucket_s *from, size_t key_size, size_t value_size) { + bucket_s *copy = malloc(sizeof(bucket_s)); if (copy == NULL) { return NULL; } - memcpy(copy, from, sizeof(mcl_bucket_s)); + memcpy(copy, from, sizeof(bucket_s)); copy->key = malloc(key_size); if (copy->key == NULL) { @@ -257,20 +257,20 @@ static mcl_bucket_s *mcl_get_bucket_copy(mcl_bucket_s *from, size_t key_size, si return copy; } -mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key) { +bucket_s *hm_get(hashmap_s *hashmap, void *key) { if (hashmap == NULL || key == NULL) { return NULL; } - size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); + size_t mutex_id = get_mutex(hashmap, hashmap->hash_fn(key)); mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_lock(mutex); - mcl_bucket_s *prev; - mcl_bucket_s *found = mcl_find_bucket(hashmap, key, &prev); + bucket_s *prev; + bucket_s *found = find_bucket(hashmap, key, &prev); if (found) { - mcl_bucket_s *copy = mcl_get_bucket_copy(found, hashmap->key_size, hashmap->value_size); + bucket_s *copy = get_bucket_copy(found, hashmap->key_size, hashmap->value_size); mtx_unlock(mutex); @@ -282,17 +282,17 @@ mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key) { return NULL; } -bool mcl_hm_remove(mcl_hashmap_s *hashmap, void *key) { +bool hm_remove(hashmap_s *hashmap, void *key) { if (hashmap == NULL || key == NULL) { return false; } - size_t mutex_id = mcl_get_mutex(hashmap, hashmap->hash_fn(key)); + size_t mutex_id = get_mutex(hashmap, hashmap->hash_fn(key)); mtx_t *mutex = &(hashmap->locks[mutex_id]); mtx_lock(mutex); - mcl_bucket_s *prev; - mcl_bucket_s *to_remove = mcl_find_bucket(hashmap, key, &prev); + bucket_s *prev; + bucket_s *to_remove = find_bucket(hashmap, key, &prev); if (to_remove == NULL) { mtx_unlock(mutex); @@ -301,14 +301,14 @@ bool mcl_hm_remove(mcl_hashmap_s *hashmap, void *key) { } /* Free the content of the bucket */ - mcl_free_bucket_content(hashmap, to_remove); + 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_s *next_bucket = to_remove->next; + bucket_s *next_bucket = to_remove->next; to_remove->key = next_bucket->key; to_remove->value = next_bucket->value; to_remove->next = next_bucket->next; diff --git a/hashmap/myhashmap.h b/hashmap/myhashmap.h index c969d77..590b17a 100644 --- a/hashmap/myhashmap.h +++ b/hashmap/myhashmap.h @@ -13,11 +13,11 @@ * 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 { - void *key; /**< Pointer to the key */ - void *value; /**< Pointer to the value */ - struct mcl_bucket *next; /**< Pointer to the next bucket in case of collision */ -} mcl_bucket_s; +typedef struct bucket { + void *key; /**< Pointer to the key */ + void *value; /**< Pointer to the value */ + struct bucket *next; /**< Pointer to the next bucket in case of collision */ +} bucket_s; /** * @brief Function pointer type for a hash function @@ -56,17 +56,17 @@ typedef void free_value_f(void *value); * Contains function pointers for hash computation, key comparison, * and memory management, along with the bucket array. */ -typedef struct mcl_hashmap { - hash_f *hash_fn; /**< Hash function */ - equal_f *equal_fn; /**< Equality comparison function */ - free_key_f *free_key_fn; /**< Key deallocation function (optional) */ - free_value_f *free_value_fn; /**< Value deallocation function (optional) */ - size_t key_size; /**< Size in bytes of the key */ - size_t value_size; /**< Size in bytes of the value */ - mcl_bucket_s map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ - mtx_t *locks; /**< Mutex array */ - size_t num_locks; /**< Number of mutex */ -} mcl_hashmap_s; +typedef struct hashmap { + hash_f *hash_fn; /**< Hash function */ + equal_f *equal_fn; /**< Equality comparison function */ + free_key_f *free_key_fn; /**< Key deallocation function (optional) */ + free_value_f *free_value_fn; /**< Value deallocation function (optional) */ + size_t key_size; /**< Size in bytes of the key */ + size_t value_size; /**< Size in bytes of the value */ + bucket_s map[MYCLIB_HASHMAP_SIZE]; /**< Array of bucket chains */ + mtx_t *locks; /**< Mutex array */ + size_t num_locks; /**< Number of mutex */ +} hashmap_s; /** * @brief Initialize a new hash map with user-defined behavior functions @@ -83,7 +83,7 @@ typedef struct mcl_hashmap { * @param[in] value_size Size in bytes of each value to be stored * @return A pointer to the newly initialized hash map, or NULL on failure */ -mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); +hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size); /** * @brief Free all resources used by the hash map @@ -93,14 +93,14 @@ mcl_hashmap_s *mcl_hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_k * * @param[in] hashmap Pointer to the hash map to free */ -void mcl_hm_free(mcl_hashmap_s *hashmap); +void hm_free(hashmap_s *hashmap); /** - * @brief Free a bucket returned by mcl_hm_get() + * @brief Free a bucket returned by get * * @param[in] bucket Pointer to the bucket to free */ -void mcl_hm_free_bucket(mcl_bucket_s *bucket); +void hm_free_bucket(bucket_s *bucket); /** * @brief Insert or update a key-value pair in the hash map @@ -114,7 +114,7 @@ void mcl_hm_free_bucket(mcl_bucket_s *bucket); * @param[in] value Pointer to the value to insert (will be copied, must not be NULL) * @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure) */ -bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value); +bool hm_set(hashmap_s *hashmap, void *key, void *value); /** * @brief Retrieve a bucket by key @@ -126,7 +126,7 @@ bool mcl_hm_set(mcl_hashmap_s *hashmap, void *key, void *value); * @param[in] key Pointer to the key to search for * @return Pointer to the copy of the bucket, to avoid race conditions, or NULL if not found or on invalid input */ -mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key); +bucket_s *hm_get(hashmap_s *hashmap, void *key); /** * @brief Remove a key-value pair from the hash map @@ -138,6 +138,6 @@ mcl_bucket_s *mcl_hm_get(mcl_hashmap_s *hashmap, void *key); * @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_s *hashmap, void *key); +bool hm_remove(hashmap_s *hashmap, void *key); #endif /* MYCLIB_HASHMAP_H */ diff --git a/string/mystring.c b/string/mystring.c index 72465cc..1a3ff96 100644 --- a/string/mystring.c +++ b/string/mystring.c @@ -190,7 +190,7 @@ void string_free(string_s *string) { free(string); } -size_t string_length(string_s *string) { +size_t string_len(string_s *string) { if (string == NULL) { return 0; } @@ -206,7 +206,7 @@ size_t string_length(string_s *string) { return len; } -size_t string_capacity(string_s *string) { +size_t string_cap(string_s *string) { if (string == NULL) { return 0; } diff --git a/test/hashmap/hm1.c b/test/hashmap/hm1.c index d9d4730..0d345fd 100644 --- a/test/hashmap/hm1.c +++ b/test/hashmap/hm1.c @@ -53,7 +53,7 @@ void test_hm1(void) { /* This hashmap will contain names as keys and a custom type as value */ size_t key_size = sizeof(char) * MAX_STR_LEN; size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN; - mcl_hashmap_s *map = mcl_hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); + hashmap_s *map = hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); assert(map != NULL); /* Make a new value */ @@ -63,11 +63,11 @@ void test_hm1(void) { strncpy(p1.favourite_brand, "Ferrari", sizeof(p1.favourite_brand)); /* Insert a new pair */ - assert(mcl_hm_set(map, "John", &p1)); + assert(hm_set(map, "John", &p1)); /* Retrieve the data */ /* Remember to free the value from the get function */ - mcl_bucket_s *john = mcl_hm_get(map, "John"); + bucket_s *john = hm_get(map, "John"); assert(john != NULL); char *name = (char *)john->key; @@ -80,11 +80,11 @@ void test_hm1(void) { assert(strcmp(fav_brand, "Ferrari") == 0); /* Free the bucket */ - mcl_hm_free_bucket(john); + hm_free_bucket(john); /* Remove a key from hash map */ - assert(mcl_hm_remove(map, "John")); + assert(hm_remove(map, "John")); /* Deallocate */ - mcl_hm_free(map); + hm_free(map); }