From f388ed68085178271aab7d8f113ad7ccc56d9edd Mon Sep 17 00:00:00 2001 From: TryExceptElse Date: Thu, 21 Nov 2019 00:27:30 -0800 Subject: [PATCH 1/3] Correct year in comment. --- src/macro_collections.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macro_collections.h b/src/macro_collections.h index 593291a..6845934 100644 --- a/src/macro_collections.h +++ b/src/macro_collections.h @@ -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 */ From db7af704a6cbc67149a6b3b21fb1b0be8b4425d8 Mon Sep 17 00:00:00 2001 From: TryExceptElse Date: Thu, 21 Nov 2019 00:31:00 -0800 Subject: [PATCH 2/3] Update examples. --- examples/deque/even_odd.c | 4 +- examples/deque/iterator.c | 19 ++++--- examples/hashmap/foreach.c | 8 +-- examples/hashmap/iterator.c | 7 ++- examples/hashset/iterator.c | 6 ++- examples/hashset/set_ops.c | 17 ++++--- examples/heap/heapsort.c | 7 ++- examples/heap/iterator.c | 7 ++- examples/iterators/deque.c | 6 ++- examples/iterators/hashmap.c | 7 ++- examples/iterators/hashset.c | 7 ++- examples/iterators/heap.c | 10 ++-- examples/iterators/linkedlist.c | 7 ++- examples/iterators/list.c | 7 ++- examples/iterators/queue.c | 7 ++- examples/iterators/stack.c | 7 ++- examples/iterators/treemap.c | 7 ++- examples/iterators/treeset.c | 7 ++- examples/linkedlist/iterator.c | 2 +- examples/linkedlist/linked_list_node.c | 2 +- examples/list/iterator.c | 7 ++- examples/list/list.c | 11 ++-- examples/list/string.c | 31 ++++++------ examples/multimap/multimap.c | 34 +++++++++---- examples/multiset/multiset_operations.c | 25 ++++++---- examples/others/for_each.c | 50 +++++++++---------- .../separate_header_source/containers.c | 8 +-- .../separate_header_source/containers.h | 15 ++++-- examples/others/separate_header_source/main.c | 32 ++++++------ examples/others/word_counter/counter.c | 4 +- examples/queue/ice_cream.c | 7 ++- examples/queue/iterator.c | 7 ++- examples/stack/hanoi.c | 11 ++-- examples/stack/iterator.c | 7 ++- examples/treemap/iterator.c | 7 ++- examples/treemap/pseudo_multiset.c | 8 ++- examples/treeset/iterator.c | 7 ++- examples/treeset/set_ops.c | 17 ++++--- examples/treeset/tree_print.c | 8 ++- 39 files changed, 278 insertions(+), 169 deletions(-) diff --git a/examples/deque/even_odd.c b/examples/deque/even_odd.c index 47092c2..1a741b1 100644 --- a/examples/deque/even_odd.c +++ b/examples/deque/even_odd.c @@ -12,7 +12,9 @@ #include #include -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[]) { diff --git a/examples/deque/iterator.c b/examples/deque/iterator.c index c21837f..3557880 100644 --- a/examples/deque/iterator.c +++ b/examples/deque/iterator.c @@ -13,7 +13,9 @@ #include // 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,12 @@ 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 +173,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 +196,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; } diff --git a/examples/hashmap/foreach.c b/examples/hashmap/foreach.c index fde60ec..5835306 100644 --- a/examples/hashmap/foreach.c +++ b/examples/hashmap/foreach.c @@ -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; } diff --git a/examples/hashmap/iterator.c b/examples/hashmap/iterator.c index f63d74c..d3a6260 100644 --- a/examples/hashmap/iterator.c +++ b/examples/hashmap/iterator.c @@ -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; } diff --git a/examples/hashset/iterator.c b/examples/hashset/iterator.c index 57c2f26..ec99501 100644 --- a/examples/hashset/iterator.c +++ b/examples/hashset/iterator.c @@ -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; } diff --git a/examples/hashset/set_ops.c b/examples/hashset/set_ops.c index c8b59fe..7e1e1a1 100644 --- a/examples/hashset/set_ops.c +++ b/examples/hashset/set_ops.c @@ -18,7 +18,10 @@ #include #include -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; } diff --git a/examples/heap/heapsort.c b/examples/heap/heapsort.c index 43f6da6..4064e1a 100644 --- a/examples/heap/heapsort.c +++ b/examples/heap/heapsort.c @@ -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; } diff --git a/examples/heap/iterator.c b/examples/heap/iterator.c index 9f6dac4..7d85c74 100644 --- a/examples/heap/iterator.c +++ b/examples/heap/iterator.c @@ -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; } diff --git a/examples/iterators/deque.c b/examples/iterators/deque.c index a9241b9..ecd6952 100644 --- a/examples/iterators/deque.c +++ b/examples/iterators/deque.c @@ -10,7 +10,9 @@ #include "cmc/deque.h" #include -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; } diff --git a/examples/iterators/hashmap.c b/examples/iterators/hashmap.c index 179d816..3167a84 100644 --- a/examples/iterators/hashmap.c +++ b/examples/iterators/hashmap.c @@ -10,7 +10,10 @@ #include "cmc/hashmap.h" #include -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; } diff --git a/examples/iterators/hashset.c b/examples/iterators/hashset.c index 88033e9..3552017 100644 --- a/examples/iterators/hashset.c +++ b/examples/iterators/hashset.c @@ -10,7 +10,10 @@ #include "cmc/hashset.h" #include -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; } diff --git a/examples/iterators/heap.c b/examples/iterators/heap.c index f86b82f..7eac514 100644 --- a/examples/iterators/heap.c +++ b/examples/iterators/heap.c @@ -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; } diff --git a/examples/iterators/linkedlist.c b/examples/iterators/linkedlist.c index 02c5627..de5ceba 100644 --- a/examples/iterators/linkedlist.c +++ b/examples/iterators/linkedlist.c @@ -10,7 +10,10 @@ #include "cmc/linkedlist.h" #include -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; } diff --git a/examples/iterators/list.c b/examples/iterators/list.c index cd4ca91..622d871 100644 --- a/examples/iterators/list.c +++ b/examples/iterators/list.c @@ -10,7 +10,10 @@ #include "cmc/list.h" #include -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; } diff --git a/examples/iterators/queue.c b/examples/iterators/queue.c index cac9eb6..7457c43 100644 --- a/examples/iterators/queue.c +++ b/examples/iterators/queue.c @@ -10,7 +10,10 @@ #include "cmc/queue.h" #include -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; } diff --git a/examples/iterators/stack.c b/examples/iterators/stack.c index a2219f3..c8f8d41 100644 --- a/examples/iterators/stack.c +++ b/examples/iterators/stack.c @@ -10,7 +10,10 @@ #include "cmc/stack.h" #include -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; } diff --git a/examples/iterators/treemap.c b/examples/iterators/treemap.c index f3bbd3d..a8821b4 100644 --- a/examples/iterators/treemap.c +++ b/examples/iterators/treemap.c @@ -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; } diff --git a/examples/iterators/treeset.c b/examples/iterators/treeset.c index 048c035..42dbc61 100644 --- a/examples/iterators/treeset.c +++ b/examples/iterators/treeset.c @@ -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; } diff --git a/examples/linkedlist/iterator.c b/examples/linkedlist/iterator.c index be8c424..c7c40d0 100644 --- a/examples/linkedlist/iterator.c +++ b/examples/linkedlist/iterator.c @@ -13,7 +13,7 @@ #include // Creates a linkedlist of integers -LINKEDLIST_GENERATE(ll, linkedlist, /* FMOD */, int) +CMC_GENERATE_LINKEDLIST((ll, linkedlist, int) int main(int argc, char const *argv[]) { diff --git a/examples/linkedlist/linked_list_node.c b/examples/linkedlist/linked_list_node.c index 5b23955..f8139e6 100644 --- a/examples/linkedlist/linked_list_node.c +++ b/examples/linkedlist/linked_list_node.c @@ -12,7 +12,7 @@ #include #include -LINKEDLIST_GENERATE(l, list, /* static */, int) +CMC_GENERATE_LINKEDLIST((l, list, int) int main(void) { diff --git a/examples/list/iterator.c b/examples/list/iterator.c index 5ad2777..2187110 100644 --- a/examples/list/iterator.c +++ b/examples/list/iterator.c @@ -13,7 +13,10 @@ #include // 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; } diff --git a/examples/list/list.c b/examples/list/list.c index 0d9a26e..25ca5bb 100644 --- a/examples/list/list.c +++ b/examples/list/list.c @@ -10,7 +10,10 @@ #include "cmc/list.h" #include -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; } diff --git a/examples/list/string.c b/examples/list/string.c index a745c2b..7c7b20e 100644 --- a/examples/list/string.c +++ b/examples/list/string.c @@ -11,7 +11,10 @@ #include #include -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; } diff --git a/examples/multimap/multimap.c b/examples/multimap/multimap.c index 7c5a851..b959015 100644 --- a/examples/multimap/multimap.c +++ b/examples/multimap/multimap.c @@ -1,5 +1,5 @@ -#include "multimap.h" -#include "foreach.h" +#include "cmc/multimap.h" +#include "utl/foreach.h" #include #include #include @@ -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; } diff --git a/examples/multiset/multiset_operations.c b/examples/multiset/multiset_operations.c index 3208e5d..01022d6 100644 --- a/examples/multiset/multiset_operations.c +++ b/examples/multiset/multiset_operations.c @@ -7,12 +7,15 @@ * Leonardo Vencovsky (https://github.com/LeoVen) * */ -#include +#include #include #include #include -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; } diff --git a/examples/others/for_each.c b/examples/others/for_each.c index 23c43f1..9cfa5b2 100644 --- a/examples/others/for_each.c +++ b/examples/others/for_each.c @@ -4,12 +4,12 @@ #include // 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; } diff --git a/examples/others/separate_header_source/containers.c b/examples/others/separate_header_source/containers.c index a33268e..04a01c5 100644 --- a/examples/others/separate_header_source/containers.c +++ b/examples/others/separate_header_source/containers.c @@ -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) diff --git a/examples/others/separate_header_source/containers.h b/examples/others/separate_header_source/containers.h index 244011a..c53e400 100644 --- a/examples/others/separate_header_source/containers.h +++ b/examples/others/separate_header_source/containers.h @@ -1,9 +1,14 @@ #include #include #include -#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) diff --git a/examples/others/separate_header_source/main.c b/examples/others/separate_header_source/main.c index a877c76..ce82d86 100644 --- a/examples/others/separate_header_source/main.c +++ b/examples/others/separate_header_source/main.c @@ -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); }); diff --git a/examples/others/word_counter/counter.c b/examples/others/word_counter/counter.c index c3239aa..c3b103e 100644 --- a/examples/others/word_counter/counter.c +++ b/examples/others/word_counter/counter.c @@ -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) diff --git a/examples/queue/ice_cream.c b/examples/queue/ice_cream.c index 289c0eb..1e55a60 100644 --- a/examples/queue/ice_cream.c +++ b/examples/queue/ice_cream.c @@ -12,7 +12,10 @@ #include #include -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; } diff --git a/examples/queue/iterator.c b/examples/queue/iterator.c index 9a41459..377b2e4 100644 --- a/examples/queue/iterator.c +++ b/examples/queue/iterator.c @@ -13,7 +13,10 @@ #include // 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; } diff --git a/examples/stack/hanoi.c b/examples/stack/hanoi.c index 72d9b1d..7f05832 100644 --- a/examples/stack/hanoi.c +++ b/examples/stack/hanoi.c @@ -11,7 +11,10 @@ #include #include -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; } diff --git a/examples/stack/iterator.c b/examples/stack/iterator.c index c0ac391..c7074f3 100644 --- a/examples/stack/iterator.c +++ b/examples/stack/iterator.c @@ -13,7 +13,10 @@ #include // 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; } diff --git a/examples/treemap/iterator.c b/examples/treemap/iterator.c index d19e2a0..32feb9f 100644 --- a/examples/treemap/iterator.c +++ b/examples/treemap/iterator.c @@ -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; } diff --git a/examples/treemap/pseudo_multiset.c b/examples/treemap/pseudo_multiset.c index 63470b9..984d666 100644 --- a/examples/treemap/pseudo_multiset.c +++ b/examples/treemap/pseudo_multiset.c @@ -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; } diff --git a/examples/treeset/iterator.c b/examples/treeset/iterator.c index 8a39c0e..ea968e3 100644 --- a/examples/treeset/iterator.c +++ b/examples/treeset/iterator.c @@ -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; } diff --git a/examples/treeset/set_ops.c b/examples/treeset/set_ops.c index a4dceeb..602daf6 100644 --- a/examples/treeset/set_ops.c +++ b/examples/treeset/set_ops.c @@ -18,7 +18,10 @@ #include #include -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; } diff --git a/examples/treeset/tree_print.c b/examples/treeset/tree_print.c index 04194fc..b65a7d9 100644 --- a/examples/treeset/tree_print.c +++ b/examples/treeset/tree_print.c @@ -12,7 +12,11 @@ #include #include -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; } From 994a168364f73608ddf5c17adfb064943dd6947d Mon Sep 17 00:00:00 2001 From: TryExceptElse Date: Fri, 22 Nov 2019 00:27:45 -0800 Subject: [PATCH 3/3] Remove brackets to be consistent with surrounding code. --- examples/deque/iterator.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/deque/iterator.c b/examples/deque/iterator.c index 3557880..6c49785 100644 --- a/examples/deque/iterator.c +++ b/examples/deque/iterator.c @@ -26,11 +26,10 @@ int main(int argc, char const *argv[]) for (int i = 0; i < 20; i++) { // Add odd numbers to the front, even numbers to the back. - if (i % 2 == 0) { + if (i % 2 == 0) d_push_front(my_deque, i); - } else { + else d_push_back(my_deque, i); - } } // Iterate from the start (front index) to the end (rear index) of the deque