4 * Copyright (C) 2003 Eric J Bohm
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021110307 USA
23 /* Double linked list implementation.
25 * You allocate the data and give dlist the pointer.
26 * If your data is complex set the dlist->del_func to a an appropriate
27 * delete function. Otherwise dlist will just use free.
33 * Return pointer to node at marker.
34 * else null if no nodes.
37 inline void *dlist_mark(Dlist *list)
39 if(list->marker!=NULL)
40 return(list->marker->data);
46 * Set marker to start.
49 inline void dlist_start(Dlist *list)
51 list->marker=list->head;
58 inline void dlist_end(Dlist *list)
60 list->marker=list->head;
63 /* internal use function
64 * quickie inline to consolidate the marker movement logic
67 * when direction true it moves marker after
68 * when direction false it moves marker before.
69 * return pointer to data at new marker
70 * if nowhere to move the marker in desired direction return null
72 inline void *_dlist_mark_move(Dlist *list,int direction)
76 if( list->marker->next!=NULL)
77 list->marker=list->marker->next;
83 if( list->marker->prev!=NULL)
84 list->marker=list->marker->prev;
88 if(list->marker!=list->head)
89 return(list->marker->data);
95 * Create new linked list to store nodes of datasize.
96 * return null if list cannot be created.
98 Dlist *dlist_new(size_t datasize)
101 if((list=malloc(sizeof(Dlist))))
105 list->data_size=datasize;
107 list->head=&(list->headnode);
108 list->head->prev=NULL;
109 list->head->next=NULL;
110 list->head->data=NULL;
116 * Create new linked list to store nodes of datasize set list
117 * data node delete function to the passed in del_func
118 * return null if list cannot be created.
120 Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*))
123 list=dlist_new(datasize);
125 list->del_func=del_func;
131 * remove marker node from list
132 * call data_delete function on data if registered.
133 * otherwise call free.
134 * when direction true it moves marker after
135 * when direction false it moves marker before.
139 void dlist_delete(Dlist *list,int direction)
141 if((list->marker != list->head)&&(list->marker!=NULL))
145 _dlist_mark_move(list,direction);
146 if(list->head->next==corpse)
147 list->head->next=corpse->next;
148 if(list->head->prev==corpse)
149 list->head->prev=corpse->prev;
150 if(corpse->prev!=NULL) //should be impossible
151 corpse->prev->next=corpse->next;
152 if(corpse->next!=NULL) //should be impossible
153 corpse->next->prev=corpse->prev;
154 list->del_func(corpse->data);
161 * Insert node containing data at marker.
162 * If direction true it inserts after.
163 * If direction false it inserts before.
164 * move marker to inserted node
165 * return pointer to inserted node
167 void *dlist_insert(Dlist *list,void *data,int direction)
169 DL_node *new_node=NULL;
170 if(list==NULL || data==NULL)
172 if(list->marker==NULL) //in case the marker ends up unset
173 list->marker=list->head;
174 if((new_node=malloc(sizeof(DL_node))))
180 if(list->head->next==NULL) //no l
182 list->head->next=list->head->prev=new_node;
183 new_node->prev=list->head;
184 new_node->next=list->head;
188 new_node->next=list->marker->next;
189 new_node->prev=list->marker;
190 list->marker->next->prev=new_node;
191 list->marker->next=new_node;
195 new_node->prev=list->marker->prev;
196 new_node->next=list->marker;
197 list->marker->prev->next=new_node;
198 list->marker->prev=new_node;
200 list->marker=new_node;
206 return(list->marker->data);
210 * Remove DL_node from list without deallocating data.
211 * if marker == killme .
212 * when direction true it moves marker after
213 * when direction false it moves marker before.
214 * to previous if there is no next.
216 void *_dlist_remove(Dlist *list,DL_node *killme,int direction)
220 void *killer_data=killme->data;
221 // take care of head and marker pointers.
222 if(list->marker==killme)
223 _dlist_mark_move(list,direction);
224 if(killme ==list->head->next)
225 list->head->next=killme->next;
226 if(killme==list->head->prev)
227 list->head->prev=killme->prev;
229 if(killme->prev !=NULL)
230 killme->prev->next=killme->next;
231 if(killme->next !=NULL)
232 killme->next->prev=killme->prev;
243 * Insert node containing data after end.
245 void dlist_push(Dlist *list,void *data)
247 list->marker=list->head->prev;
248 dlist_insert(list,data,1);
252 * Insert node containing data at start.
255 void dlist_unshift(Dlist *list,void *data)
258 list->marker=list->head->next;
259 dlist_insert(list,data,0);
264 * Remove end node from list.
265 * Return pointer to data in removed node.
269 void *dlist_pop(Dlist *list)
271 return(_dlist_remove(list,list->head->prev,0));
275 * Remove start node from list.
276 * Return pointer to data in removed node.
280 void *dlist_shift(Dlist *list)
282 return(_dlist_remove(list,list->head->next,1));
287 * destroy the list freeing all memory
291 void dlist_destroy(Dlist *list)
297 while (dlist_mark(list)) {
298 dlist_delete(list,1);
305 * Return void pointer to list_data element matching comp function criteria
307 * Does not move the marker.
310 void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *))
312 /* test the comp function on each node */
313 struct dl_node *nodepointer;
314 dlist_for_each_nomark(list,nodepointer)
315 if(comp(target,nodepointer->data))
316 return(nodepointer->data);
321 * Apply the node_operation function to each data node in the list
323 void dlist_transform(struct dlist *list, void (*node_operation)(void *))
325 struct dl_node *nodepointer;
326 dlist_for_each_nomark(list,nodepointer)
327 node_operation(nodepointer->data);
331 * insert new into list in sorted order
332 * sorter function in form int sorter(new,ith)
333 * must return 1 for when new should go before ith
335 * return pointer to inserted node
336 * NOTE: assumes list is already sorted
338 void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *))
340 for(dlist_start(list),dlist_next(list); \
341 list->marker!=list->head && !sorter(new,list->marker->data);dlist_next(list));
342 return(dlist_insert_before(list,new));