Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

sigNodeSet.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** sigNodeSet.c
00026 **
00027 ** based on set_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 */
00031 
00032 # include "lclintMacros.nf"
00033 # include "llbasic.h"
00034 # include "intSet.h"
00035 
00036 static bool sigNodeSet_member (sigNodeSet p_s, sigNode p_el);
00037 
00038 /*@only@*/ sigNodeSet
00039 sigNodeSet_new ()
00040 {
00041   sigNodeSet s = (sigNodeSet) dmalloc (sizeof (*s));
00042 
00043   s->entries = 0;
00044   s->nspace = sigNodeSetBASESIZE;
00045   s->elements = (sigNode *) dmalloc (sizeof (*s->elements) * sigNodeSetBASESIZE);
00046 
00047   return (s);
00048 }
00049 
00050 /*@only@*/ sigNodeSet
00051 sigNodeSet_singleton (sigNode el)
00052 {
00053   sigNodeSet s = (sigNodeSet) dmalloc (sizeof (*s));
00054 
00055   s->entries = 1;
00056   s->nspace = sigNodeSetBASESIZE - 1;
00057   s->elements = (sigNode *) dmalloc (sizeof (*s->elements) * sigNodeSetBASESIZE);
00058   s->elements[0] = el;
00059 
00060   return (s);
00061 }
00062 
00063 static void
00064 sigNodeSet_grow (/*@notnull@*/ sigNodeSet s)
00065 {
00066   int i;
00067   sigNode *newelements; 
00068 
00069   s->nspace = sigNodeSetBASESIZE;
00070   newelements = (sigNode *) dmalloc (sizeof (*newelements) 
00071                                            * (s->entries + s->nspace));
00072   
00073   for (i = 0; i < s->entries; i++)
00074     {
00075       newelements[i] = s->elements[i];
00076     }
00077 
00078   sfree (s->elements); 
00079   s->elements = newelements;
00080 }
00081 
00082 /*
00083 ** Ensures: if *e \in *s
00084 **          then unchanged (*s) & result = false
00085 **          else *s' = insert (*s, *e) & result = true
00086 ** Modifies: *s
00087 */
00088 
00089 bool
00090 sigNodeSet_insert (sigNodeSet s, /*@owned@*/ sigNode el)
00091 {
00092   llassert (sigNodeSet_isDefined (s));
00093 
00094   if (sigNodeSet_member (s, el))
00095     {
00096       sigNode_free (el);
00097       return FALSE;
00098     }
00099   else
00100     {
00101       if (s->nspace <= 0)
00102         {
00103           sigNodeSet_grow (s);
00104         }
00105 
00106       s->nspace--;
00107       s->elements[s->entries] = el;
00108       s->entries++;
00109       return TRUE;
00110     }
00111 }
00112 
00113 static bool
00114 sigNodeSet_member (sigNodeSet s, sigNode el)
00115 {
00116   if (sigNodeSet_isUndefined (s)) 
00117     {
00118       return FALSE;
00119     }
00120   else
00121     {
00122       int i;
00123       
00124       for (i = 0; i < s->entries; i++)
00125         {
00126           if (sigNode_equal (el, s->elements[i]))
00127             return TRUE;
00128         }
00129       return FALSE;
00130     }
00131 }
00132 
00133 /*@only@*/ cstring
00134 sigNodeSet_unparse (sigNodeSet s)
00135 {
00136   int i;
00137   cstring st = cstring_undefined;
00138 
00139   if (sigNodeSet_isDefined (s))
00140     {
00141       for (i = 0; i < s->entries; i++)
00142         {
00143           if (i == 0)
00144             {
00145               st = sigNode_unparse (s->elements[i]);
00146             }
00147           else
00148             st = message ("%q, %q", st, sigNode_unparse (s->elements[i]));
00149         }
00150     }
00151      
00152   return st;
00153 }
00154 
00155 /*@only@*/ cstring
00156 sigNodeSet_unparseSomeSigs (sigNodeSet s)
00157 {
00158   int i;
00159   cstring st = cstring_undefined;
00160 
00161   if (sigNodeSet_isDefined (s))
00162     {
00163       for (i = 0; i < s->entries; i++)
00164         {
00165           cstring t = sigNode_unparseText (s->elements[i]);
00166           
00167           if (i == 0)
00168             {
00169               st = cstring_copy (t);
00170               cstring_free (t);
00171             }
00172           else if (i > 5 && (s->entries > 8))
00173             {
00174               return (message ("%q; %q; ... (%d more signatures)",
00175                                st, t, (s->entries - i - 1)));
00176             }
00177           else
00178             {
00179               st = message ("%q; %q", st, t);
00180             }
00181         }
00182     }
00183      
00184   return st;
00185 }
00186 
00187 /*@only@*/ cstring
00188 sigNodeSet_unparsePossibleAritys (sigNodeSet s)
00189 {
00190   int i;
00191   intSet is = intSet_new ();
00192   cstring st;
00193 
00194   if (sigNodeSet_isDefined (s))
00195     {
00196       for (i = 0; i < s->entries; i++)
00197         {
00198           int arity = ltokenList_size ((s->elements[i])->domain);
00199           (void) intSet_insert (is, arity);
00200         }
00201     }
00202 
00203   st = intSet_unparseText (is);
00204   intSet_free (is);
00205   return (st);
00206 }
00207 
00208 void
00209 sigNodeSet_free (sigNodeSet s)
00210 {
00211   if (sigNodeSet_isDefined (s))
00212     {
00213       int i;
00214       for (i = 0; i < s->entries; i++)
00215         {
00216           sigNode_free (s->elements[i]); 
00217         }
00218       
00219       sfree (s->elements); 
00220       sfree (s);
00221     }
00222 }

Generated at Fri Nov 3 18:57:43 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000