summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/vectors/lower_bound.c
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-20 15:09:10 +0200
committertylov <[email protected]>2023-07-20 15:12:29 +0200
commit900295256d825fc323149cd223c49787f32a3696 (patch)
tree6c79cf4209e3975bb6865e2940b9cb56ea469c73 /misc/examples/vectors/lower_bound.c
parent224a04f7fa7549ed94d2a1415eb25829e39a7cca (diff)
downloadSTC-modified-900295256d825fc323149cd223c49787f32a3696.tar.gz
STC-modified-900295256d825fc323149cd223c49787f32a3696.zip
Moved examples to sub-directories. Added cotask1.c cotask2.c examples.
Diffstat (limited to 'misc/examples/vectors/lower_bound.c')
-rw-r--r--misc/examples/vectors/lower_bound.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/misc/examples/vectors/lower_bound.c b/misc/examples/vectors/lower_bound.c
new file mode 100644
index 00000000..bea828f2
--- /dev/null
+++ b/misc/examples/vectors/lower_bound.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+
+#define i_key int
+#define i_cmp_native
+#include <stc/cvec.h>
+
+#define i_key int
+#include <stc/csset.h>
+
+int main(void)
+{
+ // TEST SORTED VECTOR
+ {
+ int key, *res;
+ cvec_int vec = c_init(cvec_int, {40, 600, 1, 7000, 2, 500, 30});
+
+ cvec_int_sort(&vec);
+
+ key = 100;
+ res = cvec_int_lower_bound(&vec, key).ref;
+ if (res)
+ printf("Sorted Vec %d: lower bound: %d\n", key, *res); // 500
+
+ key = 10;
+ cvec_int_iter it1 = cvec_int_lower_bound(&vec, key);
+ if (it1.ref)
+ printf("Sorted Vec %3d: lower_bound: %d\n", key, *it1.ref); // 30
+
+ key = 600;
+ cvec_int_iter it2 = cvec_int_binary_search(&vec, key);
+ if (it2.ref)
+ printf("Sorted Vec %d: bin. search: %d\n", key, *it2.ref); // 600
+
+ c_foreach (i, cvec_int, it1, it2)
+ printf(" %d\n", *i.ref);
+
+ puts("");
+ cvec_int_drop(&vec);
+ }
+
+ // TEST SORTED SET
+ {
+ int key, *res;
+ csset_int set = c_init(csset_int, {40, 600, 1, 7000, 2, 500, 30});
+
+ key = 100;
+ res = csset_int_lower_bound(&set, key).ref;
+ if (res)
+ printf("Sorted Set %d: lower bound: %d\n", key, *res); // 500
+
+ key = 10;
+ csset_int_iter it1 = csset_int_lower_bound(&set, key);
+ if (it1.ref)
+ printf("Sorted Set %3d: lower bound: %d\n", key, *it1.ref); // 30
+
+ key = 600;
+ csset_int_iter it2 = csset_int_find(&set, key);
+ if (it2.ref)
+ printf("Sorted Set %d: find : %d\n", key, *it2.ref); // 600
+
+ c_foreach (i, csset_int, it1, it2)
+ printf(" %d\n", *i.ref);
+
+ csset_int_drop(&set);
+ }
+}