summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/lower_bound.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/lower_bound.c')
-rw-r--r--misc/examples/lower_bound.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/misc/examples/lower_bound.c b/misc/examples/lower_bound.c
new file mode 100644
index 00000000..2477bc14
--- /dev/null
+++ b/misc/examples/lower_bound.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+#define i_val int
+#include <stc/cvec.h>
+
+#define i_val int
+#include <stc/csset.h>
+
+int main()
+{
+ // TEST SORTED VECTOR
+ c_auto (cvec_int, vec)
+ {
+ int key, *res;
+
+ c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30})
+ cvec_int_push(&vec, *i.ref);
+
+ cvec_int_sort(&vec);
+
+ key = 500;
+ res = cvec_int_lower_bound(&vec, key).ref;
+ if (res != cvec_int_end(&vec).ref)
+ printf("Sorted Vec %d: lower bound: %d\n", key, *res); // 600
+
+ key = 550;
+ res = cvec_int_lower_bound(&vec, key).ref;
+ if (res != cvec_int_end(&vec).ref)
+ printf("Sorted Vec %d: lower_bound: %d\n", key, *res); // 500
+
+ key = 500;
+ res = cvec_int_binary_search(&vec, key).ref;
+ if (res != cvec_int_end(&vec).ref)
+ printf("Sorted Vec %d: bin. search: %d\n", key, *res); // 500
+ puts("");
+ }
+
+ // TEST SORTED SET
+ c_auto (csset_int, set)
+ {
+ int key, *res;
+
+ c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30})
+ csset_int_push(&set, *i.ref);
+
+ key = 500;
+ res = csset_int_lower_bound(&set, key).ref;
+ if (res != csset_int_end(&set).ref)
+ printf("Sorted Set %d: lower bound: %d\n", key, *res); // 600
+
+ key = 550;
+ res = csset_int_lower_bound(&set, key).ref;
+ if (res != csset_int_end(&set).ref)
+ printf("Sorted Set %d: lower bound: %d\n", key, *res); // 600
+
+ key = 500;
+ res = csset_int_find(&set, key).ref;
+ if (res != csset_int_end(&set).ref)
+ printf("Sorted Set %d: find : %d\n", key, *res); // 600
+ }
+ return 0;
+}