summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/linkedlists/new_list.c
blob: 7518929a72fa06892c3502aa38e110a5c78af410 (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
67
68
69
70
#include <stdio.h>
#include <stc/forward.h>

forward_clist(clist_i32, int);
forward_clist(clist_pnt, struct Point);

typedef struct {
    clist_i32 intlist;
    clist_pnt pntlist;
} MyStruct;

#define i_key int
#define i_tag i32
#define i_is_forward
#include <stc/clist.h>

typedef struct Point { int x, y; } Point;
int point_cmp(const Point* a, const Point* b) {
    int c = a->x - b->x;
    return c ? c : a->y - b->y;
}

#define i_key Point
#define i_cmp point_cmp
#define i_is_forward
#define i_tag pnt
#include <stc/clist.h>

#define i_key float
#define i_use_cmp               // use < and == operators for comparison 
#include <stc/clist.h>

void MyStruct_drop(MyStruct* s);
#define i_type MyList
#define i_key MyStruct
#define i_keydrop MyStruct_drop   // define drop function
#define i_no_clone                // must explicitely exclude or define cloning support because of drop.
#include <stc/clist.h>

void MyStruct_drop(MyStruct* s) {
    clist_i32_drop(&s->intlist);
    clist_pnt_drop(&s->pntlist);
}


int main(void)
{
    MyStruct my = {0};
    clist_i32_push_back(&my.intlist, 123);
    clist_pnt_push_back(&my.pntlist, c_LITERAL(Point){123, 456});
    MyStruct_drop(&my);

    clist_pnt plist = c_init(clist_pnt, {{42, 14}, {32, 94}, {62, 81}});
    clist_pnt_sort(&plist);

    c_foreach (i, clist_pnt, plist) 
        printf(" (%d %d)", i.ref->x, i.ref->y);
    puts("");
    clist_pnt_drop(&plist);


    clist_float flist = c_init(clist_float, {123.3f, 321.2f, -32.2f, 78.2f});
    clist_float_sort(&flist);

    c_foreach (i, clist_float, flist)
        printf(" %g", (double)*i.ref);

    puts("");
    clist_float_drop(&flist);
}