1 /************************************************************
3 ** COPYRIGHT (C) 1993 UNIVERSITY OF PITTSBURGH
4 ** COPYRIGHT (C) 1996 GANNON UNIVERSITY
7 ** This software is distributed on an as-is basis
8 ** with no warranty implied or intended. No author
9 ** or distributor takes responsibility to anyone
10 ** regarding its use of or suitability.
12 ** The software may be distributed and modified
13 ** freely for academic and other non-commercial
14 ** use but may NOT be utilized or included in whole
15 ** or part within any commercial product.
17 ** This copyright notice must remain on all copies
18 ** and modified versions of this software.
20 ************************************************************/
24 /* ------------------------------------------------------------------------
25 * List processing facilities based on spl place & route routines (1/90 stf)
26 * ------------------------------------------------------------------------
38 /*---- --- for use with these definitions of TRUE and FALSE -------------- */
39 #define NOT(x) (((x) == FALSE) ? TRUE : FALSE)
42 #define malloc Tcl_Alloc
43 #define calloc(a,b) my_calloc(a, b) /* see utility.c */
44 #define free(a) Tcl_Free((char *)(a))
45 #define realloc(a,b) Tcl_Realloc((char *)(a), b)
47 #define fprintf tcl_printf /* defined by xcircuit */
48 #define fflush tcl_stdflush
51 /*------------------------------------------------------------------------
52 * These list processing functions are based on the following two
54 *------------------------------------------------------------------------*/
55 typedef struct list_struct list
;
56 typedef struct indexed_list ilist
;
71 /*------------------------------------------------------------------------ */
72 /* NOTE: It is highly advisable to initialize ALL list pointers to NULL,
73 * Otherwise, 'random' values that point off into undesirable sections
74 * of memory may be become "->next" pointers. THIS IS A BAD THING. */
75 /*------------------------------------------------------------------------ */
78 /* ------------------------------------------------------------------------
79 * concat_list: append a generic thing to the head of a list.
80 * works for null lists.
81 * ------------------------------------------------------------------------
84 extern list
*concat_list();
88 /* ------------------------------------------------------------------------
89 * append_list: append two generic lists together. (<l2> is attached to the
90 * end of <l1>. If both lists are null, it returns null.
91 * ------------------------------------------------------------------------
94 extern list
*append_list();
97 /*---------------------------------------------------------------
99 * cut out a generic list element from the middle of a list
100 * note the parameter must be the address of the real thing, not
101 * a copy. so this should be called with the address of a
102 * subfield ie remove_list_element(&(thing->next))
103 * both remove_list_element(thing->next)
104 * and remove_list_element(&thing) (unless thing is global) will not work.
105 *---------------------------------------------------------------
107 extern void remove_list_element();
110 /*--------------------------------------------------------------------------
112 * Clip all of the elements of the given list who's ->this field return TRUE
113 * to the given <testFn>. (Recursive)
114 *--------------------------------------------------------------------------
116 extern list
*delete_if();
117 /* list **lptr; List to work on. */
118 /* int (*testFn)(); Should take a single *int as its arg, return TRUE/FALSE */
120 /* ------------------------------------------------------------------------
121 * pushnew - add a unique element to the front of a linked list
122 * ------------------------------------------------------------------------
124 extern void pushnew();
128 /* ------------------------------------------------------------------------
129 * push - add to the front of a linked list
130 * ------------------------------------------------------------------------
136 /* ------------------------------------------------------------------------
137 * pop - (FILO) pop from the front of a linked list
138 * ------------------------------------------------------------------------
143 /* ------------------------------------------------------------------------
144 * queue - add to the back of a linked list
145 * ------------------------------------------------------------------------
151 /* ------------------------------------------------------------------------
152 * trans_item - pull from an unknown position within the first list, push
154 * ------------------------------------------------------------------------
156 extern int *trans_item();
161 /* ------------------------------------------------------------------------
162 * rem_item - pull from an unknown position within the list
163 * ------------------------------------------------------------------------
165 extern int *rem_item();
169 /* ------------------------------------------------------------------------
170 * repl_item - pull from an unknown position within the list, replace with
172 * ------------------------------------------------------------------------
174 extern int *repl_item();
178 /* ------------------------------------------------------------------------
179 * find_item - see if the item is in the list
180 * ------------------------------------------------------------------------
182 extern list
*find_item();
186 /* ------------------------------------------------------------------------
187 * nth - Return the Nth element of the list <l> given that there are at
188 * least <n> elements on <l>.
189 * ------------------------------------------------------------------------
195 /* ------------------------------------------------------------------------
196 * nth_cdr - Return the list pointer containing the Nth element of the
197 * list <l> given that there are at least <n> elements on <l>.
198 * ------------------------------------------------------------------------
200 extern list
*nth_cdr();
204 /* ------------------------------------------------------------------------
206 * ------------------------------------------------------------------------
208 extern int list_length();
211 /* ------------------------------------------------------------------------
212 * member_p - returns TRUE iff the given pointer returns TRUE when compared
213 * with the ->this slot of some list element.
214 * ------------------------------------------------------------------------
216 extern int member_p();
217 /* int *e; The element to look for */
218 /* list *l; The list to look in */
219 /* int (*testFn)(); The function to test by - <e> is the first arg,
220 l-this second (expected to take two *int's as args,
222 /* ------------------------------------------------------------------------
223 * member - returns list pointer to the first item that returns TRUE to
224 * the testFn(e, ll->this) query. Returns NULL otherwise.
225 * ------------------------------------------------------------------------
227 extern list
*member();
228 /* int *e; The element to look for */
229 /* list *l; The list to look in */
230 /* int (*testFn)(); The function to test by - <e> is the first arg,
231 l-this second (expected to take two *int's as args,
234 /* ------------------------------------------------------------------------
235 * rcopy_list: make a copy of a list, reversing it for speed/convience
236 * ------------------------------------------------------------------------
238 extern list
*rcopy_list();
241 /* ------------------------------------------------------------------------
242 * copy_list: make a copy of a list, preserving order
243 * ------------------------------------------------------------------------
245 extern list
*copy_list();
248 /* ------------------------------------------------------------------------
249 * delete_duplicates Modify the given list by deleting duplicate elements:
250 * ------------------------------------------------------------------------
252 extern list
*delete_duplicates();
254 /* int (*testFn)(); Should take two *int's as args, return TRUE/FALSE */
256 /* ------------------------------------------------------------------------
257 * my_free wipe this pointer from memory.
258 * ------------------------------------------------------------------------
260 extern int *my_free();
263 /* ------------------------------------------------------------------------
264 * free_list wipe this list from memory. Do not disturb any list elements.
265 * ------------------------------------------------------------------------
267 extern list
*free_list();
270 /* ------------------------------------------------------------------------
271 * sort_list (destructive) sort the given list via orderFn.
272 * ------------------------------------------------------------------------
276 /* int (*orderFn)(); Should take two *int's as args, return TRUE/FALSE */
277 /* TRUE implies the first element belongs in front of the second */
279 /* ------------------------------------------------------------------------
280 * merge_sort_list (destructive) merge sort the given list via orderFn.
281 * ------------------------------------------------------------------------
283 list
*merge_sort_list();
285 /* int (*orderFn)(); Should take two *int's as args, return TRUE/FALSE */
286 /* TRUE implies the first element belongs in front of the second */
288 /* ------------------------------------------------------------------------
289 * slow_sort_list (destructive) sort the given list via orderFn.
290 * ------------------------------------------------------------------------
292 list
*slow_sort_list();
294 /* int (*orderFn)(); Should take two *int's as args, return TRUE/FALSE */
295 /* TRUE implies the first element belongs in front of the second */
299 /*====================================================================================
300 INDEXED LIST (ilist) FUNCTIONS:
301 *====================================================================================*/
302 /* ------------------------------------------------------------------------
303 * reverse_copy_ilist: make a copy of an indexed list, inverting the order
304 * while maintaining the original indexing
305 * ------------------------------------------------------------------------
307 extern ilist
*reverse_copy_ilist();
309 /* ------------------------------------------------------------------------
310 * rcopy_ilist: make a copy of an indexed list, inverting order
311 * ------------------------------------------------------------------------
313 extern ilist
*rcopy_ilist();
315 /* ------------------------------------------------------------------------
316 * copy_ilist: make a copy of an indexed list, preserving order
317 * ------------------------------------------------------------------------
319 extern ilist
*copy_ilist();
321 /* ------------------------------------------------------------------------
322 * remove_indexed_element remove (by index) an element from a list structure
323 * NOTE: This renumbers the elements following the discovery.
324 * ------------------------------------------------------------------------
326 extern int *remove_indexed_element();
330 /* ------------------------------------------------------------------------
331 * irem_item - pull from an unknown position within an indexed list
332 * (no renumbering). This search is limited by non-NULL <howFar>.
333 * ------------------------------------------------------------------------
335 extern int *irem_item();
338 * int howFar; If != NULL, places a limit on the search */
340 /* ------------------------------------------------------------------------
341 * find_indexed_element finds (by index) an element from a list structure
342 * NOTE: This returns the list pointer to the requested element.
343 * ------------------------------------------------------------------------
345 extern ilist
*find_indexed_element();
349 /* ------------------------------------------------------------------------
350 * indexed_list_insert an element to an indexed list structure.
351 * ------------------------------------------------------------------------
353 extern int indexed_list_insert();
358 /* ------------------------------------------------------------------------
359 * ordered_ilist_insert an element to an indexed list structure.
360 * Quite similar to "indexed_list_insert, saving that whenever an element having
361 * the same index value is encountered, rather than replacing the entry (as in
362 * the aforementioned function) a new list element is created, and placed in front
363 * of the first such entry encountered.
364 * ------------------------------------------------------------------------
366 extern int ordered_ilist_insert();
371 /* ------------------------------------------------------------------------
373 * ------------------------------------------------------------------------
375 extern int ilist_length();
378 /* ------------------------------------------------------------------------
380 * ------------------------------------------------------------------------
385 /* ------------------------------------------------------------------------
387 * ------------------------------------------------------------------------
393 /* ------------------------------------------------------------------------
394 * append_ilist: append two generic indexed lists together.
395 * Works for either list null.
396 * If both lists are null, it returns null.
397 * ------------------------------------------------------------------------
399 extern ilist
*append_ilist();
402 /* ------------------------------------------------------------------------
403 * free_ilist wipe this list from memory. Do not disturb any list elements.
404 * ------------------------------------------------------------------------
406 extern ilist
*free_ilist();
411 /* ------------------------------------------------------------------------
413 * ------------------------------------------------------------------------