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

clauseStack.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 ** clauseStack.c
00026 **
00027 ** based on list_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 */
00031 
00032 # include "lclintMacros.nf"
00033 # include "basic.h"
00034 
00035 clauseStack
00036 clauseStack_new ()
00037 {
00038   clauseStack s = (clauseStack) dmalloc (sizeof (*s));
00039   
00040   s->nelements = 0;
00041   s->nspace = clauseStackBASESIZE; 
00042   s->elements = (clause *) dmalloc (sizeof (*s->elements) * clauseStackBASESIZE);
00043   s->current = 0;
00044   
00045   return (s);
00046 }
00047 
00048 static void
00049 clauseStack_grow (clauseStack s)
00050 {
00051   int i;
00052   clause *newelements; 
00053   
00054   s->nspace += clauseStackBASESIZE; 
00055 
00056   newelements = (clause *) dmalloc (sizeof (*newelements)
00057                                     * (s->nelements + s->nspace));
00058   
00059   if (newelements == (clause *) 0)
00060     {
00061       llfatalerror (cstring_makeLiteral ("clauseStack_grow: out of memory!"));
00062     }
00063   
00064   for (i = 0; i < s->nelements; i++)
00065     {
00066       newelements[i] = s->elements[i];
00067     }
00068   
00069   sfree (s->elements);
00070   s->elements = newelements;
00071 }
00072 
00073 void clauseStack_push (clauseStack s, clause el)
00074 {
00075   if (s->nspace <= 0)
00076     clauseStack_grow (s);
00077   
00078   s->nspace--;
00079   s->elements[s->nelements] = el;
00080   s->nelements++;
00081   }
00082 
00083 void clauseStack_pop (clauseStack s)
00084 {
00085   s->nelements--;
00086   s->nspace++;
00087   }
00088 
00089 clause clauseStack_top (clauseStack s)
00090 {
00091   return (s->elements[s->nelements - 1]);
00092 }
00093 
00094 void clauseStack_switchTop (clauseStack s, clause x)
00095 {
00096   llassert (s->nelements > 0);
00097   
00098   s->elements[s->nelements - 1] = x;
00099 }
00100 
00101 void
00102 clauseStack_removeFirst (clauseStack s, clause key)
00103 {
00104   if (clauseStack_top (s) == key) 
00105     {
00106       clauseStack_pop (s);
00107     }
00108   else
00109     {
00110       int i;
00111       
00112       for (i = s->nelements - 2; i >= 0; i--)
00113         {
00114           clause el = s->elements[i];
00115           
00116           if (el == key) 
00117             {
00118               int j;
00119 
00120               for (j = i; j < s->nelements - 1; j++)
00121                 {
00122                   s->elements[j] = s->elements[j + 1];
00123                 }
00124 
00125               s->nelements--;
00126               s->nspace++;
00127               return;
00128             }
00129         }
00130       
00131       llbuglit ("clauseStack_removeFirst: not found");
00132     }
00133 }
00134 
00135 int
00136 clauseStack_controlDepth (clauseStack s)
00137 {
00138   int depth = 0;
00139   int i;
00140 
00141   for (i = 0; i < s->nelements; i++)
00142     {
00143       clause current = s->elements[i];
00144 
00145       if (clause_isConditional (current))
00146         {
00147           depth++;
00148         }
00149     }
00150 
00151   return depth;
00152 }
00153 
00154 cstring
00155 clauseStack_unparse (clauseStack s)
00156 {
00157   int i;
00158   cstring st = cstring_makeLiteral ("[");
00159   
00160   for (i = 0; i < s->nelements; i++)
00161     {
00162       if (i == 0)
00163         {
00164           st = message ("%q %s", st, clause_unparse (s->elements[i]));
00165         }
00166       else
00167         st = message ("%q, %s", st, clause_unparse (s->elements[i]));
00168     }
00169   
00170   st = message ("%q ]", st);
00171   return st;
00172 }
00173 
00174 void
00175 clauseStack_clear (clauseStack s)
00176 {
00177   s->nspace += s->nelements;
00178   s->nelements = 0;
00179 }
00180 
00181 void
00182 clauseStack_free (clauseStack s)
00183 {
00184   sfree (s->elements); 
00185   sfree (s);
00186 }

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