forked from myide/inception
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_list.h
188 lines (161 loc) · 4.55 KB
/
my_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
/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef _list_h_
#define _list_h_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct st_list {
struct st_list *prev,*next;
void *data;
} LIST;
typedef int (*list_walk_action)(void *,void *);
extern LIST *list_add(LIST *root,LIST *element);
extern LIST *list_delete(LIST *root,LIST *element);
extern LIST *list_cons(void *data,LIST *root);
extern LIST *list_reverse(LIST *root);
extern void list_free(LIST *root,unsigned int free_data);
extern unsigned int list_length(LIST *);
extern int list_walk(LIST *,list_walk_action action,unsigned char * argument);
#define list_rest(a) ((a)->next)
#define list_push(a,b) (a)=list_cons((b),(a))
#define list_pop(A) {LIST *old=(A); (A)=list_delete(old,old); my_free(old); }
#define LIST_BASE_NODE_T(TYPE)\
struct {\
uint count; /* count of nodes in list */\
TYPE * start; /* pointer to list start, NULL if empty */\
TYPE * end; /* pointer to list end, NULL if empty */\
}\
#define LIST_NODE_T(TYPE)\
struct {\
TYPE * prev;\
TYPE * next;\
}\
#define LIST_INIT(BASE)\
do{\
(BASE).count = 0;\
(BASE).start = NULL;\
(BASE).end = NULL;\
}while(0)
#define LIST_ADD_FIRST(NAME, BASE, N)\
do{\
((BASE).count)++;\
((N)->NAME).next = (BASE).start;\
((N)->NAME).prev = NULL;\
if ((BASE).start != NULL) {\
(((BASE).start)->NAME).prev = (N);\
}\
(BASE).start = (N);\
if ((BASE).end == NULL) {\
(BASE).end = (N);\
}\
}while(0)
#define LIST_ADD_LAST(NAME, BASE, N)\
do{\
((BASE).count)++;\
((N)->NAME).prev = (BASE).end;\
((N)->NAME).next = NULL;\
if ((BASE).end != NULL) {\
(((BASE).end)->NAME).next = (N);\
}\
(BASE).end = (N);\
if ((BASE).start == NULL) {\
(BASE).start = (N);\
}\
}while(0)
#define LIST_DATA_APPEND(lst,d) \
do{\
lst_node_t* node;\
node = (lst_node_t*)my_malloc(sizeof(lst_node_t), MYF(MY_WME));\
node->data = (d);\
LIST_ADD_LAST(link, (lst), node);\
}while(0)
#define LIST_DATA_FLAG_APPEND(lst,d,f,g) \
do{\
lst_node_t* node;\
node = (lst_node_t*)my_malloc(sizeof(lst_node_t), MYF(MY_WME));\
node->data = (d);\
node->flag = (f);\
node->attr = (g);\
LIST_ADD_LAST(link, (lst), node);\
}while(0)
#define LIST_DATA_ADD_FIRST(lst,d) \
do{\
lst_node_t* node;\
node = (lst_node_t*)my_malloc(sizeof(lst_node_t), MYF(MY_WME));\
node->data = (void*)(d);\
LIST_ADD_FIRST(link, (lst), node);\
}while(0)
#define LIST_INSERT_BEFORE(NAME, BASE, NODE1, NODE2)\
do{\
((BASE).count)++;\
((NODE2)->NAME).next = (NODE1);\
((NODE2)->NAME).prev = ((NODE1)->NAME).prev;\
if (((NODE1)->NAME).prev != NULL) {\
((((NODE1)->NAME).prev)->NAME).next = (NODE2);\
}\
((NODE1)->NAME).prev = (NODE2);\
if ((BASE).start == (NODE1)) {\
(BASE).start = (NODE2);\
}\
}while(0)
#define LIST_INSERT_AFTER(NAME, BASE, NODE1, NODE2)\
do{\
((BASE).count)++;\
((NODE2)->NAME).prev = (NODE1);\
((NODE2)->NAME).next = ((NODE1)->NAME).next;\
if (((NODE1)->NAME).next != NULL) {\
((((NODE1)->NAME).next)->NAME).prev = (NODE2);\
}\
((NODE1)->NAME).next = (NODE2);\
if ((BASE).end == (NODE1)) {\
(BASE).end = (NODE2);\
}\
}while(0)
#define LIST_REMOVE(NAME, BASE, N)\
do{\
((BASE).count)--;\
if (((N)->NAME).next != NULL) {\
((((N)->NAME).next)->NAME).prev = ((N)->NAME).prev;\
} else {\
(BASE).end = ((N)->NAME).prev;\
}\
if (((N)->NAME).prev != NULL) {\
((((N)->NAME).prev)->NAME).next = ((N)->NAME).next;\
} else {\
(BASE).start = ((N)->NAME).next;\
}\
((N)->NAME).next = NULL;\
((N)->NAME).prev = NULL;\
}while(0)
#define LIST_GET_NEXT(NAME, N)\
(((N)->NAME).next)
#define LIST_GET_PREV(NAME, N)\
(((N)->NAME).prev)
#define LIST_GET_LEN(BASE)\
(BASE).count
#define LIST_GET_FIRST(BASE)\
(BASE).start
#define LIST_GET_LAST(BASE)\
(BASE).end
typedef struct lst_node_struct lst_node_t;
struct lst_node_struct
{
void* data;
void* flag;
LIST_NODE_T(lst_node_t) link;
};
typedef LIST_BASE_NODE_T(lst_node_t) my_lst_t;
#ifdef __cplusplus
}
#endif
#endif