diff options
| author | Tyge Løvset <[email protected]> | 2020-07-30 09:59:24 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-30 09:59:24 +0200 |
| commit | 3bf571bd7f0c8eface28bb5d2b7607d934865e00 (patch) | |
| tree | 567c83c7847f7f47bb432fe8f0efec7a25796b2a /examples | |
| parent | 5e526acf20bb3792a7faf87d9daf6b3aa95ff3b6 (diff) | |
| download | STC-modified-3bf571bd7f0c8eface28bb5d2b7607d934865e00.tar.gz STC-modified-3bf571bd7f0c8eface28bb5d2b7607d934865e00.zip | |
Renamed cvecpq.h to cvec_pq.h and changed API. Added pqueue initialization example in inits.c. Documented my 64bit PRNG engine.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/geek7.c | 10 | ||||
| -rw-r--r-- | examples/heap.c | 16 | ||||
| -rw-r--r-- | examples/inits.c | 27 | ||||
| -rw-r--r-- | examples/priority.c | 13 |
4 files changed, 46 insertions, 20 deletions
diff --git a/examples/geek7.c b/examples/geek7.c index 36805dce..2524ea95 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -24,11 +24,11 @@ After inserting all the elements excluding the ones which are to be deleted, Pop #include <stdio.h>
#include <stc/clist.h>
#include <stc/cmap.h>
-#include <stc/cvecpq.h>
+#include <stc/cvec_pq.h>
declare_cmap(ii, int, int);
declare_cvec(i, int);
-declare_cvec_priority_queue(i, >);
+declare_cvec_pqueue(i, >);
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
@@ -62,15 +62,15 @@ void findElementsAfterDel(int arr[], int m, int del[], // Else push it in the min heap
else
- cvecpq_i_push(&heap, arr[i]);
+ cvec_i_pqueue_push(&heap, arr[i]);
}
// Print top k elements in the min heap
for (int i = 0; i < k; ++i) {
- printf("%d ", cvecpq_i_top(&heap));
+ printf("%d ", cvec_i_pqueue_top(&heap));
// Pop the top element
- cvecpq_i_pop(&heap);
+ cvec_i_pqueue_pop(&heap);
}
cvec_i_destroy(&heap);
}
diff --git a/examples/heap.c b/examples/heap.c index 0f4ab4c2..72232d58 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -1,10 +1,10 @@ #include <stdio.h>
#include <time.h>
-#include "stc/cvecpq.h"
-#include "stc/crand.h"
+#include <stc/crand.h>
+#include <stc/cvec_pq.h>
declare_cvec(f, float);
-declare_cvec_priority_queue(f, >);
+declare_cvec_pqueue(f, >);
int main()
{
@@ -15,22 +15,22 @@ int main() clock_t start = clock();
for (int i=0; i<N; ++i)
cvec_f_push_back(&vec, crand_gen_i32(&pcg));
- cvecpq_f_build(&vec);
+ cvec_f_pqueue_build(&vec);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", cvecpq_f_top(&vec)), cvecpq_f_pop(&vec);
+ printf("%.0f ", cvec_f_pqueue_top(&vec)), cvec_f_pqueue_pop(&vec);
start = clock();
for (int i=M; i<N; ++i)
- cvecpq_f_pop(&vec);
+ cvec_f_pqueue_pop(&vec);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
pcg = crand_eng32_init(seed);
start = clock();
for (int i=0; i<N; ++i)
- cvecpq_f_push(&vec, crand_gen_i32(&pcg));
+ cvec_f_pqueue_push(&vec, crand_gen_i32(&pcg));
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", cvecpq_f_top(&vec)), cvecpq_f_pop(&vec);
+ printf("%.0f ", cvec_f_pqueue_top(&vec)), cvec_f_pqueue_pop(&vec);
puts("");
}
diff --git a/examples/inits.c b/examples/inits.c index 0769ce3c..16da0a4f 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -1,7 +1,7 @@ #include <stdio.h>
#include <stc/cstr.h>
#include <stc/cmap.h>
-#include <stc/cvec.h>
+#include <stc/cvec_pq.h>
#include <stc/clist.h>
declare_cmap(id, int, cstr_t, cstr_destroy); // Map of int -> cstr_t
@@ -11,8 +11,31 @@ typedef struct {int x, y;} ipair_t; declare_cvec(ip, ipair_t, c_default_destroy, c_mem_compare);
declare_clist(ip, ipair_t, c_default_destroy, c_mem_compare);
+declare_cvec(f, float);
+declare_cvec_pqueue(f, >);
+
int main(void) {
+ // regular vector:
+ cvec_f floats = cvec_init;
+ c_push(&floats, cvec_f, c_items(4.0f, 2.0f, 5.0f, 3.0f, 1.0f));
+ // Output:
+ c_foreach (i, cvec_f, floats) printf("%.1f ", *i.item);
+ puts("");
+
+ // reorganise as a priority queue, and add more numbers:
+ cvec_f_pqueue_build(&floats);
+ c_push(&floats, cvec_f_pqueue, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f));
+ // Output sorted:
+ while (cvec_size(floats) > 0) {
+ printf("%.1f ", cvec_f_pqueue_top(&floats));
+ cvec_f_pqueue_pop(&floats);
+ }
+ puts("\n");
+ cvec_f_destroy(&floats);
+
+ // ------------------
+
int year = 2020;
cmap_id idnames = cmap_init;
c_push(&idnames, cmap_id, c_items(
@@ -23,6 +46,7 @@ int main(void) { c_foreach (i, cmap_id, idnames)
printf("%d: %s\n", i.item->key, i.item->value.str);
+ puts("");
cmap_id_destroy(&idnames);
// ------------------
@@ -42,6 +66,7 @@ int main(void) { c_foreach (i, cmap_cnt, countries)
printf("%s: %d\n", i.item->key.str, i.item->value);
+ puts("");
cmap_cnt_destroy(&countries);
// ------------------
diff --git a/examples/priority.c b/examples/priority.c index 64449304..015f24fb 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -1,25 +1,26 @@ #include <stdio.h>
#include <time.h>
-#include <stc/cvecpq.h>
+#include <stc/cvec_pq.h>
#include <stc/cmap.h>
#include <stc/crand.h>
declare_cvec(i, uint32_t);
-declare_cvec_priority_queue(i, >); // min-heap (increasing values)
+declare_cvec_pqueue(i, >); // min-heap (increasing values)
int main() {
crand_eng32_t pcg = crand_eng32_init(time(NULL));
+ crand_uniform_i32_t dist = crand_uniform_i32_init(0, 100000000);
cvec_i heap = cvec_init;
- // Push ten million random numbers to queue
+ // Push ten million random numbers to priority queue
for (int i=0; i<10000000; ++i)
- cvecpq_i_push(&heap, crand_gen_i32(&pcg));
+ cvec_i_pqueue_push(&heap, crand_uniform_i32(&pcg, dist));
// Extract the hundred smallest.
for (int i=0; i<100; ++i) {
- printf("%u ", cvecpq_i_top(&heap));
- cvecpq_i_pop(&heap);
+ printf("%u ", cvec_i_pqueue_top(&heap));
+ cvec_i_pqueue_pop(&heap);
}
cvec_i_destroy(&heap);
}
|
