Merge pull request #7 from TryExceptElse/master

Update Examples
This commit is contained in:
Leonardo Vencovsky authored and GitHub committed 2019-11-22 14:23:25 -03:00
commit 345cedcfd3
40 files changed
+278 -170

No files matched your search

+3 -1
View File
@@ -12,7 +12,9 @@
#include <stdlib.h>
#include <assert.h>
DEQUE_GENERATE(d, deque, /* static */, size_t)
CMC_GENERATE_DEQUE(d, deque, size_t);
typedef struct deque deque;
typedef struct deque_iter deque_iter;
int main(int argc, char const *argv[])
{
+10 -8
View File
@@ -13,7 +13,9 @@
#include <inttypes.h>
// Creates a deque of integers
DEQUE_GENERATE(d, deque, /* FMOD */, int)
CMC_GENERATE_DEQUE(d, deque, size_t);
typedef struct deque deque;
typedef struct deque_iter deque_iter;
int main(int argc, char const *argv[])
{
@@ -23,11 +25,11 @@ int main(int argc, char const *argv[])
// Add elements to the deque
for (int i = 0; i < 20; i++)
{
// Add odd numbers to the front
d_push_front_if(my_deque, i, !(i % 2 == 0));
// And even numbers to the back
d_push_back_if(my_deque, i, i % 2 == 0);
// Add odd numbers to the front, even numbers to the back.
if (i % 2 == 0)
d_push_front(my_deque, i);
else
d_push_back(my_deque, i);
}
// Iterate from the start (front index) to the end (rear index) of the deque
@@ -170,7 +172,7 @@ int main(int argc, char const *argv[])
for (d_iter_to_start(&it); !d_iter_end(&it); d_iter_next(&it))
{
// Get a reference to the current value
int *r = d_iter_rvalue(&it);
size_t *r = d_iter_rvalue(&it);
if (r != NULL && *r % 3 == 0)
*r = 7;
@@ -193,7 +195,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by d_iter_new()
d_iter_free(iter);
// Dispose the deque created
d_free(my_deque);
d_free(my_deque, NULL);
return 0;
}
+5 -3
View File
@@ -32,7 +32,9 @@ int intcmp(int a, int b)
}
// Generate all the code for the hashmap
HASHMAP_GENERATE(hm, hashmap, /* FMOD */, int, double)
CMC_GENERATE_HASHMAP(hm, hashmap, int, double)
typedef struct hashmap hashmap;
typedef struct hashmap_iter hashmap_iter;
int main(void)
{
@@ -53,13 +55,13 @@ int main(void)
}
// Iterate over the HashMap using FOR_EACH macro
FOR_EACH(hm, hashmap, map, {
CMC_FOR_EACH(hm, hashmap, map, {
// Inside, the variable 'iter' will be available and from it you can access
// the key, value or index.
printf("MAP[%4d] = %.3lf\n", hm_iter_key(&iter), hm_iter_value(&iter));
});
hm_free(map);
hm_free(map, NULL);
return 0;
}
+5 -2
View File
@@ -32,7 +32,10 @@ int intcmp(int a, int b)
}
// Creates a hashmap of integers
HASHMAP_GENERATE(hm, hashmap, /* FMOD */, int, double)
CMC_GENERATE_HASHMAP(hm, hashmap, int, double);
typedef struct hashmap hashmap;
typedef struct hashmap_iter hashmap_iter;
int main(int argc, char const *argv[])
{
@@ -224,7 +227,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by hm_iter_new()
hm_iter_free(iter);
// Dispose the hashmap created
hm_free(my_map);
hm_free(my_map, NULL);
return 0;
}
+4 -2
View File
@@ -32,7 +32,9 @@ int intcmp(int a, int b)
}
// Creates a hashset of integers
HASHSET_GENERATE(hs, hashset, /* FMOD */, int)
CMC_GENERATE_HASHSET(hs, hashset, int)
typedef struct hashset hashset;
typedef struct hashset_iter hashset_iter;
int main(int argc, char const *argv[])
{
@@ -188,7 +190,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by hs_iter_new()
hs_iter_free(iter);
// Dispose the hashset created
hs_free(my_hashset);
hs_free(my_hashset, NULL);
return 0;
}
+10 -7
View File
@@ -18,7 +18,10 @@
#include <stdio.h>
#include <stdlib.h>
HASHSET_GENERATE(set, set, /* static */, int)
CMC_GENERATE_HASHSET(set, set, int)
typedef struct set set;
typedef struct set_iter set_iter;
void print_set(set *s)
{
@@ -104,12 +107,12 @@ int main(int argc, char const *argv[])
printf("=\n");
print_set(set6);
set_free(set1);
set_free(set2);
set_free(set3);
set_free(set4);
set_free(set5);
set_free(set6);
set_free(set1, NULL);
set_free(set2, NULL);
set_free(set3, NULL);
set_free(set4, NULL);
set_free(set5, NULL);
set_free(set6, NULL);
return 0;
}
+5 -2
View File
@@ -15,7 +15,10 @@ static int intcmp(int a, int b)
return a - b;
}
HEAP_GENERATE(h, heap, static, int)
CMC_GENERATE_HEAP(h, heap, int)
typedef struct heap heap;
typedef struct heap_iter heap_iter;
int main(int argc, char const *argv[])
{
@@ -49,7 +52,7 @@ int main(int argc, char const *argv[])
}
free(buff);
h_free(h);
h_free(h, NULL);
return 0;
}
+5 -2
View File
@@ -19,7 +19,10 @@ int intcmp(int a, int b)
}
// Creates a heap of integers
HEAP_GENERATE(h, heap, /* FMOD */, int)
CMC_GENERATE_HEAP(h, heap, int);
typedef struct heap heap;
typedef struct heap_iter heap_iter;
int main(int argc, char const *argv[])
{
@@ -175,7 +178,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by h_iter_new()
h_iter_free(iter);
// Dispose the heap created
h_free(my_heap);
h_free(my_heap, NULL);
return 0;
}
+4 -2
View File
@@ -10,7 +10,9 @@
#include "cmc/deque.h"
#include <stdio.h>
DEQUE_GENERATE(d, deque, static, int)
CMC_GENERATE_DEQUE(d, deque, int);
typedef struct deque deque;
typedef struct deque_iter deque_iter;
int main(int argc, char const *argv[])
{
@@ -54,7 +56,7 @@ int main(int argc, char const *argv[])
printf("D[%2d] = %2d\n", d_iter_index(&iter), d_iter_value(&iter));
}
d_free(d);
d_free(d, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/hashmap.h"
#include <stdio.h>
HASHMAP_GENERATE(hm, hmap, static, int, double)
CMC_GENERATE_HASHMAP(hm, hmap, int, double)
typedef struct hmap hmap;
typedef struct hmap_iter hmap_iter;
size_t inthash(int t)
{
@@ -70,7 +73,7 @@ int main(int argc, char const *argv[])
printf("HM[%2d] = { %2d : %2.2lf }\n", hm_iter_index(&iter), hm_iter_key(&iter), hm_iter_value(&iter));
}
hm_free(hm);
hm_free(hm, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/hashset.h"
#include <stdio.h>
HASHSET_GENERATE(hs, hset, static, int)
CMC_GENERATE_HASHSET(hs, hset, int);
typedef struct hset hset;
typedef struct hset_iter hset_iter;
size_t inthash(int t)
{
@@ -69,7 +72,7 @@ int main(int argc, char const *argv[])
printf("HS[%2d] = %2d\n", hs_iter_index(&iter), hs_iter_value(&iter));
}
hs_free(hs);
hs_free(hs, NULL);
return 0;
}
+7 -3
View File
@@ -15,12 +15,16 @@ int intcmp(int a, int b)
return (a > b) - (a < b);
}
HEAP_GENERATE(h, heap, static, int)
CMC_GENERATE_HEAP(h, heap, int)
typedef struct heap heap;
typedef struct heap_iter heap_iter;
int main(int argc, char const *argv[])
{
// Initialize heap and add some elements
heap *h = h_new(10, MaxHeap, intcmp);
heap *h = h_new(10, cmc_max_heap, intcmp);
for (int i = 0; i < 10; i++)
h_insert(h, i);
@@ -57,7 +61,7 @@ int main(int argc, char const *argv[])
printf("H[%2d] = %2d\n", h_iter_index(&iter), h_iter_value(&iter));
}
h_free(h);
h_free(h, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/linkedlist.h"
#include <stdio.h>
LINKEDLIST_GENERATE(ll, linkedlist, static, int)
CMC_GENERATE_LINKEDLIST(ll, linkedlist, int);
typedef struct linkedlist linkedlist;
typedef struct linkedlist_iter linkedlist_iter;
int main(int argc, char const *argv[])
{
@@ -52,7 +55,7 @@ int main(int argc, char const *argv[])
printf("LL[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
}
ll_free(ll);
ll_free(ll, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/list.h"
#include <stdio.h>
LIST_GENERATE(l, list, static, int)
CMC_GENERATE_LIST(l, list, int);
typedef struct list list;
typedef struct list_iter list_iter;
int main(int argc, char const *argv[])
{
@@ -52,7 +55,7 @@ int main(int argc, char const *argv[])
printf("L[%2d] = %2d\n", l_iter_index(&iter), l_iter_value(&iter));
}
l_free(l);
l_free(l, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/queue.h"
#include <stdio.h>
QUEUE_GENERATE(q, queue, static, int)
CMC_GENERATE_QUEUE(q, queue, int)
typedef struct queue queue;
typedef struct queue_iter queue_iter;
int main(int argc, char const *argv[])
{
@@ -52,7 +55,7 @@ int main(int argc, char const *argv[])
printf("Q[%2d] = %2d\n", q_iter_index(&iter), q_iter_value(&iter));
}
q_free(q);
q_free(q, NULL);
return 0;
}
+5 -2
View File
@@ -10,7 +10,10 @@
#include "cmc/stack.h"
#include <stdio.h>
STACK_GENERATE(s, stack, static, int)
CMC_GENERATE_STACK(s, stack, int)
typedef struct stack stack;
typedef struct stack_iter stack_iter;
int main(int argc, char const *argv[])
{
@@ -52,7 +55,7 @@ int main(int argc, char const *argv[])
printf("S[%2d] = %2d\n", s_iter_index(&iter), s_iter_value(&iter));
}
s_free(s);
s_free(s, NULL);
return 0;
}
+5 -2
View File
@@ -15,7 +15,10 @@ int intcmp(int a, int b)
return (a > b) - (a < b);
}
TREEMAP_GENERATE(tm, tmap, static, int, double)
CMC_GENERATE_TREEMAP(tm, tmap, int, double)
typedef struct tmap tmap;
typedef struct tmap_iter tmap_iter;
int main(int argc, char const *argv[])
{
@@ -58,7 +61,7 @@ int main(int argc, char const *argv[])
printf("TM[%2d] = { %2d : %2.2lf }\n", tm_iter_index(&iter), tm_iter_key(&iter), tm_iter_value(&iter));
}
tm_free(hm);
tm_free(hm, NULL);
return 0;
}
+5 -2
View File
@@ -15,7 +15,10 @@ int intcmp(int a, int b)
return (a > b) - (a < b);
}
TREESET_GENERATE(ts, tset, static, int)
CMC_GENERATE_TREESET(ts, tset, int);
typedef struct tset tset;
typedef struct tset_iter tset_iter;
int main(int argc, char const *argv[])
{
@@ -57,7 +60,7 @@ int main(int argc, char const *argv[])
printf("TS[%2d] = %2d\n", ts_iter_index(&iter), ts_iter_value(&iter));
}
ts_free(ts);
ts_free(ts, NULL);
return 0;
}
+1 -1
View File
@@ -13,7 +13,7 @@
#include <inttypes.h>
// Creates a linkedlist of integers
LINKEDLIST_GENERATE(ll, linkedlist, /* FMOD */, int)
CMC_GENERATE_LINKEDLIST((ll, linkedlist, int)
int main(int argc, char const *argv[])
{
+1 -1
View File
@@ -12,7 +12,7 @@
#include <assert.h>
#include <inttypes.h>
LINKEDLIST_GENERATE(l, list, /* static */, int)
CMC_GENERATE_LINKEDLIST((l, list, int)
int main(void)
{
+5 -2
View File
@@ -13,7 +13,10 @@
#include <inttypes.h>
// Creates a list of integers
LIST_GENERATE(l, list, /* FMOD */, int)
CMC_GENERATE_LIST(l, list, int);
typedef struct list list;
typedef struct list_iter list_iter;
int main(int argc, char const *argv[])
{
@@ -189,7 +192,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by l_iter_new()
l_iter_free(iter);
// Dispose the list created
l_free(my_list);
l_free(my_list, NULL);
return 0;
}
+7 -4
View File
@@ -10,7 +10,10 @@
#include "cmc/list.h"
#include <stdio.h>
LIST_GENERATE(l, list, /* FMOD */, int)
CMC_GENERATE_LIST(l, list, int);
typedef struct list list;
typedef struct list_iter list_iter;
int main(int argc, char const *argv[])
{
@@ -36,7 +39,7 @@ int main(int argc, char const *argv[])
}
// Erase all values from the list
l_clear(my_list);
l_clear(my_list, NULL);
// Now that list is empty, add only even numbers
for (int i = 0; i < 200; i++)
@@ -44,7 +47,7 @@ int main(int argc, char const *argv[])
// Add even numbers to a given index. In this case they are being added
// to the end of the list (count index == push_back). With a condition
// of being even (i % 2 == 0)
bool added = l_push_if(my_list, i, l_count(my_list), i % 2 == 0);
if (i % 2 == 0) l_push_back(my_list, i);
// If added is true, the element was added to the list
}
@@ -69,7 +72,7 @@ int main(int argc, char const *argv[])
}
// Release resources
l_free(my_list);
l_free(my_list, NULL);
return 0;
}
+17 -14
View File
@@ -11,7 +11,10 @@
#include <stdio.h>
#include <string.h>
LIST_GENERATE(str, string, /* FMOD */, char)
CMC_GENERATE_LIST(str, string, char);
typedef struct string string;
typedef struct string_iter string_iter;
// Comparison function
int chrcmp(char a, char b)
@@ -43,13 +46,13 @@ int main(int argc, char const *argv[])
str_push_front(my_str, ' ');
// Add a sequence of characters to the front of the list
str_prepend(my_str, example1, strlen(example1));
str_seq_push_front(my_str, example1, strlen(example1));
// Add a charater to the back of the list
str_push_back(my_str, ' ');
// Add a sequence of characters to the back of the list
str_append(my_str, example3, strlen(example3));
str_seq_push_back(my_str, example3, strlen(example3));
// Iterate through each letter and print it
string_iter iter;
@@ -84,7 +87,7 @@ int main(int argc, char const *argv[])
// Now that we know where the first word is, remove it, including the
// extra space. [from, to] are inclusive.
str_remove(my_str, 0, index);
str_seq_pop_at(my_str, 0, index);
// Print it once again
for (str_iter_to_start(&iter); !str_iter_end(&iter); str_iter_next(&iter))
@@ -101,7 +104,7 @@ int main(int argc, char const *argv[])
index = str_indexof(my_str, ' ', chrcmp, false);
// Extract last word including white space
string *str_out = str_extract(my_str, index, str_count(my_str) - 1);
string *str_out = str_seq_sublist(my_str, index, str_count(my_str) - 1);
if (!str_out)
printf("Could not extract string\n");
@@ -126,8 +129,8 @@ int main(int argc, char const *argv[])
}
// Free strings
str_free(my_str);
str_free(str_out);
str_free(my_str, NULL);
str_free(str_out, NULL);
printf("\n----------------------------------------\n\n");
@@ -135,29 +138,29 @@ int main(int argc, char const *argv[])
// Using prepend, insert, append, remove and extract functions
string *str = str_new(100);
str_prepend(str, "Hello World!", 12);
str_seq_push_front(str, "Hello World!", 12);
print_str(str);
str_insert(str, "Awesome ", 8, 6);
str_seq_push_at(str, "Awesome ", 8, 6);
print_str(str);
str_append(str, " Indigo.", 8);
str_seq_push_back(str, " Indigo.", 8);
print_str(str);
str_remove(str, 14, 20);
str_seq_pop_at(str, 14, 20);
print_str(str);
string *str_res = str_extract(str, 5, 12);
string *str_res = str_seq_sublist(str, 5, 12);
print_str(str);
print_str(str_res);
str_free(str);
str_free(str_res);
str_free(str, NULL);
str_free(str_res, NULL);
return 0;
}
+23 -11
View File
@@ -1,5 +1,5 @@
#include "multimap.h"
#include "foreach.h"
#include "cmc/multimap.h"
#include "utl/foreach.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
@@ -21,7 +21,10 @@ size_t inthash(int t)
return a;
}
MULTIMAP_GENERATE(mm, multi_map, , int, int)
CMC_GENERATE_MULTIMAP(mm, multi_map, int, int);
typedef struct multi_map multi_map;
typedef struct multi_map_iter multi_map_iter;
int main(int argc, char const *argv[])
{
@@ -50,14 +53,19 @@ int main(int argc, char const *argv[])
printf("Sum (unique): %" PRId64 "\n", sum);
FOR_EACH_MAP(mm, multi_map, int, int, map, {
printf("[%3" PRIu64 "] = { %3d : %3d }\n", index, key, value);
})
CMC_FOR_EACH(mm, multi_map, map, {
printf(
"[%3" PRIu64 "] = { %3d : %3d }\n",
iter.index,
iter.curr_entry->key,
iter.curr_entry->value
);
});
printf("Removing all items\n");
for (int i = min; i < max; i++)
if (!mm_remove_all(map, i))
if (!mm_remove_all(map, i, NULL))
printf("ERROR with %d:%d\n", i, i);
sum = 0;
@@ -67,11 +75,15 @@ int main(int argc, char const *argv[])
printf("Sum: %" PRId64 "\n", sum);
FOR_EACH_MAP(mm, multi_map, int, int, map, {
printf("[%2" PRIu64 "] = { %2d : %5d }\n", index, key, value);
})
CMC_FOR_EACH(mm, multi_map, map, {
printf("[%2" PRIu64 "] = { %2d : %5d }\n",
iter.index,
iter.curr_entry->key,
iter.curr_entry->value
);
});
mm_free(map);
mm_free(map, NULL);
return 0;
}
+14 -11
View File
@@ -7,12 +7,15 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <ext/multiset.h>
#include <cmc/multiset.h>
#include <utl/assert.h>
#include <inttypes.h>
#include <stdio.h>
MULTISET_GENERATE(ms, multiset, , char)
CMC_GENERATE_MULTISET(ms, multiset, char);
typedef struct multiset multiset;
typedef struct multiset_iter multiset_iter;
struct pair
{
@@ -105,15 +108,15 @@ int main(int argc, char const *argv[])
printf("\nA SYM DIFF B:\n");
print_set(SD);
ms_free(A);
ms_free(B);
ms_free(U);
ms_free(I);
ms_free(DA);
ms_free(DB);
ms_free(AB);
ms_free(BA);
ms_free(SD);
ms_free(A, NULL);
ms_free(B, NULL);
ms_free(U, NULL);
ms_free(I, NULL);
ms_free(DA, NULL);
ms_free(DB, NULL);
ms_free(AB, NULL);
ms_free(BA, NULL);
ms_free(SD, NULL);
return 0;
}
+25 -25
View File
@@ -4,12 +4,12 @@
#include <stdbool.h>
// Create containers to use only in this file
COLLECTION_GENERATE(LIST, l, list, static, , int)
COLLECTION_GENERATE(LINKEDLIST, ll, llist, static, , int)
COLLECTION_GENERATE(STACK, s, stack, static, , int)
COLLECTION_GENERATE(QUEUE, q, queue, static, , int)
COLLECTION_GENERATE(DEQUE, d, deque, static, , int)
COLLECTION_GENERATE(TREESET, ts, tset, static, , int)
CMC_GENERATE_LIST(l, list, int)
CMC_GENERATE_LINKEDLIST(ll, llist, int)
CMC_GENERATE_STACK(s, stack, int)
CMC_GENERATE_QUEUE(q, queue, int)
CMC_GENERATE_DEQUE(d, deque, int)
CMC_GENERATE_TREESET(ts, tset, int)
int intcmp(int a, int b)
{
@@ -19,12 +19,12 @@ int intcmp(int a, int b)
int main(int argc, char const *argv[])
{
// Initialize all of them with an internal capacity of 100
list *l = l_new(100);
llist *ll = ll_new();
stack *s = s_new(100);
queue *q = q_new(100);
deque *d = d_new(100);
tset *ts = ts_new(intcmp);
struct list *l = l_new(100);
struct llist *ll = ll_new();
struct stack *s = s_new(100);
struct queue *q = q_new(100);
struct deque *d = d_new(100);
struct tset *ts = ts_new(intcmp);
// Add 20 elements to each of them
for (int i = 0; i < 20; i++)
@@ -54,11 +54,11 @@ int main(int argc, char const *argv[])
ts_insert(ts, 24);
ts_insert(ts, 25);
// All FOR_EACH macros contains three available variables:
// All CMC_FOR_EACH macros contains three available variables:
// - value : the elements that changes with each iteration
// - index : iteration index (first iteration is 0 and last is count - 1)
// - iter : iterator struct
FOR_EACH(l, list, l, {
CMC_FOR_EACH(l, list, l, {
int value = l_iter_value(&iter);
size_t index = l_iter_index(&iter);
if (index == 0)
@@ -69,7 +69,7 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
FOR_EACH(s, stack, s, {
CMC_FOR_EACH(s, stack, s, {
int value = s_iter_value(&iter);
size_t index = s_iter_index(&iter);
if (index == 0)
@@ -80,7 +80,7 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
FOR_EACH(q, queue, q, {
CMC_FOR_EACH(q, queue, q, {
int value = q_iter_value(&iter);
size_t index = q_iter_index(&iter);
if (index == 0)
@@ -91,7 +91,7 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
FOR_EACH(d, deque, d, {
CMC_FOR_EACH(d, deque, d, {
int value = d_iter_value(&iter);
size_t index = d_iter_index(&iter);
if (index == 0)
@@ -102,7 +102,7 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
FOR_EACH(ll, llist, ll, {
CMC_FOR_EACH(ll, llist, ll, {
int value = ll_iter_value(&iter);
size_t index = ll_iter_index(&iter);
if (index == 0)
@@ -113,7 +113,7 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
FOR_EACH(ts, tset, ts, {
CMC_FOR_EACH(ts, tset, ts, {
int value = ts_iter_value(&iter);
size_t index = ts_iter_index(&iter);
if (index == 0)
@@ -124,12 +124,12 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
});
l_free(l);
ll_free(ll);
s_free(s);
q_free(q);
d_free(d);
ts_free(ts);
l_free(l, NULL);
ll_free(ll, NULL);
s_free(s, NULL);
q_free(q, NULL);
d_free(d, NULL);
ts_free(ts, NULL);
return 0;
}
@@ -1,6 +1,6 @@
#include "containers.h"
COLLECTION_GENERATE_SOURCE(LIST, il, int_list, , , int)
COLLECTION_GENERATE_SOURCE(STACK, cs, char_stack, , , char)
COLLECTION_GENERATE_SOURCE(QUEUE, queue, index_queue, , , size_t)
COLLECTION_GENERATE_SOURCE(DEQUE, dq, deque, , , int)
CMC_GENERATE_LIST_SOURCE(il, int_list, int)
CMC_GENERATE_STACK_SOURCE(cs, char_stack, char)
CMC_GENERATE_QUEUE_SOURCE(queue, index_queue, size_t)
CMC_GENERATE_DEQUE_SOURCE(dq, deque, int)
@@ -1,9 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "macro_collections.h"
COLLECTION_GENERATE_HEADER(LIST, il, int_list, , , int)
COLLECTION_GENERATE_HEADER(STACK, cs, char_stack, , , char)
COLLECTION_GENERATE_HEADER(QUEUE, queue, index_queue, , , size_t)
COLLECTION_GENERATE_HEADER(DEQUE, dq, deque, , , int)
#include "cmc/list.h"
#include "cmc/stack.h"
#include "cmc/queue.h"
#include "cmc/deque.h"
CMC_GENERATE_LIST_HEADER(il, int_list, int)
CMC_GENERATE_STACK_HEADER(cs, char_stack, char)
CMC_GENERATE_QUEUE_HEADER(queue, index_queue, size_t)
CMC_GENERATE_DEQUE_HEADER(dq, deque, int)
+16 -16
View File
@@ -39,11 +39,11 @@ int main(void)
{
srand((unsigned)time(NULL));
int_list *list = il_new(10);
struct int_list *list = il_new(10);
for (int i = 0; i < 100; i++)
{
il_push(list, i, list->count == 0 ? list->count : i % list->count);
il_push_at(list, i, list->count == 0 ? list->count : i % list->count);
}
printf("\n[ ");
@@ -53,7 +53,7 @@ int main(void)
printf("%d ]\n", il_get(list, list->count - 1));
il_free(list);
il_free(list, NULL);
list = il_new(10);
@@ -92,13 +92,13 @@ int main(void)
}
}
il_free(list);
il_free(list, NULL);
list = il_new(1000);
for (int i = 0; i < 1000; i++)
{
il_push_if(list, i, list->count, i % 2 == 0);
if (i % 2 == 0) il_push_at(list, i, list->count);
}
// Print all even numbers
@@ -113,9 +113,9 @@ int main(void)
printf("%d ]\n", il_get(list, list->count - 1));
}
il_free(list);
il_free(list, NULL);
char_stack *stack = cs_new(100);
struct char_stack *stack = cs_new(100);
for (int i = 0; i < 2000; i++)
{
@@ -136,9 +136,9 @@ int main(void)
printf("\nStack size: %" PRIu64 "", cs_count(stack));
printf("\nStack capacity : %" PRIu64 "\n", cs_capacity(stack));
cs_free(stack);
cs_free(stack, NULL);
index_queue *idxs = queue_new(100);
struct index_queue *idxs = queue_new(100);
size_t sum0 = 0, sum1 = 0, curr;
@@ -174,25 +174,25 @@ int main(void)
if (sum0 == sum1)
printf("\nElements were preserved -> %" PRIu64 " : %" PRIu64 "\n", sum0, sum1);
queue_free(idxs);
queue_free(idxs, NULL);
printf("\nUsing ForEach\n\n");
int_list *integers = il_new(1000);
struct int_list *integers = il_new(1000);
for (size_t i = 0; i < il_capacity(integers); i++)
il_push_back(integers, i);
int sum = 0;
FOR_EACH(il, int_list, integers, {
CMC_FOR_EACH(il, int_list, integers, {
sum += il_iter_value(&iter);
});
printf("\n\nSUM: %d\n", sum);
il_free(integers);
il_free(integers, NULL);
deque *d = dq_new(1000);
struct deque *d = dq_new(1000);
int sum2 = 0, numbers = 0, r = 0;
@@ -245,7 +245,7 @@ int main(void)
printf("Theoretical Sum: 50005000\nTotal Sum: %d\n", sum2);
dq_free(d);
dq_free(d, NULL);
d = dq_new(32);
@@ -254,7 +254,7 @@ int main(void)
int sum3 = 0;
FOR_EACH(dq, deque, d, {
CMC_FOR_EACH(dq, deque, d, {
sum3 += dq_iter_value(&iter);
});
+2 -2
View File
@@ -45,7 +45,7 @@ typedef struct wc_pair_s
SAC_STACK_GENERATE(tl, TList, , pthread_t, n_threads)
/* A list used as a dynamic string */
LIST_GENERATE(str, String, , char)
CMC_GENERATE_LIST((str, String, , char)
/* A mapping between word and word count */
HASHMAP_GENERATE(hm, HashMap, , char *, size_t)
@@ -69,7 +69,7 @@ int chrcmp(char *a, char *b)
}
/* Heap used to sort the { word : word_count } pairs */
HEAP_GENERATE(hp, Heap, , wc_pair)
CMC_GENERATE_HEAP((hp, Heap, wc_pair)
/* Used by Heap to sort wc_pair */
int cmp_wcpair(wc_pair p1, wc_pair p2)
+5 -2
View File
@@ -12,7 +12,10 @@
#include <stdlib.h>
#include <time.h>
QUEUE_GENERATE(q, queue, /* static */, const char *)
CMC_GENERATE_QUEUE(q, queue, const char *);
typedef struct queue queue;
typedef struct queue_iter queue_iter;
int main(int argc, char const *argv[])
{
@@ -70,7 +73,7 @@ int main(int argc, char const *argv[])
printf("\n%lu ice creams remaining to be sold on the next day.\n");
}
q_free(lineup);
q_free(lineup, NULL);
return 0;
}
+5 -2
View File
@@ -13,7 +13,10 @@
#include <inttypes.h>
// Creates a queue of integers
QUEUE_GENERATE(q, queue, /* FMOD */, int)
CMC_GENERATE_QUEUE(q, queue, int);
typedef struct queue queue;
typedef struct queue_iter queue_iter;
int main(int argc, char const *argv[])
{
@@ -189,7 +192,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by q_iter_new()
q_iter_free(iter);
// Dispose the queue created
q_free(my_queue);
q_free(my_queue, NULL);
return 0;
}
+7 -4
View File
@@ -11,7 +11,10 @@
#include <stdio.h>
#include <stdlib.h>
STACK_GENERATE(s, stack, /* static */, int)
CMC_GENERATE_STACK(s, stack, int);
typedef struct stack stack;
typedef struct stack_iter stack_iter;
void print_hanoi(stack *t1, stack *t2, stack *t3);
const char *draw(int value);
@@ -31,9 +34,9 @@ int main(int argc, char const *argv[])
move_stack(tower1, tower3, tower2, s_count(tower1));
s_free(tower1);
s_free(tower2);
s_free(tower3);
s_free(tower1, NULL);
s_free(tower2, NULL);
s_free(tower3, NULL);
return 0;
}
+5 -2
View File
@@ -13,7 +13,10 @@
#include <inttypes.h>
// Creates a stack of integers
STACK_GENERATE(s, stack, /* FMOD */, int)
CMC_GENERATE_STACK(s, stack, int);
typedef struct stack stack;
typedef struct stack_iter stack_iter;
int main(int argc, char const *argv[])
{
@@ -189,7 +192,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by s_iter_new()
s_iter_free(iter);
// Dispose the stack created
s_free(my_stack);
s_free(my_stack, NULL);
return 0;
}
+5 -2
View File
@@ -19,7 +19,10 @@ int intcmp(int a, int b)
}
// Creates a treemap of integers
TREEMAP_GENERATE(tm, treemap, /* FMOD */, int, double)
CMC_GENERATE_TREEMAP(tm, treemap, int, double);
typedef struct treemap treemap;
typedef struct treemap_iter treemap_iter;
int main(int argc, char const *argv[])
{
@@ -211,7 +214,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by tm_iter_new()
tm_iter_free(iter);
// Dispose the treemap created
tm_free(my_map);
tm_free(my_map, NULL);
return 0;
}
+6 -2
View File
@@ -17,7 +17,11 @@
// Mapping an integer by size_t which will represent the key's multiplicity
// (key could be anything and multiplicity could be an integer, preferably
// unsigned)
TREEMAP_GENERATE(tm, tmap, , int, size_t)
CMC_GENERATE_TREEMAP(tm, tmap, int, size_t);
typedef struct tmap tmap;
typedef struct tmap_iter tmap_iter;
typedef struct tmap_node tmap_node;
// Compare function used by the treemap
int intcmp(int a, int b)
@@ -80,7 +84,7 @@ int main(int argc, char const *argv[])
printf("Map[%2d] = %" PRIuMAX "\n", tm_iter_key(&it), tm_iter_value(&it));
}
tm_free(map);
tm_free(map, NULL);
return 0;
}
+5 -2
View File
@@ -32,7 +32,10 @@ int intcmp(int a, int b)
}
// Creates a treeset of integers
TREESET_GENERATE(ts, treeset, /* FMOD */, int)
CMC_GENERATE_TREESET(ts, treeset, int);
typedef struct treeset treeset;
typedef struct treeset_iter treeset_iter;
int main(int argc, char const *argv[])
{
@@ -188,7 +191,7 @@ int main(int argc, char const *argv[])
// Dispose the iterator called by ts_iter_new()
ts_iter_free(iter);
// Dispose the treeset created
ts_free(my_treeset);
ts_free(my_treeset, NULL);
return 0;
}
+10 -7
View File
@@ -18,7 +18,10 @@
#include <stdio.h>
#include <stdlib.h>
TREESET_GENERATE(set, set, /* static */, int)
CMC_GENERATE_TREESET(set, set, int);
typedef struct set set;
typedef struct set_iter set_iter;
void print_set(set *s)
{
@@ -92,12 +95,12 @@ int main(int argc, char const *argv[])
printf("=\n");
print_set(set6);
set_free(set1);
set_free(set2);
set_free(set3);
set_free(set4);
set_free(set5);
set_free(set6);
set_free(set1, NULL);
set_free(set2, NULL);
set_free(set3, NULL);
set_free(set4, NULL);
set_free(set5, NULL);
set_free(set6, NULL);
return 0;
}
+6 -2
View File
@@ -12,7 +12,11 @@
#include <stdlib.h>
#include <time.h>
TREESET_GENERATE(set, set, , int)
CMC_GENERATE_TREESET(set, set, int);
typedef struct set set;
typedef struct set_iter set_iter;
typedef struct set_node set_node;
void print_tree(set_node *root, int height)
{
@@ -45,7 +49,7 @@ int main(int argc, char const *argv[])
print_tree(set1->root, 0);
set_free(set1);
set_free(set1, NULL);
return 0;
}
+1 -1
View File
@@ -46,7 +46,7 @@
#include "utl/assert.h" /* Added in 27/06/2019 */
#include "utl/foreach.h" /* Added in 25/02/2019 */
#include "utl/log.h" /* Added in 21/06/2109 */
#include "utl/log.h" /* Added in 21/06/2019 */
#include "utl/test.h" /* Added in 26/06/2019 */
#include "utl/timer.h" /* Added in 12/04/2019 */