(Temporarily) set "animate" to "none" by default (broken feature).
[gf1.git] / linklist.c
blob0eb8c983462135a35ec2d8725402bed10fcca6f9
1 /*
2 ** FACILITY: linklist.c
3 **
4 ** MODULE DESCRIPTION:
5 **
6 ** general routines for linked lists
7 ** + newlist: initialize new list
8 ** + insertll: insert item in list
9 ** + searchll: search for item in list
10 ** + emptyll: delete all items from a linked list
11 ** + doll: run a function for every item from the linked list
12 ** + getnextll: get the next item from the list
13 ** + copyll: copy a complete linked list
14 ** + llitembynr: return pointer to the data of item nr of the list
15 ** + llrembynr: remove item nr from the list and return pointer
16 ** to the data
17 ** + pushll: add item to the end of a linked list
18 ** + findlastll: find last item in the last that
19 ** corresponds with what we want
20 ** (+ delll: delete an item from the list)
22 ** AUTHORS:
24 ** Kurt Van den Branden
26 ** CREATION DATE: 22/01/97
28 ** DESIGN ISSUES:
30 ** these functions should be as general as possible
31 ** you can put any data at the nodes from the list
32 ** you have to provide your own compare-routine
36 ** Copyright (C) 1998 Kurt Van den Branden
38 ** This program is free software; you can redistribute it and/or modify
39 ** it under the terms of the GNU General Public License as published by
40 ** the Free Software Foundation; either version 2 of the License, or
41 ** (at your option) any later version.
42 **
43 ** This program is distributed in the hope that it will be useful,
44 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
45 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 ** GNU General Public License for more details.
47 **
48 ** You should have received a copy of the GNU General Public License
49 ** along with this program; if not, write to the Free Software
50 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
56 ** INCLUDE FILES
60 #include <stdio.h>
61 #include <malloc.h>
62 #include "linklist.h"
64 #ifdef FASTLLIST
66 **++
67 ** FUNCTIONAL DESCRIPTION:
69 ** newlist: initialize a new linked list
71 ** FORMAL PARAMETERS:
73 ** head: pointer to header of new list
75 ** RETURN VALUE:
77 ** 0: OK
78 ** -1: error
80 **--
82 int newlist (listheader * head)
84 head->firstitem = NULL;
85 head->curnr = -1;
86 head->curptr = NULL;
87 head->lastptr = NULL;
88 return (0);
90 #endif
93 **++
94 ** FUNCTIONAL DESCRIPTION:
96 ** insertll: insert item in a linked list
98 ** FORMAL PARAMETERS:
100 ** head: pointer to head of linked list
101 ** newitem: void-pointer to item that has to be inserted in the list
102 ** compfunction: pointer to a function that can compare to items
103 ** it should return -1, 0, 1. to indicate that the
104 ** first item is smaller, equal, larger than the second
106 ** RETURN VALUE:
108 ** 0 : OK
109 ** -1: error
110 ** 1 : item already in the list (0 was returned by compfunction)
112 **--
114 int insertll (listheader * head, void * newitem, int (* compfunction)())
116 listitem *curitem,
117 *previtem,
118 *dumitem;
119 int compresult;
121 #ifdef FASTLLIST
122 head->curnr = -1;
123 head->curptr = NULL;
124 #endif
126 if (head->firstitem == NULL)
128 dumitem = (listitem *) malloc (sizeof (listitem));
129 dumitem->itemptr = newitem;
130 dumitem->next = NULL;
131 head->firstitem = dumitem;
132 #ifdef FASTLLIST
133 head->lastptr = dumitem;
134 #endif
135 return (0);
138 previtem = NULL;
139 curitem = head->firstitem;
140 while (curitem != NULL)
142 compresult = (* compfunction)(curitem->itemptr, newitem);
143 if (compresult == 0)
145 return (1);
147 else if (compresult > 0)
149 dumitem = (listitem *) malloc (sizeof (listitem));
150 dumitem->itemptr = newitem;
151 dumitem->next = NULL;
152 if (previtem == NULL)
154 head->firstitem = dumitem;
156 else
158 previtem->next = dumitem;
160 dumitem->next = curitem;
161 return (0);
163 else
165 previtem = curitem;
166 curitem = curitem->next;
169 dumitem = (listitem *) malloc (sizeof (listitem));
170 dumitem->itemptr = newitem;
171 dumitem->next = NULL;
172 previtem->next = dumitem;
174 #ifdef FASTLLIST
175 head->lastptr = dumitem;
176 #endif
178 return (0);
183 **++
184 ** FUNCTIONAL DESCRIPTION:
186 ** llremitem: delete item from a linked list
188 ** FORMAL PARAMETERS:
190 ** head: pointer to head of linked list
191 ** newitem: void-pointer to an item that has to be compared with
192 ** what has to be deleted
193 ** compfunction: pointer to a function that can compare to items
194 ** it should return -1, 0, 1. to indicate that the
195 ** first item is smaller, equal, larger than the second
197 ** RETURN VALUE:
199 ** pointer to data (has to be deleted by calling procedure)
200 ** NULL
202 **--
204 void * llremitem (listheader * head, void * newitem, int (* compfunction)())
206 listitem *curitem,
207 *previtem;
208 int compresult;
209 void * data;
211 if ((head == NULL) || (head->firstitem == NULL))
213 return (NULL);
216 previtem = NULL;
217 curitem = head->firstitem;
218 while (curitem != NULL)
220 compresult = (* compfunction)(curitem->itemptr, newitem);
221 if (compresult == 0)
223 if (previtem == NULL)
225 head->firstitem = curitem->next;
227 else
229 previtem->next = curitem->next;
231 #ifdef FASTLLIST
232 if (head->lastptr == curitem)
233 head->lastptr = previtem;
234 head->curptr = NULL;
235 head->curnr = -1;
236 #endif
237 data = curitem->itemptr;
238 free (curitem);
239 return (data);
241 else if (compresult > 0)
243 return (NULL);
245 else
247 previtem = curitem;
248 curitem = curitem->next;
252 return (NULL);
257 **++
258 ** FUNCTIONAL DESCRIPTION:
260 ** pushll: add item to the end of a linked list
262 ** FORMAL PARAMETERS:
264 ** head: pointer to head of linked list
265 ** newitem: void-pointer to item that has to be inserted in the list
267 ** RETURN VALUE:
269 ** 0 : OK
270 ** -1: error
272 **--
274 int pushll (listheader * head, void * newitem)
276 listitem *dumitem;
278 dumitem = (listitem *) malloc (sizeof (listitem));
279 dumitem->itemptr = newitem;
280 dumitem->next = NULL;
282 #ifdef FASTLLIST
283 if (head->lastptr == NULL)
284 head->firstitem = dumitem;
285 else
286 head->lastptr->next = dumitem;
287 head->lastptr = dumitem;
288 #else
289 listitem *curitem,
290 *previtem;
292 if (head->firstitem == NULL)
294 head->firstitem = dumitem;
295 return (0);
298 previtem = NULL;
299 curitem = head->firstitem;
300 while (curitem != NULL)
302 previtem = curitem;
303 curitem = curitem->next;
305 previtem->next = dumitem;
306 #endif
308 return (0);
313 **++
314 ** FUNCTIONAL DESCRIPTION:
316 ** unshiftll: add item to the beginning of a linked list
318 ** FORMAL PARAMETERS:
320 ** head: pointer to head of linked list
321 ** newitem: void-pointer to item that has to be inserted in the list
323 ** RETURN VALUE:
325 ** 0 : OK
326 ** -1: error
328 **--
330 int unshiftll (listheader * head, void * newitem)
332 listitem *dumitem;
334 dumitem = (listitem *) malloc (sizeof (listitem));
335 dumitem->itemptr = newitem;
336 dumitem->next = head->firstitem;
337 head->firstitem = dumitem;
339 #ifdef FASTLLIST
340 if (head->lastptr == NULL)
341 head->lastptr = dumitem;
342 head->curptr = NULL;
343 head->curnr = -1;
344 #endif
346 return (0);
351 **++
352 ** FUNCTIONAL DESCRIPTION:
354 ** searchll : find an item in a linked list
356 ** FORMAL PARAMETERS:
358 ** head: pointer to listhead
359 ** item: pointer to item to search for
360 ** compfunction: pointer to a function that can compare to items
361 ** it should return -1, 0, 1. to indicate that the
362 ** first item is smaller, equal, larger than the second
364 ** RETURN VALUE:
366 ** pointer to the found item, or NULL if not found
368 **--
370 void * searchll (listheader * head, void * item, int (* compfunction)())
372 listitem * curitem;
373 int compresult;
375 curitem = head->firstitem;
376 while (curitem != NULL)
378 compresult = (* compfunction)(curitem->itemptr, item);
379 if (compresult == 0)
381 return (curitem->itemptr);
383 else if (compresult > 0)
385 return (NULL);
387 curitem = curitem->next;
389 return (NULL);
394 **++
395 ** FUNCTIONAL DESCRIPTION:
397 ** emptyll: remove all items from a linked list
399 ** FORMAL PARAMETERS:
401 ** head: pointer to the listheader
402 ** delfunction: pointer to a function that is called to delete each
403 ** item from the list, it is called with 1 parameter,
404 ** a void-pointer to the item, no return-value is expected
406 ** RETURN VALUE:
408 ** 0: OK
409 ** -1: error
411 **--
413 int emptyll (listheader * head, void (* delfunction)(void *))
415 listitem * curitem,
416 * dumitem;
418 if (head == NULL)
420 return (0);
423 curitem = head->firstitem;
424 while (curitem != NULL)
426 (* delfunction)(curitem->itemptr);
427 dumitem = curitem->next;
428 free (curitem);
429 curitem = dumitem;
431 head->firstitem = NULL;
433 #ifdef FASTLLIST
434 head->lastptr = NULL;
435 head->curptr = NULL;
436 head->curnr = -1;
437 #endif
439 return (0);
444 **++
445 ** FUNCTIONAL DESCRIPTION:
447 ** doll: execute a function for all the items from a linked list
449 ** FORMAL PARAMETERS:
451 ** head: pointer to the listheader
452 ** dofunction: pointer to a function that is called for each
453 ** item from the list, it is called with 1 parameter,
454 ** a void-pointer to the item, no return-value is expected
456 ** RETURN VALUE:
458 ** 0: OK
459 ** -1: error
461 **--
463 int doll (listheader * head, void (* dofunction)())
465 listitem * curitem;
467 curitem = head->firstitem;
468 while (curitem != NULL)
470 (* dofunction)(curitem->itemptr);
471 curitem = curitem->next;
473 return (0);
478 **++
479 ** FUNCTIONAL DESCRIPTION:
481 ** llitembynr: return the data for item nr from the list
483 ** FORMAL PARAMETERS:
485 ** head: pointer to the listheader
486 ** nr : item to return, the first item of the list = 1
488 ** RETURN VALUE:
490 ** pointer to data
491 ** NULL on error
493 **--
495 void * llitembynr (listheader * head, int nr)
497 listitem * curitem;
498 int counter;
500 if (head == NULL)
502 return (NULL);
505 #ifdef FASTLLIST
506 if (head->curnr == (nr - 1))
507 curitem = head->curptr->next;
508 else if (head->curnr == nr)
509 curitem = head->curptr;
510 else
512 #endif
513 curitem = head->firstitem;
514 counter = 1;
515 while ((curitem != NULL) && (counter != nr))
517 counter++;
518 curitem = curitem->next;
520 #ifdef FASTLLIST
522 #endif
524 if (curitem != NULL)
526 #ifdef FASTLLIST
527 head->curnr = nr;
528 head->curptr = curitem;
529 #endif
530 return (curitem->itemptr);
532 return (NULL);
537 **++
538 ** FUNCTIONAL DESCRIPTION:
540 ** llrembynr: remove item nr from the list and return pointer to
541 ** the data from this item
543 ** FORMAL PARAMETERS:
545 ** head: pointer to the listheader
546 ** nr : item to remove, the first item of the list = 1
548 ** RETURN VALUE:
550 ** pointer to data
551 ** NULL on error
553 **--
555 void * llrembynr (listheader * head, int nr)
557 listitem * previtem = NULL,
558 * curitem;
559 int counter;
560 void * data;
562 if (head == NULL)
564 return (NULL);
567 #ifdef FASTLLIST
568 // use information from the last llitembynr
569 if (head->curnr == (nr - 1))
571 previtem = head->curptr;
572 curitem = head->curptr->next;
574 else
576 #endif
577 curitem = head->firstitem;
578 counter = 1;
579 while ((curitem != NULL) && (counter != nr))
581 counter++;
582 previtem = curitem;
583 curitem = curitem->next;
585 #ifdef FASTLLIST
587 #endif
589 if (curitem != NULL)
591 if (previtem == NULL)
593 head->firstitem = curitem->next;
595 else
597 previtem->next = curitem->next;
599 #ifdef FASTLLIST
600 if (curitem == head->lastptr)
601 head->lastptr = previtem;
602 head->curptr = NULL;
603 head->curnr = -1;
604 #endif
606 data = curitem->itemptr;
607 free (curitem);
608 return (data);
611 return (NULL);
616 **++
617 ** FUNCTIONAL DESCRIPTION:
619 ** copyll: copy a complete linked list and all the data in it
621 ** FORMAL PARAMETERS:
623 ** head: pointer to the listheader
624 ** copyfunction: pointer to a function that is called for each
625 ** item from the list, it is called with 1 parameter,
626 ** a void-pointer to the item.
627 ** as return-value, a void pointer to a copy of the input
628 ** item is expected
630 ** RETURN VALUE:
632 ** pointer to the head of the copied list
634 **--
636 listheader * copyll (listheader * head, void * (* copyfunction)())
638 listitem * curitem,
639 * newitem = NULL,
640 * dumitem;
641 listheader * newheader;
643 if (head == NULL)
645 return (NULL);
648 curitem = head->firstitem;
649 newheader = (listheader *) malloc (sizeof (listheader));
650 newheader->firstitem = NULL;
651 while (curitem != NULL)
653 dumitem = (listitem *) malloc (sizeof (listitem));
655 dumitem->next = NULL;
656 dumitem->itemptr = (* copyfunction)(curitem->itemptr);
657 if (newitem == NULL)
659 newheader->firstitem = dumitem;
661 else
663 newitem->next = dumitem;
665 newitem = dumitem;
667 curitem = curitem->next;
670 #ifdef FASTLLIST
671 newheader->lastptr = newitem;
672 newheader->curptr = NULL;
673 newheader->curnr = -1;
674 #endif
676 return (newheader);
681 **++
682 ** FUNCTIONAL DESCRIPTION:
684 ** getnextll
686 ** FORMAL PARAMETERS:
688 ** head: header of the linked list
689 ** curptr: pointer to the current item in the list
691 ** RETURN VALUE:
693 ** pointer to the next item in the linked list (the item after curptr)
694 ** the order of the list is the same order then during inclusion
695 ** if curptr is not found in the list, or there is no next item, the
696 ** NULL is returned.
698 **--
700 void * getnextll (listheader * head, void * curptr)
702 listitem * curitem;
704 curitem = head->firstitem;
705 while ((curitem != NULL) && (curitem->itemptr != curptr))
707 curitem = curitem->next;
709 if ((curitem == NULL) || (curitem->next == NULL))
711 return (NULL);
714 curitem = curitem->next;
715 return (curitem->itemptr);
720 **++
721 ** FUNCTIONAL DESCRIPTION:
723 ** findlastll
725 ** FORMAL PARAMETERS:
727 ** head: header of the linked list
728 ** checkfunction: function to be called with each item from the list
729 ** and returning 1 or 0 (1: item we are looking for, 0: not)
731 ** RETURN VALUE:
733 ** number of the last item in the list that makes the checkfunction
734 ** return 1
736 **--
738 int findlastll (listheader * head, int (* checkfunction)(void*))
740 listitem * curitem;
741 int theitem = 0,
742 nr = 1;
744 curitem = head->firstitem;
745 while (curitem != NULL)
747 if ((* checkfunction) (curitem->itemptr) == 1)
748 theitem = nr;
750 nr++;
751 curitem = curitem->next;
754 return (theitem);
759 ** returns the number of items in the linked list
761 int lllength (listheader * head)
763 listitem * curitem;
764 int counter = 0;
766 curitem = head->firstitem;
767 while (curitem != NULL)
769 counter++;
770 curitem = curitem->next;
773 return (counter);