summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/new_queue.c
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
committerTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
commit5f57d597cd27aef55adbcb3b452973b0c6e33667 (patch)
treedfd59c2fd0e36a6ef37912a9d0cc5a65970f1524 /misc/examples/new_queue.c
parent1763be8c8cbbc0896477fcf924edd4180d1345a9 (diff)
downloadSTC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.tar.gz
STC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.zip
Restructured folders: examples, benchmarks, tests into misc folder.
Diffstat (limited to 'misc/examples/new_queue.c')
-rw-r--r--misc/examples/new_queue.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c
new file mode 100644
index 00000000..bc7a95c9
--- /dev/null
+++ b/misc/examples/new_queue.c
@@ -0,0 +1,45 @@
+#include <stc/crandom.h>
+#include <stc/forward.h>
+#include <stdio.h>
+#include <time.h>
+
+forward_cqueue(cqueue_pnt, struct Point);
+
+struct Point { int x, y; } typedef Point;
+int point_cmp(const Point* a, const Point* b) {
+ int c = c_default_cmp(&a->x, &b->x);
+ return c ? c : c_default_cmp(&a->y, &b->y);
+}
+#define i_val Point
+#define i_cmp point_cmp
+#define i_opt c_is_forward
+#define i_tag pnt
+#include <stc/cqueue.h>
+
+#define i_type IQ
+#define i_val int
+#include <stc/cqueue.h>
+
+int main() {
+ int n = 50000000;
+ stc64_t rng = stc64_new(time(NULL));
+ stc64_uniform_t dist = stc64_uniform_new(0, n);
+
+ c_auto (IQ, Q)
+ {
+ // Push eight million random numbers onto the queue.
+ c_forrange (n)
+ IQ_push(&Q, stc64_uniform(&rng, &dist));
+
+ // Push or pop on the queue 50 million times
+ printf("befor: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q));
+ c_forrange (n) {
+ int r = stc64_uniform(&rng, &dist);
+ if (r & 3)
+ IQ_push(&Q, r);
+ else
+ IQ_pop(&Q);
+ }
+ printf("after: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q));
+ }
+}