diff --git a/examples/hashmap/hm1.c b/examples/hashmap/hm1.c index b1e8e0d..36eb0e6 100644 --- a/examples/hashmap/hm1.c +++ b/examples/hashmap/hm1.c @@ -1,7 +1,80 @@ #include +#include +#include + +#include "../../hashmap/myhashmap.h" + +#define MAX_STR_LEN 64 + +/* My custom data type stored as value */ +struct my_custom_type_t { + int age; + char favourite_brand[MAX_STR_LEN]; +}; + +/* Let's hash our name */ +static unsigned int my_hash_func(const void *key) { + char *name = (char *)key; + size_t len = strlen(name); + + unsigned int hash = 0; + for (size_t i = 0; i < len; ++i) { + hash += (int)name[i]; + } + + return hash % 2069; +} + +/* Let's write our compare function */ +bool my_equal_fun(const void *key_a, const void *key_b) { + char *name_a = (char *)key_a; + char *name_b = (char *)key_b; + + if (strcmp(name_a, name_b) == 0) { + return true; + } + + return false; +} + +/* And our last two functions, the free key and value called inside mcl_hm_remove() */ +void my_free_key(void *key) { free(key); } + +void my_free_value(void *value) { + struct my_custom_type_t *mct = (struct my_custom_type_t *)value; + free(mct); +} int main(void) { - puts("TODO: Hashmap"); + /* Allocate a new hashmap */ + /* Pass your custom hash, equal and free functions */ + /* 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 *map = mcl_hm_init(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size); - return 0; + /* Set a new value */ + struct my_custom_type_t p1 = { + .age = 21, + }; + strncpy(p1.favourite_brand, "Ferrari", sizeof(p1.favourite_brand)); + + mcl_hm_set(map, "John", &p1); + + /* Retrieve the data */ + mcl_bucket *john = mcl_hm_get(map, "John"); + char *name = (char *)john->key; + struct my_custom_type_t *john_v = (struct my_custom_type_t *)john->value; + int age = john_v->age; + char *fav_brand = john_v->favourite_brand; + fprintf(stdout, "Name: %s\nAge: %d\nFavourite brand: %s\n", name, age, fav_brand); + mcl_hm_free_bucket(john); + + /* Remove a key from hash map */ + mcl_hm_remove(map, "John"); + + /* Deallocate */ + mcl_hm_free(map); + + return 0; } diff --git a/examples/queue/q1.c b/examples/queue/q1.c index 6d231d0..4031ae7 100644 --- a/examples/queue/q1.c +++ b/examples/queue/q1.c @@ -1,53 +1,50 @@ -#include #include #include "../../queue/myqueue.h" int main(void) { + /* Allocate a new queue */ + /* Always remember to check return values */ mcl_queue *queue = mcl_queue_init(3, sizeof(int)); - assert(queue != NULL); int val, out; + /* Push value to the ring buffer */ val = 1; - assert(mcl_queue_push(queue, &val) == 0); + mcl_queue_push(queue, &val); val = 2; - assert(mcl_queue_push(queue, &val) == 0); + mcl_queue_push(queue, &val); + /* Retrieve values */ int front = *((int *)mcl_queue_get_front(queue)); int rear = *((int *)mcl_queue_get_rear(queue)); printf("Front: %d, Rear: %d\n", front, rear); - assert(front == 1); - assert(rear == 2); - assert(mcl_queue_pop(queue, &out) == 0); + /* Remove an element from the buffer */ + mcl_queue_pop(queue, &out); printf("Pop: %d\n", out); - assert(out == 1); front = *((int *)mcl_queue_get_front(queue)); printf("Front after pop: %d\n", front); - assert(front == 2); val = 3; - assert(mcl_queue_push(queue, &val) == 0); + mcl_queue_push(queue, &val); rear = *((int *)mcl_queue_get_rear(queue)); printf("Rear after push 3: %d\n", rear); - assert(rear == 3); val = 4; int res = mcl_queue_push(queue, &val); - assert(res == 0); + /* Clear queue */ while (mcl_queue_pop(queue, &out) == 0) { printf("Pop: %d\n", out); } - printf("Queue is now empty\n"); - - assert(mcl_queue_pop(queue, &out) == -1); + puts("Queue is now empty"); + /* Deallocate memory */ mcl_queue_free(queue); - printf("All tests passed successfully.\n"); + return 0; } diff --git a/examples/string/str1.c b/examples/string/str1.c index c2250e9..7f043fa 100644 --- a/examples/string/str1.c +++ b/examples/string/str1.c @@ -1,24 +1,33 @@ -#include #include #include "../../string/mystring.h" int main(void) { + size_t length; + size_t capacity; + const char *c_str; + + /* Allocate a new dynamic string with an initial capacity */ + /* Always remember to check return values */ mcl_string *str = mcl_string_new("Hello, world!", 512); - assert(str != NULL); - printf("%s\nsize: %ld, cap: %ld\n", mcl_string_cstr(str), (long)mcl_string_length(str), (long)mcl_string_capacity(str)); - assert(mcl_string_length(str) == 13); + /* Retrieve a C str from mcl_string with mcl_string_cstr() */ + c_str = mcl_string_cstr(str); + length = mcl_string_length(str); + capacity = mcl_string_capacity(str); + printf("%s\nlength: %ld, capacity: %ld\n", c_str, length, capacity); - int ret = mcl_string_append(str, " How are you?"); - assert(ret == 0); + /* Append text to a mcl_string */ + mcl_string_append(str, " How are you?"); - printf("After append:\n"); - printf("%s\nsize: %ld, cap: %ld\n", mcl_string_cstr(str), (long)mcl_string_length(str), (long)mcl_string_capacity(str)); - assert(mcl_string_length(str) == 26); + puts("After append:"); + c_str = mcl_string_cstr(str); + length = mcl_string_length(str); + capacity = mcl_string_capacity(str); + printf("%s\nsize: %ld, cap: %ld\n", c_str, length, capacity); + /* Always deallocate memory */ mcl_string_free(str); - printf("All tests passed successfully.\n"); return 0; }