1 /*-----------------------------------------------------------------
2 SDCCset.c - contains support routines for sets
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
30 /*-----------------------------------------------------------------*/
31 /* newSet - will allocate & return a new set entry */
32 /*-----------------------------------------------------------------*/
38 lp = Safe_alloc ( sizeof (set));
40 // fprintf(stderr, "out of virtual memory: %s\n", __FILE__);
44 lp->item = lp->curr = lp->next = NULL;
49 /*-----------------------------------------------------------------*/
50 /* setFromSet - creates a list from another list */
51 /*-----------------------------------------------------------------*/
59 addSetHead (&lfl, lp->item);
67 /*-----------------------------------------------------------------*/
68 /* isSetsEqual - are the lists equal, they are equal if they have */
69 /* the same objects & the same number of objects */
70 /*-----------------------------------------------------------------*/
72 isSetsEqual (set * dest, set * src)
76 for (; dest && src; dest = dest->next, src = src->next)
78 if (!isinSet (src1, dest->item))
86 /*-----------------------------------------------------------------*/
87 /* isSetsEqualWith - are the lists equal, they are equal if they have */
88 /* the same objects & the same number of objects , compare function */
89 /*-----------------------------------------------------------------*/
91 isSetsEqualWith (set * dest, set * src, int (*cFunc) (void *, void *))
95 for (; dest && src; dest = dest->next, src = src->next)
97 if (!isinSetWith (src1, dest->item, cFunc))
105 /*-----------------------------------------------------------------*/
106 /* addSetIfnotP - adds to a linked list if not already present */
107 /*-----------------------------------------------------------------*/
109 addSetIfnotP (set ** list, void *item)
112 if (isinSet (*list, item))
115 addSetHead (list, item);
120 /*-----------------------------------------------------------------*/
121 /* addSetHead - add item to head of linked list */
122 /*-----------------------------------------------------------------*/
124 addSetHead (set ** list, void *item)
131 assert (lp != lp->item);
137 /*-----------------------------------------------------------------*/
138 /* addSet - add an item to a linear linked list */
139 /*-----------------------------------------------------------------*/
141 addSet (set ** list, void *item)
145 /* item added to the tail of the list */
147 /* if the list is empty */
150 lp = *list = newSet ();
154 /* go to the end of the list */
155 for (lp = *list; lp->next; lp = lp->next);
156 lp = lp->next = newSet ();
165 /*-----------------------------------------------------------------*/
166 /* deleteItemIf - will delete from set if cond function returns 1 */
167 /*-----------------------------------------------------------------*/
169 deleteItemIf (set ** sset, int (*cond) (void *, va_list),...)
176 // On the x86 va_list is just a pointer, so due to pass by value
177 // ap is not mofified by the called function. On the PPC va_list
178 // is a pointer to a structure, so ap is modified. Re-init each time.
181 if ((*cond) (sp->item, ap))
183 deleteSetItem (sset, sp->item);
193 /*-----------------------------------------------------------------*/
194 /* deleteSetItem - will delete a given item from the list */
195 /*-----------------------------------------------------------------*/
197 deleteSetItem (set ** list, void *item)
201 /* if list is empty */
205 /* if this item is at the head of the list */
206 if ((*list)->item == item)
209 *list = (*list)->next;
213 /* find the item in the list */
214 for (lp = *list; lp->next; lp = lp->next)
216 if (lp->next->item == item) /* the next one is it */
218 lp1 = lp->next; /* this one will need to be freed */
219 lp->next = lp->next->next; /* take out of list */
224 /* could not find it */
228 /*-----------------------------------------------------------------*/
229 /* isinSet - the item is present in the linked list */
230 /*-----------------------------------------------------------------*/
232 isinSet (set * list, void *item)
236 for (lp = list; lp; lp = lp->next)
237 if (lp->item == item)
243 /*-----------------------------------------------------------------*/
244 /* isinSetWith - the item is present in the linked list */
245 /*-----------------------------------------------------------------*/
247 isinSetWith (set * list, void *item, int (*cFunc) (void *, void *))
251 for (lp = list; lp; lp = lp->next)
252 if ((*cFunc) (lp->item, item))
258 /*-----------------------------------------------------------------*/
259 /* unionSets - will return the union of the two lists */
260 /*-----------------------------------------------------------------*/
262 unionSets (set * list1, set * list2, int throw)
267 /* add all elements in the first list */
268 for (lp = list1; lp; lp = lp->next)
269 addSet (&un, lp->item);
271 /* now for all those in list2 which does not */
272 /* already exist in the list add */
273 for (lp = list2; lp; lp = lp->next)
274 if (!isinSet (un, lp->item))
275 addSet (&un, lp->item);
280 setToNull ((void **) &list2);
283 setToNull ((void **) &list1);
286 setToNull ((void **) &list1);
287 setToNull ((void **) &list2);
293 /*-----------------------------------------------------------------*/
294 /* unionSetsWith - will return the union of the two lists */
295 /*-----------------------------------------------------------------*/
297 unionSetsWith (set * list1, set * list2, int (*cFunc) (), int throw)
302 /* add all elements in the first list */
303 for (lp = list1; lp; lp = lp->next)
304 addSet (&un, lp->item);
306 /* now for all those in list2 which does not */
307 /* already exist in the list add */
308 for (lp = list2; lp; lp = lp->next)
309 if (!isinSetWith (un, lp->item, (int (*)(void *, void *)) cFunc))
310 addSet (&un, lp->item);
315 setToNull ((void **) &list2);
318 setToNull ((void **) &list1);
321 setToNull ((void **) &list1);
322 setToNull ((void **) &list2);
328 /*-----------------------------------------------------------------*/
329 /* intersectSets - returns list of items in common to two lists */
330 /*-----------------------------------------------------------------*/
332 intersectSets (set * list1, set * list2, int throw)
337 /* we can take any one of the lists and iterate over it */
338 for (lp = list1; lp; lp = lp->next)
339 if (isinSet (list2, lp->item))
340 addSetHead (&in, lp->item);
345 setToNull ((void **) &list2);
348 setToNull ((void **) &list1);
351 setToNull ((void **) &list1);
352 setToNull ((void **) &list2);
358 /*-----------------------------------------------------------------*/
359 /* intersectSetsWith - returns list of items in common to two lists */
360 /*-----------------------------------------------------------------*/
362 intersectSetsWith (set * list1, set * list2,
363 int (*cFunc) (void *, void *), int throw)
368 /* we can take any one of the lists and iterate over it */
369 for (lp = list1; lp; lp = lp->next)
370 if (isinSetWith (list2, lp->item, cFunc))
371 addSetHead (&in, lp->item);
376 setToNull ((void **) &list2);
379 setToNull ((void **) &list1);
382 setToNull ((void **) &list1);
383 setToNull ((void **) &list2);
389 /*-----------------------------------------------------------------*/
390 /* elementsInSet - elements count of a set */
391 /*-----------------------------------------------------------------*/
393 elementsInSet (set * s)
407 /*-----------------------------------------------------------------*/
408 /* reverseSet - reverse the order of the items of a set */
409 /*-----------------------------------------------------------------*/
427 /*-----------------------------------------------------------------*/
428 /* subtractFromSet - take away from set1 elements of set2 */
429 /*-----------------------------------------------------------------*/
431 subtractFromSet (set * left, set * right, int throw)
433 set *result = setFromSet (left);
438 for (loop = right; loop; loop = loop->next)
439 if (isinSet (result, loop->item))
440 deleteSetItem (&result, loop->item);
446 setToNull ((void **) &right);
449 setToNull ((void **) &left);
452 setToNull ((void **) &left);
453 setToNull ((void **) &right);
460 /*-----------------------------------------------------------------*/
461 /* applyToSet - will call the supplied function with each item */
462 /*-----------------------------------------------------------------*/
464 applyToSet (set * list, int (*somefunc) (void *, va_list),...)
470 for (lp = list; lp; lp = lp->next)
472 va_start (ap, somefunc);
473 rvalue += (*somefunc) (lp->item, ap);
479 /*-----------------------------------------------------------------*/
480 /* applyToSetFTrue - will call the supplied function with each item */
481 /* until list is exhausted or a true is returned */
482 /*-----------------------------------------------------------------*/
484 applyToSetFTrue (set * list, int (*somefunc) (void *, va_list),...)
490 for (lp = list; lp; lp = lp->next)
492 va_start (ap, somefunc);
493 rvalue += (*somefunc) (lp->item, ap);
501 /*-----------------------------------------------------------------*/
502 /* peekSet - will return the first element of the set */
503 /*-----------------------------------------------------------------*/
513 /*-----------------------------------------------------------------*/
514 /* setFirstItem - gets the first item in the set, begins iteration */
515 /*-----------------------------------------------------------------*/
517 setFirstItem (set * sset)
527 /*-----------------------------------------------------------------*/
528 /* setNextItem - gets the next item, changes the iteration */
529 /*-----------------------------------------------------------------*/
531 setNextItem (set * sset)
533 if (sset && sset->curr)
535 sset->curr = sset->curr->next;
537 return sset->curr->item;
542 /*-----------------------------------------------------------------*/
543 /* getSet - will delete & return the first item from the set */
544 /*-----------------------------------------------------------------*/
551 /* if list is empty then we cannot delete */
553 return (void *) NULL;
556 /* find the item in the list */
558 item = lp->item; /* save the item */
564 /*-----------------------------------------------------------------*/
565 /* setToNull - will throw away the entire list */
566 /*-----------------------------------------------------------------*/
568 setToNull (void **item)