summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-02 11:07:52 +0200
committerTyge Løvset <[email protected]>2020-09-02 11:07:52 +0200
commitd3bb64882aa00c9c1b3056d74796b59fc2180f0e (patch)
treee4db986a486e3f84ac5cc9177f5feb76ba91070e /examples
parent721254982e82292d321e7d98480ac6ec22b4a2c6 (diff)
downloadSTC-modified-d3bb64882aa00c9c1b3056d74796b59fc2180f0e.tar.gz
STC-modified-d3bb64882aa00c9c1b3056d74796b59fc2180f0e.zip
Rewrote cpqueue to a real adapter class. Added some missing functions in cvec, clist, cmap. Added c_init() and c_destroy() generic algoritms on containers.
Diffstat (limited to 'examples')
-rw-r--r--examples/ex_gaussian.c6
-rw-r--r--examples/geek7.c13
-rw-r--r--examples/heap.c29
-rw-r--r--examples/inits.c34
-rw-r--r--examples/phonebook.c72
-rw-r--r--examples/priority.c19
6 files changed, 126 insertions, 47 deletions
diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c
index dcb78d7b..75efec51 100644
--- a/examples/ex_gaussian.c
+++ b/examples/ex_gaussian.c
@@ -19,7 +19,7 @@ declare_cvec(e, cmap_i_entry_t, c_default_destroy, compare);
int main()
{
enum {N = 10000000};
- const double Mean = 12.0, StdDev = 8.0, Mag = 12000.0 / StdDev;
+ const double Mean = -12.0, StdDev = 8.0, Mag = 12000.0 / StdDev;
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
@@ -32,7 +32,7 @@ int main()
cmap_i mhist = cmap_init;
for (size_t i = 0; i < N; ++i) {
int index = round( crand_normal_f64(&dist) );
- ++ cmap_i_insert(&mhist, index, 0)->value;
+ cmap_i_insert(&mhist, index, 0)->value += 1;
}
// Transfer map to vec and sort it by map keys.
@@ -55,4 +55,4 @@ int main()
cstr_destroy(&bar);
cmap_i_destroy(&mhist);
cvec_e_destroy(&vhist);
-} \ No newline at end of file
+}
diff --git a/examples/geek7.c b/examples/geek7.c
index 9a60c0a9..65af4486 100644
--- a/examples/geek7.c
+++ b/examples/geek7.c
@@ -24,11 +24,12 @@ 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/cvec.h>
#include <stc/cpqueue.h>
declare_cmap(ii, int, int);
declare_cvec(i, int);
-declare_cvec_pqueue(i, >);
+declare_cpqueue(i, >, cvec);
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
@@ -43,7 +44,7 @@ void findElementsAfterDel(int arr[], int m, int del[],
cmap_ii_insert(&mp, del[i], 0)->value++;
}
- cvec_i heap = cvec_init;
+ cpqueue_i heap = cpqueue_i_init();
for (int i = 0; i < m; ++i) {
@@ -62,17 +63,17 @@ void findElementsAfterDel(int arr[], int m, int del[],
// Else push it in the min heap
else
- cvec_i_pqueue_push(&heap, arr[i]);
+ cpqueue_i_push(&heap, arr[i]);
}
// Print top k elements in the min heap
for (int i = 0; i < k; ++i) {
- printf("%d ", cvec_i_pqueue_top(&heap));
+ printf("%d ", *cpqueue_i_top(&heap));
// Pop the top element
- cvec_i_pqueue_pop(&heap);
+ cpqueue_i_pop(&heap);
}
- cvec_i_destroy(&heap);
+ cpqueue_i_destroy(&heap);
}
int main()
diff --git a/examples/heap.c b/examples/heap.c
index c6624819..82b351fd 100644
--- a/examples/heap.c
+++ b/examples/heap.c
@@ -1,36 +1,45 @@
#include <stdio.h>
#include <time.h>
#include <stc/crandom.h>
+#include <stc/cvec.h>
#include <stc/cpqueue.h>
declare_cvec(f, float);
-declare_cvec_pqueue(f, >);
+declare_cpqueue(f, >, cvec);
int main()
{
uint32_t seed = time(NULL);
- crand_rng32_t pcg = crand_rng32_init(seed);
- int N = 30000000, M = 100;
- cvec_f vec = cvec_init;
+ crand_rng32_t pcg;
+ int N = 3000000, M = 100;
+
+ cpqueue_f pq = cpqueue_f_init();
+
+ pcg = crand_rng32_init(seed);
clock_t start = clock();
for (int i=0; i<N; ++i)
- cvec_f_push_back(&vec, crand_i32(&pcg));
- cvec_f_pqueue_build(&vec);
+ cvec_f_push_back(&pq, crand_i32(&pcg));
+
+ cpqueue_f_build(&pq);
printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
for (int i=0; i<M; ++i)
- printf("%.0f ", cvec_f_pqueue_top(&vec)), cvec_f_pqueue_pop(&vec);
+ printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
+
start = clock();
for (int i=M; i<N; ++i)
- cvec_f_pqueue_pop(&vec);
+ cpqueue_f_pop(&pq);
printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
pcg = crand_rng32_init(seed);
start = clock();
for (int i=0; i<N; ++i)
- cvec_f_pqueue_push(&vec, crand_i32(&pcg));
+ cpqueue_f_push(&pq, crand_i32(&pcg));
printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
+
for (int i=0; i<M; ++i)
- printf("%.0f ", cvec_f_pqueue_top(&vec)), cvec_f_pqueue_pop(&vec);
+ printf("%.0f ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
puts("");
+
+ cpqueue_f_destroy(&pq);
}
diff --git a/examples/inits.c b/examples/inits.c
index c55bf83f..ca916577 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stc/cstr.h>
#include <stc/cmap.h>
+#include <stc/cvec.h>
#include <stc/cpqueue.h>
#include <stc/clist.h>
@@ -16,37 +17,35 @@ declare_cvec(ip, ipair_t, c_default_destroy, ipair_compare);
declare_clist(ip, ipair_t, c_default_destroy, ipair_compare);
declare_cvec(f, float);
-declare_cvec_pqueue(f, >);
+declare_cpqueue(f, >, cvec);
int main(void) {
// CVEC FLOAT / PRIORITY QUEUE
- cvec_f floats = cvec_init;
- c_push(&floats, cvec_f, c_items(4.0f, 2.0f, 5.0f, 3.0f, 1.0f));
+ c_init(cvec_f, floats, c_items(4.0f, 2.0f, 5.0f, 3.0f, 1.0f));
c_foreach (i, cvec_f, floats) printf("%.1f ", *i.item);
puts("");
// CVEC PRIORITY QUEUE
- cvec_f_pqueue_build(&floats); // reorganise vec
- c_push(&floats, cvec_f_pqueue, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f));
+ cpqueue_f_build(&floats); // reorganise vec
+ c_push(&floats, cpqueue_f, c_items(40.0f, 20.0f, 50.0f, 30.0f, 10.0f));
// sorted:
while (cvec_size(floats) > 0) {
- printf("%.1f ", cvec_f_pqueue_top(&floats));
- cvec_f_pqueue_pop(&floats);
+ printf("%.1f ", *cpqueue_f_top(&floats));
+ cpqueue_f_pop(&floats);
}
puts("\n");
- cvec_f_destroy(&floats);
+ cpqueue_f_destroy(&floats);
// CMAP ID
int year = 2020;
- cmap_id idnames = cmap_init;
- c_push(&idnames, cmap_id, c_items(
+ c_init(cmap_id, idnames, c_items(
{100, cstr_make("Hello")},
{110, cstr_make("World")},
{120, cstr_from("Howdy, -%d-", year)},
@@ -59,10 +58,7 @@ int main(void) {
// CMAP CNT
- cmap_cnt countries = cmap_init;
-
- cmap_cnt_insert(&countries, "Greenland", 0)->value += 20;
- c_push(&countries, cmap_cnt, c_items(
+ c_init(cmap_cnt, countries, c_items(
{"Norway", 100},
{"Denmark", 50},
{"Iceland", 10},
@@ -72,7 +68,7 @@ int main(void) {
{"Spain", 10},
{"France", 10},
));
-
+ cmap_cnt_insert(&countries, "Greenland", 0)->value += 20;
cmap_cnt_insert(&countries, "Sweden", 0)->value += 20;
cmap_cnt_insert(&countries, "Norway", 0)->value += 20;
cmap_cnt_insert(&countries, "Finland", 0)->value += 20;
@@ -84,14 +80,14 @@ int main(void) {
// CVEC PAIR
- cvec_ip pairs1 = cvec_init;
- c_push(&pairs1, cvec_ip, c_items(
+ c_init(cvec_ip, pairs1, c_items(
{5, 6},
{3, 4},
{1, 2},
{7, 8},
));
cvec_ip_sort(&pairs1);
+
c_foreach (i, cvec_ip, pairs1)
printf("(%d %d) ", i.item->x, i.item->y);
puts("");
@@ -99,14 +95,14 @@ int main(void) {
// CLIST PAIR
- clist_ip pairs2 = clist_init;
- c_push(&pairs2, clist_ip, c_items(
+ c_init(clist_ip, pairs2, c_items(
{5, 6},
{3, 4},
{1, 2},
{7, 8},
));
clist_ip_sort(&pairs2);
+
c_foreach (i, clist_ip, pairs2)
printf("(%d %d) ", i.item->value.x, i.item->value.y);
puts("");
diff --git a/examples/phonebook.c b/examples/phonebook.c
new file mode 100644
index 00000000..15df04e3
--- /dev/null
+++ b/examples/phonebook.c
@@ -0,0 +1,72 @@
+// The MIT License (MIT)
+// Copyright (c) 2018 Maksim Andrianov
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+// IN THE SOFTWARE.
+
+// Program to emulates the phone book.
+
+#include <stdio.h>
+#include <stc/cmap.h>
+#include <stc/cstr.h>
+
+declare_cmap_str();
+
+void print_phone_book(cmap_str phone_book)
+{
+ c_foreach (i, cmap_str, phone_book)
+ printf("%s\t- %s\n", i.item->key.str, i.item->value.str);
+}
+
+int main(int argc, char **argv)
+{
+ bool erased;
+ cmap_str phone_book = cmap_init;
+ c_push(&phone_book, cmap_str, c_items(
+ {"Lilia Friedman", "(892) 670-4739"},
+ {"Tariq Beltran", "(489) 600-7575"},
+ {"Laiba Juarez", "(303) 885-5692"},
+ {"Elliott Mooney", "(945) 616-4482"},
+ ));
+
+ printf("Phone book:\n");
+ print_phone_book(phone_book);
+
+ cmap_try_emplace(str, &phone_book, "Zak Byers", cstr_make("(551) 396-1880"));
+ cmap_try_emplace(str, &phone_book, "Zak Byers", cstr_make("(551) 396-1990"));
+
+ printf("\nPhone book after adding Zak Byers:\n");
+ print_phone_book(phone_book);
+
+ if (cmap_str_find(&phone_book, "Tariq Beltran") != NULL)
+ printf("\nTariq Beltran is in phone book\n");
+
+ erased = cmap_str_erase(&phone_book, "Tariq Beltran");
+ erased = cmap_str_erase(&phone_book, "Elliott Mooney");
+
+ printf("\nPhone book after erasing Tariq and Elliott:\n");
+ print_phone_book(phone_book);
+
+ cmap_str_put(&phone_book, "Zak Byers", "(555) 396-188");
+
+ printf("\nPhone book after update phone of Zak Byers:\n");
+ print_phone_book(phone_book);
+
+ cmap_str_destroy(&phone_book);
+ puts("done");
+} \ No newline at end of file
diff --git a/examples/priority.c b/examples/priority.c
index 7dfff7da..2451d2d2 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -1,34 +1,35 @@
#include <stdio.h>
#include <time.h>
+#include <stc/cvec.h>
#include <stc/cpqueue.h>
#include <stc/cmap.h>
#include <stc/crandom.h>
declare_cvec(i, int64_t);
-declare_cvec_pqueue(i, >); // min-heap (increasing values)
+declare_cpqueue(i, >, cvec); // min-heap (increasing values)
int main() {
- size_t N = 100000000;
+ size_t N = 10000000;
crand_rng64_t pcg = crand_rng64_init(time(NULL));
crand_uniform_i64_t dist = crand_uniform_i64_init(pcg, 0, N * 10);
- cvec_i heap = cvec_init;
+ cpqueue_i heap = cpqueue_i_init();
// Push ten million random numbers to priority queue
for (int i=0; i<N; ++i)
- cvec_i_pqueue_push(&heap, crand_uniform_i64(&dist));
+ cpqueue_i_push(&heap, crand_uniform_i64(&dist));
// push some negative numbers too.
- c_push(&heap, cvec_i_pqueue, c_items(-231, -32, -873, -4, -343));
+ c_push(&heap, cpqueue_i, c_items(-231, -32, -873, -4, -343));
for (int i=0; i<N; ++i)
- cvec_i_pqueue_push(&heap, crand_uniform_i64(&dist));
+ cpqueue_i_push(&heap, crand_uniform_i64(&dist));
// Extract the hundred smallest.
for (int i=0; i<100; ++i) {
- printf("%zd ", cvec_i_pqueue_top(&heap));
- cvec_i_pqueue_pop(&heap);
+ printf("%zd ", *cpqueue_i_top(&heap));
+ cpqueue_i_pop(&heap);
}
- cvec_i_destroy(&heap);
+ cpqueue_i_destroy(&heap);
}