]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 University of Virginia, |
616915dd | 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 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | ||
25 | /* | |
26 | ** lslOpSet.c | |
27 | ** | |
28 | ** based on set_template.c | |
29 | ** | |
30 | ** where T has T_equal (or change this) and T_unparse | |
31 | */ | |
32 | ||
1b8ae690 | 33 | # include "splintMacros.nf" |
616915dd | 34 | # include "llbasic.h" |
35 | # include "checking.h" /* for lslOp_equal */ | |
36 | ||
37 | static bool lslOpSet_member (lslOpSet p_s, lslOp p_el); | |
38 | ||
39 | lslOpSet lslOpSet_new () | |
40 | { | |
41 | lslOpSet s = (lslOpSet) dmalloc (sizeof (*s)); | |
42 | ||
43 | s->entries = 0; | |
44 | s->nspace = lslOpSetBASESIZE; | |
45 | s->elements = (lslOp *) | |
46 | dmalloc (sizeof (*s->elements) * lslOpSetBASESIZE); | |
47 | ||
48 | return (s); | |
49 | } | |
50 | ||
51 | static /*@only@*/ lslOpSet | |
52 | lslOpSet_predict (int size) | |
53 | { | |
54 | lslOpSet s = (lslOpSet) dmalloc (sizeof (*s)); | |
55 | ||
56 | s->entries = 0; | |
57 | ||
58 | if (size > 0) | |
59 | { | |
60 | s->nspace = size; | |
61 | s->elements = (lslOp *) dmalloc (sizeof (*s->elements) * size); | |
62 | } | |
63 | else | |
64 | { | |
65 | s->nspace = 0; | |
66 | s->elements = NULL; | |
67 | } | |
68 | ||
69 | return (s); | |
70 | } | |
71 | ||
72 | static void | |
73 | lslOpSet_grow (/*@notnull@*/ lslOpSet s) | |
74 | { | |
75 | int i; | |
76 | lslOp *newelements; | |
77 | ||
78 | s->nspace = lslOpSetBASESIZE; | |
79 | newelements = (lslOp *) dmalloc (sizeof (*newelements) | |
80 | * (s->entries + s->nspace)); | |
81 | ||
82 | if (newelements == (lslOp *) 0) | |
83 | { | |
84 | llfatalerror (cstring_makeLiteral ("lslOpSet_grow: out of memory!")); | |
85 | } | |
86 | ||
87 | for (i = 0; i < s->entries; i++) | |
88 | { | |
89 | newelements[i] = s->elements[i]; | |
90 | } | |
91 | ||
92 | sfree (s->elements); | |
93 | s->elements = newelements; | |
94 | } | |
95 | ||
96 | /* | |
97 | ** Ensures: if *e \in *s | |
98 | ** then unchanged (*s) & result = false | |
99 | ** else *s' = insert (*s, *e) & result = true | |
100 | ** Modifies: *s | |
101 | */ | |
102 | ||
103 | bool | |
104 | lslOpSet_insert (lslOpSet s, /*@only@*/ lslOp el) | |
105 | { | |
106 | llassert (lslOpSet_isDefined (s)); | |
107 | ||
108 | if (lslOpSet_member (s, el)) | |
109 | { | |
110 | lslOp_free (el); | |
111 | return FALSE; | |
112 | } | |
113 | else | |
114 | { | |
115 | if (s->nspace <= 0) | |
116 | lslOpSet_grow (s); | |
117 | s->nspace--; | |
118 | s->elements[s->entries] = el; | |
119 | s->entries++; | |
120 | return TRUE; | |
121 | } | |
122 | } | |
123 | ||
124 | static bool | |
125 | lslOpSet_member (lslOpSet s, lslOp el) | |
126 | { | |
127 | if (lslOpSet_isDefined (s)) | |
128 | { | |
129 | int i; | |
130 | ||
131 | for (i = 0; i < lslOpSet_size (s); i++) | |
132 | { | |
133 | if (lslOp_equal (el, s->elements[i])) | |
134 | return TRUE; | |
135 | } | |
136 | } | |
137 | ||
138 | return FALSE; | |
139 | } | |
140 | ||
141 | /*@only@*/ cstring | |
142 | lslOpSet_unparse (lslOpSet s) | |
143 | { | |
144 | if (lslOpSet_isDefined (s)) | |
145 | { | |
146 | int i; | |
147 | cstring st = cstring_makeLiteral ("{"); | |
148 | ||
149 | for (i = 0; i < lslOpSet_size (s); i++) | |
150 | { | |
151 | st = message ("%q %q", st, lslOp_unparse (s->elements[i])); | |
152 | } | |
153 | ||
154 | st = message ("%q}", st); | |
155 | return st; | |
156 | } | |
157 | else | |
158 | { | |
159 | return (cstring_makeLiteral ("{ }")); | |
160 | } | |
161 | } | |
162 | ||
163 | /*@only@*/ lslOpSet | |
164 | lslOpSet_copy (lslOpSet s) | |
165 | { | |
166 | if (lslOpSet_isDefined (s)) | |
167 | { | |
168 | lslOpSet t = lslOpSet_predict (lslOpSet_size (s)); | |
169 | int i; | |
170 | ||
171 | for (i = 0; i < lslOpSet_size (s); i++) | |
172 | { | |
173 | (void) lslOpSet_insert (t, lslOp_copy (s->elements[i])); | |
174 | } | |
175 | ||
176 | return t; | |
177 | } | |
178 | else | |
179 | { | |
180 | return lslOpSet_undefined; | |
181 | } | |
182 | } | |
183 | ||
184 | void | |
185 | lslOpSet_free (lslOpSet s) | |
186 | { | |
187 | if (lslOpSet_isDefined (s)) | |
188 | { | |
189 | int i; | |
190 | for (i = 0; i < s->entries; i++) | |
191 | { | |
192 | lslOp_free (s->elements[i]); | |
193 | } | |
194 | ||
195 | sfree (s->elements); | |
196 | sfree (s); | |
197 | } | |
198 | } |