-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.h
71 lines (55 loc) · 1.94 KB
/
list.h
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
/*
* list.h
*
* copyright (c) 2019, 2020 Xiongfei Shi
*
* author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
* license: Apache-2.0
*
* https://github.com/shixiongfei/actor
*/
#ifndef __LIST_H__
#define __LIST_H__
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct list_s list_t;
struct list_s {
list_t *head;
list_t *tail;
};
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-offsetof(type, member)))
#define LIST_INIT(l) \
{ &(l), &(l) }
#define list_init(l) \
do { \
(l)->head = (l); \
(l)->tail = (l); \
} while (0)
void list_unshift(list_t *list, list_t *node);
void list_push(list_t *list, list_t *node);
void list_erase(list_t *node);
void list_replace(list_t *old_node, list_t *new_node);
void list_rotate(list_t *list);
list_t *list_shift(list_t *list);
list_t *list_pop(list_t *list);
int list_length(list_t *list);
#define list_head(n) ((n)->head)
#define list_tail(n) ((n)->tail)
#define list_first(l) list_tail(l)
#define list_last(l) list_head(l)
#define list_prev(n) list_head(n)
#define list_next(n) list_tail(n)
#define list_empty(l) (((l) == list_head(l)) && (list_head(l) == list_tail(l)))
#define list_foreach(l, p, t) \
for ((p) = list_first(l), (t) = list_next(p); (l) != (p); \
(p) = (t), (t) = list_next(p))
#define list_foreachreverse(l, p, t) \
for ((p) = list_last(l), (t) = list_prev(p); (l) != (p); \
(p) = (t), (t) = list_prev(p))
#ifdef __cplusplus
};
#endif
#endif /* __LIST_H__ */