fix: examples
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
#define MAX_STR_LEN 64
|
||||
|
||||
/* My custom data type stored as value */
|
||||
struct my_custom_type_t {
|
||||
struct my_custom_type {
|
||||
int age;
|
||||
char favourite_brand[MAX_STR_LEN];
|
||||
};
|
||||
@@ -41,7 +41,7 @@ bool my_equal_fun(const void *key_a, const void *key_b) {
|
||||
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;
|
||||
struct my_custom_type *mct = (struct my_custom_type *)value;
|
||||
free(mct);
|
||||
}
|
||||
|
||||
@@ -51,10 +51,10 @@ int main(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 *map = mcl_hm_init(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size);
|
||||
mcl_hashmap_s *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 = {
|
||||
struct my_custom_type p1 = {
|
||||
.age = 21,
|
||||
};
|
||||
strncpy(p1.favourite_brand, "Ferrari", sizeof(p1.favourite_brand));
|
||||
@@ -62,9 +62,9 @@ int main(void) {
|
||||
mcl_hm_set(map, "John", &p1);
|
||||
|
||||
/* Retrieve the data */
|
||||
mcl_bucket *john = mcl_hm_get(map, "John");
|
||||
mcl_bucket_s *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;
|
||||
struct my_custom_type *john_v = (struct my_custom_type *)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);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
int main(void) {
|
||||
/* Allocate a new queue */
|
||||
/* Always remember to check return values */
|
||||
mcl_queue *queue = mcl_queue_init(3, sizeof(int));
|
||||
mcl_queue_s *queue = mcl_queue_init(3, sizeof(int));
|
||||
|
||||
int val, out;
|
||||
|
||||
@@ -17,25 +17,26 @@ int main(void) {
|
||||
mcl_queue_push(queue, &val);
|
||||
|
||||
/* Retrieve values */
|
||||
int front = *((int *)mcl_queue_get_front(queue));
|
||||
int rear = *((int *)mcl_queue_get_rear(queue));
|
||||
int front, rear;
|
||||
mcl_queue_get_front(queue, &front);
|
||||
mcl_queue_get_rear(queue, &rear);
|
||||
printf("Front: %d, Rear: %d\n", front, rear);
|
||||
|
||||
/* Remove an element from the buffer */
|
||||
mcl_queue_pop(queue, &out);
|
||||
printf("Pop: %d\n", out);
|
||||
|
||||
front = *((int *)mcl_queue_get_front(queue));
|
||||
mcl_queue_get_front(queue, &front);
|
||||
printf("Front after pop: %d\n", front);
|
||||
|
||||
val = 3;
|
||||
mcl_queue_push(queue, &val);
|
||||
|
||||
rear = *((int *)mcl_queue_get_rear(queue));
|
||||
mcl_queue_get_rear(queue, &rear);
|
||||
printf("Rear after push 3: %d\n", rear);
|
||||
|
||||
val = 4;
|
||||
int res = mcl_queue_push(queue, &val);
|
||||
mcl_queue_push(queue, &val);
|
||||
|
||||
/* Clear queue */
|
||||
while (mcl_queue_pop(queue, &out) == 0) {
|
||||
|
||||
@@ -9,15 +9,17 @@ int main(void) {
|
||||
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_s *str = mcl_string_new("Hello, world!", 512);
|
||||
if (str == NULL) {
|
||||
printf("Failed to initialize string");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
free(c_str);
|
||||
printf("%s\nlength: %lld, capacity: %lld\n", c_str, length, capacity);
|
||||
|
||||
/* Append text to a mcl_string */
|
||||
mcl_string_append(str, " How are you?");
|
||||
@@ -26,8 +28,7 @@ int main(void) {
|
||||
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);
|
||||
free(c_str);
|
||||
printf("%s\nsize: %lld, cap: %lld\n", c_str, length, capacity);
|
||||
|
||||
/* Always deallocate memory */
|
||||
mcl_string_free(str);
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../../string/mystring.h"
|
||||
|
||||
int main(void) {
|
||||
mcl_string *str = mcl_string_new("Hello, world!", 0);
|
||||
mcl_string_append(str, " How are you?");
|
||||
|
||||
char *c_str = mcl_string_cstr(str);
|
||||
size_t len = mcl_string_length(str);
|
||||
size_t cap = mcl_string_capacity(str);
|
||||
|
||||
fprintf(stdout, "%s\nlen: %ld\ncapacity: %ld\n", c_str, len, cap);
|
||||
|
||||
mcl_string_free(str);
|
||||
free(c_str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user