Changed Safe_calloc to use 2 arguements to mimic teh standard calloc function
[fw/sdcc] / src / SDCCset.c
index 25fbf0c2f34a9213c39c2de2e0f65cd5518f56bb..2632e9c515fad4dab6b038d441f762566c61cb3c 100644 (file)
@@ -1,25 +1,25 @@
 /*-----------------------------------------------------------------
-    SDCCset.c - contains support routines for sets 
-                
+    SDCCset.c - contains support routines for sets
+
     Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
 
     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the
     Free Software Foundation; either version 2, or (at your option) any
     later version.
-    
+
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
-    
+
     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-    
+
     In other words, you are welcome to use, share and improve this program.
     You are forbidden to forbid anyone else to use, share and improve
-    what you give them.   Help stamp out software-hoarding!  
+    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
 #include <stdio.h>
@@ -34,10 +34,10 @@ set *newSet  ()
 {
   set *lp ;
 
-  lp = Safe_calloc(sizeof(set));
+  lp = Safe_calloc(1,sizeof(set));
 //  if (lp == 0) {
-//     fprintf(stderr, "out of virtual memory: %s\n", __FILE__);
-//     exit(1);
+//  fprintf(stderr, "out of virtual memory: %s\n", __FILE__);
+//  exit(1);
 //  }
 
   lp->item = lp->curr= lp->next = NULL;
@@ -51,14 +51,14 @@ set *newSet  ()
 set *setFromSet (set *lp)
 {
   set *lfl = NULL ;
-  
+
   while (lp) {
     addSetHead(&lfl,lp->item);
     lp = lp->next ;
   }
 
   return lfl ;
-  
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -70,11 +70,11 @@ int isSetsEqual ( set *dest, set *src )
     set *src1 = src ;
 
     for (; dest && src ; dest=dest->next , src=src->next) {
-       if (!isinSet(src1, dest->item))
-           return 0;
+  if (!isinSet(src1, dest->item))
+      return 0;
     }
     if ( !dest && !src)
-       return 1;
+  return 1;
     return 0;
 }
 
@@ -87,11 +87,11 @@ int isSetsEqualWith ( set *dest, set *src , int (*cFunc)(void *,void *))
     set *src1 = src ;
 
     for (; dest && src ; dest=dest->next , src=src->next) {
-       if (!isinSetWith(src1, dest->item,cFunc))
-           return 0;
+  if (!isinSetWith(src1, dest->item,cFunc))
+      return 0;
     }
     if ( !dest && !src)
-       return 1;
+  return 1;
     return 0;
 }
 
@@ -100,12 +100,12 @@ int isSetsEqualWith ( set *dest, set *src , int (*cFunc)(void *,void *))
 /*-----------------------------------------------------------------*/
 void *addSetIfnotP ( set **list, void *item)
 {
-  
+
     if (isinSet(*list,item))
-       return item ;
-    
+  return item ;
+
     addSetHead(list,item);
-    
+
     return item;
 }
 
@@ -115,14 +115,14 @@ void *addSetIfnotP ( set **list, void *item)
 void *addSetHead (set **list, void *item )
 {
     set *lp = newSet();
-    
+
     lp->item = item ;
     lp->next = *list ;
-    
+
     assert(lp != lp->item);
     *list = lp ;
     return item ;
-    
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -131,21 +131,21 @@ void *addSetHead (set **list, void *item )
 void *addSet ( set **list , void *item )
 {
     set *lp ;
-    
+
     /* item added to the tail of the list */
-    
+
     /* if the list is empty */
     if (*list == NULL ) {
-       lp = *list = newSet();
+  lp = *list = newSet();
     } else {
-       /* go to the end of the list */
-       for (lp = *list ; lp->next ; lp = lp->next );
-       lp = lp->next = newSet();
+  /* go to the end of the list */
+  for (lp = *list ; lp->next ; lp = lp->next );
+  lp = lp->next = newSet();
     }
-    
+
     /* lp now all set */
     lp->item = item ;
-    
+
     return item ;
 }
 
@@ -158,15 +158,15 @@ void deleteItemIf ( set **sset, int (*cond) (void *, va_list), ... )
     va_list ap;
 
     va_start(ap,cond);
-    
+
     while (sp) {
-       if ((*cond)(sp->item,ap)) {
-           deleteSetItem(sset,sp->item);
-           sp = *sset ;
-           continue ;
-       }
+  if ((*cond)(sp->item,ap)) {
+      deleteSetItem(sset,sp->item);
+      sp = *sset ;
+      continue ;
+  }
 
-       sp = sp->next ;
+  sp = sp->next ;
     }
 }
 
@@ -176,29 +176,29 @@ void deleteItemIf ( set **sset, int (*cond) (void *, va_list), ... )
 void deleteSetItem ( set **list, void *item )
 {
     set *lp , *lp1;
-    
+
     /* if list is empty */
     if (*list == NULL )
-       return ;
-    
+  return ;
+
     /* if this item is at the head of the list */
     if ((*list)->item == item ) {
-       lp = *list ;
-       *list = (*list)->next ; 
-       return ;
+  lp = *list ;
+  *list = (*list)->next ;
+  return ;
     }
-    
+
     /* find the item in the list */
     for (lp = *list ; lp->next ; lp = lp->next ) {
-       if (lp->next->item == item ) /* the next one is it */ {
-           lp1 = lp->next ; /* this one will need to be freed */
-           lp->next = lp->next->next ; /* take out of list */     
-           return ;
-       }
+  if (lp->next->item == item ) /* the next one is it */ {
+      lp1 = lp->next ; /* this one will need to be freed */
+      lp->next = lp->next->next ; /* take out of list */
+      return ;
+  }
     }
-    
+
     /* could not find it */
-    return ;    
+    return ;
 }
 
 /*-----------------------------------------------------------------*/
@@ -207,11 +207,11 @@ void deleteSetItem ( set **list, void *item )
 int isinSet (set *list, void *item )
 {
     set *lp ;
-    
+
     for (lp = list ; lp ; lp = lp->next )
-       if ( lp->item == item )
-           return 1;
-    
+  if ( lp->item == item )
+      return 1;
+
     return 0;
 }
 
@@ -221,11 +221,11 @@ int isinSet (set *list, void *item )
 int isinSetWith (set *list, void *item , int (*cFunc)(void *,void *) )
 {
     set *lp ;
-    
+
     for (lp = list ; lp ; lp = lp->next )
-       if ( (*cFunc)(lp->item,item) )
-           return 1;
-    
+  if ( (*cFunc)(lp->item,item) )
+      return 1;
+
     return 0;
 }
 
@@ -236,27 +236,27 @@ set *unionSets (set *list1 , set *list2, int throw)
 {
     set *un = NULL ;
     set *lp ;
-    
+
     /* add all elements in the first list */
     for (lp = list1 ; lp ; lp = lp->next )
-       addSet(&un,lp->item);
-    
+  addSet(&un,lp->item);
+
     /* now for all those in list2 which does not */
     /* already exist in the list add             */
     for (lp = list2 ; lp ; lp = lp->next )
-       if (!isinSet(un,lp->item))
-           addSet (&un,lp->item);
-    
+  if (!isinSet(un,lp->item))
+      addSet (&un,lp->item);
+
     switch (throw) {
     case THROW_SRC :
-       setToNull ((void **)&list2);
-       break;
+  setToNull ((void **)&list2);
+  break;
     case THROW_DEST :
-       setToNull ((void **)&list1);
-       break;
+  setToNull ((void **)&list1);
+  break;
     case THROW_BOTH :
-       setToNull ((void **)&list1);
-       setToNull ((void **)&list2);
+  setToNull ((void **)&list1);
+  setToNull ((void **)&list2);
     }
 
     return un;
@@ -269,27 +269,27 @@ set *unionSetsWith (set *list1 , set *list2, int (*cFunc)(),int throw)
 {
     set *un = NULL ;
     set *lp ;
-    
+
     /* add all elements in the first list */
     for (lp = list1 ; lp ; lp = lp->next )
-       addSet (&un,lp->item);
-    
+  addSet (&un,lp->item);
+
     /* now for all those in list2 which does not */
     /* already exist in the list add             */
     for (lp = list2 ; lp ; lp = lp->next )
-       if (!isinSetWith(un,lp->item,cFunc))
-           addSet (&un,lp->item);
-    
+  if (!isinSetWith(un,lp->item,cFunc))
+      addSet (&un,lp->item);
+
     switch (throw) {
     case THROW_SRC :
-       setToNull ((void **)&list2);
-       break;
+  setToNull ((void **)&list2);
+  break;
     case THROW_DEST :
-       setToNull ((void **)&list1);
-       break;
+  setToNull ((void **)&list1);
+  break;
     case THROW_BOTH :
-       setToNull ((void **)&list1);
-       setToNull ((void **)&list2);
+  setToNull ((void **)&list1);
+  setToNull ((void **)&list2);
     }
 
     return un;
@@ -302,54 +302,54 @@ set *intersectSets (set *list1, set *list2, int throw)
 {
     set *in = NULL;
     set *lp ;
-    
+
     /* we can take any one of the lists and iterate over it */
-    for (lp = list1 ; lp ; lp = lp->next ) 
-       if (isinSet (list2,lp->item) ) 
-           addSetHead(&in,lp->item);
+    for (lp = list1 ; lp ; lp = lp->next )
+  if (isinSet (list2,lp->item) )
+      addSetHead(&in,lp->item);
 
     switch (throw) {
     case THROW_SRC :
-       setToNull ((void **)&list2);
-       break;
+  setToNull ((void **)&list2);
+  break;
     case THROW_DEST :
-       setToNull ((void **)&list1);
-       break;
+  setToNull ((void **)&list1);
+  break;
     case THROW_BOTH :
-       setToNull ((void **)&list1);
-       setToNull ((void **)&list2);
+  setToNull ((void **)&list1);
+  setToNull ((void **)&list2);
     }
-    
-    return in; 
+
+    return in;
 }
 
 /*-----------------------------------------------------------------*/
 /* intersectSetsWith - returns list of items in common to two lists*/
 /*-----------------------------------------------------------------*/
-set *intersectSetsWith (set *list1, set *list2, 
-                       int (*cFunc)(void *,void *),int throw)
+set *intersectSetsWith (set *list1, set *list2,
+      int (*cFunc)(void *,void *),int throw)
 {
     set *in = NULL;
     set *lp ;
-    
+
     /* we can take any one of the lists and iterate over it */
-    for (lp = list1 ; lp ; lp = lp->next ) 
-       if (isinSetWith (list2,lp->item,cFunc) ) 
-           addSetHead(&in,lp->item);
+    for (lp = list1 ; lp ; lp = lp->next )
+  if (isinSetWith (list2,lp->item,cFunc) )
+      addSetHead(&in,lp->item);
 
     switch (throw) {
     case THROW_SRC :
-       setToNull ((void **)&list2);
-       break;
+  setToNull ((void **)&list2);
+  break;
     case THROW_DEST :
-       setToNull ((void **)&list1);
-       break;
+  setToNull ((void **)&list1);
+  break;
     case THROW_BOTH :
-       setToNull ((void **)&list1);
-       setToNull ((void **)&list2);
+  setToNull ((void **)&list1);
+  setToNull ((void **)&list2);
     }
-    
-    return in; 
+
+    return in;
 }
 
 /*-----------------------------------------------------------------*/
@@ -361,8 +361,8 @@ int elementsInSet (set *s)
     int count = 0 ;
 
     while (loop) {
-       count++ ;
-       loop = loop->next ;
+  count++ ;
+  loop = loop->next ;
     }
 
     return count ;
@@ -377,22 +377,22 @@ set *subtractFromSet (set *left, set *right, int throw)
     set *loop ;
 
     if (right) {
-       for (loop = right ; loop ; loop = loop->next)
-           if (isinSet(result,loop->item)) 
-               deleteSetItem (&result,loop->item);
+  for (loop = right ; loop ; loop = loop->next)
+      if (isinSet(result,loop->item))
+    deleteSetItem (&result,loop->item);
     }
-    
+
     switch (throw) {
     case THROW_SRC :
-       setToNull ((void **)&right);
-       break;
+  setToNull ((void **)&right);
+  break;
     case THROW_DEST :
-       setToNull ((void **)&left);
-       break;
+  setToNull ((void **)&left);
+  break;
     case THROW_BOTH :
-       setToNull ((void **)&left);
-       setToNull ((void **)&right);
-       break ;
+  setToNull ((void **)&left);
+  setToNull ((void **)&right);
+  break ;
     }
 
     return result ;
@@ -406,11 +406,11 @@ int applyToSet ( set *list , int (*somefunc)(void *, va_list ), ...)
     set *lp ;
     va_list ap;
     int rvalue = 0 ;
-    
+
     for (lp = list ; lp ; lp = lp->next ) {
-         va_start(ap,somefunc);
-         rvalue += (*somefunc)(lp->item,ap) ;
-         va_end(ap);
+    va_start(ap,somefunc);
+    rvalue += (*somefunc)(lp->item,ap) ;
+    va_end(ap);
     }
     return rvalue;
 }
@@ -424,13 +424,13 @@ int applyToSetFTrue ( set *list , int (*somefunc)(void *, va_list ), ...)
     set *lp ;
     va_list ap;
     int rvalue = 0 ;
-    
+
     for (lp = list ; lp ; lp = lp->next ) {
-         va_start(ap,somefunc);
-         rvalue += (*somefunc)(lp->item,ap);
-         va_end(ap);
-         if (rvalue)
-               break;
+    va_start(ap,somefunc);
+    rvalue += (*somefunc)(lp->item,ap);
+    va_end(ap);
+    if (rvalue)
+    break;
     }
     return rvalue;
 }
@@ -441,8 +441,8 @@ int applyToSetFTrue ( set *list , int (*somefunc)(void *, va_list ), ...)
 void *peekSet ( set *sp)
 {
     if (!sp)
-       return NULL ;
-    
+  return NULL ;
+
     return sp->item;
 }
 
@@ -452,8 +452,8 @@ void *peekSet ( set *sp)
 void *setFirstItem (set *sset)
 {
     if (sset) {
-       sset->curr = sset ;
-       return sset->item ;
+  sset->curr = sset ;
+  return sset->item ;
     }
 
     return NULL ;
@@ -464,9 +464,9 @@ void *setFirstItem (set *sset)
 void *setNextItem (set *sset)
 {
     if (sset && sset->curr ) {
-       sset->curr = sset->curr->next ;
-       if ( sset->curr )
-           return sset->curr->item ;       
+  sset->curr = sset->curr->next ;
+  if ( sset->curr )
+      return sset->curr->item ;
     }
     return NULL ;
 }
@@ -478,17 +478,17 @@ void *getSet (set **list)
 {
     set *lp;
     void *item ;
-    
+
     /* if list is empty then we cannot delete */
     if (*list == NULL )
-       return (void *) NULL ;
-    
-    
+  return (void *) NULL ;
+
+
     /* find the item in the list */
     lp = *list ;
     item = lp->item; /* save the item */
-    
-    *list = lp->next ;   
+
+    *list = lp->next ;
     return item ;
 }
 
@@ -497,12 +497,12 @@ void *getSet (set **list)
 /*-----------------------------------------------------------------*/
 void setToNull (void **item )
 {
-    
+
     if ( !item  )
-       return ;
+  return ;
 
     if (! *item )
-       return ;
-    free(*item);  
+  return ;
+    free(*item);
     *item = NULL ;
 }