lst.c revision 1.5 1 /* $NetBSD: lst.c,v 1.5 2020/08/21 02:20:47 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "lst.h"
36 #include "make_malloc.h"
37
38 #ifndef MAKE_NATIVE
39 static char rcsid[] = "$NetBSD: lst.c,v 1.5 2020/08/21 02:20:47 rillig Exp $";
40 #else
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: lst.c,v 1.5 2020/08/21 02:20:47 rillig Exp $");
44 #endif /* not lint */
45 #endif
46
47 typedef struct ListNode {
48 struct ListNode *prevPtr; /* previous element in list */
49 struct ListNode *nextPtr; /* next in list */
50 unsigned int useCount: 8, /* Count of functions using the node.
51 * node may not be deleted until count
52 * goes to 0 */
53 flags: 8; /* Node status flags */
54 void *datum; /* datum associated with this element */
55 } *ListNode;
56 /*
57 * Flags required for synchronization
58 */
59 #define LN_DELETED 0x0001 /* List node should be removed when done */
60
61 typedef enum {
62 Head, Middle, Tail, Unknown
63 } Where;
64
65 typedef struct List {
66 ListNode firstPtr; /* first node in list */
67 ListNode lastPtr; /* last node in list */
68 /*
69 * fields for sequential access
70 */
71 Where atEnd; /* Where in the list the last access was */
72 Boolean isOpen; /* true if list has been Lst_Open'ed */
73 ListNode curPtr; /* current node, if open. NULL if
74 * *just* opened */
75 ListNode prevPtr; /* Previous node, if open. Used by
76 * Lst_Remove */
77 } *List;
78
79 /*
80 * PAlloc (var, ptype) --
81 * Allocate a pointer-typedef structure 'ptype' into the variable 'var'
82 */
83 #define PAlloc(var, ptype) \
84 var = (ptype) bmake_malloc(sizeof *(var))
85
86 /*
87 * LstValid --
88 * Return TRUE if the list is valid
89 */
90 static Boolean
91 LstValid(Lst l)
92 {
93 return l != NULL;
94 }
95
96 /*
97 * LstNodeValid --
98 * Return TRUE if the list node is valid
99 */
100 static Boolean
101 LstNodeValid(LstNode ln)
102 {
103 return ln != NULL;
104 }
105
106 /*
107 * LstIsEmpty (l) --
108 * TRUE if the list l is empty.
109 */
110 static Boolean
111 LstIsEmpty(Lst l)
112 {
113 return l->firstPtr == NULL;
114 }
115
116 /* Create and initialize a new, empty list. */
117 Lst
118 Lst_Init(void)
119 {
120 List nList;
121
122 PAlloc (nList, List);
123
124 nList->firstPtr = NULL;
125 nList->lastPtr = NULL;
126 nList->isOpen = FALSE;
127 nList->atEnd = Unknown;
128
129 return nList;
130 }
131
132 /*-
133 *-----------------------------------------------------------------------
134 * Lst_Duplicate --
135 * Duplicate an entire list. If a function to copy a void *is
136 * given, the individual client elements will be duplicated as well.
137 *
138 * Input:
139 * l the list to duplicate
140 * copyProc A function to duplicate each void *
141 *
142 * Results:
143 * The new Lst structure or NULL if failure.
144 *
145 * Side Effects:
146 * A new list is created.
147 *-----------------------------------------------------------------------
148 */
149 Lst
150 Lst_Duplicate(Lst l, DuplicateProc *copyProc)
151 {
152 Lst nl;
153 ListNode ln;
154 List list = l;
155
156 if (!LstValid(l)) {
157 return NULL;
158 }
159
160 nl = Lst_Init();
161 if (nl == NULL) {
162 return NULL;
163 }
164
165 ln = list->firstPtr;
166 while (ln != NULL) {
167 if (copyProc != NULL) {
168 if (Lst_AtEnd(nl, copyProc(ln->datum)) == FAILURE) {
169 return NULL;
170 }
171 } else if (Lst_AtEnd(nl, ln->datum) == FAILURE) {
172 return NULL;
173 }
174
175 ln = ln->nextPtr;
176 }
177
178 return nl;
179 }
180
181 /*-
182 *-----------------------------------------------------------------------
183 * Lst_Destroy --
184 * Destroy a list and free all its resources. If the freeProc is
185 * given, it is called with the datum from each node in turn before
186 * the node is freed.
187 *
188 * Results:
189 * None.
190 *
191 * Side Effects:
192 * The given list is freed in its entirety.
193 *
194 *-----------------------------------------------------------------------
195 */
196 void
197 Lst_Destroy(Lst list, FreeProc *freeProc)
198 {
199 ListNode ln;
200 ListNode tln = NULL;
201
202 if (list == NULL)
203 return;
204
205 /* To ease scanning */
206 if (list->lastPtr != NULL)
207 list->lastPtr->nextPtr = NULL;
208 else {
209 free(list);
210 return;
211 }
212
213 if (freeProc) {
214 for (ln = list->firstPtr; ln != NULL; ln = tln) {
215 tln = ln->nextPtr;
216 freeProc(ln->datum);
217 free(ln);
218 }
219 } else {
220 for (ln = list->firstPtr; ln != NULL; ln = tln) {
221 tln = ln->nextPtr;
222 free(ln);
223 }
224 }
225
226 free(list);
227 }
228
229 /*
230 * Functions to modify a list
231 */
232
233 /*-
234 *-----------------------------------------------------------------------
235 * Lst_InsertBefore --
236 * Insert a new node with the given piece of data before the given
237 * node in the given list.
238 *
239 * Input:
240 * l list to manipulate
241 * ln node before which to insert d
242 * d datum to be inserted
243 *
244 * Results:
245 * SUCCESS or FAILURE.
246 *
247 * Side Effects:
248 * the firstPtr field will be changed if ln is the first node in the
249 * list.
250 *
251 *-----------------------------------------------------------------------
252 */
253 ReturnStatus
254 Lst_InsertBefore(Lst l, LstNode ln, void *d)
255 {
256 ListNode nLNode; /* new lnode for d */
257 ListNode lNode = ln;
258 List list = l;
259
260
261 /*
262 * check validity of arguments
263 */
264 if (LstValid(l) && (LstIsEmpty(l) && ln == NULL))
265 goto ok;
266
267 if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
268 return FAILURE;
269 }
270
271 ok:
272 PAlloc (nLNode, ListNode);
273
274 nLNode->datum = d;
275 nLNode->useCount = nLNode->flags = 0;
276
277 if (ln == NULL) {
278 nLNode->prevPtr = nLNode->nextPtr = NULL;
279 list->firstPtr = list->lastPtr = nLNode;
280 } else {
281 nLNode->prevPtr = lNode->prevPtr;
282 nLNode->nextPtr = lNode;
283
284 if (nLNode->prevPtr != NULL) {
285 nLNode->prevPtr->nextPtr = nLNode;
286 }
287 lNode->prevPtr = nLNode;
288
289 if (lNode == list->firstPtr) {
290 list->firstPtr = nLNode;
291 }
292 }
293
294 return SUCCESS;
295 }
296
297 /*-
298 *-----------------------------------------------------------------------
299 * Lst_InsertAfter --
300 * Create a new node and add it to the given list after the given node.
301 *
302 * Input:
303 * l affected list
304 * ln node after which to append the datum
305 * d said datum
306 *
307 * Results:
308 * SUCCESS if all went well.
309 *
310 * Side Effects:
311 * A new ListNode is created and linked in to the List. The lastPtr
312 * field of the List will be altered if ln is the last node in the
313 * list. lastPtr and firstPtr will alter if the list was empty and
314 * ln was NULL.
315 *
316 *-----------------------------------------------------------------------
317 */
318 ReturnStatus
319 Lst_InsertAfter(Lst l, LstNode ln, void *d)
320 {
321 List list;
322 ListNode lNode;
323 ListNode nLNode;
324
325 if (LstValid(l) && (ln == NULL && LstIsEmpty(l))) {
326 goto ok;
327 }
328
329 if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
330 return FAILURE;
331 }
332 ok:
333
334 list = l;
335 lNode = ln;
336
337 PAlloc (nLNode, ListNode);
338 nLNode->datum = d;
339 nLNode->useCount = nLNode->flags = 0;
340
341 if (lNode == NULL) {
342 nLNode->nextPtr = nLNode->prevPtr = NULL;
343 list->firstPtr = list->lastPtr = nLNode;
344 } else {
345 nLNode->prevPtr = lNode;
346 nLNode->nextPtr = lNode->nextPtr;
347
348 lNode->nextPtr = nLNode;
349 if (nLNode->nextPtr != NULL) {
350 nLNode->nextPtr->prevPtr = nLNode;
351 }
352
353 if (lNode == list->lastPtr) {
354 list->lastPtr = nLNode;
355 }
356 }
357
358 return SUCCESS;
359 }
360
361 /*-
362 *-----------------------------------------------------------------------
363 * Lst_AtFront --
364 * Place a piece of data at the front of a list
365 *
366 * Results:
367 * SUCCESS or FAILURE
368 *
369 * Side Effects:
370 * A new ListNode is created and stuck at the front of the list.
371 * hence, firstPtr (and possible lastPtr) in the list are altered.
372 *
373 *-----------------------------------------------------------------------
374 */
375 ReturnStatus
376 Lst_AtFront(Lst l, void *d)
377 {
378 LstNode front;
379
380 front = Lst_First(l);
381 return Lst_InsertBefore(l, front, d);
382 }
383
384 /*-
385 *-----------------------------------------------------------------------
386 * Lst_AtEnd --
387 * Add a node to the end of the given list
388 *
389 * Input:
390 * l List to which to add the datum
391 * d Datum to add
392 *
393 * Results:
394 * SUCCESS if life is good.
395 *
396 * Side Effects:
397 * A new ListNode is created and added to the list.
398 *
399 *-----------------------------------------------------------------------
400 */
401 ReturnStatus
402 Lst_AtEnd(Lst l, void *d)
403 {
404 LstNode end;
405
406 end = Lst_Last(l);
407 return Lst_InsertAfter(l, end, d);
408 }
409
410 /*-
411 *-----------------------------------------------------------------------
412 * Lst_Remove --
413 * Remove the given node from the given list.
414 *
415 * Results:
416 * SUCCESS or FAILURE.
417 *
418 * Side Effects:
419 * The list's firstPtr will be set to NULL if ln is the last
420 * node on the list. firsPtr and lastPtr will be altered if ln is
421 * either the first or last node, respectively, on the list.
422 *
423 *-----------------------------------------------------------------------
424 */
425 ReturnStatus
426 Lst_Remove(Lst l, LstNode ln)
427 {
428 List list = l;
429 ListNode lNode = ln;
430
431 if (!LstValid(l) || !LstNodeValid(ln)) {
432 return FAILURE;
433 }
434
435 /*
436 * unlink it from the list
437 */
438 if (lNode->nextPtr != NULL) {
439 lNode->nextPtr->prevPtr = lNode->prevPtr;
440 }
441 if (lNode->prevPtr != NULL) {
442 lNode->prevPtr->nextPtr = lNode->nextPtr;
443 }
444
445 /*
446 * if either the firstPtr or lastPtr of the list point to this node,
447 * adjust them accordingly
448 */
449 if (list->firstPtr == lNode) {
450 list->firstPtr = lNode->nextPtr;
451 }
452 if (list->lastPtr == lNode) {
453 list->lastPtr = lNode->prevPtr;
454 }
455
456 /*
457 * Sequential access stuff. If the node we're removing is the current
458 * node in the list, reset the current node to the previous one. If the
459 * previous one was non-existent (prevPtr == NULL), we set the
460 * end to be Unknown, since it is.
461 */
462 if (list->isOpen && (list->curPtr == lNode)) {
463 list->curPtr = list->prevPtr;
464 if (list->curPtr == NULL) {
465 list->atEnd = Unknown;
466 }
467 }
468
469 /*
470 * the only way firstPtr can still point to ln is if ln is the last
471 * node on the list (the list is circular, so lNode->nextptr == lNode in
472 * this case). The list is, therefore, empty and is marked as such
473 */
474 if (list->firstPtr == lNode) {
475 list->firstPtr = NULL;
476 }
477
478 /*
479 * note that the datum is unmolested. The caller must free it as
480 * necessary and as expected.
481 */
482 if (lNode->useCount == 0) {
483 free(ln);
484 } else {
485 lNode->flags |= LN_DELETED;
486 }
487
488 return SUCCESS;
489 }
490
491 /*-
492 *-----------------------------------------------------------------------
493 * Lst_Replace --
494 * Replace the datum in the given node with the new datum
495 *
496 * Results:
497 * SUCCESS or FAILURE.
498 *
499 * Side Effects:
500 * The datum field fo the node is altered.
501 *
502 *-----------------------------------------------------------------------
503 */
504 ReturnStatus
505 Lst_Replace(LstNode ln, void *d)
506 {
507 if (ln == NULL) {
508 return FAILURE;
509 } else {
510 (ln)->datum = d;
511 return SUCCESS;
512 }
513 }
514
515
516 /*
517 * Node-specific functions
518 */
519
520 /*-
521 *-----------------------------------------------------------------------
522 * Lst_First --
523 * Return the first node on the given list.
524 *
525 * Results:
526 * The first node or NULL if the list is empty.
527 *
528 * Side Effects:
529 * None.
530 *
531 *-----------------------------------------------------------------------
532 */
533 LstNode
534 Lst_First(Lst l)
535 {
536 if (!LstValid(l) || LstIsEmpty(l)) {
537 return NULL;
538 } else {
539 return l->firstPtr;
540 }
541 }
542
543 /*-
544 *-----------------------------------------------------------------------
545 * Lst_Last --
546 * Return the last node on the list l.
547 *
548 * Results:
549 * The requested node or NULL if the list is empty.
550 *
551 * Side Effects:
552 * None.
553 *
554 *-----------------------------------------------------------------------
555 */
556 LstNode
557 Lst_Last(Lst l)
558 {
559 if (!LstValid(l) || LstIsEmpty(l)) {
560 return NULL;
561 } else {
562 return l->lastPtr;
563 }
564 }
565
566 /*-
567 *-----------------------------------------------------------------------
568 * Lst_Succ --
569 * Return the successor to the given node on its list.
570 *
571 * Results:
572 * The successor of the node, if it exists (note that on a circular
573 * list, if the node is the only one in the list, it is its own
574 * successor).
575 *
576 * Side Effects:
577 * None.
578 *
579 *-----------------------------------------------------------------------
580 */
581 LstNode
582 Lst_Succ(LstNode ln)
583 {
584 if (ln == NULL) {
585 return NULL;
586 } else {
587 return ln->nextPtr;
588 }
589 }
590
591 /*-
592 *-----------------------------------------------------------------------
593 * Lst_Prev --
594 * Return the predecessor to the given node on its list.
595 *
596 * Results:
597 * The predecessor of the node, if it exists (note that on a circular
598 * list, if the node is the only one in the list, it is its own
599 * predecessor).
600 *
601 * Side Effects:
602 * None.
603 *
604 *-----------------------------------------------------------------------
605 */
606 LstNode
607 Lst_Prev(LstNode ln)
608 {
609 if (ln == NULL) {
610 return NULL;
611 } else {
612 return ln->prevPtr;
613 }
614 }
615
616 /*-
617 *-----------------------------------------------------------------------
618 * Lst_Datum --
619 * Return the datum stored in the given node.
620 *
621 * Results:
622 * The datum or NULL if the node is invalid.
623 *
624 * Side Effects:
625 * None.
626 *
627 *-----------------------------------------------------------------------
628 */
629 void *
630 Lst_Datum(LstNode ln)
631 {
632 if (ln != NULL) {
633 return ln->datum;
634 } else {
635 return NULL;
636 }
637 }
638
639
640 /*
641 * Functions for entire lists
642 */
643
644 /*-
645 *-----------------------------------------------------------------------
646 * Lst_IsEmpty --
647 * Return TRUE if the given list is empty.
648 *
649 * Results:
650 * TRUE if the list is empty, FALSE otherwise.
651 *
652 * Side Effects:
653 * None.
654 *
655 * A list is considered empty if its firstPtr == NULL (or if
656 * the list itself is NULL).
657 *-----------------------------------------------------------------------
658 */
659 Boolean
660 Lst_IsEmpty(Lst l)
661 {
662 return !LstValid(l) || LstIsEmpty(l);
663 }
664
665 /*-
666 *-----------------------------------------------------------------------
667 * Lst_Find --
668 * Find a node on the given list using the given comparison function
669 * and the given datum.
670 *
671 * Results:
672 * The found node or NULL if none matches.
673 *
674 * Side Effects:
675 * None.
676 *
677 *-----------------------------------------------------------------------
678 */
679 LstNode
680 Lst_Find(Lst l, const void *d, int (*cProc)(const void *, const void *))
681 {
682 return Lst_FindFrom(l, Lst_First(l), d, cProc);
683 }
684
685 /*-
686 *-----------------------------------------------------------------------
687 * Lst_FindFrom --
688 * Search for a node starting and ending with the given one on the
689 * given list using the passed datum and comparison function to
690 * determine when it has been found.
691 *
692 * Results:
693 * The found node or NULL
694 *
695 * Side Effects:
696 * None.
697 *
698 *-----------------------------------------------------------------------
699 */
700 LstNode
701 Lst_FindFrom(Lst l, LstNode ln, const void *d,
702 int (*cProc)(const void *, const void *))
703 {
704 ListNode tln;
705
706 if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
707 return NULL;
708 }
709
710 tln = ln;
711
712 do {
713 if ((*cProc)(tln->datum, d) == 0)
714 return tln;
715 tln = tln->nextPtr;
716 } while (tln != ln && tln != NULL);
717
718 return NULL;
719 }
720
721 /*-
722 * See if a given datum is on a given list.
723 */
724 LstNode
725 Lst_Member(Lst l, void *d)
726 {
727 List list = l;
728 ListNode lNode;
729
730 if (list == NULL) {
731 return NULL;
732 }
733 lNode = list->firstPtr;
734 if (lNode == NULL) {
735 return NULL;
736 }
737
738 do {
739 if (lNode->datum == d) {
740 return lNode;
741 }
742 lNode = lNode->nextPtr;
743 } while (lNode != NULL && lNode != list->firstPtr);
744
745 return NULL;
746 }
747
748 /*-
749 *-----------------------------------------------------------------------
750 * Lst_ForEach --
751 * Apply the given function to each element of the given list. The
752 * function should return 0 if Lst_ForEach should continue and non-
753 * zero if it should abort.
754 *
755 * Results:
756 * None.
757 *
758 * Side Effects:
759 * Only those created by the passed-in function.
760 *
761 *-----------------------------------------------------------------------
762 */
763 /*VARARGS2*/
764 int
765 Lst_ForEach(Lst l, int (*proc)(void *, void *), void *d)
766 {
767 return Lst_ForEachFrom(l, Lst_First(l), proc, d);
768 }
769
770 /*-
771 *-----------------------------------------------------------------------
772 * Lst_ForEachFrom --
773 * Apply the given function to each element of the given list,
774 * starting from a given point.
775 *
776 * If the list is circular, the application will wrap around to the
777 * beginning of the list again.
778 *
779 * The function should return 0 if traversal should continue, and
780 * non-zero if it should abort.
781 *
782 * Results:
783 * None.
784 *
785 * Side Effects:
786 * Only those created by the passed-in function.
787 *
788 *-----------------------------------------------------------------------
789 */
790 /*VARARGS2*/
791 int
792 Lst_ForEachFrom(Lst l, LstNode ln, int (*proc)(void *, void *),
793 void *d)
794 {
795 ListNode tln = ln;
796 List list = l;
797 ListNode next;
798 Boolean done;
799 int result;
800
801 if (!LstValid(list) || LstIsEmpty(list)) {
802 return 0;
803 }
804
805 do {
806 /*
807 * Take care of having the current element deleted out from under
808 * us.
809 */
810
811 next = tln->nextPtr;
812
813 /*
814 * We're done with the traversal if
815 * - the next node to examine is the first in the queue or
816 * doesn't exist and
817 * - nothing's been added after the current node (check this
818 * after proc() has been called).
819 */
820 done = (next == NULL || next == list->firstPtr);
821
822 (void)tln->useCount++;
823 result = (*proc)(tln->datum, d);
824 (void)tln->useCount--;
825
826 /*
827 * Now check whether a node has been added.
828 * Note: this doesn't work if this node was deleted before
829 * the new node was added.
830 */
831 if (next != tln->nextPtr) {
832 next = tln->nextPtr;
833 done = 0;
834 }
835
836 if (tln->flags & LN_DELETED) {
837 free((char *)tln);
838 }
839 tln = next;
840 } while (!result && !LstIsEmpty(list) && !done);
841
842 return result;
843 }
844
845 /*-
846 *-----------------------------------------------------------------------
847 * Lst_Concat --
848 * Concatenate two lists. New elements are created to hold the data
849 * elements, if specified, but the elements themselves are not copied.
850 * If the elements should be duplicated to avoid confusion with another
851 * list, the Lst_Duplicate function should be called first.
852 * If LST_CONCLINK is specified, the second list is destroyed since
853 * its pointers have been corrupted and the list is no longer useable.
854 *
855 * Input:
856 * l1 The list to which l2 is to be appended
857 * l2 The list to append to l1
858 * flags LST_CONCNEW if LstNode's should be duplicated
859 * LST_CONCLINK if should just be relinked
860 *
861 * Results:
862 * SUCCESS if all went well. FAILURE otherwise.
863 *
864 * Side Effects:
865 * New elements are created and appended the first list.
866 *-----------------------------------------------------------------------
867 */
868 ReturnStatus
869 Lst_Concat(Lst l1, Lst l2, int flags)
870 {
871 ListNode ln; /* original LstNode */
872 ListNode nln; /* new LstNode */
873 ListNode last; /* the last element in the list. Keeps
874 * bookkeeping until the end */
875 List list1 = l1;
876 List list2 = l2;
877
878 if (!LstValid(l1) || !LstValid(l2)) {
879 return FAILURE;
880 }
881
882 if (flags == LST_CONCLINK) {
883 if (list2->firstPtr != NULL) {
884 /*
885 * We set the nextPtr of the
886 * last element of list two to be NIL to make the loop easier and
887 * so we don't need an extra case should the first list turn
888 * out to be non-circular -- the final element will already point
889 * to NIL space and the first element will be untouched if it
890 * existed before and will also point to NIL space if it didn't.
891 */
892 list2->lastPtr->nextPtr = NULL;
893 /*
894 * So long as the second list isn't empty, we just link the
895 * first element of the second list to the last element of the
896 * first list. If the first list isn't empty, we then link the
897 * last element of the list to the first element of the second list
898 * The last element of the second list, if it exists, then becomes
899 * the last element of the first list.
900 */
901 list2->firstPtr->prevPtr = list1->lastPtr;
902 if (list1->lastPtr != NULL) {
903 list1->lastPtr->nextPtr = list2->firstPtr;
904 } else {
905 list1->firstPtr = list2->firstPtr;
906 }
907 list1->lastPtr = list2->lastPtr;
908 }
909 free(l2);
910 } else if (list2->firstPtr != NULL) {
911 /*
912 * We set the nextPtr of the last element of list 2 to be nil to make
913 * the loop less difficult. The loop simply goes through the entire
914 * second list creating new LstNodes and filling in the nextPtr, and
915 * prevPtr to fit into l1 and its datum field from the
916 * datum field of the corresponding element in l2. The 'last' node
917 * follows the last of the new nodes along until the entire l2 has
918 * been appended. Only then does the bookkeeping catch up with the
919 * changes. During the first iteration of the loop, if 'last' is nil,
920 * the first list must have been empty so the newly-created node is
921 * made the first node of the list.
922 */
923 list2->lastPtr->nextPtr = NULL;
924 for (last = list1->lastPtr, ln = list2->firstPtr;
925 ln != NULL;
926 ln = ln->nextPtr)
927 {
928 PAlloc (nln, ListNode);
929 nln->datum = ln->datum;
930 if (last != NULL) {
931 last->nextPtr = nln;
932 } else {
933 list1->firstPtr = nln;
934 }
935 nln->prevPtr = last;
936 nln->flags = nln->useCount = 0;
937 last = nln;
938 }
939
940 /*
941 * Finish bookkeeping. The last new element becomes the last element
942 * of list one.
943 */
944 list1->lastPtr = last;
945 last->nextPtr = NULL;
946 }
947
948 return SUCCESS;
949 }
950
951
952 /*
953 * these functions are for dealing with a list as a table, of sorts.
954 * An idea of the "current element" is kept and used by all the functions
955 * between Lst_Open() and Lst_Close().
956 *
957 * The sequential functions access the list in a slightly different way.
958 * CurPtr points to their idea of the current node in the list and they
959 * access the list based on it.
960 *
961 * If the list is circular, Lst_Next and Lst_Prev will go around the list
962 * forever. Lst_IsAtEnd must be used to determine when to stop.
963 */
964
965 /*-
966 *-----------------------------------------------------------------------
967 * Lst_Open --
968 * Open a list for sequential access. A list can still be searched,
969 * etc., without confusing these functions.
970 *
971 * Results:
972 * SUCCESS or FAILURE.
973 *
974 * Side Effects:
975 * isOpen is set TRUE and curPtr is set to NULL so the
976 * other sequential functions know it was just opened and can choose
977 * the first element accessed based on this.
978 *
979 *-----------------------------------------------------------------------
980 */
981 ReturnStatus
982 Lst_Open(Lst l)
983 {
984 if (LstValid(l) == FALSE) {
985 return FAILURE;
986 }
987 l->isOpen = TRUE;
988 l->atEnd = LstIsEmpty(l) ? Head : Unknown;
989 l->curPtr = NULL;
990
991 return SUCCESS;
992 }
993
994 /*-
995 *-----------------------------------------------------------------------
996 * Lst_Next --
997 * Return the next node for the given list.
998 *
999 * Results:
1000 * The next node or NULL if the list has yet to be opened. Also
1001 * if the list is non-circular and the end has been reached, NULL
1002 * is returned.
1003 *
1004 * Side Effects:
1005 * the curPtr field is updated.
1006 *
1007 *-----------------------------------------------------------------------
1008 */
1009 LstNode
1010 Lst_Next(Lst l)
1011 {
1012 ListNode tln;
1013 List list = l;
1014
1015 if ((LstValid(l) == FALSE) ||
1016 (list->isOpen == FALSE)) {
1017 return NULL;
1018 }
1019
1020 list->prevPtr = list->curPtr;
1021
1022 if (list->curPtr == NULL) {
1023 if (list->atEnd == Unknown) {
1024 /*
1025 * If we're just starting out, atEnd will be Unknown.
1026 * Then we want to start this thing off in the right
1027 * direction -- at the start with atEnd being Middle.
1028 */
1029 list->curPtr = tln = list->firstPtr;
1030 list->atEnd = Middle;
1031 } else {
1032 tln = NULL;
1033 list->atEnd = Tail;
1034 }
1035 } else {
1036 tln = list->curPtr->nextPtr;
1037 list->curPtr = tln;
1038
1039 if (tln == list->firstPtr || tln == NULL) {
1040 /*
1041 * If back at the front, then we've hit the end...
1042 */
1043 list->atEnd = Tail;
1044 } else {
1045 /*
1046 * Reset to Middle if gone past first.
1047 */
1048 list->atEnd = Middle;
1049 }
1050 }
1051
1052 return tln;
1053 }
1054
1055 /*-
1056 *-----------------------------------------------------------------------
1057 * Lst_Close --
1058 * Close a list which was opened for sequential access.
1059 *
1060 * Input:
1061 * l The list to close
1062 *
1063 * Results:
1064 * None.
1065 *
1066 * Side Effects:
1067 * The list is closed.
1068 *
1069 *-----------------------------------------------------------------------
1070 */
1071 void
1072 Lst_Close(Lst l)
1073 {
1074 List list = l;
1075
1076 if (LstValid(l) == TRUE) {
1077 list->isOpen = FALSE;
1078 list->atEnd = Unknown;
1079 }
1080 }
1081
1082
1083 /*
1084 * for using the list as a queue
1085 */
1086
1087 /*-
1088 *-----------------------------------------------------------------------
1089 * Lst_EnQueue --
1090 * Add the datum to the tail of the given list.
1091 *
1092 * Results:
1093 * SUCCESS or FAILURE as returned by Lst_InsertAfter.
1094 *
1095 * Side Effects:
1096 * the lastPtr field is altered all the time and the firstPtr field
1097 * will be altered if the list used to be empty.
1098 *
1099 *-----------------------------------------------------------------------
1100 */
1101 ReturnStatus
1102 Lst_EnQueue(Lst l, void *d)
1103 {
1104 if (LstValid(l) == FALSE) {
1105 return FAILURE;
1106 }
1107
1108 return Lst_InsertAfter(l, Lst_Last(l), d);
1109 }
1110
1111 /*-
1112 *-----------------------------------------------------------------------
1113 * Lst_DeQueue --
1114 * Remove and return the datum at the head of the given list.
1115 *
1116 * Results:
1117 * The datum in the node at the head or NULL if the list
1118 * is empty.
1119 *
1120 * Side Effects:
1121 * The head node is removed from the list.
1122 *
1123 *-----------------------------------------------------------------------
1124 */
1125 void *
1126 Lst_DeQueue(Lst l)
1127 {
1128 void *rd;
1129 ListNode tln;
1130
1131 tln = Lst_First(l);
1132 if (tln == NULL) {
1133 return NULL;
1134 }
1135
1136 rd = tln->datum;
1137 if (Lst_Remove(l, tln) == FAILURE) {
1138 return NULL;
1139 } else {
1140 return rd;
1141 }
1142 }
1143