summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/vectors/lower_bound.c
blob: 09cf20085657f193aa7fb6a20fcbfc026e25996d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>

#define i_key int
#define i_use_cmp
#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);
    }
}