From: borutr Date: Sun, 15 Jun 2003 18:58:11 +0000 (+0000) Subject: added function setFromSetNonRev() and mergeSets() X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=8dfea40848cf2c3df4a26e74730da6b45629a0f9;p=fw%2Fsdcc added function setFromSetNonRev() and mergeSets() git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2696 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/src/SDCCset.c b/src/SDCCset.c index f8bab736..8bc2e468 100644 --- a/src/SDCCset.c +++ b/src/SDCCset.c @@ -23,31 +23,27 @@ -------------------------------------------------------------------------*/ #include -#include "newalloc.h" #include +#include "newalloc.h" #include "SDCCset.h" /*-----------------------------------------------------------------*/ -/* newSet - will allocate & return a new set entry */ +/* newSet - will allocate & return a new set entry */ /*-----------------------------------------------------------------*/ set * newSet (void) { set *lp; - lp = Safe_alloc ( sizeof (set)); -// if (lp == 0) { - // fprintf(stderr, "out of virtual memory: %s\n", __FILE__); - // exit(1); - // } - + lp = Safe_alloc (sizeof (set)); lp->item = lp->curr = lp->next = NULL; return lp; } /*-----------------------------------------------------------------*/ -/* setFromSet - creates a list from another list */ +/* setFromSet - creates a list from another list; the order of */ +/* elements in new list is reverted */ /*-----------------------------------------------------------------*/ set * setFromSet (set * lp) @@ -64,11 +60,30 @@ setFromSet (set * lp) } +/*-----------------------------------------------------------------*/ +/* setFromSet - creates a list from another list; the order of */ +/* elements in retained */ +/*-----------------------------------------------------------------*/ +set * +setFromSetNonRev (set * lp) +{ + set *lfl = NULL; + + while (lp) + { + addSet (&lfl, lp->item); + lp = lp->next; + } + + return lfl; + +} + /*-----------------------------------------------------------------*/ /* isSetsEqual - are the lists equal, they are equal if they have */ /* the same objects & the same number of objects */ /*-----------------------------------------------------------------*/ -int +int isSetsEqual (set * dest, set * src) { set *src1 = src; @@ -84,10 +99,11 @@ isSetsEqual (set * dest, set * src) } /*-----------------------------------------------------------------*/ -/* isSetsEqualWith - are the lists equal, they are equal if they have */ -/* the same objects & the same number of objects , compare function */ +/* isSetsEqualWith - are the lists equal, they are equal if they */ +/* have the same objects & the same number of */ +/* objects , compare function */ /*-----------------------------------------------------------------*/ -int +int isSetsEqualWith (set * dest, set * src, int (*cFunc) (void *, void *)) { set *src1 = src; @@ -103,7 +119,7 @@ isSetsEqualWith (set * dest, set * src, int (*cFunc) (void *, void *)) } /*-----------------------------------------------------------------*/ -/* addSetIfnotP - adds to a linked list if not already present */ +/* addSetIfnotP - adds to a linked list if not already present */ /*-----------------------------------------------------------------*/ void * addSetIfnotP (set ** list, void *item) @@ -118,7 +134,7 @@ addSetIfnotP (set ** list, void *item) } /*-----------------------------------------------------------------*/ -/* addSetHead - add item to head of linked list */ +/* addSetHead - add item to head of linked list */ /*-----------------------------------------------------------------*/ void * addSetHead (set ** list, void *item) @@ -135,7 +151,7 @@ addSetHead (set ** list, void *item) } /*-----------------------------------------------------------------*/ -/* addSet - add an item to a linear linked list */ +/* addSet - add an item to a linear linked list */ /*-----------------------------------------------------------------*/ void * addSet (set ** list, void *item) @@ -165,7 +181,7 @@ addSet (set ** list, void *item) /*-----------------------------------------------------------------*/ /* deleteItemIf - will delete from set if cond function returns 1 */ /*-----------------------------------------------------------------*/ -void +void deleteItemIf (set ** sset, int (*cond) (void *, va_list),...) { set *sp = *sset; @@ -173,9 +189,11 @@ deleteItemIf (set ** sset, int (*cond) (void *, va_list),...) while (sp) { - // On the x86 va_list is just a pointer, so due to pass by value - // ap is not mofified by the called function. On the PPC va_list - // is a pointer to a structure, so ap is modified. Re-init each time. + /* + * On the x86 va_list is just a pointer, so due to pass by value + * ap is not mofified by the called function. On the PPC va_list + * is a pointer to a structure, so ap is modified. Re-init each time. + */ va_start (ap, cond); if ((*cond) (sp->item, ap)) @@ -193,7 +211,7 @@ deleteItemIf (set ** sset, int (*cond) (void *, va_list),...) /*-----------------------------------------------------------------*/ /* deleteSetItem - will delete a given item from the list */ /*-----------------------------------------------------------------*/ -void +void deleteSetItem (set ** list, void *item) { set *lp, *lp1; @@ -224,9 +242,9 @@ deleteSetItem (set ** list, void *item) } /*-----------------------------------------------------------------*/ -/* isinSet - the item is present in the linked list */ +/* isinSet - the item is present in the linked list */ /*-----------------------------------------------------------------*/ -int +int isinSet (set * list, void *item) { set *lp; @@ -241,7 +259,7 @@ isinSet (set * list, void *item) /*-----------------------------------------------------------------*/ /* isinSetWith - the item is present in the linked list */ /*-----------------------------------------------------------------*/ -int +int isinSetWith (set * list, void *item, int (*cFunc) (void *, void *)) { set *lp; @@ -254,7 +272,25 @@ isinSetWith (set * list, void *item, int (*cFunc) (void *, void *)) } /*-----------------------------------------------------------------*/ -/* unionSets - will return the union of the two lists */ +/* mergeSets - append list to sset */ +/*-----------------------------------------------------------------*/ +void +mergeSets (set **sset, set *list) +{ + if (*sset == NULL) { + *sset = list; + } + else { + set *lp; + + for (lp = *sset; lp->next; lp = lp->next) + ; + lp->next = list; + } +} + +/*-----------------------------------------------------------------*/ +/* unionSets - will return the union of the two lists */ /*-----------------------------------------------------------------*/ set * unionSets (set * list1, set * list2, int throw) @@ -354,7 +390,8 @@ intersectSets (set * list1, set * list2, int throw) } /*-----------------------------------------------------------------*/ -/* intersectSetsWith - returns list of items in common to two lists */ +/* intersectSetsWith - returns list of items in common to two */ +/* lists */ /*-----------------------------------------------------------------*/ set * intersectSetsWith (set * list1, set * list2, @@ -387,7 +424,7 @@ intersectSetsWith (set * list1, set * list2, /*-----------------------------------------------------------------*/ /* elementsInSet - elements count of a set */ /*-----------------------------------------------------------------*/ -int +int elementsInSet (set * s) { set *loop = s; @@ -405,7 +442,8 @@ elementsInSet (set * s) /*-----------------------------------------------------------------*/ /* indexSet - returns the i'th item in set */ /*-----------------------------------------------------------------*/ -void *indexSet(set * s, int index) +void * +indexSet (set * s, int index) { set *loop=s; @@ -423,7 +461,7 @@ void *indexSet(set * s, int index) /*-----------------------------------------------------------------*/ set * -reverseSet(set * s) +reverseSet (set * s) { set *t = NULL; set *u = NULL; @@ -474,7 +512,7 @@ subtractFromSet (set * left, set * right, int throw) /*-----------------------------------------------------------------*/ /* applyToSet - will call the supplied function with each item */ /*-----------------------------------------------------------------*/ -int +int applyToSet (set * list, int (*somefunc) (void *, va_list),...) { set *lp; @@ -491,10 +529,11 @@ applyToSet (set * list, int (*somefunc) (void *, va_list),...) } /*-----------------------------------------------------------------*/ -/* applyToSetFTrue - will call the supplied function with each item */ -/* until list is exhausted or a true is returned */ +/* applyToSetFTrue - will call the supplied function with each */ +/* item until list is exhausted or a true is */ +/* returned */ /*-----------------------------------------------------------------*/ -int +int applyToSetFTrue (set * list, int (*somefunc) (void *, va_list),...) { set *lp; @@ -576,9 +615,9 @@ getSet (set ** list) } /*-----------------------------------------------------------------*/ -/* setToNull - will throw away the entire list */ +/* setToNull - will throw away the entire list */ /*-----------------------------------------------------------------*/ -void +void setToNull (void **item) { @@ -596,7 +635,8 @@ setToNull (void **item) /* note - setToNull doesn't actually throw away the whole list. */ /* Instead it only throws away the first item. */ /*-----------------------------------------------------------------*/ -void deleteSet(set **s) +void +deleteSet (set **s) { set *curr; set *next; @@ -616,4 +656,3 @@ void deleteSet(set **s) *s = NULL; } - diff --git a/src/SDCCset.h b/src/SDCCset.h index 344007cf..7b4fc589 100644 --- a/src/SDCCset.h +++ b/src/SDCCset.h @@ -59,11 +59,13 @@ typedef int (* insetwithFunc) (void *, void *); int isinSetWith (set *, void *, insetwithFunc cfunc); int applyToSet (set * list, int (*somefunc) (void *, va_list),...); int applyToSetFTrue (set * list, int (*somefunc) (void *, va_list),...); +void mergeSets (set **sset, set *list); set *unionSets (set *, set *, int); set *unionSetsWith (set *, set *, int (*cFunc) (), int); set *intersectSets (set *, set *, int); void *addSetIfnotP (set **, void *); set *setFromSet (set *); +set *setFromSetNonRev (set *); int isSetsEqual (set *, set *); set *subtractFromSet (set *, set *, int); int elementsInSet (set *); @@ -75,6 +77,6 @@ void *setFirstItem (set *); void *setNextItem (set *); void setToNull (void **); set *reverseSet (set *); -void deleteSet(set **s); +void deleteSet (set **s); #endif