diff --git a/examples/deque/README.md b/examples/deque/README.md new file mode 100644 index 0000000..1235002 --- /dev/null +++ b/examples/deque/README.md @@ -0,0 +1,3 @@ +# Deque examples + +* even_odd.c - separates odd numbers to one end of the deque and even number to the other. Sum only half of it to give the total sum of even numbers. diff --git a/examples/deque/even_odd.c b/examples/deque/even_odd.c new file mode 100644 index 0000000..f588158 --- /dev/null +++ b/examples/deque/even_odd.c @@ -0,0 +1,51 @@ +/** + * even_odd.c + * + * Creation Date: 03/04/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ +#include +#include +#include +#include "deque.h" + +DEQUE_GENERATE(d, deque, /* static */, size_t) + +int main(int argc, char const *argv[]) +{ + // 1- Add odd numbers to the start of the deque and even numbers to the end. + // 2- Iterate over the deque, starting from the end, until index is half + // the deque count and stop. + // 3- Sum of all even numbers + + deque *numbers = d_new(1000); + + for (size_t i = 1; i <= 100000; i++) + { + i % 2 == 0 ? d_push_back(numbers, i) : d_push_front(numbers, i); + } + + deque_iter iter; + d_iter_new(&iter, numbers); + + size_t result, index, sum = 0; + + for (d_iter_toend(&iter); !d_iter_start(&iter);) + { + d_iter_prev(&iter, &result, &index); + + if ((double)index < (double)d_count(numbers) / 2.0) + break; + + sum += result; + } + + printf("Total sum: %lu\n", sum); + + assert(sum == 2500050000); + + return 0; +} diff --git a/examples/hashmap/README.md b/examples/hashmap/README.md new file mode 100644 index 0000000..ac07aca --- /dev/null +++ b/examples/hashmap/README.md @@ -0,0 +1,3 @@ +# Hashmap examples + +Currently none diff --git a/examples/hashset/README.md b/examples/hashset/README.md new file mode 100644 index 0000000..8fe249f --- /dev/null +++ b/examples/hashset/README.md @@ -0,0 +1,3 @@ +# Hashset examples + +* set_ops.c - Demonstrates common set operations like *union*, *intersection*, *difference* and *symmetric difference*. diff --git a/examples/set_ops.c b/examples/hashset/set_ops.c similarity index 81% rename from examples/set_ops.c rename to examples/hashset/set_ops.c index f794f0f..af6a04c 100644 --- a/examples/set_ops.c +++ b/examples/hashset/set_ops.c @@ -1,9 +1,25 @@ +/** + * set_ops.c + * + * Creation Date: 03/04/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ + +// Demonstration of set operations like: +// - Union +// - Intersection +// - Difference +// - Symmetric Difference + #include #include -#include "../src/macro_collections.h" +#include "hashset.h" // change to hashset to have the same effects -COLLECTION_GENERATE(TREESET, PUBLIC, set, set, , , int) +HASHSET_GENERATE(set, set, /* static */, int) void print_set(set *s) { @@ -42,10 +58,8 @@ size_t inthash(int t) int main(int argc, char const *argv[]) { - set *set1 = set_new(intcmp); - set *set2 = set_new(intcmp); - //set *set1 = set_new(50, 0.9, intcmp, inthash); - //set *set2 = set_new(50, 0.9, intcmp, inthash); + set *set1 = set_new(50, 0.9, intcmp, inthash); + set *set2 = set_new(50, 0.9, intcmp, inthash); for (int i = 1; i <= 20; i++) set_insert(set1, i); diff --git a/examples/heap/README.md b/examples/heap/README.md new file mode 100644 index 0000000..0bba70f --- /dev/null +++ b/examples/heap/README.md @@ -0,0 +1,3 @@ +# Heap examples + +* heapsort.c - Uses the heap to sort the elements in a given array. diff --git a/examples/heapsort.c b/examples/heap/heapsort.c similarity index 84% rename from examples/heapsort.c rename to examples/heap/heapsort.c index 3a76a7d..3ac1896 100644 --- a/examples/heapsort.c +++ b/examples/heap/heapsort.c @@ -1,5 +1,14 @@ +/** + * heapsort.c + * + * Creation Date: 25/03/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ #include -#include "../src/heap.h" +#include "heap.h" static int intcmp(int a, int b) { diff --git a/examples/linkedlist/README.md b/examples/linkedlist/README.md new file mode 100644 index 0000000..693d154 --- /dev/null +++ b/examples/linkedlist/README.md @@ -0,0 +1,4 @@ +# Linkedlist examples + +* iterator.c - Example of using a linked list iterator. +* linked_list_node.c - Example of using functions that add or remove elements relative to a node. diff --git a/examples/iterator.c b/examples/linkedlist/iterator.c similarity index 80% rename from examples/iterator.c rename to examples/linkedlist/iterator.c index e26cace..bfe06dc 100644 --- a/examples/iterator.c +++ b/examples/linkedlist/iterator.c @@ -1,10 +1,16 @@ +/** + * iterator.c + * + * Creation Date: 22/03/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ #include -#include -#include -#include -#include "../src/macro_collections.h" +#include "linkedlist.h" -COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, static, , int) +LINKEDLIST_GENERATE(ll, linked, static, int) int main(int argc, char const *argv[]) { diff --git a/examples/linked_list_node.c b/examples/linkedlist/linked_list_node.c similarity index 82% rename from examples/linked_list_node.c rename to examples/linkedlist/linked_list_node.c index 922ef61..a81c167 100644 --- a/examples/linked_list_node.c +++ b/examples/linkedlist/linked_list_node.c @@ -1,8 +1,17 @@ +/** + * linked_list_node.c + * + * Creation Date: 26/03/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ #include #include -#include "../src/linkedlist.h" +#include "linkedlist.h" -LINKEDLIST_GENERATE(l, list, , int) +LINKEDLIST_GENERATE(l, list, /* static */, int) int main(int argc, char const *argv[]) { diff --git a/examples/list/README.md b/examples/list/README.md new file mode 100644 index 0000000..2219c59 --- /dev/null +++ b/examples/list/README.md @@ -0,0 +1,3 @@ +# List examples + +Currently none diff --git a/examples/others/README.md b/examples/others/README.md new file mode 100644 index 0000000..8a105a9 --- /dev/null +++ b/examples/others/README.md @@ -0,0 +1,5 @@ +# Other examples + +* book_store - A book store where its books are loaded onto a list from a csv file. The newly added ones go to a queue of featured books where old ones are removed to give place to new ones. You can find a book by looking up its ISBN using a treemap or traversing the treemap and find all ISBNs of a corresponding title. +* separate_header_source - Example of how to used `PUBLIC` and `PRIVATE` parameters. +* for_each.c - Using the `FOR_EACH`. diff --git a/examples/book_store/book.c b/examples/others/book_store/book.c similarity index 100% rename from examples/book_store/book.c rename to examples/others/book_store/book.c diff --git a/examples/book_store/books.csv b/examples/others/book_store/books.csv similarity index 100% rename from examples/book_store/books.csv rename to examples/others/book_store/books.csv diff --git a/examples/book_store/main.c b/examples/others/book_store/main.c similarity index 98% rename from examples/book_store/main.c rename to examples/others/book_store/main.c index 5d1232f..21351f9 100644 --- a/examples/book_store/main.c +++ b/examples/others/book_store/main.c @@ -5,7 +5,7 @@ #include #include #include -#include "../../src/macro_collections.h" +#include "macro_collections.h" #include "book.c" #include "random.c" diff --git a/examples/book_store/random.c b/examples/others/book_store/random.c similarity index 100% rename from examples/book_store/random.c rename to examples/others/book_store/random.c diff --git a/examples/for_each.c b/examples/others/for_each.c similarity index 98% rename from examples/for_each.c rename to examples/others/for_each.c index d48972f..5316f11 100644 --- a/examples/for_each.c +++ b/examples/others/for_each.c @@ -1,7 +1,7 @@ #include #include #include -#include "../src/macro_collections.h" +#include "macro_collections.h" // Create containers to use only in this file COLLECTION_GENERATE(LIST, PUBLIC, l, list, static, , int) diff --git a/examples/separate_header_source/Makefile b/examples/others/separate_header_source/Makefile similarity index 93% rename from examples/separate_header_source/Makefile rename to examples/others/separate_header_source/Makefile index 6138a8d..d72e379 100644 --- a/examples/separate_header_source/Makefile +++ b/examples/others/separate_header_source/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Wextra INCLUDE = ./ -SRC = ../../src +SRC = ../../../src main.exe: containers.o main.o $(SRC)/macro_collections.h $(CC) $^ -o $@ diff --git a/examples/separate_header_source/containers.c b/examples/others/separate_header_source/containers.c similarity index 100% rename from examples/separate_header_source/containers.c rename to examples/others/separate_header_source/containers.c diff --git a/examples/separate_header_source/containers.h b/examples/others/separate_header_source/containers.h similarity index 100% rename from examples/separate_header_source/containers.h rename to examples/others/separate_header_source/containers.h diff --git a/examples/separate_header_source/main.c b/examples/others/separate_header_source/main.c similarity index 100% rename from examples/separate_header_source/main.c rename to examples/others/separate_header_source/main.c diff --git a/examples/queue/README.md b/examples/queue/README.md new file mode 100644 index 0000000..5cc036d --- /dev/null +++ b/examples/queue/README.md @@ -0,0 +1,3 @@ +# Queue examples + +* ice_cream.c - Example of what happens when you get late to the queue and the ice creams are sold out! diff --git a/examples/queue/ice_cream.c b/examples/queue/ice_cream.c new file mode 100644 index 0000000..9678d85 --- /dev/null +++ b/examples/queue/ice_cream.c @@ -0,0 +1,76 @@ +/** + * ice_cream.c + * + * Creation Date: 03/04/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ +#include +#include +#include +#include "queue.h" + +QUEUE_GENERATE(q, queue, /* static */, const char *) + +int main(int argc, char const *argv[]) +{ + srand((unsigned)time(NULL)); + + // 1- People are lining up to get some ice cream but there is not enough + // for everyone! Hurry! Get in the queue! + + size_t total_icecreams = 20; + + queue *lineup = q_new(10); + + q_enqueue(lineup, "Albert"); + q_enqueue(lineup, "Newton"); + q_enqueue(lineup, "Marie"); + q_enqueue(lineup, "Charles"); + q_enqueue(lineup, "Nikola"); + q_enqueue(lineup, "Thomas"); + q_enqueue(lineup, "Niels"); + q_enqueue(lineup, "Galileo"); + q_enqueue(lineup, "Stephen"); + q_enqueue(lineup, "Leonardo"); + q_enqueue(lineup, "Max"); + q_enqueue(lineup, "Dmitri"); + + while (total_icecreams > 0) + { + size_t catch = rand() % 5; + + const char *name = q_peek(lineup); + + if (!q_dequeue(lineup)) + break; + + printf("%-8s got %lu ice cream(s) from the ice cream truck\n", name, catch); + + total_icecreams = catch > total_icecreams ? 0 : total_icecreams - catch; + } + + if (!q_empty(lineup)) + { + printf("\nOh no! Someone didn't get their ice cream!\n\n"); + + while (!q_empty(lineup)) + { + const char *name = q_peek(lineup); + + q_dequeue(lineup); + + printf("%-8s got no ice cream and he is sad\n", name); + } + } + else if (total_icecreams > 0) + { + printf("\n%lu ice creams remaining to be sold on the next day.\n"); + } + + q_free(lineup); + + return 0; +} diff --git a/examples/stack/README.md b/examples/stack/README.md new file mode 100644 index 0000000..36b1c1a --- /dev/null +++ b/examples/stack/README.md @@ -0,0 +1,3 @@ +# Stack examples + +* hanoi.c - Animated example of solving the hanoi tower. diff --git a/examples/stack/hanoi.c b/examples/stack/hanoi.c new file mode 100644 index 0000000..1eeac16 --- /dev/null +++ b/examples/stack/hanoi.c @@ -0,0 +1,110 @@ +/** + * hanoi.c + * + * Creation Date: 03/04/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ +#include +#include +#include "stack.h" + +STACK_GENERATE(s, stack, /* static */, int) + +#define MAX(a, b) a > b ? a : b + +void print_hanoi(stack *t1, stack *t2, stack *t3); +const char *draw(int value); +void move_disc(stack *from, stack *to); +void move_stack(stack *t1, stack *t2, stack *t3, size_t size); + +int main(int argc, char const *argv[]) +{ + // 1- Hanoi towers showcase + + stack *tower1 = s_new(10); + stack *tower2 = s_new(10); + stack *tower3 = s_new(10); + + for (int i = 10; i > 0; i--) + s_push(tower1, i); + + move_stack(tower1, tower3, tower2, s_count(tower1)); + + s_free(tower1); + s_free(tower2); + s_free(tower3); + + return 0; +} + +void print_hanoi(stack *t1, stack *t2, stack *t3) +{ + const int max = 10; + + static int step = 0; + printf("Step %d\n", ++step); + + for (int i = max - 1; i >= 0; i--) + { + + printf(" %10s|%-10s %10s|%-10s %10s|%-10s\n", + draw(t1->buffer[i]), draw(t1->buffer[i]), + draw(t2->buffer[i]), draw(t2->buffer[i]), + draw(t3->buffer[i]), draw(t3->buffer[i])); + } + printf(" ----------------------- ----------------------- -----------------------\n\n"); +} + +const char *draw(int value) +{ + switch (value) + { + case 0: + return ""; + case 1: + return "#"; + case 2: + return "##"; + case 3: + return "###"; + case 4: + return "####"; + case 5: + return "#####"; + case 6: + return "######"; + case 7: + return "#######"; + case 8: + return "########"; + case 9: + return "#########"; + case 10: + return "##########"; + default: + return "?"; + } +} + +void move_disc(stack *from, stack *to) +{ + s_push(to, s_top(from)); + s_pop(from); +} + +void move_stack(stack *t1, stack *t2, stack *t3, size_t size) +{ + if (size == 0) + return; + + move_stack(t1, t3, t2, size - 1); + + move_disc(t1, t2); + + print_hanoi(t1, t2, t3); + + move_stack(t3, t2, t1, size - 1); +} diff --git a/examples/treemap/README.md b/examples/treemap/README.md new file mode 100644 index 0000000..39843aa --- /dev/null +++ b/examples/treemap/README.md @@ -0,0 +1,3 @@ +# Treemap examples + +Currently none diff --git a/examples/treeset/README.md b/examples/treeset/README.md new file mode 100644 index 0000000..73e791a --- /dev/null +++ b/examples/treeset/README.md @@ -0,0 +1,4 @@ +# Treeset examples + +* set_ops.c - Demonstrates common set operations like *union*, *intersection*, *difference* and *symmetric difference*. +* tree_print.c - How to print a binary tree. diff --git a/examples/treeset/set_ops.c b/examples/treeset/set_ops.c new file mode 100644 index 0000000..0857727 --- /dev/null +++ b/examples/treeset/set_ops.c @@ -0,0 +1,103 @@ +/** + * set_ops.c + * + * Creation Date: 27/03/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ + +// Demonstration of set operations like: +// - Union +// - Intersection +// - Difference +// - Symmetric Difference + +#include +#include +#include "treeset.h" + +TREESET_GENERATE(set, set, /* static */, int) + +void print_set(set *s) +{ + size_t index; + int result; + set_iter iter; + set_iter_new(&iter, s); + for (set_iter_tostart(&iter); !set_iter_end(&iter);) + { + set_iter_next(&iter, &result, &index); + if (index == 0) + printf("[ %2d, ", result); + else if (index == s->count - 1) + printf("%2d ]\n", result); + else + printf("%2d, ", result); + } +} + +int intcmp(int a, int b) +{ + return a - b; +} + +int main(int argc, char const *argv[]) +{ + set *set1 = set_new(intcmp); + set *set2 = set_new(intcmp); + + for (int i = 1; i <= 20; i++) + set_insert(set1, i); + + for (int i = 11; i <= 30; i++) + set_insert(set2, i); + + set *set3 = set_union(set1, set2); + + print_set(set1); + printf("UNION\n"); + print_set(set2); + printf("=\n"); + print_set(set3); + + printf("\n\n"); + + set *set4 = set_intersection(set1, set2); + + print_set(set1); + printf("INTERSECTION\n"); + print_set(set2); + printf("=\n"); + print_set(set4); + + printf("\n\n"); + + set *set5 = set_difference(set1, set2); + + print_set(set1); + printf("DIFFERENCE\n"); + print_set(set2); + printf("=\n"); + print_set(set5); + + printf("\n\n"); + + set *set6 = set_symmetric_difference(set1, set2); + + print_set(set1); + printf("SYMMETRIC DIFFERENCE\n"); + print_set(set2); + printf("=\n"); + print_set(set6); + + set_free(set1); + set_free(set2); + set_free(set3); + set_free(set4); + set_free(set5); + set_free(set6); + + return 0; +} diff --git a/examples/tree_print.c b/examples/treeset/tree_print.c similarity index 81% rename from examples/tree_print.c rename to examples/treeset/tree_print.c index 5029583..10d3d38 100644 --- a/examples/tree_print.c +++ b/examples/treeset/tree_print.c @@ -1,7 +1,16 @@ +/** + * tree_print.c + * + * Creation Date: 27/03/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ #include #include #include -#include "../src/treeset.h" +#include "treeset.h" TREESET_GENERATE(set, set, , int)