summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/list_splice.c
blob: 8a5dae765199d2006a33f2ef7df7126b4245390a (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
#include <stdio.h>

#define i_val int
#define i_tag i
#define i_extern // define _clist_mergesort() once
#include <stc/clist.h>

void print_ilist(const char* s, clist_i list)
{
    printf("%s", s);
    c_FOREACH (i, clist_i, list) {
        printf(" %d", *i.ref);
    }
    puts("");
}

int main ()
{
    c_AUTO (clist_i, list1, list2)
    {
        c_FORLIST (i, int, {1, 2, 3, 4, 5})
            clist_i_push_back(&list1, *i.ref);

        c_FORLIST (i, int, {10, 20, 30, 40, 50})
            clist_i_push_back(&list2, *i.ref);

        print_ilist("list1:", list1);
        print_ilist("list2:", list2);

        clist_i_iter it = clist_i_advance(clist_i_begin(&list1), 2);
        it = clist_i_splice(&list1, it, &list2);

        puts("After splice");
        print_ilist("list1:", list1);
        print_ilist("list2:", list2);

        clist_i_splice_range(&list2, clist_i_begin(&list2), &list1, it, clist_i_end(&list1));

        puts("After splice_range");
        print_ilist("list1:", list1);
        print_ilist("list2:", list2);
    }
}