2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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"
35 /*@constant int sortSetBASESIZE;@*/
36 # define sortSetBASESIZE MIDBASESIZE
38 sortSet
sortSet_new (void)
40 sortSet s
= (sortSet
) dmalloc (sizeof (*s
));
43 s
->nspace
= sortSetBASESIZE
;
44 s
->elements
= (sort
*) dmalloc (sizeof (*s
->elements
) * sortSetBASESIZE
);
49 static /*@notnull@*/ sortSet
50 sortSet_predict (int size
)
52 sortSet s
= (sortSet
) dmalloc (sizeof (*s
));
59 s
->elements
= (sort
*) dmalloc (sizeof (*s
->elements
) * size
);
71 sortSet_grow (/*@notnull@*/ sortSet s
)
76 s
->nspace
= sortSetBASESIZE
;
77 newelements
= (sort
*) dmalloc (sizeof (*newelements
) * (s
->entries
+ s
->nspace
));
79 if (newelements
== (sort
*) 0)
81 llfatalerror (cstring_makeLiteral ("sortSet_grow: out of memory!"));
84 for (i
= 0; i
< s
->entries
; i
++)
86 newelements
[i
] = s
->elements
[i
];
90 s
->elements
= newelements
;
94 ** Ensures: if *e \in *s
95 ** then unchanged (*s) & result = false
96 ** else *s' = insert (*s, *e) & result = true
101 sortSet_insert (sortSet s
, sort el
)
103 llassert (sortSet_isDefined (s
));
105 if (sortSet_member (s
, el
))
114 s
->elements
[s
->entries
] = el
;
121 sortSet_choose (sortSet s
)
123 llassert (sortSet_isDefined (s
) && s
->entries
> 0);
124 return (s
->elements
[0]);
128 sortSet_member (sortSet s
, sort el
)
130 if (sortSet_isDefined (s
))
134 for (i
= 0; i
< s
->entries
; i
++)
136 if (sort_equal (el
, s
->elements
[i
]))
148 sortSet_unparse (sortSet s
)
150 return (message ("{ %q }", sortSet_unparseClean (s
)));
152 # endif /* DEADCODE */
155 sortSet_unparseClean (sortSet s
)
157 cstring st
= cstring_undefined
;
159 if (sortSet_isDefined (s
))
163 for (i
= 0; i
< s
->entries
; i
++)
167 st
= message ("%q%s", st
, sort_unparseName (s
->elements
[i
]));
171 st
= message ("%q, %s", st
, sort_unparseName (s
->elements
[i
]));
180 sortSet_unparseOr (sortSet s
)
182 cstring st
= cstring_undefined
;
184 if (sortSet_isDefined (s
))
187 int last
= s
->entries
- 1;
189 for (i
= 0; i
< s
->entries
; i
++)
193 st
= cstring_concatFree (st
, sort_unparse (s
->elements
[i
]));
199 /* was sort_unparse ??? */
200 st
= message ("%q or %q", st
, sort_unparse (s
->elements
[i
]));
204 st
= message ("%q, %q", st
, sort_unparse (s
->elements
[i
]));
214 sortSet_free (sortSet s
)
216 if (sortSet_isDefined (s
))
224 sortSet_copy (sortSet s
)
226 sortSet t
= sortSet_predict (sortSet_size (s
));
229 if (sortSet_isDefined (s
))
231 for (i
= 0; i
< sortSet_size (s
); i
++)
233 (void) sortSet_insert (t
, s
->elements
[i
]);