]> andersk Git - splint.git/blob - src/sigNodeSet.c
e7f55d767bf39b14c0985ce5c8ca6ee8e60fa42c
[splint.git] / src / sigNodeSet.c
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.
19 **
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
23 */
24 /*
25 ** sigNodeSet.c
26 **
27 ** based on set_template.c
28 **
29 ** where T has T_equal (or change this) and T_unparse
30 */
31
32 # include "splintMacros.nf"
33 # include "llbasic.h"
34 # include "intSet.h"
35
36 static bool sigNodeSet_member (sigNodeSet p_s, sigNode p_el);
37
38 /*@only@*/ sigNodeSet
39 sigNodeSet_new ()
40 {
41   sigNodeSet s = (sigNodeSet) dmalloc (sizeof (*s));
42
43   s->entries = 0;
44   s->nspace = sigNodeSetBASESIZE;
45   s->elements = (sigNode *) dmalloc (sizeof (*s->elements) * sigNodeSetBASESIZE);
46
47   return (s);
48 }
49
50 /*@only@*/ sigNodeSet
51 sigNodeSet_singleton (sigNode el)
52 {
53   sigNodeSet s = (sigNodeSet) dmalloc (sizeof (*s));
54
55   s->entries = 1;
56   s->nspace = sigNodeSetBASESIZE - 1;
57   s->elements = (sigNode *) dmalloc (sizeof (*s->elements) * sigNodeSetBASESIZE);
58   s->elements[0] = el;
59
60   return (s);
61 }
62
63 static void
64 sigNodeSet_grow (/*@notnull@*/ sigNodeSet s)
65 {
66   int i;
67   sigNode *newelements; 
68
69   s->nspace = sigNodeSetBASESIZE;
70   newelements = (sigNode *) dmalloc (sizeof (*newelements) 
71                                            * (s->entries + s->nspace));
72   
73   for (i = 0; i < s->entries; i++)
74     {
75       newelements[i] = s->elements[i];
76     }
77
78   sfree (s->elements); 
79   s->elements = newelements;
80 }
81
82 /*
83 ** Ensures: if *e \in *s
84 **          then unchanged (*s) & result = false
85 **          else *s' = insert (*s, *e) & result = true
86 ** Modifies: *s
87 */
88
89 bool
90 sigNodeSet_insert (sigNodeSet s, /*@owned@*/ sigNode el)
91 {
92   llassert (sigNodeSet_isDefined (s));
93
94   if (sigNodeSet_member (s, el))
95     {
96       sigNode_free (el);
97       return FALSE;
98     }
99   else
100     {
101       if (s->nspace <= 0)
102         {
103           sigNodeSet_grow (s);
104         }
105
106       s->nspace--;
107       s->elements[s->entries] = el;
108       s->entries++;
109       return TRUE;
110     }
111 }
112
113 static bool
114 sigNodeSet_member (sigNodeSet s, sigNode el)
115 {
116   if (sigNodeSet_isUndefined (s)) 
117     {
118       return FALSE;
119     }
120   else
121     {
122       int i;
123       
124       for (i = 0; i < s->entries; i++)
125         {
126           if (sigNode_equal (el, s->elements[i]))
127             return TRUE;
128         }
129       return FALSE;
130     }
131 }
132
133 /*@only@*/ cstring
134 sigNodeSet_unparse (sigNodeSet s)
135 {
136   int i;
137   cstring st = cstring_undefined;
138
139   if (sigNodeSet_isDefined (s))
140     {
141       for (i = 0; i < s->entries; i++)
142         {
143           if (i == 0)
144             {
145               st = sigNode_unparse (s->elements[i]);
146             }
147           else
148             st = message ("%q, %q", st, sigNode_unparse (s->elements[i]));
149         }
150     }
151      
152   return st;
153 }
154
155 /*@only@*/ cstring
156 sigNodeSet_unparseSomeSigs (sigNodeSet s)
157 {
158   int i;
159   cstring st = cstring_undefined;
160
161   if (sigNodeSet_isDefined (s))
162     {
163       for (i = 0; i < s->entries; i++)
164         {
165           cstring t = sigNode_unparseText (s->elements[i]);
166           
167           if (i == 0)
168             {
169               st = cstring_copy (t);
170               cstring_free (t);
171             }
172           else if (i > 5 && (s->entries > 8))
173             {
174               return (message ("%q; %q; ... (%d more signatures)",
175                                st, t, (s->entries - i - 1)));
176             }
177           else
178             {
179               st = message ("%q; %q", st, t);
180             }
181         }
182     }
183      
184   return st;
185 }
186
187 /*@only@*/ cstring
188 sigNodeSet_unparsePossibleAritys (sigNodeSet s)
189 {
190   int i;
191   intSet is = intSet_new ();
192   cstring st;
193
194   if (sigNodeSet_isDefined (s))
195     {
196       for (i = 0; i < s->entries; i++)
197         {
198           int arity = ltokenList_size ((s->elements[i])->domain);
199           (void) intSet_insert (is, arity);
200         }
201     }
202
203   st = intSet_unparseText (is);
204   intSet_free (is);
205   return (st);
206 }
207
208 void
209 sigNodeSet_free (sigNodeSet s)
210 {
211   if (sigNodeSet_isDefined (s))
212     {
213       int i;
214       for (i = 0; i < s->entries; i++)
215         {
216           sigNode_free (s->elements[i]); 
217         }
218       
219       sfree (s->elements); 
220       sfree (s);
221     }
222 }
This page took 0.041013 seconds and 3 git commands to generate.