summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-08-28 16:08:08 +0200
committerTyge Løvset <[email protected]>2020-08-28 16:08:08 +0200
commit47de9987a623e8374915af40d67d72dcb56e4283 (patch)
tree44ad1af184a3121d87a3281ff04546452b927819 /examples
parentb29771837b5e37fc9175dc43ab7108d01aaaeba1 (diff)
downloadSTC-modified-47de9987a623e8374915af40d67d72dcb56e4283.tar.gz
STC-modified-47de9987a623e8374915af40d67d72dcb56e4283.zip
Some smaller fixes and cleanup.
Diffstat (limited to 'examples')
-rw-r--r--examples/demos.c3
-rw-r--r--examples/priority.c11
-rw-r--r--examples/words.c12
3 files changed, 18 insertions, 8 deletions
diff --git a/examples/demos.c b/examples/demos.c
index 4f5c2356..731197e3 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -55,8 +55,7 @@ void vectordemo1()
-//declare_cvec(cs, cstr_t, cstr_destroy, cstr_compare); // supply inline destructor of values
-declare_cvec_str(); // supply inline destructor of values
+declare_cvec_str();
void vectordemo2()
{
diff --git a/examples/priority.c b/examples/priority.c
index 780e5809..2d5c23bd 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -5,7 +5,7 @@
#include <stc/cmap.h>
#include <stc/crandom.h>
-declare_cvec(i, uint32_t);
+declare_cvec(i, int64_t);
declare_cvec_pqueue(i, >); // min-heap (increasing values)
int main() {
@@ -17,9 +17,16 @@ int main() {
for (int i=0; i<10000000; ++i)
cvec_i_pqueue_push(&heap, crandom_uniform_i32(&pcg, dist));
+ // push some negative numbers too.
+ c_push(&heap, cvec_i_pqueue, c_items(-231, -32, -873, -4, -343));
+
+ for (int i=0; i<10000000; ++i)
+ cvec_i_pqueue_push(&heap, crandom_uniform_i32(&pcg, dist));
+
+
// Extract the hundred smallest.
for (int i=0; i<100; ++i) {
- printf("%u ", cvec_i_pqueue_top(&heap));
+ printf("%d ", cvec_i_pqueue_top(&heap));
cvec_i_pqueue_pop(&heap);
}
cvec_i_destroy(&heap);
diff --git a/examples/words.c b/examples/words.c
index f8e86329..98134cf6 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -8,6 +8,7 @@ declare_cvec_str();
declare_clist_str();
declare_cmap_strkey(si, int);
+
int main1()
{
clist_str lwords = clist_init;
@@ -15,6 +16,10 @@ int main1()
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
));
+ clist_str_push_back_v(&lwords, cstr_from("%f", 123897.0 / 23.0));
+ c_foreach (w, clist_str, lwords)
+ printf("%s\n", w.item->value.str);
+ puts("");
cvec_str words = cvec_init;
c_push(&words, cvec_str, c_items(
@@ -23,9 +28,8 @@ int main1()
));
cmap_si word_map = cmap_init;
- c_foreach (w, cvec_str, words) {
+ c_foreach (w, cvec_str, words)
++cmap_si_insert(&word_map, w.item->str, 0)->value;
- }
c_foreach (pair, cmap_si, word_map) {
printf("%d occurrences of word '%s'\n",
@@ -43,7 +47,7 @@ int main1()
#include <iostream>
#include <vector>
#include <unordered_map>
-
+
int main2()
{
std::vector<std::string> words = {
@@ -59,7 +63,7 @@ int main2()
for (const auto &pair : word_map) {
std::cout << pair.second
<< " occurrences of word '"
- << pair.first << "'\n";
+ << pair.first << "'\n";
}
return 0;
}