blob: 8c09d5e063fd9b06fbdc3d51e6e795960214b97c (
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
71
72
73
74
75
76
77
78
79
80
81
82
|
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "macro.h"
#define LLIST(T) TEMPL(llist, T)
#define LINK(T) TEMPL(llist_link, T)
#define UNLINK(T) TEMPL(unlink, T)
#endif /* LINKED_LIST_H */
#ifdef TYPE
typedef struct LINK(TYPE) {
struct LINK(TYPE)* before;
struct LINK(TYPE)* after;
TYPE* value;
} LINK(TYPE);
#define L(link) (link)->value
typedef struct {
LINK(TYPE)* head;
LINK(TYPE)* tail;
LINK(TYPE)* cur;
int length;
} LLIST(TYPE);
#define FIRST(lst) (lst)->head->after
#define FIRST_V(lst) (lst)->head->after->value
#define LAST(lst) (lst)->tail->before
#define LAST_V(lst) (lst)->tail->before->value
INIT_F ( LLIST(TYPE) );
/*
* NOTE freeing a linked list alsa FFREE's all its contents.
* TODO some form of shared pointer to ensure nothing is free'd twice
* would be a good idea.
*/
FREE_F ( LLIST(TYPE) );
INIT_F ( LINK(TYPE) );
INIT_F ( LINK(TYPE), TYPE* val );
FREE_F ( LINK(TYPE) );
int UNLINK(LINK(TYPE)) ( LINK(TYPE)* );
int PUSH(LLIST(TYPE)) ( LLIST(TYPE)*, TYPE* );
TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* );
TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* );
int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src );
int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new );
int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist );
int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist );
/*
* Resets a linked list by removing all it's objects.
* FREE's all elements stored in the list.
*/
int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist );
LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new);
FMT_F(LLIST(TYPE));
/* Iterator */
// #define __BEG_LLIST(v, set) v = (set)->head
#define __BEG_LLIST(l, set) l = FIRST(set)
#define BEG_LLIST(T) LINK(T)* __BEG_LLIST
#define __END_LLIST(l, set) l != (set)->tail
#define END_LLIST(T) __END_LLIST
#define __NXT_LLIST(l, set) l = l->after
#define NXT_LLIST(T) __NXT_LLIST
#endif /* TYPE */
|