-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuni_list.h
205 lines (199 loc) · 4.31 KB
/
uni_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef __UNI_LIST_H__
#define __UNI_LIST_H__
#define UNI_LIST_H(_NAME, _FIELD)\
struct node_##_NAME##_t{\
_FIELD data;\
struct node_##_NAME##_t* next;\
struct node_##_NAME##_t* prev;\
};\
\
struct list_##_NAME##_t{\
struct node_##_NAME##_t* head;\
struct node_##_NAME##_t* tail;\
uint32_t size;\
};\
\
struct list_##_NAME##_t *list_##_NAME##_create(void);\
void list_##_NAME##_push_back(struct list_##_NAME##_t *l, _FIELD data);\
void list_##_NAME##_push_front(struct list_##_NAME##_t *l, _FIELD data);\
_FIELD list_##_NAME##_pop_front(struct list_##_NAME##_t *l);\
_FIELD list_##_NAME##_pop_back(struct list_##_NAME##_t *l);\
_FIELD list_##_NAME##_front(struct list_##_NAME##_t *l);\
_FIELD list_##_NAME##_back(struct list_##_NAME##_t *l);\
_FIELD list_##_NAME##_remove_node(struct list_##_NAME##_t *l, struct node_##_NAME##_t *node);\
struct node_##_NAME##_t* list_##_NAME##_get_node_at(struct list_##_NAME##_t *l, uint32_t pos);
#define UNI_LIST_C(_NAME, _FIELD)\
struct list_##_NAME##_t *list_##_NAME##_create(void)\
{\
struct list_##_NAME##_t *list = (struct list_##_NAME##_t*)kmalloc(sizeof(struct list_##_NAME##_t));\
list->size = 0;\
list->head = NULL;\
list->tail = NULL;\
return list;\
}\
\
void list_##_NAME##_destroy(struct list_##_NAME##_t *l)\
{\
struct node_##_NAME##_t *current = l->head;\
while(current!=NULL)\
{\
struct node_##_NAME##_t *delete = current;\
current = current->next;\
kfree(delete);\
}\
\
kfree(l);\
}\
\
void list_##_NAME##_push_back(struct list_##_NAME##_t *l, _FIELD data)\
{\
struct node_##_NAME##_t *node = (struct node_##_NAME##_t*)kmalloc(sizeof(struct node_##_NAME##_t));\
\
node->data = data;\
node->next = NULL;\
node->prev = l->tail;\
\
if (l->tail == NULL)\
{\
l->head = node;\
l->tail = node;\
}\
else\
{\
l->tail->next = node;\
l->tail = node;\
}\
\
l->size++;\
}\
\
void list_##_NAME##_push_front(struct list_##_NAME##_t *l, _FIELD data)\
{\
struct node_##_NAME##_t *node = (struct node_##_NAME##_t*)kmalloc(sizeof(struct node_##_NAME##_t));\
\
node->data = data;\
node->next = l->head;\
node->prev = NULL;\
\
if (l->head == NULL)\
{\
l->head = node;\
l->tail = node;\
}\
else\
{\
l->head->prev = node;\
l->head = node;\
}\
\
l->size++;\
}\
\
_FIELD list_##_NAME##_pop_front(struct list_##_NAME##_t *l)\
{\
struct node_##_NAME##_t* poped = l->head;\
_FIELD data = poped->data;\
\
if (l->head->next != NULL)\
{\
l->head = l->head->next;\
l->head->prev = NULL;\
}\
else\
{\
l->head = NULL;\
l->tail = NULL;\
}\
\
l->size--;\
\
kfree(poped);\
return data;\
}\
\
_FIELD list_##_NAME##_pop_back(struct list_##_NAME##_t *l)\
{\
struct node_##_NAME##_t* poped = l->tail;\
_FIELD data = poped->data;\
\
if (l->tail->prev != NULL)\
{\
l->tail = l->tail->prev;\
l->tail->next = NULL;\
}\
else\
{\
l->head = NULL;\
l->tail = NULL;\
}\
\
l->size--;\
\
kfree(poped);\
return data;\
}\
\
_FIELD list_##_NAME##_front(struct list_##_NAME##_t *l)\
{\
_FIELD data = l->head->data;\
return data;\
}\
\
_FIELD list_##_NAME##_back(struct list_##_NAME##_t *l)\
{\
_FIELD data = l->tail->data;\
return data;\
}\
\
void list_##_NAME##_insert(struct list_##_NAME##_t *l, _FIELD data, uint32_t pos)\
{\
if(pos<=0) list_##_NAME##_push_front(l, data);\
else if(pos>=l->size) list_##_NAME##_push_back(l, data);\
else\
{\
struct node_##_NAME##_t *current_node = l->head;\
for(uint32_t i=0; i<pos; i++)\
current_node = current_node->next;\
\
struct node_##_NAME##_t *node = (struct node_##_NAME##_t*)kmalloc(sizeof(struct node_##_NAME##_t));\
node->data = data;\
node->next = current_node;\
node->prev = current_node->prev;\
\
current_node->prev->next = node;\
current_node->prev = node;\
\
l->size++;\
}\
}\
\
_FIELD list_##_NAME##_remove_node(struct list_##_NAME##_t *l, struct node_##_NAME##_t *node)\
{\
if(l->head == node)\
l->head = node->next;\
else\
node->prev->next = node->next;\
\
if(l->tail == node)\
l->tail = node->prev;\
else \
node->next->prev = node->prev;\
\
l->size--;\
\
_FIELD thread = node->data;\
kfree(node);\
\
return thread;\
}\
\
struct node_##_NAME##_t* list_##_NAME##_get_node_at(struct list_##_NAME##_t *l, uint32_t pos)\
{\
struct node_##_NAME##_t* current = l->head;\
\
for(uint32_t i=0; i<pos; i++)\
current = current->next;\
\
return current;\
}
#endif