]>
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 | ** lsymbolSet.c | |
26 | ** | |
27 | ** based on set_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | */ | |
31 | ||
1b8ae690 | 32 | # include "splintMacros.nf" |
616915dd | 33 | # include "llbasic.h" |
34 | ||
35 | lsymbolSet lsymbolSet_new () | |
36 | { | |
37 | lsymbolSet s = (lsymbolSet) dmalloc (sizeof (*s)); | |
38 | ||
39 | s->entries = 0; | |
40 | s->nspace = lsymbolSetBASESIZE; | |
41 | s->elements = (lsymbol *) dmalloc (sizeof (*s->elements) * lsymbolSetBASESIZE); | |
42 | ||
43 | return (s); | |
44 | } | |
45 | ||
46 | static void | |
47 | lsymbolSet_grow (lsymbolSet s) | |
48 | { | |
49 | int i; | |
50 | lsymbol *newelements; | |
51 | ||
52 | llassert (lsymbolSet_isDefined (s)); | |
53 | ||
54 | s->nspace = lsymbolSetBASESIZE; | |
55 | newelements = (lsymbol *) dmalloc (sizeof (*newelements) | |
56 | * (s->entries + s->nspace)); | |
57 | ||
58 | if (newelements == (lsymbol *) 0) | |
59 | { | |
60 | llfatalerror (cstring_makeLiteral ("lsymbolSet_grow: out of memory!")); | |
61 | } | |
62 | ||
63 | for (i = 0; i < s->entries; i++) | |
64 | { | |
65 | newelements[i] = s->elements[i]; | |
66 | } | |
67 | ||
68 | sfree (s->elements); | |
69 | s->elements = newelements; | |
70 | } | |
71 | ||
72 | /* | |
73 | ** Ensures: if *e \in *s | |
74 | ** then unchanged (*s) & result = false | |
75 | ** else *s' = insert (*s, *e) & result = true | |
76 | ** Modifies: *s | |
77 | */ | |
78 | ||
79 | bool | |
80 | lsymbolSet_insert (lsymbolSet s, lsymbol el) | |
81 | { | |
82 | llassert (lsymbolSet_isDefined (s)); | |
83 | ||
84 | if (lsymbolSet_member (s, el)) | |
85 | { | |
86 | return FALSE; | |
87 | } | |
88 | else | |
89 | { | |
90 | if (s->nspace <= 0) | |
91 | lsymbolSet_grow (s); | |
92 | s->nspace--; | |
93 | s->elements[s->entries] = el; | |
94 | s->entries++; | |
95 | return TRUE; | |
96 | } | |
97 | } | |
98 | ||
99 | bool | |
100 | lsymbolSet_member (lsymbolSet s, lsymbol el) | |
101 | { | |
102 | if (lsymbolSet_isDefined (s)) | |
103 | { | |
104 | int i; | |
105 | ||
106 | for (i = 0; i < s->entries; i++) | |
107 | { | |
108 | /* was: &el == &s->elements[i] ! */ | |
109 | ||
110 | if (lsymbol_equal (el, s->elements[i])) | |
111 | { | |
112 | return TRUE; | |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | return FALSE; | |
118 | } | |
119 | ||
120 | /*@only@*/ cstring | |
121 | lsymbolSet_unparse (lsymbolSet s) | |
122 | { | |
123 | if (lsymbolSet_isDefined (s)) | |
124 | { | |
125 | int i; | |
126 | cstring st = cstring_makeLiteral ("{"); | |
127 | ||
128 | for (i = 0; i < s->entries; i++) | |
129 | { | |
130 | if (i == 0) | |
131 | { | |
132 | st = message ("%q %s", st, | |
133 | cstring_fromChars (lsymbol_toChars (s->elements[i]))); | |
134 | } | |
135 | else | |
136 | st = message ("%q, %s", st, | |
137 | cstring_fromChars (lsymbol_toChars (s->elements[i]))); | |
138 | } | |
139 | ||
140 | st = message ("%q }", st); | |
141 | return st; | |
142 | } | |
143 | else | |
144 | { | |
145 | return (cstring_makeLiteral ("{ }")); | |
146 | } | |
147 | } | |
148 | ||
149 | void | |
150 | lsymbolSet_free (/*@null@*/ lsymbolSet s) | |
151 | { | |
152 | if (lsymbolSet_isDefined (s)) | |
153 | { | |
154 | sfree (s->elements); | |
155 | sfree (s); | |
156 | } | |
157 | } |