diff options
| author | Tyge Løvset <[email protected]> | 2022-06-17 18:04:37 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-06-17 18:04:37 +0200 |
| commit | 755bcee8a97b2912d047895d37f5b60672486144 (patch) | |
| tree | 86f662acd342394601984ff8e13f61a803c5275b /examples | |
| parent | fb39cd5db944d13b6de110885081e483234b453f (diff) | |
| download | STC-modified-755bcee8a97b2912d047895d37f5b60672486144.tar.gz STC-modified-755bcee8a97b2912d047895d37f5b60672486144.zip | |
Various refactoring. Renamed c_apply_arr() => c_apply_array()
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/cpque.c | 24 | ||||
| -rw-r--r-- | examples/inits.c | 2 | ||||
| -rw-r--r-- | examples/read.c | 2 | ||||
| -rw-r--r-- | examples/sidebyside.cpp | 53 |
4 files changed, 42 insertions, 39 deletions
diff --git a/examples/cpque.c b/examples/cpque.c index 8a3564f0..2c08f796 100644 --- a/examples/cpque.c +++ b/examples/cpque.c @@ -1,20 +1,16 @@ // Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue #include <stdio.h> +#include <stdbool.h> // Example of dynamic compare function -static int (*icmp_fn)(const int* x, const int* y); +static bool (*less_fn)(const int* x, const int* y); #define i_val int -#define i_cmp icmp_fn +#define i_less less_fn #define i_type ipque #include <stc/cpque.h> -#define imix_less(left, right) ((*(left) ^ 1) < (*(right) ^ 1)) -static int imax_cmp(const int* x, const int* y) { return *x - *y; } -static int imin_cmp(const int* x, const int* y) { return *y - *x; } -static int imix_cmp(const int* x, const int* y) { return c_less_cmp(imix_less, x, y); } - void print_queue(ipque q) { ipque copy = ipque_clone(q); while (!ipque_empty(copy)) { @@ -25,23 +21,27 @@ void print_queue(ipque q) { ipque_drop(©); } +static bool int_less(const int* x, const int* y) { return *x < *y; } +static bool int_greater(const int* x, const int* y) { return *x > *y; } +static bool int_lambda(const int* x, const int* y) { return (*x ^ 1) < (*y ^ 1); } + int main() { const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); c_auto (ipque, q, q2, q3) // init() and defered drop() { - icmp_fn = imax_cmp; + less_fn = int_less; c_forrange (i, n) ipque_push(&q, data[i]); print_queue(q); - icmp_fn = imin_cmp; - c_apply_arr(v, ipque_push(&q2, *v), const int, data, n); + less_fn = int_greater; + c_apply_array(v, ipque_push(&q2, *v), const int, data, n); print_queue(q2); - icmp_fn = imix_cmp; - c_apply_arr(v, ipque_push(&q3, *v), const int, data, n); + less_fn = int_lambda; + c_apply_array(v, ipque_push(&q3, *v), const int, data, n); print_queue(q3); } } diff --git a/examples/inits.c b/examples/inits.c index d8114536..dc67075c 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -41,7 +41,7 @@ int main(void) // PRIORITY QUEUE - c_apply_arr(v, cpque_f_push(&floats, *v), const float, nums, c_arraylen(nums)); + c_apply_array(v, cpque_f_push(&floats, *v), const float, nums, c_arraylen(nums)); puts("\npop and show high priorites first:"); while (! cpque_f_empty(floats)) { diff --git a/examples/read.c b/examples/read.c index 60698789..26fc46dd 100644 --- a/examples/read.c +++ b/examples/read.c @@ -9,7 +9,7 @@ cvec_str read_file(const char* name) c_autovar (FILE* f = fopen(name, "r"), fclose(f)) c_autovar (cstr line = cstr_init(), cstr_drop(&line)) while (cstr_getline(&line, f)) - cvec_str_emplace_back(&vec, cstr_str(&line)); + cvec_str_push(&vec, cstr_clone(line)); return vec; } diff --git a/examples/sidebyside.cpp b/examples/sidebyside.cpp index 4d63496b..f2021436 100644 --- a/examples/sidebyside.cpp +++ b/examples/sidebyside.cpp @@ -1,54 +1,57 @@ #include <iostream> #include <map> #include <string> - -#define i_key_str -#define i_val int -#define i_tag si -#include <stc/cmap.h> +#include <stc/cstr.h> #define i_key int #define i_val int #define i_tag ii #include <stc/csmap.h> +#define i_key_str +#define i_val int +#define i_tag si +#include <stc/cmap.h> + int main() { { - std::map<std::string, int> food = - {{"burger", 5}, {"pizza", 12}, {"steak", 15}}; + std::map<int, int> hist; + hist.emplace(12, 100).first->second += 1; + hist.emplace(13, 100).first->second += 1; + hist.emplace(12, 100).first->second += 1; - for (auto i: food) + for (auto i: hist) std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - c_auto (cmap_si, food) + + c_auto (csmap_ii, hist) { - c_apply(v, cmap_si_emplace(&food, c_pair(v)), cmap_si_raw, - {{"burger", 5}, {"pizza", 12}, {"steak", 15}}); + csmap_ii_insert(&hist, 12, 100).ref->second += 1; + csmap_ii_insert(&hist, 13, 100).ref->second += 1; + csmap_ii_insert(&hist, 12, 100).ref->second += 1; - c_foreach (i, cmap_si, food) - printf("%s, %d\n", i.ref->first.str, i.ref->second); + c_foreach (i, csmap_ii, hist) + printf("%d, %d\n", i.ref->first, i.ref->second); puts(""); } - + // =================================================== { - std::map<int, int> hist; - ++ hist.emplace(12, 100).first->second; - ++ hist.emplace(13, 100).first->second; - ++ hist.emplace(12, 100).first->second; + std::map<std::string, int> food = + {{"burger", 5}, {"pizza", 12}, {"steak", 15}}; - for (auto i: hist) + for (auto i: food) std::cout << i.first << ", " << i.second << std::endl; std::cout << std::endl; } - c_auto (csmap_ii, hist) + + c_auto (cmap_si, food) { - ++ csmap_ii_insert(&hist, 12, 100).ref->second; - ++ csmap_ii_insert(&hist, 13, 100).ref->second; - ++ csmap_ii_insert(&hist, 12, 100).ref->second; + c_apply(v, cmap_si_emplace(&food, c_pair(v)), cmap_si_raw, + {{"burger", 5}, {"pizza", 12}, {"steak", 15}}); - c_foreach (i, csmap_ii, hist) - printf("%d, %d\n", i.ref->first, i.ref->second); + c_foreach (i, cmap_si, food) + printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second); puts(""); } } |
