1 #ifndef LINKED_LIST_H
2 #define LINKED_LIST_H
3 
4 /*
5  * This work was supported by the United States Government, and is
6  * not subject to copyright.
7  *
8  * $Log: linklist.h,v $
9  * Revision 1.3  1997/01/21 19:15:23  dar
10  * made C++ compatible
11  *
12  * Revision 1.2  1993/10/15  18:49:23  libes
13  * CADDETC certified
14  *
15  * Revision 1.5  1993/03/19  20:44:12  libes
16  * added included
17  *
18  * Revision 1.4  1993/01/19  22:17:27  libes
19  * *** empty log message ***
20  *
21  * Revision 1.3  1992/08/18  17:15:40  libes
22  * rm'd extraneous error messages
23  *
24  * Revision 1.2  1992/06/08  18:07:35  libes
25  * prettied up interface to print_objects_when_running
26  */
27 
28 /*************/
29 /* constants */
30 /*************/
31 
32 #define LINK_NULL   (Link)NULL
33 #define LIST_NULL   (Linked_List)NULL
34 
35 /*****************/
36 /* packages used */
37 /*****************/
38 
39 #include <sc_export.h>
40 #include "basic.h"
41 #include "memory.h"
42 
43 /************/
44 /* typedefs */
45 /************/
46 
47 typedef struct Linked_List_ * Linked_List;
48 
49 /****************/
50 /* modules used */
51 /****************/
52 
53 #include "error.h"
54 
55 /***************************/
56 /* hidden type definitions */
57 /***************************/
58 
59 typedef struct Link_ {
60     struct Link_  * next;
61     struct Link_  * prev;
62     Generic     data;
63 } * Link;
64 
65 struct Linked_List_ {
66     Link mark;
67 };
68 
69 /********************/
70 /* global variables */
71 /********************/
72 
73 extern SC_EXPRESS_EXPORT Error ERROR_empty_list;
74 extern SC_EXPRESS_EXPORT struct freelist_head LINK_fl;
75 extern SC_EXPRESS_EXPORT struct freelist_head LIST_fl;
76 
77 /******************************/
78 /* macro function definitions */
79 /******************************/
80 
81 #define LINK_new()  (struct Link_ *)MEM_new(&LINK_fl)
82 #define LINK_destroy(x) MEM_destroy(&LINK_fl,(Freelist *)(Generic)x)
83 #define LIST_new()  (struct Linked_List_ *)MEM_new(&LIST_fl)
84 #define LIST_destroy(x) MEM_destroy(&LIST_fl,(Freelist *)(Generic)x)
85 
86 /** accessing links */
87 #define LINKdata(link)  (link)->data
88 #define LINKnext(link)  (link)->next
89 #define LINKprev(link)  (link)->prev
90 
91 /** iteration */
92 #define LISTdo(list, elt, type) LISTdo_n( list, elt, type, a )
93 
94 /** LISTdo_n: LISTdo with nesting
95  * parameter 'uniq' changes the variable names, allowing us to nest it without -Wshadow warnings
96  */
97 #define LISTdo_n(list, elt, type, uniq)                                         \
98    {struct Linked_List_ * _ ## uniq ## l = (list);                              \
99     type        elt;                                                            \
100     Link        _ ## uniq ## p;                                                 \
101     if( _ ## uniq ## l ) {                                                      \
102     for( _ ## uniq ## p = _ ## uniq ## l->mark;                                 \
103        ( _ ## uniq ## p = _ ## uniq ## p->next ) != _ ## uniq ## l->mark; ) {   \
104         ( elt ) = ( type ) ( ( _ ## uniq ## p )->data );
105 
106 #define LISTdo_links(list, link)                    \
107    {Linked_List     __i = (list);                   \
108     Link        link;                               \
109     if (__i != LIST_NULL) {                         \
110     for ((link) = __i->mark; ((link) = (link)->next) != __i->mark; ) {
111 
112 #define LISTod  }}}
113 
114 /** accessing */
115 #define LISTpeek_first(list)                        \
116     (((struct Linked_List_*)list)->mark->next->data)
117 
118 /** function aliases */
119 #define LISTadd_all(list, items)                    \
120     LISTdo(items, e, Generic) {                     \
121         LISTadd_last(list, e);                      \
122     } LISTod;
123 
124 /***********************/
125 /* function prototypes */
126 /***********************/
127 
128 extern SC_EXPRESS_EXPORT void LISTinitialize PROTO( ( void ) );
129 extern SC_EXPRESS_EXPORT void LISTcleanup PROTO( ( void ) );
130 extern SC_EXPRESS_EXPORT Linked_List LISTcreate PROTO( ( void ) );
131 extern SC_EXPRESS_EXPORT Linked_List LISTcopy PROTO( ( Linked_List ) );
132 extern SC_EXPRESS_EXPORT void LISTsort PROTO( ( Linked_List, int (*comp)(void*, void*) ) );
133 extern SC_EXPRESS_EXPORT void LISTswap PROTO( ( Link, Link ) );
134 extern SC_EXPRESS_EXPORT Generic  LISTadd_first PROTO( ( Linked_List, Generic ) );
135 extern SC_EXPRESS_EXPORT Generic  LISTadd_last PROTO( ( Linked_List, Generic ) );
136 extern SC_EXPRESS_EXPORT Generic  LISTadd_after PROTO( ( Linked_List, Link, Generic ) );
137 extern SC_EXPRESS_EXPORT Generic  LISTadd_before PROTO( ( Linked_List, Link, Generic ) );
138 extern SC_EXPRESS_EXPORT Generic  LISTremove_first PROTO( ( Linked_List ) );
139 extern SC_EXPRESS_EXPORT Generic  LISTget_first PROTO( ( Linked_List ) );
140 extern SC_EXPRESS_EXPORT Generic  LISTget_second PROTO( ( Linked_List ) );
141 extern SC_EXPRESS_EXPORT Generic  LISTget_nth PROTO( ( Linked_List, int ) );
142 extern SC_EXPRESS_EXPORT void LISTfree PROTO( ( Linked_List ) );
143 extern SC_EXPRESS_EXPORT int  LISTget_length PROTO( ( Linked_List ) );
144 extern SC_EXPRESS_EXPORT bool LISTempty( Linked_List list );
145 
146 #endif /*LINKED_LIST_H*/
147