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

lsymbolSet.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 ** lsymbolSet.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  
00035 lsymbolSet lsymbolSet_new ()
00036 {
00037   lsymbolSet s = (lsymbolSet) dmalloc (sizeof (*s));
00038 
00039   s->entries = 0;
00040   s->nspace = lsymbolSetBASESIZE;
00041   s->elements = (lsymbol *) dmalloc (sizeof (*s->elements) * lsymbolSetBASESIZE);
00042 
00043   return (s);
00044 }
00045 
00046 static void
00047 lsymbolSet_grow (lsymbolSet s)
00048 {
00049   int i;
00050   lsymbol *newelements; 
00051 
00052   llassert (lsymbolSet_isDefined (s));
00053 
00054   s->nspace = lsymbolSetBASESIZE;
00055   newelements = (lsymbol *) dmalloc (sizeof (*newelements) 
00056                                        * (s->entries + s->nspace));
00057 
00058   if (newelements == (lsymbol *) 0)
00059     {
00060       llfatalerror (cstring_makeLiteral ("lsymbolSet_grow: out of memory!"));
00061     }
00062 
00063   for (i = 0; i < s->entries; i++)
00064     {
00065       newelements[i] = s->elements[i]; 
00066     }
00067 
00068   sfree (s->elements); 
00069   s->elements = newelements;
00070 }
00071 
00072 /*
00073 ** Ensures: if *e \in *s
00074 **          then unchanged (*s) & result = false
00075 **          else *s' = insert (*s, *e) & result = true
00076 ** Modifies: *s
00077 */
00078 
00079 bool
00080 lsymbolSet_insert (lsymbolSet s, lsymbol el)
00081 {
00082   llassert (lsymbolSet_isDefined (s));
00083 
00084   if (lsymbolSet_member (s, el))
00085     {
00086       return FALSE;
00087     }
00088   else
00089     {
00090       if (s->nspace <= 0)
00091         lsymbolSet_grow (s);
00092       s->nspace--;
00093       s->elements[s->entries] = el;
00094       s->entries++;
00095       return TRUE;
00096     }
00097 }
00098 
00099 bool
00100 lsymbolSet_member (lsymbolSet s, lsymbol el)
00101 {
00102   if (lsymbolSet_isDefined (s))
00103     {
00104       int i;
00105       
00106       for (i = 0; i < s->entries; i++)
00107         {
00108           /* was: &el == &s->elements[i] ! */
00109 
00110           if (lsymbol_equal (el, s->elements[i]))
00111             {
00112               return TRUE;
00113             }
00114         }
00115     }
00116 
00117   return FALSE;
00118 }
00119 
00120 /*@only@*/ cstring
00121 lsymbolSet_unparse (lsymbolSet s)
00122 {
00123   if (lsymbolSet_isDefined (s))
00124     {
00125       int i;
00126       cstring st = cstring_makeLiteral ("{");
00127       
00128       for (i = 0; i < s->entries; i++)
00129         {
00130           if (i == 0)
00131             {
00132               st = message ("%q %s", st, 
00133                             cstring_fromChars (lsymbol_toChars (s->elements[i])));
00134             }
00135           else
00136             st = message ("%q, %s", st, 
00137                           cstring_fromChars (lsymbol_toChars (s->elements[i])));
00138         }
00139       
00140       st = message ("%q }", st);
00141       return st;
00142     }
00143   else
00144     {
00145       return (cstring_makeLiteral ("{ }"));
00146     }
00147 }
00148 
00149 void
00150 lsymbolSet_free (/*@null@*/ lsymbolSet s)
00151 {
00152   if (lsymbolSet_isDefined (s))
00153     {
00154       sfree (s->elements); 
00155       sfree (s);
00156     }
00157 }

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