diff options
| author | Tyge Lovset <[email protected]> | 2023-03-31 22:08:07 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2023-03-31 22:08:07 +0200 |
| commit | 5693ae9ae0d1a18627ba540e0240da010b282296 (patch) | |
| tree | ccb0dbe519d9a27fc944446ecc1a574cd9d86fcc /misc/examples/functor.c | |
| parent | 56c394ede691143a32d53f4094df37dc49dc0a29 (diff) | |
| download | STC-modified-5693ae9ae0d1a18627ba540e0240da010b282296.tar.gz STC-modified-5693ae9ae0d1a18627ba540e0240da010b282296.zip | |
Added stc/extend.h: A generalized way to type-safely extend a container with new members which can be accessed from the template parameters. See examples/functor.c
Diffstat (limited to 'misc/examples/functor.c')
| -rw-r--r-- | misc/examples/functor.c | 47 |
1 files changed, 18 insertions, 29 deletions
diff --git a/misc/examples/functor.c b/misc/examples/functor.c index f8074c3a..45158cd1 100644 --- a/misc/examples/functor.c +++ b/misc/examples/functor.c @@ -6,35 +6,24 @@ // i_hash/i_eq: has self for cmap and cset types only #include <stdio.h> -#include <stdbool.h> -#include <stc/forward.h> -#include <stc/algo/crange.h> -#include <stc/cstr.h> -// predeclare -forward_cpque(ipque, int); - -struct { - ipque Q; - bool (*less)(const int*, const int*); -} typedef IPQueue; - - -#define i_type ipque +#define i_type IPQue #define i_val int -#define i_opt c_is_forward // needed to avoid re-type-define container type -#define i_less(x, y) c_container_of(self, IPQueue, Q)->less(x, y) -#include <stc/cpque.h> +#define i_ext bool (*less)(const int*, const int*) +#define i_less(x, y) c_getcon(self)->less(x, y) +#define i_con cpque +#include <stc/extend.h> -void print_queue(const char* name, IPQueue q) { +void print_queue(const char* name, IPQueExt q) { // NB: make a clone because there is no way to traverse // priority_queue's content without erasing the queue. - IPQueue copy = {ipque_clone(q.Q), q.less}; + IPQueExt copy = {q.less, IPQue_clone(q.get)}; - for (printf("%s: \t", name); !ipque_empty(©.Q); ipque_pop(©.Q)) - printf("%d ", *ipque_top(©.Q)); + for (printf("%s: \t", name); !IPQue_empty(©.get); IPQue_pop(©.get)) + printf("%d ", *IPQue_top(©.get)); puts(""); - ipque_drop(©.Q); + + IPQue_drop(©.get); } static bool int_less(const int* x, const int* y) { return *x < *y; } @@ -49,21 +38,21 @@ int main() printf("%d ", data[i]); puts(""); - IPQueue q1 = {ipque_init(), int_less}; // Max priority queue - IPQueue minq1 = {ipque_init(), int_greater}; // Min priority queue - IPQueue q5 = {ipque_init(), int_lambda}; // Using lambda to compare elements. + IPQueExt q1 = {int_less}; // Max priority queue + IPQueExt minq1 = {int_greater}; // Min priority queue + IPQueExt q5 = {int_lambda}; // Using lambda to compare elements. c_forrange (i, n) - ipque_push(&q1.Q, data[i]); + IPQue_push(&q1.get, data[i]); print_queue("q1", q1); c_forrange (i, n) - ipque_push(&minq1.Q, data[i]); + IPQue_push(&minq1.get, data[i]); print_queue("minq1", minq1); c_forrange (i, n) - ipque_push(&q5.Q, data[i]); + IPQue_push(&q5.get, data[i]); print_queue("q5", q5); - c_drop(ipque, &q1.Q, &minq1.Q, &q5.Q); + c_drop(IPQue, &q1.get, &minq1.get, &q5.get); } |
