2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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
27 ** based on set_template.c
29 ** where T has T_equal (or change this) and T_unparse
32 # include "splintMacros.nf"
38 return usymIdSet_undefined;
41 static /*@notnull@*/ /*@only@*/ usymIdSet
42 usymIdSet_newEmpty (void)
44 usymIdSet s = (usymIdSet) dmalloc (sizeof (*s));
47 s->nspace = usymIdSetBASESIZE;
48 s->elements = (usymId *) dmalloc (sizeof (*s->elements) * usymIdSetBASESIZE);
54 usymIdSet_grow (/*@notnull@*/ usymIdSet s)
59 s->nspace = usymIdSetBASESIZE;
60 newelements = (usymId *) dmalloc (sizeof (*newelements) * (s->entries + s->nspace));
62 for (i = 0; i < s->entries; i++)
64 newelements[i] = s->elements[i];
68 s->elements = newelements;
72 usymIdSet_single (usymId t)
74 usymIdSet s = (usymIdSet) dmalloc (sizeof (*s));
77 s->nspace = usymIdSetBASESIZE - 1;
78 s->elements = (usymId *) dmalloc (sizeof (*s->elements) * usymIdSetBASESIZE);
85 usymIdSet_insert (/*@returned@*/ usymIdSet s, usymId el)
87 if (usymIdSet_isUndefined (s))
89 s = usymIdSet_newEmpty ();
92 if (usymIdSet_member (s, el))
101 s->elements[s->entries] = el;
108 usymIdSet_copy (/*@notnull@*/ usymIdSet s)
110 int size = s->entries + 1;
111 usymIdSet t = (usymIdSet) dmalloc (sizeof (*t));
114 t->entries = s->entries;
116 t->elements = (usymId *) dmalloc (sizeof (*t->elements) * size);
118 for (i = 0; i < s->entries; i++)
120 t->elements[i] = s->elements[i];
127 usymIdSet_add (usymIdSet s, usymId el)
129 if (usymIdSet_isDefined (s))
131 llassert (!usymIdSet_member (s, el));
133 return (usymIdSet_insert (usymIdSet_copy (s), el));
137 return (usymIdSet_single (el));
142 usymIdSet_removeFresh (/*@temp@*/ usymIdSet s, usymId el)
144 if (usymIdSet_isDefined (s))
146 usymIdSet t = usymIdSet_newEmpty ();
149 for (i = 0; i < s->entries; i++)
151 if (!usymId_equal (el, s->elements[i]))
153 t = usymIdSet_insert (t, s->elements[i]);
161 return usymIdSet_undefined;
166 usymIdSet_newUnion (usymIdSet s1, usymIdSet s2)
168 usymIdSet t = usymIdSet_new ();
170 usymIdSet_elements (s1, current)
172 t = usymIdSet_insert (t, current);
173 } end_usymIdSet_elements;
175 usymIdSet_elements (s2, current)
177 t = usymIdSet_insert (t, current);
178 } end_usymIdSet_elements;
184 ** returns a new usymIdSet comprised of all elements
185 ** in s which are not in t.
189 usymIdSet_subtract (usymIdSet s, usymIdSet t)
191 usymIdSet r = usymIdSet_new ();
193 usymIdSet_elements (s, current)
195 if (!usymIdSet_member (t, current))
197 r = usymIdSet_insert (r, current);
199 } end_usymIdSet_elements;
205 usymIdSet_member (usymIdSet s, usymId el)
207 if (usymIdSet_isUndefined (s))
215 for (i = 0; i < s->entries; i++)
217 if (usymId_equal (el, s->elements[i]))
225 usymIdSet_free (/*@only@*/ usymIdSet s)
227 if (!usymIdSet_isUndefined (s))
230 for (i = 0; i < s->entries; i++)
232 /* usymId_free (s->elements[i]); */
240 cstring usymIdSet_dump (usymIdSet lset)
242 cstring st = cstring_undefined;
244 if (!usymIdSet_isUndefined (lset))
249 for (i = 0; i < lset->entries; i++)
251 usymId current = lset->elements[i];
253 if (!usymId_isInvalid (current))
255 current = usymtab_convertId (current);
259 st = message ("%d", current);
264 st = message ("%q,%d", st, current);
273 ** end of list is '@' or '\0'
277 usymIdSet_undump (char **s)
279 usymIdSet t = usymIdSet_new ();
284 while ((c = **s) != '\0' && c != '@' && c != '#' && c != '\n')
288 while (c != '@' && c != '#' && c != ',' && c != '\0' && c != '\n')
290 while (c >= '0' && c <= '9')
293 tid += (int) (c - '0');
300 llcontbug (message ("usymIdSet_undump: loop: %s",
301 cstring_fromChars (*s)));
308 /*@innerbreak@*/ break;
313 t = usymIdSet_insert (t, usymId_fromInt (tid));
326 usymIdSet_unparse (usymIdSet ll)
328 cstring s = cstring_undefined;
330 if (!usymIdSet_isUndefined (ll))
334 for (i = 0; i < ll->entries; i++)
336 usymId current = ll->elements[i];
339 s = uentry_getName (usymtab_getGlobalEntry (current));
341 s = message ("%q, %q", s, uentry_getName (usymtab_getGlobalEntry (current)));
349 usymIdSet_compare (usymIdSet l1, usymIdSet l2)
351 if (usymIdSet_isUndefined (l1))
353 return (usymIdSet_size (l2) == 0 ? 0 : 1);
356 if (usymIdSet_isUndefined (l2))
358 return (usymIdSet_size (l1) == 0 ? 0 : 1);
362 int li1 = l1->entries;
363 int li2 = l2->entries;
364 int leastelements = (li1 < li2) ? li1 : li2;
367 while (i < leastelements)
369 if (usymId_equal (l1->elements[i], l2->elements[i]))
375 if (l1->elements[i] > l2->elements[i])
386 return (int_compare (li1, li2));