From 09f08e7ac65624d63f1eb04fba2317a2e0b2b0b7 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 25 Apr 2022 11:54:07 +0200 Subject: Added lower_bound() examples with both sorted cvec and sorted set (csset). --- examples/lower_bound.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/lower_bound.c (limited to 'examples') diff --git a/examples/lower_bound.c b/examples/lower_bound.c new file mode 100644 index 00000000..578209f8 --- /dev/null +++ b/examples/lower_bound.c @@ -0,0 +1,47 @@ +#include + +#define i_val int +#include + +#define i_val int +#include + + +int main() +{ + c_auto (csset_int, set) + c_auto (cvec_int, vec) + { + int key, *res; + + // TEST SORTED VECTOR + c_apply(t, cvec_int_push(&vec, t), int, { + 40, 600, 1, 7000, 2, 500, 30, + }); + + cvec_int_sort(&vec); + + key = 550; + res = cvec_int_lower_bound(&vec, key).ref; + + if (res != cvec_int_end(&vec).ref) + printf("Vec key %d: lower bound: %d\n", key, *res); // 600 + else + printf("Vec element not found\n"); + + + // TEST SORTED SET + c_apply(t, csset_int_push(&set, t), int, { + 40, 600, 1, 7000, 2, 500, 30, + }); + + key = 550; + res = csset_int_lower_bound(&set, key).ref; + + if (res != csset_int_end(&set).ref) + printf("Set key %d: lower bound: %d\n", key, *res); // 600 + else + printf("Set element not found\n"); + } + return 0; +} -- cgit v1.2.3