Update examples.

This commit is contained in:
TryExceptElse committed 2019-11-22 00:11:01 -08:00
1 parent f388ed6808
commit db7af704a6
39 files changed
+278 -169

No files matched your search

+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)