summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-11-06 13:47:10 +0100
committerTyge Løvset <[email protected]>2022-11-06 13:47:10 +0100
commitbdbdf76616281f305ffc0af7f347f3fd8eaf0016 (patch)
treec541b8d9a924c2e3e429beb9900e494f21e7aa8b /examples
parentb69006463de7b3f11d974bdddb5782282482f515 (diff)
downloadSTC-modified-bdbdf76616281f305ffc0af7f347f3fd8eaf0016.tar.gz
STC-modified-bdbdf76616281f305ffc0af7f347f3fd8eaf0016.zip
Updated examples/cpque.c to better explain the i_less_functor template parameter.
Diffstat (limited to 'examples')
-rw-r--r--examples/cpque.c51
1 files changed, 25 insertions, 26 deletions
diff --git a/examples/cpque.c b/examples/cpque.c
index 502a8c7b..63d07c6b 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -1,5 +1,9 @@
// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
-// Example of dynamic compare function
+// Example of per-instance less-function on a single priority queue type
+//
+// Note: i_less_functor: available for cpque types
+// i_cmp_functor: available for csmap and csset types
+// i_hash_functor/i_eq_functor: available for cmap and cset types
#include <stdio.h>
#include <stdbool.h>
@@ -14,9 +18,11 @@ struct {
bool (*less)(const int*, const int*);
} typedef IPQueue;
+#define IPQueue_obj(less) ((IPQueue){ipque_init(), less})
+
#define i_type ipque
#define i_val int
-#define i_opt c_declared // avoid redeclaring container type
+#define i_opt c_declared // needed to avoid re-type-define container type
#define i_less_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y) // <== This.
#include <stc/cpque.h>
@@ -27,7 +33,7 @@ struct {
} while(0)
void print_queue(const char* name, IPQueue q) {
- // NB: make a copy because there is no way to traverse
+ // 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};
@@ -46,27 +52,20 @@ int main()
const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data);
print("data", data, n);
- IPQueue q1 = {ipque_init(), int_less}; // Max priority queue
-
- c_forrange (i, n)
- ipque_push(&q1.Q, data[i]);
-
- print_queue("q1", q1);
-
- // Min priority queue
- // std::greater<int> makes the max priority queue act as a min priority queue
- IPQueue minq1 = {ipque_init(), int_greater};
- c_forrange (i, n) ipque_push(&minq1.Q, data[i]);
-
- print_queue("minq1", minq1);
-
- // Using lambda to compare elements.
- IPQueue q5 = {ipque_init(), int_lambda};
-
- c_forrange (i, n)
- ipque_push(&q5.Q, data[i]);
-
- print_queue("q5", q5);
-
- c_drop(ipque, &q1.Q, &minq1.Q, &q5.Q);
+ c_with (IPQueue q1 = IPQueue_obj(int_less), ipque_drop(&q1.Q)) // Max priority queue
+ c_with (IPQueue minq1 = IPQueue_obj(int_greater), ipque_drop(&minq1.Q)) // Min priority queue
+ c_with (IPQueue q5 = IPQueue_obj(int_lambda), ipque_drop(&q5.Q)) // Using lambda to compare elements.
+ {
+ c_forrange (i, n)
+ ipque_push(&q1.Q, data[i]);
+ print_queue("q1", q1);
+
+ c_forrange (i, n)
+ ipque_push(&minq1.Q, data[i]);
+ print_queue("minq1", minq1);
+
+ c_forrange (i, n)
+ ipque_push(&q5.Q, data[i]);
+ print_queue("q5", q5);
+ }
}