/[cvs]/eggdrop1.9/src/egglib/linked_list.c
ViewVC logotype

Contents of /eggdrop1.9/src/egglib/linked_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sun Oct 28 13:30:35 2001 UTC (17 years, 11 months ago) by ite
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -0 lines
File MIME type: text/x-chdr
FILE REMOVED
Renamed src/adns, src/compat, src/egglib to lib/adns, lib/compat, lib/egglib respectively.

1 #include <stdio.h>
2 #include "mempool.h"
3 #include "linked_list.h"
4
5 static mempool_t *linked_list_node_mempool = NULL;
6
7 #define NEW_LIST ((linked_list_t *)malloc(sizeof(linked_list_t)))
8 #define NEW_NODE ((linked_list_node_t *)mempool_get_chunk(linked_list_node_mempool))
9 #define KILL_LIST(x) (free((x)))
10 #define KILL_NODE(x) (mempool_free_chunk(linked_list_node_mempool, (x)))
11
12 linked_list_t *linked_list_create(linked_list_t *list, linked_list_cmp_func cmp, int flags)
13 {
14 linked_list_t *mylist;
15
16 if (!linked_list_node_mempool) {
17 linked_list_node_mempool = mempool_create(NULL, 32, sizeof(linked_list_node_t));
18 }
19
20 if (list) mylist = list;
21 else mylist = NEW_LIST;
22 if (!mylist) return((linked_list_t *)0);
23 memset(mylist, 0, sizeof(*mylist));
24 mylist->cmp = cmp;
25 mylist->flags = flags;
26 return(mylist);
27 }
28
29 int linked_list_destroy(linked_list_t *list)
30 {
31 linked_list_node_t *node, *next;
32
33 if (!list) return(0);
34 for (node = list->head; node; node = next) {
35 next = node->next;
36 KILL_NODE(node);
37 }
38 if (!(list->flags & LINKED_LIST_STATIC)) KILL_LIST(list);
39
40 return(0);
41 }
42
43 int linked_list_walk(linked_list_t *list, linked_list_node_func func, void *param)
44 {
45 linked_list_node_t *node;
46
47 for (node = list->head; node; node = node->next) {
48 func(node->data, param);
49 }
50 return(0);
51 }
52
53 linked_list_cursor_t *linked_list_cursor_create(linked_list_cursor_t *cursor, linked_list_t *list)
54 {
55 linked_list_cursor_t *c;
56
57 if (cursor) c = cursor;
58 else c = (linked_list_cursor_t *)malloc(sizeof(*c));
59 memset(c, 0, sizeof(*c));
60
61 c->list = list;
62 c->node = list->head;
63 if (cursor) c->flags = LINKED_LIST_STATIC;
64 return(c);
65 }
66
67 int linked_list_cursor_destroy(linked_list_cursor_t *cursor)
68 {
69 if (cursor && !(cursor->flags & LINKED_LIST_STATIC)) free(cursor);
70 return(0);
71 }
72
73 int linked_list_cursor_home(linked_list_cursor_t *cursor)
74 {
75 cursor->node = cursor->list->head;
76 return(0);
77 }
78
79 int linked_list_cursor_end(linked_list_cursor_t *cursor)
80 {
81 cursor->node = cursor->list->tail;
82 return(0);
83 }
84
85 int linked_list_cursor_get(linked_list_cursor_t *cursor, void *itemptr)
86 {
87 if (cursor->node) {
88 *(void **)itemptr = cursor->node->data;
89 return(1);
90 }
91 return(0);
92 }
93
94 int linked_list_cursor_prev(linked_list_cursor_t *cursor)
95 {
96 if (cursor->node) {
97 cursor->node = cursor->node->prev;
98 return(1);
99 }
100 return(0);
101 }
102
103 int linked_list_cursor_next(linked_list_cursor_t *cursor)
104 {
105 if (cursor->node) {
106 cursor->node = cursor->node->next;
107 return(1);
108 }
109 return(0);
110 }
111
112 int linked_list_cursor_find(linked_list_cursor_t *cursor, void *key)
113 {
114 linked_list_node_t *node;
115 int diff;
116 int dir, lastdir;
117
118 if (cursor->list->flags & LINKED_LIST_SORTED) {
119 node = cursor->node;
120 diff = cursor->list->cmp(key, node->key);
121 if (diff > 0) {
122 while (node && diff > 0) {
123 diff = cursor->list->cmp(key, node->key);
124 if (diff) node = node->next;
125 }
126 }
127 else if (diff < 0) {
128 while (node && diff < 0) {
129 diff = cursor->list->cmp(key, node->key);
130 if (diff) node = node->prev;
131 }
132 }
133 }
134 else {
135 for (node = cursor->list->head; node; node = node->next) {
136 diff = cursor->list->cmp(key, node->key);
137 if (!diff) break;
138 }
139 }
140
141 if (node) {
142 cursor->node = node;
143 return(1);
144 }
145 else return(0);
146 }
147
148 int linked_list_cursor_prepend(linked_list_cursor_t *cursor, void *key, void *data)
149 {
150 linked_list_node_t *node;
151
152 node = NEW_NODE;
153 if (cursor->node) {
154 if (node->prev = cursor->node->prev) node->prev->next = node;
155 else cursor->list->head = node;
156 node->next = cursor->node;
157 cursor->node->prev = node;
158 }
159 else if (cursor->list->head) {
160 node->next = cursor->list->head;
161 node->prev = (linked_list_node_t *)0;
162 node->next->prev = node;
163 cursor->list->head = node;
164 }
165 else {
166 cursor->list->head = node;
167 cursor->list->tail = node;
168 node->next = node->prev = (linked_list_node_t *)0;
169 }
170
171 node->key = key;
172 node->data = data;
173 cursor->node = node;
174 return(0);
175 }
176
177 int linked_list_cursor_append(linked_list_cursor_t *cursor, void *key, void *data)
178 {
179 linked_list_node_t *node;
180
181 node = NEW_NODE;
182 if (cursor->node) {
183 node->prev = cursor->node;
184 if (node->next = cursor->node->next) node->next->prev = node;
185 else cursor->list->tail = node;
186 cursor->node->next = node;
187 }
188 else if (cursor->list->tail) {
189 node->prev = cursor->list->tail;
190 node->next = (linked_list_node_t *)0;
191 cursor->list->tail = node;
192 node->prev->next = node;
193 }
194 else {
195 cursor->list->head = node;
196 cursor->list->tail = node;
197 node->next = node->prev = (linked_list_node_t *)0;
198 }
199
200 node->key = key;
201 node->data = data;
202 cursor->node = node;
203 return(0);
204 }
205
206 int linked_list_cursor_replace(linked_list_cursor_t *cursor, void *key, void *data)
207 {
208 cursor->node->key = key;
209 cursor->node->data = data;
210 return(0);
211 }
212
213 int linked_list_cursor_delete(linked_list_cursor_t *cursor)
214 {
215 if (cursor->node->prev) cursor->node->prev->next = cursor->node->next;
216 else cursor->list->head = cursor->node->next;
217 if (cursor->node->next) {
218 cursor->node->next->prev = cursor->node->prev;
219 cursor->node = cursor->node->next;
220 }
221 else {
222 cursor->list->tail = cursor->node->prev;
223 cursor->node = cursor->node->prev;
224 }
225 return(0);
226 }
227
228 int linked_list_append(linked_list_t *list, void *key, void *data)
229 {
230 linked_list_node_t *node;
231
232 node = NEW_NODE;
233 if (node->prev = list->tail) node->prev->next = node;
234 node->next = (linked_list_node_t *)0;
235 list->tail = node;
236 if (!list->head) list->head = node;
237 node->key = key;
238 node->data = data;
239 return(0);
240 }
241
242 int linked_list_prepend(linked_list_t *list, void *key, void *data)
243 {
244 linked_list_node_t *node;
245
246 node = NEW_NODE;
247 node->prev = (linked_list_node_t *)0;
248 if (list->head) list->head->prev = node;
249 node->next = list->head;
250 node->key = key;
251 node->data = data;
252 list->head = node;
253 if (!list->tail) list->tail = node;
254 return(0);
255 }
256
257 int linked_list_int_cmp(const void *left, const void *right)
258 {
259 return((int)left - (int)right);
260 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23