diff options
| author | _Tradam <[email protected]> | 2023-09-08 01:29:47 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-08 01:29:47 +0000 |
| commit | 3c76c7f3d5db3f9586a90d03f8fbb02d79de9acd (patch) | |
| tree | afbe4b540967223911f7c5de36559b82154f02f3 /misc/examples/vectors | |
| parent | 0841165881871ee01b782129be681209aeed2423 (diff) | |
| parent | 1a72205fe05c2375cfd380dd8381a8460d9ed8d1 (diff) | |
| download | STC-modified-modified.tar.gz STC-modified-modified.zip | |
Diffstat (limited to 'misc/examples/vectors')
| -rw-r--r-- | misc/examples/vectors/lower_bound.c | 66 | ||||
| -rw-r--r-- | misc/examples/vectors/new_vec.c | 43 | ||||
| -rw-r--r-- | misc/examples/vectors/stack.c | 32 |
3 files changed, 141 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..09cf2008 --- /dev/null +++ b/misc/examples/vectors/lower_bound.c @@ -0,0 +1,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); + } +} diff --git a/misc/examples/vectors/new_vec.c b/misc/examples/vectors/new_vec.c new file mode 100644 index 00000000..88efd55a --- /dev/null +++ b/misc/examples/vectors/new_vec.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stc/forward.h> + +forward_cvec(cvec_i32, int); +forward_cvec(cvec_pnt, struct Point); + +typedef struct MyStruct { + cvec_i32 intvec; + cvec_pnt pntvec; +} MyStruct; + +#define i_key int +#define i_tag i32 +#define i_is_forward +#include <stc/cvec.h> + +typedef struct Point { int x, y; } Point; + +#define i_key Point +#define i_tag pnt +#define i_less(a, b) a->x < b->x || (a->x == b->x && a->y < b->y) +#define i_eq(a, b) a->x == b->x && a->y == b->y +#define i_is_forward +#include <stc/cvec.h> + +int main(void) +{ + MyStruct my = {0}; + + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){42, 14}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 94}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){62, 81}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 91}); + + cvec_pnt_sort(&my.pntvec); + + c_foreach (i, cvec_pnt, my.pntvec) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + + cvec_i32_drop(&my.intvec); + cvec_pnt_drop(&my.pntvec); +} diff --git a/misc/examples/vectors/stack.c b/misc/examples/vectors/stack.c new file mode 100644 index 00000000..6297fb6f --- /dev/null +++ b/misc/examples/vectors/stack.c @@ -0,0 +1,32 @@ + +#include <stdio.h> + +#define i_tag i +#define i_capacity 100 +#define i_key int +#include <stc/cstack.h> + +#define i_tag c +#define i_key char +#include <stc/cstack.h> + +int main(void) { + cstack_i stack = {0}; + cstack_c chars = {0}; + + c_forrange (i, 101) + cstack_i_push(&stack, (int)(i*i)); + + printf("%d\n", *cstack_i_top(&stack)); + + c_forrange (i, 90) + cstack_i_pop(&stack); + + c_foreach (i, cstack_i, stack) + printf(" %d", *i.ref); + puts(""); + printf("top: %d\n", *cstack_i_top(&stack)); + + cstack_i_drop(&stack); + cstack_c_drop(&chars); +} |
