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

intSet.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 ** intSet.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 "basic.h"
00034 # include "intSet.h"
00035 
00036 /*@only@*/ intSet
00037 intSet_new ()
00038 {
00039   intSet s = (intSet) dmalloc (sizeof (*s));
00040   
00041   s->entries = 0;
00042   s->nspace = intSetBASESIZE;
00043   s->elements = (int *) dmalloc (sizeof (*s->elements) * intSetBASESIZE);
00044 
00045   return (s);
00046 }
00047 
00048 static void
00049 intSet_grow (intSet s)
00050 {
00051   int i;
00052   int *newelements;
00053 
00054   s->nspace = intSetBASESIZE;
00055   newelements = (int *) dmalloc (sizeof (*newelements) * (s->entries + s->nspace));
00056 
00057   for (i = 0; i < s->entries; i++)
00058     {
00059       newelements[i] = s->elements[i];
00060     }
00061 
00062   sfree (s->elements); 
00063   s->elements = newelements; 
00064 }
00065 
00066 /*
00067 ** Ensures: if *e \in *s
00068 **          then unchanged (*s) & result = false
00069 **          else *s' = insert (*s, *e) & result = true
00070 ** Modifies: *s
00071 */
00072 
00073 bool
00074 intSet_insert (intSet s, int el)
00075 {
00076   int i;
00077 
00078   for (i = 0; i < s->entries; i++)
00079     {
00080       if (s->elements[i] >= el)
00081         break;
00082     }
00083 
00084   if (s->entries > 0 && s->elements[i] == el)
00085     {
00086       return FALSE;
00087     }
00088   else
00089     {
00090       if (s->nspace <= 0)
00091         intSet_grow (s);
00092 
00093       s->nspace--;
00094 
00095       if (i == (s->entries - 1))
00096         {
00097           s->elements[s->entries] = el;
00098         }
00099       else
00100         {
00101           int j;
00102           
00103           for (j = s->entries; j > i; j--)
00104             {
00105               s->elements[j] = s->elements[j-1];
00106             }
00107           s->elements[i] = el;
00108         }
00109       
00110       s->entries++;      
00111       return TRUE;
00112     }
00113 }
00114 
00115 bool
00116 intSet_member (intSet s, int el)
00117 {
00118   int i;
00119 
00120   for (i = 0; i < s->entries; i++)
00121     {
00122       if (el == s->elements[i])
00123         {
00124           return TRUE;
00125         }
00126       if (el > s->elements[i]) 
00127         {
00128           return FALSE;
00129         }
00130     }
00131   return FALSE;
00132 }
00133 
00134 # ifndef NOLCL
00135 /*@only@*/ cstring
00136 intSet_unparseText (intSet s)
00137 {
00138   int i;
00139   cstring st = cstring_undefined;
00140   int lastentry = s->entries - 1;
00141 
00142   for (i = 0; i < s->entries; i++)
00143     {
00144       if (i == 0)
00145         st = message ("%d", s->elements[i]);
00146       else if (i == lastentry)
00147         st = message ("%q or %d", st, s->elements[i]);
00148       else
00149         st = message ("%q, %d", st, s->elements[i]);
00150     }
00151 
00152   return st;
00153 }
00154 # endif
00155 
00156 /*@only@*/ cstring
00157 intSet_unparse (intSet s)
00158 {
00159   int i;
00160   cstring st = cstring_makeLiteral ("{");
00161 
00162   for (i = 0; i < s->entries; i++)
00163     {
00164       st = message ("%q %d", st, s->elements[i]);
00165     }
00166 
00167   st = message ("%q}", st);
00168   return st;
00169 }
00170 
00171 void
00172 intSet_free (intSet s)
00173 {
00174   sfree (s->elements); 
00175   sfree (s);
00176 }

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