summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/lower_bound.c
blob: 2477bc145f9741af8e9622444bb9c9cb957c1314 (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
#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;
}