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.
34 * Return pointer to node at marker.
35 * else null if no nodes.
38 inline void *dlist_mark(Dlist *list)
40 if(list->marker!=NULL)
41 return(list->marker->data);
47 * Set marker to start.
50 inline void dlist_start(Dlist *list)
52 list->marker=list->head;
59 inline void dlist_end(Dlist *list)
61 list->marker=list->head;
64 /* internal use function
65 * quickie inline to consolidate the marker movement logic
68 * when direction true it moves marker after
69 * when direction false it moves marker before.
70 * return pointer to data at new marker
71 * if nowhere to move the marker in desired direction return null
73 inline void *_dlist_mark_move(Dlist *list,int direction)
77 if( list->marker && list->marker->next!=NULL)
78 list->marker=list->marker->next;
84 if( list->marker && list->marker->prev!=NULL)
85 list->marker=list->marker->prev;
89 if(list->marker!=list->head)
90 return(list->marker->data);
96 * Create new linked list to store nodes of datasize.
97 * return null if list cannot be created.
99 Dlist *dlist_new(size_t datasize)
102 if((list=malloc(sizeof(Dlist))))
106 list->data_size=datasize;
108 list->head=&(list->headnode);
109 list->head->prev=NULL;
110 list->head->next=NULL;
111 list->head->data=NULL;
117 * Create new linked list to store nodes of datasize set list
118 * data node delete function to the passed in del_func
119 * return null if list cannot be created.
121 Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*))
124 list=dlist_new(datasize);
126 list->del_func=del_func;
132 * remove marker node from list
133 * call data_delete function on data if registered.
134 * otherwise call free.
135 * when direction true it moves marker after
136 * when direction false it moves marker before.
140 void dlist_delete(Dlist *list,int direction)
142 if((list->marker != list->head)&&(list->marker!=NULL))
146 _dlist_mark_move(list,direction);
147 if(list->head->next==corpse)
148 list->head->next=corpse->next;
149 if(list->head->prev==corpse)
150 list->head->prev=corpse->prev;
151 if(corpse->prev!=NULL) //should be impossible
152 corpse->prev->next=corpse->next;
153 if(corpse->next!=NULL) //should be impossible
154 corpse->next->prev=corpse->prev;
155 list->del_func(corpse->data);
162 * Insert node containing data at marker.
163 * If direction true it inserts after.
164 * If direction false it inserts before.
165 * move marker to inserted node
166 * return pointer to inserted node
168 void *dlist_insert(Dlist *list,void *data,int direction)
170 DL_node *new_node=NULL;
171 if(list==NULL || data==NULL)
173 if(list->marker==NULL) //in case the marker ends up unset
174 list->marker=list->head;
175 if((new_node=malloc(sizeof(DL_node))))
181 if(list->head->next==NULL) //no l
183 list->head->next=list->head->prev=new_node;
184 new_node->prev=list->head;
185 new_node->next=list->head;
189 new_node->next=list->marker->next;
190 new_node->prev=list->marker;
191 list->marker->next->prev=new_node;
192 list->marker->next=new_node;
196 new_node->prev=list->marker->prev;
197 new_node->next=list->marker;
198 list->marker->prev->next=new_node;
199 list->marker->prev=new_node;
201 list->marker=new_node;
207 return(list->marker->data);
211 * Insert dl_node at marker.
212 * If direction true it inserts after.
213 * If direction false it inserts before.
214 * move marker to inserted node
215 * return pointer to inserted node
217 void *_dlist_insert_dlnode(struct dlist *list,struct dl_node *new_node,int direction)
219 if(list==NULL || new_node==NULL)
221 if(list->marker==NULL) //in case the marker ends up unset
222 list->marker=list->head;
224 if(list->head->next==NULL)
226 list->head->next=list->head->prev=new_node;
227 new_node->prev=list->head;
228 new_node->next=list->head;
232 new_node->next=list->marker->next;
233 new_node->prev=list->marker;
234 list->marker->next->prev=new_node;
235 list->marker->next=new_node;
239 new_node->prev=list->marker->prev;
240 new_node->next=list->marker;
241 list->marker->prev->next=new_node;
242 list->marker->prev=new_node;
244 list->marker=new_node;
245 return(list->marker);
251 * Remove DL_node from list without deallocating data.
252 * if marker == killme .
253 * when direction true it moves marker after
254 * when direction false it moves marker before.
255 * to previous if there is no next.
257 void *_dlist_remove(Dlist *list,DL_node *killme,int direction)
261 void *killer_data=killme->data;
262 // take care of head and marker pointers.
263 if(list->marker==killme)
264 _dlist_mark_move(list,direction);
265 if(killme ==list->head->next)
266 list->head->next=killme->next;
267 if(killme==list->head->prev)
268 list->head->prev=killme->prev;
270 if(killme->prev !=NULL)
271 killme->prev->next=killme->next;
272 if(killme->next !=NULL)
273 killme->next->prev=killme->prev;
283 * move dl_node from source to dest
284 * if marker == target .
285 * when direction true it moves marker after
286 * when direction false it moves marker before.
287 * to previous if there is no next.
289 void dlist_move(struct dlist *source, struct dlist *dest, struct dl_node *target,int direction)
294 if(target==source->head)
296 //not even going to try
300 // take care of head and marker pointers.
301 if(source->marker==target)
302 _dlist_mark_move(source,direction);
303 if(target ==source->head->next)
304 source->head->next=target->next;
305 if(target==source->head->prev)
306 source->head->prev=target->prev;
312 source->head->next=NULL;
313 source->head->prev=NULL;
317 if(target->prev !=NULL)
318 target->prev->next=target->next;
319 if(target->next !=NULL)
320 target->next->prev=target->prev;
325 _dlist_insert_dlnode(dest,target,direction);
332 * Insert node containing data after end.
334 void dlist_push(Dlist *list,void *data)
336 list->marker=list->head->prev;
337 dlist_insert(list,data,1);
341 * Insert node containing data at start.
344 void dlist_unshift(Dlist *list,void *data)
347 list->marker=list->head->next;
348 dlist_insert(list,data,0);
351 void dlist_unshift_sorted(Dlist *list, void *data,
352 int (*sorter)(void *new_elem, void *old_elem))
354 if (list->count == 0)
355 dlist_unshift(list, data);
357 list->marker=list->head->next;
358 dlist_insert_sorted(list, data, sorter);
363 * Remove end node from list.
364 * Return pointer to data in removed node.
368 void *dlist_pop(Dlist *list)
370 return(_dlist_remove(list,list->head->prev,0));
374 * Remove start node from list.
375 * Return pointer to data in removed node.
379 void *dlist_shift(Dlist *list)
381 return(_dlist_remove(list,list->head->next,1));
386 * destroy the list freeing all memory
390 void dlist_destroy(Dlist *list)
396 while (dlist_mark(list)) {
397 dlist_delete(list,1);
404 * Return void pointer to list_data element matching comp function criteria
406 * Does not move the marker.
409 void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *))
411 /* test the comp function on each node */
412 struct dl_node *nodepointer;
413 dlist_for_each_nomark(list,nodepointer)
414 if(comp(target,nodepointer->data))
415 return(nodepointer->data);
420 * Apply the node_operation function to each data node in the list
422 void dlist_transform(struct dlist *list, void (*node_operation)(void *))
424 struct dl_node *nodepointer;
425 dlist_for_each_nomark(list,nodepointer)
426 node_operation(nodepointer->data);
430 * insert new into list in sorted order
431 * sorter function in form int sorter(new,ith)
432 * must return 1 for when new should go before ith
434 * return pointer to inserted node
435 * NOTE: assumes list is already sorted
437 void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *))
439 for(dlist_start(list),dlist_next(list); \
440 list->marker!=list->head && !sorter(new,list->marker->data);dlist_next(list));
441 return(dlist_insert_before(list,new));
445 * NOTE: internal use only
447 int _dlist_merge(struct dlist *listsource, struct dlist *listdest, unsigned int passcount, int (*compare)(void *, void *))
450 struct dl_node *l1head;
451 struct dl_node *l2head;
452 struct dl_node *target;
453 unsigned int l1count=0;
454 unsigned int l2count=0;
455 unsigned int mergecount=0;
456 while(listsource->count>0)
458 l1head=listsource->head->next;
460 while((l1count<passcount)&&(l2head!=listsource->head))
465 // so now we have two lists to merge
467 if(l2head==listsource->head)
475 while(l1count>0 || l2count>0)
478 if((l2count>0)&&(l1count>0))
480 // we have things to merge
481 int result=compare(l1head->data,l2head->data);
487 dlist_move(listsource,listdest,target,1);
489 if(l2head==listsource->head)
497 dlist_move(listsource,listdest,target,1);
503 // only have l1 to work with
508 dlist_move(listsource,listdest,target,1);
514 // only have l2 to work with
517 if(l2head==listsource->head)
525 dlist_move(listsource,listdest,target,1);
531 { //nothing left and this should be unreachable
539 * mergesort the list based on compare
540 * compare function in form int sorter(void * a,void * b)
541 * must return >0 for a after b
542 * must return <0 for a before b
545 * NOTE: mergesort changes the mark pointer
547 void dlist_sort_custom(struct dlist *list, int (*compare)(void *, void *))
550 struct dlist *listsource, *listdest, *swap;
551 struct dlist *templist;
552 unsigned int passcount = 1;
553 unsigned int mergecount = 1;
556 templist = dlist_new(list->data_size);
558 // do nothing if there isn't anything to sort
561 if(listsource->count<2)
569 mergecount=_dlist_merge(listsource, listdest, passcount, compare);
572 passcount=passcount*2;
580 // now put the input list pointers right
581 // list pointers = newlist pointers
582 // including the forward and next nodes prev and back pointers
585 list->marker = listdest->marker;
586 list->count = listdest->count;
587 list->data_size = listdest->data_size;
588 list->del_func = listdest->del_func;
589 list->head->prev = listdest->head->prev;
590 list->head->next = listdest->head->next;
591 list->head->data = listdest->head->data;
592 list->head->next->prev=list->head;
593 list->head->prev->next=list->head;
594 templist->head->next=NULL;
595 templist->head->prev=NULL;
603 dlist_destroy(templist);
608 /* internal use function
609 swaps elements a and b
610 No sense in juggling node pointers when we can just swap the data pointers
613 void _dlist_swap(struct dlist *list, struct dl_node *a, struct dl_node *b)