summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/functor.c
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2023-04-12 15:55:33 -0400
committerrealtradam <[email protected]>2023-04-12 15:55:33 -0400
commit0841165881871ee01b782129be681209aeed2423 (patch)
tree8a76b61dcaab381b6b42305201ae8b6259f6b6c0 /misc/examples/functor.c
parent554f3e8acf7855b5d6a90cc68cefb7445460b03c (diff)
parent0516aa3ae823ed9a22b2c5f776948c8447c32c31 (diff)
downloadSTC-modified-0841165881871ee01b782129be681209aeed2423.tar.gz
STC-modified-0841165881871ee01b782129be681209aeed2423.zip
Merge branch 'master' into modified
Diffstat (limited to 'misc/examples/functor.c')
-rw-r--r--misc/examples/functor.c74
1 files changed, 32 insertions, 42 deletions
diff --git a/misc/examples/functor.c b/misc/examples/functor.c
index 6d42c4f8..c0a4f8e8 100644
--- a/misc/examples/functor.c
+++ b/misc/examples/functor.c
@@ -1,40 +1,29 @@
// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
// Example of per-instance less-function on a single priority queue type
//
-// Note: i_less_functor: available for cpque types only
-// i_cmp_functor: available for csmap and csset types only
-// i_hash_functor/i_eq_functor: available for cmap and cset types only
+// Note: i_less: has self for cpque types only
+// i_cmp: has self for csmap and csset types only
+// 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_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y)
-#include <stc/cpque.h>
+#define i_extend 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, IPQue_ext 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};
+ IPQue_ext copy = {q.less, IPQue_clone(q.get)};
- for (printf("%s: \t", name); !ipque_empty(&copy.Q); ipque_pop(&copy.Q))
- printf("%d ", *ipque_top(&copy.Q));
+ for (printf("%s: \t", name); !IPQue_empty(&copy.get); IPQue_pop(&copy.get))
+ printf("%d ", *IPQue_top(&copy.get));
puts("");
- ipque_drop(&copy.Q);
+
+ IPQue_drop(&copy.get);
}
static bool int_less(const int* x, const int* y) { return *x < *y; }
@@ -43,26 +32,27 @@ 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);
+ const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data);
printf("data: \t");
- c_forrange (i, n) printf("%d ", data[i]);
+ c_forrange (i, n)
+ 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.
- c_defer (ipque_drop(&q1.Q), ipque_drop(&minq1.Q), ipque_drop(&q5.Q))
- {
- c_forrange (i, n)
- ipque_push(&q1.Q, data[i]);
- print_queue("q1", q1);
+ IPQue_ext q1 = {int_less}; // Max priority queue
+ IPQue_ext minq1 = {int_greater}; // Min priority queue
+ IPQue_ext q5 = {int_lambda}; // Using lambda to compare elements.
+
+ c_forrange (i, n)
+ IPQue_push(&q1.get, data[i]);
+ print_queue("q1", q1);
+
+ c_forrange (i, n)
+ IPQue_push(&minq1.get, data[i]);
+ print_queue("minq1", minq1);
- c_forrange (i, n)
- ipque_push(&minq1.Q, data[i]);
- print_queue("minq1", minq1);
+ c_forrange (i, n)
+ IPQue_push(&q5.get, data[i]);
+ print_queue("q5", q5);
- c_forrange (i, n)
- ipque_push(&q5.Q, data[i]);
- print_queue("q5", q5);
- }
+ c_drop(IPQue, &q1.get, &minq1.get, &q5.get);
}