Removed PUBLIC and PRIVATE, corrected some examples and new test

This commit is contained in:
Leonardo committed 2019-05-30 21:23:37 -03:00
1 parent a9b775419d
commit 0f84d65656
39 files changed
+322 -327

No files matched your search

+2 -2
View File
@@ -1,5 +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`.
* separate_header_source - Example of how to compile the header and source separately.
* for_each.c - Using the `FOR_EACH` macro.
+23 -13
View File
@@ -9,11 +9,11 @@
#include "book.c"
#include "random.c"
COLLECTION_GENERATE(LIST, PUBLIC, l, list, , , book *)
COLLECTION_GENERATE(LIST, PUBLIC, il, intlist, , , int)
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, , , book *)
COLLECTION_GENERATE(TREEMAP, PUBLIC, map, map, , int, char *)
COLLECTION_GENERATE(TREESET, PUBLIC, set, set, , , char *)
COLLECTION_GENERATE(LIST, l, list, , , book *)
COLLECTION_GENERATE(LIST, il, intlist, , , int)
COLLECTION_GENERATE(QUEUE, q, queue, , , book *)
COLLECTION_GENERATE(TREEMAP, map, map, , int, char *)
COLLECTION_GENERATE(TREESET, set, set, , , char *)
list *load(void);
void shuffle(list *l);
@@ -48,7 +48,9 @@ int main(int argc, char const *argv[])
set *counter = set_new(chrcmp);
// Add everything to its place.
FOR_EACH(l, list, book *, books, {
FOR_EACH(l, list, books, {
book *value = l_iter_value(&iter);
// Keep the last added elements
if (q_count(shelf) > 7)
{
@@ -64,7 +66,8 @@ int main(int argc, char const *argv[])
printf("Library Size: %d\n", map_count(library));
printf("Unique book titles: %d\n\n", set_count(counter));
printf("Showcase Books:\n");
FOR_EACH(q, queue, book *, shelf, {
FOR_EACH(q, queue, shelf, {
book *value = q_iter_value(&iter);
printf("[ %d : %-45s ]\n", value->isbn, value->name);
})
printf("\n");
@@ -113,13 +116,16 @@ int main(int argc, char const *argv[])
intlist *search_list = il_new(32);
FOR_EACH_MAP(map, map, int, char *, library, {
FOR_EACH(map, map, library, {
int key = map_iter_key(&iter);
char *value = map_iter_value(&iter);
if (chrcmp(buffer, value) == 0)
il_push_back(search_list, key);
})
FOR_EACH(il, intlist, int, search_list, {
printf("[ %d ]\n", value);
FOR_EACH(il, intlist, search_list, {
printf("[ %d ]\n", il_iter_value(&iter));
})
il_free(search_list);
@@ -127,9 +133,13 @@ int main(int argc, char const *argv[])
}
// Free all memory used by struct book
FOR_EACH(l, list, book *, books, {
free(value->name);
free(value);
FOR_EACH(l, list, books, {
book *b = l_iter_value(&iter);
if (b)
free(b->name);
free(b);
})
l_free(books);
+25 -13
View File
@@ -4,16 +4,16 @@
#include "macro_collections.h"
// Create containers to use only in this file
COLLECTION_GENERATE(LIST, PUBLIC, l, list, static, , int)
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, llist, static, , int)
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, static, , int)
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, static, , int)
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, static, , int)
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, static, , int)
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)
int intcmp(int a, int b)
{
return a - b;
return (a > b) - (a < b);
}
int main(int argc, char const *argv[])
@@ -58,7 +58,9 @@ int main(int argc, char const *argv[])
// - 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, int, l, {
FOR_EACH(l, list, l, {
int value = l_iter_value(&iter);
size_t index = l_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -67,7 +69,9 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
})
FOR_EACH(s, stack, int, s, {
FOR_EACH(s, stack, s, {
int value = s_iter_value(&iter);
size_t index = s_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -76,7 +80,9 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
})
FOR_EACH(q, queue, int, q, {
FOR_EACH(q, queue, q, {
int value = q_iter_value(&iter);
size_t index = q_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -85,7 +91,9 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
})
FOR_EACH(d, deque, int, d, {
FOR_EACH(d, deque, d, {
int value = d_iter_value(&iter);
size_t index = d_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -94,7 +102,9 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
})
FOR_EACH(ll, llist, int, ll, {
FOR_EACH(ll, llist, ll, {
int value = ll_iter_value(&iter);
size_t index = ll_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -103,7 +113,9 @@ int main(int argc, char const *argv[])
printf("%d, ", value);
})
FOR_EACH(ts, tset, int, ts, {
FOR_EACH(ts, tset, ts, {
int value = ts_iter_value(&iter);
size_t index = ts_iter_index(&iter);
if (index == 0)
printf("[ %d, ", value);
else if (index == iter.target->count - 1)
@@ -1,6 +1,6 @@
#include "containers.h"
COLLECTION_GENERATE_SOURCE(LIST, PUBLIC, il, int_list, , , int)
COLLECTION_GENERATE_SOURCE(STACK, PRIVATE, cs, char_stack, , , char)
COLLECTION_GENERATE_SOURCE(QUEUE, PUBLIC, queue, index_queue, , , size_t)
COLLECTION_GENERATE_SOURCE(DEQUE, PUBLIC, dq, deque, , , int)
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)
@@ -3,7 +3,7 @@
#include <stdbool.h>
#include "macro_collections.h"
COLLECTION_GENERATE_HEADER(LIST, PUBLIC, il, int_list, , , int)
COLLECTION_GENERATE_HEADER(STACK, PRIVATE, cs, char_stack, , , char)
COLLECTION_GENERATE_HEADER(QUEUE, PUBLIC, queue, index_queue, , , size_t)
COLLECTION_GENERATE_HEADER(DEQUE, PUBLIC, dq, deque, , , int)
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)
@@ -184,9 +184,8 @@ int main(void)
il_push_back(integers, i);
int sum = 0;
FOR_EACH(il, int_list, int, integers, {
printf("%d, ", value);
sum += value;
FOR_EACH(il, int_list, integers, {
sum += il_iter_value(&iter);
})
printf("\n\nSUM: %d\n", sum);
@@ -255,8 +254,8 @@ int main(void)
int sum3 = 0;
FOR_EACH(dq, deque, int, d, {
sum3 += value;
FOR_EACH(dq, deque, d, {
sum3 += dq_iter_value(&iter);
})
printf("Deque sum: %d\n", sum3);