diff options
| author | Tyge Løvset <[email protected]> | 2020-07-23 20:35:07 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-23 20:35:07 +0200 |
| commit | 1c686f72bf9857c904b1320df7248ee5144d484e (patch) | |
| tree | 112754382836a80450da6bd0ed2c24aa48fc23fb /examples | |
| parent | c6fdaca5aea99cc7e57661c1637101a78d01ba86 (diff) | |
| download | STC-modified-1c686f72bf9857c904b1320df7248ee5144d484e.tar.gz STC-modified-1c686f72bf9857c904b1320df7248ee5144d484e.zip | |
Changed cvecpq.h priority queue API and crandom.h API
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/advanced.c | 11 | ||||
| -rw-r--r-- | examples/benchmark.c | 6 | ||||
| -rw-r--r-- | examples/geek7.c | 8 | ||||
| -rw-r--r-- | examples/heap.c | 18 | ||||
| -rw-r--r-- | examples/list.c | 4 | ||||
| -rw-r--r-- | examples/priority.c | 10 | ||||
| -rw-r--r-- | examples/rngbirthday.c | 8 | ||||
| -rw-r--r-- | examples/rngtest.c | 12 |
8 files changed, 36 insertions, 41 deletions
diff --git a/examples/advanced.c b/examples/advanced.c index b27f214d..68ddc9ef 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -63,17 +63,12 @@ declare_CMap(vk, Viking, int, c_defaultDestroy, vikingvw_equals, vikingvw_hash, int main() { -/* CMap_vk vikings = cmap_init; - cmap_vk_put(&vikings, (VikingVw) {"Einar", "Norway"}, 20); - cmap_vk_put(&vikings, (VikingVw) {"Olaf", "Denmark"}, 24); - cmap_vk_put(&vikings, (VikingVw) {"Harald", "Iceland"}, 12); -*/ - CMap_vk vikings = cmap_vk_from( (CMapInput_vk[]) { + c_push(&vikings, cmap_vk, c_items( {{"Einar", "Norway"}, 20}, {{"Olaf", "Denmark"}, 24}, - {{"Harald", "Iceland"}, 12} - }, 3); + {{"Harald", "Iceland"}, 12}, + )); CMapEntry_vk* e = cmap_vk_find(&vikings, (VikingVw) {"Einar", "Norway"}); e->value += 5; // update diff --git a/examples/benchmark.c b/examples/benchmark.c index 3bcd3d5c..3c1b49bb 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -26,9 +26,9 @@ KHASH_MAP_INIT_INT64(ii, uint64_t) size_t seed = 1234;
static const float maxLoadFactor = 0.77f;
-sfc64_random_t rng;
-#define SEED(s) rng = sfc64_seed(seed)
-#define RAND(N) (sfc64_random(&rng) & ((1 << N) - 1))
+crandom64_t rng;
+#define SEED(s) rng = crandom64_init(seed)
+#define RAND(N) (crandom64(&rng) & ((1 << N) - 1))
#define CMAP_SETUP(tag, Key, Value) CMap_##tag map = cmap_init \
diff --git a/examples/geek7.c b/examples/geek7.c index bac3f21b..c5da63ec 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -28,7 +28,7 @@ After inserting all the elements excluding the ones which are to be deleted, Pop declare_CMap(ii, int, int);
declare_CVec(i, int);
-declare_CVec_priorityQ(i, >);
+declare_CVec_priority_queue(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
- cvec_i_pushPriorityQ(&heap, arr[i]);
+ cvecpq_i_push(&heap, arr[i]);
}
// Print top k elements in the min heap
for (int i = 0; i < k; ++i) {
- printf("%d ", cvec_i_topPriorityQ(&heap));
+ printf("%d ", cvecpq_i_top(&heap));
// Pop the top element
- cvec_i_popPriorityQ(&heap);
+ cvecpq_i_pop(&heap);
}
cvec_i_destroy(&heap);
}
diff --git a/examples/heap.c b/examples/heap.c index 6fbb346f..e4e91544 100644 --- a/examples/heap.c +++ b/examples/heap.c @@ -4,33 +4,33 @@ #include "stc/crandom.h"
declare_CVec(f, float);
-declare_CVec_priorityQ(f, >);
+declare_CVec_priority_queue(f, >);
int main()
{
uint32_t seed = time(NULL);
- pcg32_random_t pcg = pcg32_seed(seed, 0);
+ crandom32_t pcg = crandom32_init(seed);
int N = 30000000, M = 100;
CVec_f vec = cvec_init;
clock_t start = clock();
for (int i=0; i<N; ++i)
- cvec_f_pushBack(&vec, pcg32_random(&pcg));
- cvec_f_buildPriorityQ(&vec);
+ cvec_f_pushBack(&vec, crandom32(&pcg));
+ cvecpq_f_build(&vec);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", cvec_f_topPriorityQ(&vec)), cvec_f_popPriorityQ(&vec);
+ printf("%.0f ", cvecpq_f_top(&vec)), cvecpq_f_pop(&vec);
start = clock();
for (int i=M; i<N; ++i)
- cvec_f_popPriorityQ(&vec);
+ cvecpq_f_pop(&vec);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
- pcg = pcg32_seed(seed, 0);
+ pcg = crandom32_init(seed);
start = clock();
for (int i=0; i<N; ++i)
- cvec_f_pushPriorityQ(&vec, pcg32_random(&pcg));
+ cvecpq_f_push(&vec, crandom32(&pcg));
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", cvec_f_topPriorityQ(&vec)), cvec_f_popPriorityQ(&vec);
+ printf("%.0f ", cvecpq_f_top(&vec)), cvecpq_f_pop(&vec);
puts("");
}
diff --git a/examples/list.c b/examples/list.c index 694ec60b..81569260 100644 --- a/examples/list.c +++ b/examples/list.c @@ -9,10 +9,10 @@ int main() { c_foreach (i, clist_ix, list) printf("%zu ", i.item->value);
puts("");
- pcg32_random_t pcg = pcg32_seed(time(NULL), 0);
+ crandom32_t pcg = crandom32_init(time(NULL));
int n;
for (int i=0; i<10000000; ++i) // ten million
- clist_ix_pushBack(&list, pcg32_random(&pcg));
+ clist_ix_pushBack(&list, crandom32(&pcg));
n = 100;
c_foreach (i, clist_ix, list)
if (n--) printf("%8d: %10zu\n", 100 - n, i.item->value); else break;
diff --git a/examples/priority.c b/examples/priority.c index d6168a6b..4032b19c 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -6,21 +6,21 @@ #include <stc/crandom.h>
declare_CVec(i, uint32_t);
-declare_CVec_priorityQ(i, >); // min-heap (increasing values)
+declare_CVec_priority_queue(i, >); // min-heap (increasing values)
declare_CMap(ii, int, int);
int main() {
- pcg32_random_t pcg = pcg32_seed(time(NULL), 0);
+ crandom32_t pcg = crandom32_init(time(NULL));
CVec_i heap = cvec_init;
// Push ten million random numbers to queue
for (int i=0; i<10000000; ++i)
- cvec_i_pushPriorityQ(&heap, pcg32_random(&pcg));
+ cvecpq_i_push(&heap, crandom32(&pcg));
// Extract the hundred smallest.
for (int i=0; i<100; ++i) {
- printf("%u ", cvec_i_topPriorityQ(&heap));
- cvec_i_popPriorityQ(&heap);
+ printf("%u ", cvecpq_i_top(&heap));
+ cvecpq_i_pop(&heap);
}
cvec_i_destroy(&heap);
}
\ No newline at end of file diff --git a/examples/rngbirthday.c b/examples/rngbirthday.c index 1be96449..560b275a 100644 --- a/examples/rngbirthday.c +++ b/examples/rngbirthday.c @@ -15,12 +15,12 @@ const static uint64_t mask = (1ull << 52) - 1; void repeats(void)
{
- sfc64_random_t rng = sfc64_seed(seed);
+ crandom64_t rng = crandom64_init(seed);
CMap_ic m = cmap_init;
cmap_ic_reserve(&m, N);
clock_t now = clock();
for (size_t i = 0; i < N; ++i) {
- uint64_t k = sfc64_random(&rng) & mask;
+ uint64_t k = crandom64(&rng) & mask;
int v = ++cmap_ic_at(&m, k, 0)->value;
if (v > 1) printf("%zu: %x - %d\n", i, k, v);
}
@@ -34,12 +34,12 @@ declare_CVec(x, uint64_t); void distribution(void)
{
- pcg32_random_t rng = pcg32_seed(seed, seed); // time(NULL), time(NULL));
+ crandom32_t rng = crandom32_init(seed); // time(NULL), time(NULL));
const size_t N = 1ull << 28, M = 1ull << 9; // 1ull << 10;
CMap_x map = cmap_x_make(M);
clock_t now = clock();
for (size_t i = 0; i < N; ++i) {
- ++cmap_x_at(&map, pcg32_randomBounded(&rng, M), 0)->value;
+ ++cmap_x_at(&map, crandom32b(&rng, M), 0)->value;
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
diff --git a/examples/rngtest.c b/examples/rngtest.c index c465b5c3..af7c7dca 100644 --- a/examples/rngtest.c +++ b/examples/rngtest.c @@ -14,26 +14,26 @@ int main(void) uint64_t v;
printf("start\n");
- pcg32_random_t pcg = pcg32_seed(time(NULL), 1);
+ crandom32_t pcg = crandom32_init(time(NULL));
before = clock(); \
v = 0;
for (size_t i=0; i<NN; i++) {
- v += pcg32_random(&pcg) & 0xffffffff;
+ v += crandom32(&pcg);
}
difference = clock() - before;
printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
- sfc64_random_t sfc = sfc64_seed(time(NULL));
+ crandom64_t sfc = crandom64_init(time(NULL));
before = clock(); \
v = 0;
for (size_t i=0; i<NN; i++) {
- v += sfc64_random(&sfc) & 0xffffffff;
+ v += crandom64(&sfc) & 0xffffffff;
}
difference = clock() - before;
printf("sfc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
- for (int i=0; i<8; ++i) printf("%f ", sfc64_randomFloat(&sfc));
+ for (int i=0; i<8; ++i) printf("%f ", crandom64f(&sfc));
puts("");
- for (int i=0; i<8; ++i) printf("%f ", pcg32_randomFloat(&pcg));
+ for (int i=0; i<8; ++i) printf("%f ", crandom32f(&pcg));
puts("");
}
\ No newline at end of file |
