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

lslOpSet.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 /*
00026 ** lslOpSet.c
00027 **
00028 ** based on set_template.c
00029 **
00030 ** where T has T_equal (or change this) and T_unparse
00031 */
00032 
00033 # include "lclintMacros.nf"
00034 # include "llbasic.h"
00035 # include "checking.h"          /* for lslOp_equal */
00036 
00037 static bool lslOpSet_member (lslOpSet p_s, lslOp p_el);
00038 
00039 lslOpSet lslOpSet_new ()
00040 {
00041   lslOpSet s = (lslOpSet) dmalloc (sizeof (*s));
00042 
00043   s->entries = 0;
00044   s->nspace = lslOpSetBASESIZE;
00045   s->elements = (lslOp *)
00046     dmalloc (sizeof (*s->elements) * lslOpSetBASESIZE);
00047   
00048   return (s);
00049 }
00050 
00051 static /*@only@*/ lslOpSet
00052 lslOpSet_predict (int size)
00053 {
00054   lslOpSet s = (lslOpSet) dmalloc (sizeof (*s));
00055   
00056   s->entries = 0;
00057 
00058   if (size > 0)
00059     {
00060       s->nspace = size;
00061       s->elements = (lslOp *) dmalloc (sizeof (*s->elements) * size);
00062     }
00063   else
00064     {
00065       s->nspace = 0;
00066       s->elements = NULL;
00067     }
00068 
00069   return (s);
00070 }
00071 
00072 static void
00073 lslOpSet_grow (/*@notnull@*/ lslOpSet s)
00074 {
00075   int i;
00076   lslOp *newelements;
00077 
00078   s->nspace = lslOpSetBASESIZE;
00079   newelements = (lslOp *) dmalloc (sizeof (*newelements)
00080                                          * (s->entries + s->nspace));
00081 
00082   if (newelements == (lslOp *) 0)
00083     {
00084       llfatalerror (cstring_makeLiteral ("lslOpSet_grow: out of memory!"));
00085     }
00086 
00087   for (i = 0; i < s->entries; i++)
00088     {
00089       newelements[i] = s->elements[i];
00090     }
00091 
00092   sfree (s->elements); 
00093   s->elements = newelements;
00094 }
00095 
00096 /*
00097 ** Ensures: if *e \in *s
00098 **          then unchanged (*s) & result = false
00099 **          else *s' = insert (*s, *e) & result = true
00100 ** Modifies: *s
00101 */
00102 
00103 bool
00104 lslOpSet_insert (lslOpSet s, /*@only@*/ lslOp el)
00105 {
00106   llassert (lslOpSet_isDefined (s));
00107 
00108   if (lslOpSet_member (s, el))
00109     {
00110       lslOp_free (el);
00111       return FALSE;
00112     }
00113   else
00114     {
00115       if (s->nspace <= 0)
00116         lslOpSet_grow (s);
00117       s->nspace--;
00118       s->elements[s->entries] = el;
00119       s->entries++;
00120       return TRUE;
00121     }
00122 }
00123 
00124 static bool
00125 lslOpSet_member (lslOpSet s, lslOp el)
00126 {
00127   if (lslOpSet_isDefined (s))
00128     {
00129       int i;
00130       
00131       for (i = 0; i < lslOpSet_size (s); i++)
00132         {
00133           if (lslOp_equal (el, s->elements[i]))
00134             return TRUE;
00135         }
00136     }
00137 
00138   return FALSE;
00139 }
00140 
00141 /*@only@*/ cstring
00142 lslOpSet_unparse (lslOpSet s)
00143 {
00144   if (lslOpSet_isDefined (s))
00145     {
00146       int i;
00147       cstring st = cstring_makeLiteral ("{");
00148       
00149       for (i = 0; i < lslOpSet_size (s); i++)
00150         {
00151           st = message ("%q   %q", st, lslOp_unparse (s->elements[i]));
00152         }
00153       
00154       st = message ("%q}", st);
00155       return st;
00156     }
00157   else
00158     {
00159       return (cstring_makeLiteral ("{ }"));
00160     }
00161 }
00162 
00163 /*@only@*/ lslOpSet
00164 lslOpSet_copy (lslOpSet s)
00165 {
00166   if (lslOpSet_isDefined (s))
00167     {
00168       lslOpSet t = lslOpSet_predict (lslOpSet_size (s));
00169       int i;
00170       
00171       for (i = 0; i < lslOpSet_size (s); i++)
00172         {
00173           (void) lslOpSet_insert (t, lslOp_copy (s->elements[i])); 
00174         }
00175       
00176       return t;
00177     }
00178   else
00179     {
00180       return lslOpSet_undefined;
00181     }
00182 }
00183 
00184 void
00185 lslOpSet_free (lslOpSet s)
00186 {
00187   if (lslOpSet_isDefined (s))
00188     {
00189       int i;
00190       for (i = 0; i < s->entries; i++)
00191         {
00192           lslOp_free (s->elements[i]); 
00193         }
00194       
00195       sfree (s->elements); 
00196       sfree (s);
00197     }
00198 }

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