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 -------------------------------------------------------------------------*/
31 /*-----------------------------------------------------------------*/
32 /* newSet - will allocate & return a new set entry */
33 /*-----------------------------------------------------------------*/
39 lp = Safe_calloc (1, sizeof (set));
41 // fprintf(stderr, "out of virtual memory: %s\n", __FILE__);
45 lp->item = lp->curr = lp->next = NULL;
50 /*-----------------------------------------------------------------*/
51 /* setFromSet - creates a list from another list */
52 /*-----------------------------------------------------------------*/
60 addSetHead (&lfl, lp->item);
68 /*-----------------------------------------------------------------*/
69 /* isSetsEqual - are the lists equal, they are equal if they have */
70 /* the same objects & the same number of objects */
71 /*-----------------------------------------------------------------*/
73 isSetsEqual (set * dest, set * src)
77 for (; dest && src; dest = dest->next, src = src->next)
79 if (!isinSet (src1, dest->item))
87 /*-----------------------------------------------------------------*/
88 /* isSetsEqualWith - are the lists equal, they are equal if they have */
89 /* the same objects & the same number of objects , compare function */
90 /*-----------------------------------------------------------------*/
92 isSetsEqualWith (set * dest, set * src, int (*cFunc) (void *, void *))
96 for (; dest && src; dest = dest->next, src = src->next)
98 if (!isinSetWith (src1, dest->item, cFunc))
106 /*-----------------------------------------------------------------*/
107 /* addSetIfnotP - adds to a linked list if not already present */
108 /*-----------------------------------------------------------------*/
110 addSetIfnotP (set ** list, void *item)
113 if (isinSet (*list, item))
116 addSetHead (list, item);
121 /*-----------------------------------------------------------------*/
122 /* addSetHead - add item to head of linked list */
123 /*-----------------------------------------------------------------*/
125 addSetHead (set ** list, void *item)
132 assert (lp != lp->item);
138 /*-----------------------------------------------------------------*/
139 /* addSet - add an item to a linear linked list */
140 /*-----------------------------------------------------------------*/
142 addSet (set ** list, void *item)
146 /* item added to the tail of the list */
148 /* if the list is empty */
151 lp = *list = newSet ();
155 /* go to the end of the list */
156 for (lp = *list; lp->next; lp = lp->next);
157 lp = lp->next = newSet ();
166 /*-----------------------------------------------------------------*/
167 /* deleteItemIf - will delete from set if cond function returns 1 */
168 /*-----------------------------------------------------------------*/
170 deleteItemIf (set ** sset, int (*cond) (void *, va_list),...)
179 if ((*cond) (sp->item, ap))
181 deleteSetItem (sset, sp->item);
190 /*-----------------------------------------------------------------*/
191 /* deleteSetItem - will delete a given item from the list */
192 /*-----------------------------------------------------------------*/
194 deleteSetItem (set ** list, void *item)
198 /* if list is empty */
202 /* if this item is at the head of the list */
203 if ((*list)->item == item)
206 *list = (*list)->next;
210 /* find the item in the list */
211 for (lp = *list; lp->next; lp = lp->next)
213 if (lp->next->item == item) /* the next one is it */
215 lp1 = lp->next; /* this one will need to be freed */
216 lp->next = lp->next->next; /* take out of list */
221 /* could not find it */
225 /*-----------------------------------------------------------------*/
226 /* isinSet - the item is present in the linked list */
227 /*-----------------------------------------------------------------*/
229 isinSet (set * list, void *item)
233 for (lp = list; lp; lp = lp->next)
234 if (lp->item == item)
240 /*-----------------------------------------------------------------*/
241 /* isinSetWith - the item is present in the linked list */
242 /*-----------------------------------------------------------------*/
244 isinSetWith (set * list, void *item, int (*cFunc) (void *, void *))
248 for (lp = list; lp; lp = lp->next)
249 if ((*cFunc) (lp->item, item))
255 /*-----------------------------------------------------------------*/
256 /* unionSets - will return the union of the two lists */
257 /*-----------------------------------------------------------------*/
259 unionSets (set * list1, set * list2, int throw)
264 /* add all elements in the first list */
265 for (lp = list1; lp; lp = lp->next)
266 addSet (&un, lp->item);
268 /* now for all those in list2 which does not */
269 /* already exist in the list add */
270 for (lp = list2; lp; lp = lp->next)
271 if (!isinSet (un, lp->item))
272 addSet (&un, lp->item);
277 setToNull ((void **) &list2);
280 setToNull ((void **) &list1);
283 setToNull ((void **) &list1);
284 setToNull ((void **) &list2);
290 /*-----------------------------------------------------------------*/
291 /* unionSetsWith - will return the union of the two lists */
292 /*-----------------------------------------------------------------*/
294 unionSetsWith (set * list1, set * list2, int (*cFunc) (), int throw)
299 /* add all elements in the first list */
300 for (lp = list1; lp; lp = lp->next)
301 addSet (&un, lp->item);
303 /* now for all those in list2 which does not */
304 /* already exist in the list add */
305 for (lp = list2; lp; lp = lp->next)
306 if (!isinSetWith (un, lp->item, (int (*)(void *, void *)) cFunc))
307 addSet (&un, lp->item);
312 setToNull ((void **) &list2);
315 setToNull ((void **) &list1);
318 setToNull ((void **) &list1);
319 setToNull ((void **) &list2);
325 /*-----------------------------------------------------------------*/
326 /* intersectSets - returns list of items in common to two lists */
327 /*-----------------------------------------------------------------*/
329 intersectSets (set * list1, set * list2, int throw)
334 /* we can take any one of the lists and iterate over it */
335 for (lp = list1; lp; lp = lp->next)
336 if (isinSet (list2, lp->item))
337 addSetHead (&in, lp->item);
342 setToNull ((void **) &list2);
345 setToNull ((void **) &list1);
348 setToNull ((void **) &list1);
349 setToNull ((void **) &list2);
355 /*-----------------------------------------------------------------*/
356 /* intersectSetsWith - returns list of items in common to two lists */
357 /*-----------------------------------------------------------------*/
359 intersectSetsWith (set * list1, set * list2,
360 int (*cFunc) (void *, void *), int throw)
365 /* we can take any one of the lists and iterate over it */
366 for (lp = list1; lp; lp = lp->next)
367 if (isinSetWith (list2, lp->item, cFunc))
368 addSetHead (&in, lp->item);
373 setToNull ((void **) &list2);
376 setToNull ((void **) &list1);
379 setToNull ((void **) &list1);
380 setToNull ((void **) &list2);
386 /*-----------------------------------------------------------------*/
387 /* elementsInSet - elements count of a set */
388 /*-----------------------------------------------------------------*/
390 elementsInSet (set * s)
404 /*-----------------------------------------------------------------*/
405 /* subtractFromSet - take away from set1 elements of set2 */
406 /*-----------------------------------------------------------------*/
408 subtractFromSet (set * left, set * right, int throw)
410 set *result = setFromSet (left);
415 for (loop = right; loop; loop = loop->next)
416 if (isinSet (result, loop->item))
417 deleteSetItem (&result, loop->item);
423 setToNull ((void **) &right);
426 setToNull ((void **) &left);
429 setToNull ((void **) &left);
430 setToNull ((void **) &right);
437 /*-----------------------------------------------------------------*/
438 /* applyToSet - will call the supplied function with each item */
439 /*-----------------------------------------------------------------*/
441 applyToSet (set * list, int (*somefunc) (void *, va_list),...)
447 for (lp = list; lp; lp = lp->next)
449 va_start (ap, somefunc);
450 rvalue += (*somefunc) (lp->item, ap);
456 /*-----------------------------------------------------------------*/
457 /* applyToSetFTrue - will call the supplied function with each item */
458 /* until list is exhausted or a true is returned */
459 /*-----------------------------------------------------------------*/
461 applyToSetFTrue (set * list, int (*somefunc) (void *, va_list),...)
467 for (lp = list; lp; lp = lp->next)
469 va_start (ap, somefunc);
470 rvalue += (*somefunc) (lp->item, ap);
478 /*-----------------------------------------------------------------*/
479 /* peekSet - will return the first element of the set */
480 /*-----------------------------------------------------------------*/
490 /*-----------------------------------------------------------------*/
491 /* setFirstItem - gets the first item in the set, begins iteration */
492 /*-----------------------------------------------------------------*/
494 setFirstItem (set * sset)
504 /*-----------------------------------------------------------------*/
505 /* setNextItem - gets the next item, changes the iteration */
506 /*-----------------------------------------------------------------*/
508 setNextItem (set * sset)
510 if (sset && sset->curr)
512 sset->curr = sset->curr->next;
514 return sset->curr->item;
519 /*-----------------------------------------------------------------*/
520 /* getSet - will delete & return the first item from the set */
521 /*-----------------------------------------------------------------*/
528 /* if list is empty then we cannot delete */
530 return (void *) NULL;
533 /* find the item in the list */
535 item = lp->item; /* save the item */
541 /*-----------------------------------------------------------------*/
542 /* setToNull - will throw away the entire list */
543 /*-----------------------------------------------------------------*/
545 setToNull (void **item)