improve examples

This commit is contained in:
2025-08-05 00:10:53 +02:00
parent 2376e5d0a0
commit 306e7d8496
3 changed files with 107 additions and 28 deletions

View File

@@ -1,7 +1,80 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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) { 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);
/* 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; return 0;
} }

View File

@@ -1,53 +1,50 @@
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include "../../queue/myqueue.h" #include "../../queue/myqueue.h"
int main(void) { int main(void) {
/* Allocate a new queue */
/* Always remember to check return values */
mcl_queue *queue = mcl_queue_init(3, sizeof(int)); mcl_queue *queue = mcl_queue_init(3, sizeof(int));
assert(queue != NULL);
int val, out; int val, out;
/* Push value to the ring buffer */
val = 1; val = 1;
assert(mcl_queue_push(queue, &val) == 0); mcl_queue_push(queue, &val);
val = 2; 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 front = *((int *)mcl_queue_get_front(queue));
int rear = *((int *)mcl_queue_get_rear(queue)); int rear = *((int *)mcl_queue_get_rear(queue));
printf("Front: %d, Rear: %d\n", front, rear); 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); printf("Pop: %d\n", out);
assert(out == 1);
front = *((int *)mcl_queue_get_front(queue)); front = *((int *)mcl_queue_get_front(queue));
printf("Front after pop: %d\n", front); printf("Front after pop: %d\n", front);
assert(front == 2);
val = 3; val = 3;
assert(mcl_queue_push(queue, &val) == 0); mcl_queue_push(queue, &val);
rear = *((int *)mcl_queue_get_rear(queue)); rear = *((int *)mcl_queue_get_rear(queue));
printf("Rear after push 3: %d\n", rear); printf("Rear after push 3: %d\n", rear);
assert(rear == 3);
val = 4; val = 4;
int res = mcl_queue_push(queue, &val); int res = mcl_queue_push(queue, &val);
assert(res == 0);
/* Clear queue */
while (mcl_queue_pop(queue, &out) == 0) { while (mcl_queue_pop(queue, &out) == 0) {
printf("Pop: %d\n", out); printf("Pop: %d\n", out);
} }
printf("Queue is now empty\n"); puts("Queue is now empty");
assert(mcl_queue_pop(queue, &out) == -1);
/* Deallocate memory */
mcl_queue_free(queue); mcl_queue_free(queue);
printf("All tests passed successfully.\n");
return 0; return 0;
} }

View File

@@ -1,24 +1,33 @@
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include "../../string/mystring.h" #include "../../string/mystring.h"
int main(void) { 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); 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)); /* Retrieve a C str from mcl_string with mcl_string_cstr() */
assert(mcl_string_length(str) == 13); 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?"); /* Append text to a mcl_string */
assert(ret == 0); mcl_string_append(str, " How are you?");
printf("After append:\n"); puts("After append:");
printf("%s\nsize: %ld, cap: %ld\n", mcl_string_cstr(str), (long)mcl_string_length(str), (long)mcl_string_capacity(str)); c_str = mcl_string_cstr(str);
assert(mcl_string_length(str) == 26); 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); mcl_string_free(str);
printf("All tests passed successfully.\n");
return 0; return 0;
} }