-
Notifications
You must be signed in to change notification settings - Fork 9
/
c_list.h
124 lines (91 loc) · 2.71 KB
/
c_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef C_LIST_H
#define C_LIST_H
#define LIST_INIT_SIZE 1000
#define LIST_INC_SIZE 1000
#include <stdint.h>
typedef struct c_list_node c_list_node;
typedef struct c_list c_list;
#ifdef __cplusplus
extern "C"
{
#endif
#define HF_List c_list
typedef c_list YC_List;
#define List_Create( Type ) \
list_init(sizeof(Type))
#define List_Free(list) \
list_free(list)
#define List_Push_Back(Type,list,v) \
{\
c_list_node *node = list_node_new(sizeof(Type)); \
*(Type*)(node->data) = v; \
_list_push_back(list,node);\
}
#define List_Push_Front(Type,list,v) \
{\
c_list_node *node = list_node_new(sizeof(Type)); \
*(Type*)node->data = v; \
_list_push_front(list,node);\
}
#define List_Erase_Back(list) \
list->size--; \
list_erase_back(list)
#define List_Erase_Front(list) \
list->size--;\
list_erase_front(list)
#define List_Erase(Type,list,v) \
{\
c_list_node *cur = list->head->next;\
assert( nd != list->head && nd != list->tail );\ \
while ( cur )\
{\
if ( *(Type*)cur->data == *(Type*)nd->data )\
{\
cur->pre->next = cur->next;\
cur->next->pre = cur->pre;\
free(cur);\
break;\
}\
cur = cur->next;\
\
}\
}
#define List_Free(list) \
list_free(list)
#define List_Read_Front(Type,list) \
*(Type*)(_list_read_front(list)->data)
#define List_Read_Back(Type,list) \
*(Type*)(_list_read_back(list)->data)
#define List_Begin(list) list->head->next
#define List_End(list) list->tail
struct c_list_node
{
c_list_node *pre;
c_list_node *next;
void *data;
};
struct c_list
{
c_list_node *head;
c_list_node *tail;
c_list_node *iterator;
uint32_t size;
};
c_list_node* list_node_new(int unit);
c_list* list_init(int unit);
void list_free(c_list *list);
void _list_push_back(c_list *list, c_list_node *nd);
void _list_push_front(c_list *list, c_list_node *nd);
void list_push_back (c_list *list, void *value, int unit);
void list_push_front(c_list *list, void *value, int unit);
void list_erase_back(c_list *list);
void list_erase_front(c_list *list);
void list_erase(c_list *list, c_list_node *nd);
c_list_node *_list_read_back(c_list *list);
c_list_node *_list_read_front(c_list *list);
void * list_read_back(c_list *list);
void * list_read_front(c_list *list);
#ifdef __cplusplus
}
#endif
#endif // C_LIST_H