Some consistency changes to library & headers flags.
[splint-patched.git] / src / lsymbolSet.c
blob84f55f1613114d96171a4f4cb160d9f2c8ddaa91
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 ** Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
15 **
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
25 ** lsymbolSet.c
27 ** based on set_template.c
29 ** where T has T_equal (or change this) and T_unparse
32 # include "splintMacros.nf"
33 # include "basic.h"
35 /*@constant int lsymbolSetBASESIZE;@*/
36 # define lsymbolSetBASESIZE MIDBASESIZE
38 lsymbolSet lsymbolSet_new (void)
40 lsymbolSet s = (lsymbolSet) dmalloc (sizeof (*s));
42 s->entries = 0;
43 s->nspace = lsymbolSetBASESIZE;
44 s->elements = (lsymbol *) dmalloc (sizeof (*s->elements) * lsymbolSetBASESIZE);
46 return (s);
49 static void
50 lsymbolSet_grow (lsymbolSet s)
52 int i;
53 lsymbol *newelements;
55 llassert (lsymbolSet_isDefined (s));
57 s->nspace = lsymbolSetBASESIZE;
58 newelements = (lsymbol *) dmalloc (sizeof (*newelements)
59 * (s->entries + s->nspace));
61 if (newelements == (lsymbol *) 0)
63 llfatalerror (cstring_makeLiteral ("lsymbolSet_grow: out of memory!"));
66 for (i = 0; i < s->entries; i++)
68 newelements[i] = s->elements[i];
71 sfree (s->elements);
72 s->elements = newelements;
76 ** Ensures: if *e \in *s
77 ** then unchanged (*s) & result = false
78 ** else *s' = insert (*s, *e) & result = true
79 ** Modifies: *s
82 bool
83 lsymbolSet_insert (lsymbolSet s, lsymbol el)
85 llassert (lsymbolSet_isDefined (s));
87 if (lsymbolSet_member (s, el))
89 return FALSE;
91 else
93 if (s->nspace <= 0)
94 lsymbolSet_grow (s);
95 s->nspace--;
96 s->elements[s->entries] = el;
97 s->entries++;
98 return TRUE;
102 bool
103 lsymbolSet_member (lsymbolSet s, lsymbol el)
105 if (lsymbolSet_isDefined (s))
107 int i;
109 for (i = 0; i < s->entries; i++)
111 /* was: &el == &s->elements[i] ! */
113 if (lsymbol_equal (el, s->elements[i]))
115 return TRUE;
120 return FALSE;
123 # ifdef DEADCODE
124 /*@only@*/ cstring
125 lsymbolSet_unparse (lsymbolSet s)
127 if (lsymbolSet_isDefined (s))
129 int i;
130 cstring st = cstring_makeLiteral ("{");
132 for (i = 0; i < s->entries; i++)
134 if (i == 0)
136 st = message ("%q %s", st,
137 cstring_fromChars (lsymbol_toChars (s->elements[i])));
139 else
140 st = message ("%q, %s", st,
141 cstring_fromChars (lsymbol_toChars (s->elements[i])));
144 st = message ("%q }", st);
145 return st;
147 else
149 return (cstring_makeLiteral ("{ }"));
152 # endif /* DEADCODE */
154 void
155 lsymbolSet_free (/*@null@*/ lsymbolSet s)
157 if (lsymbolSet_isDefined (s))
159 sfree (s->elements);
160 sfree (s);