/[ascend]/trunk/base/generic/compiler/sets.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/sets.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (show annotations) (download) (as text)
Fri Dec 16 00:20:44 2005 UTC (14 years, 9 months ago) by jds
File MIME type: text/x-csrc
File size: 10040 byte(s)
Fixed various bugs & compiler warnings:
- numerous AssertAllocateMemory & AssertMemory() bugs
- converted strdup() calls to ascstrdup() (and ascfree())
- several minor (mostly VisualC) compiler warnings (old style declarations, assignment in conditional, ...)
1 /*
2 * Implementation of set routines
3 * by Tom Epperly
4 * August 1, 1989
5 * Version: $Revision: 1.13 $
6 * Version control file: $RCSfile: sets.c,v $
7 * Date last modified: $Date: 1998/01/27 11:00:21 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING.
28 *
29 * This is the implementation of the set routines
30 */
31
32 #include "utilities/ascConfig.h"
33 #include "utilities/ascMalloc.h"
34 #include "utilities/ascPanic.h"
35 #include "general/list.h"
36 #include "general/pool.h"
37 #include "general/dstring.h"
38 #include "compiler/compiler.h"
39 #include "compiler/fractions.h"
40 #include "compiler/dimen.h"
41 #include "compiler/functype.h"
42 #include "compiler/types.h"
43 #include "compiler/sets.h"
44 #include "compiler/setio.h"
45 #include "compiler/evaluate.h"
46 #include "compiler/exprs.h"
47
48 #ifndef TRUE
49 #define TRUE 1
50 #endif
51
52 #ifndef lint
53 static CONST char SetFuncsID[] = "$Id: sets.c,v 1.13 1998/01/27 11:00:21 ballan Exp $";
54 #endif
55
56 #define SETUSESPOOL TRUE
57
58 #if (!SETUSESPOOL)
59
60 #define SMALLOC (struct Set *)ascmalloc((unsigned)sizeof(struct Set))
61 #define SFREE(s) ascfree((char *)(s))
62 void sets_init_pool(void) {}
63 void sets_destroy_pool(void) {}
64 void sets_report_pool(void)
65 {
66 FPRINTF(ASCERR,"SetsPool not used at all\n");
67 }
68
69
70 #else
71
72 static pool_store_t g_sets_pool = NULL;
73 /* global for our memory manager */
74 /* aim for 4096 chunks including malloc overhead */
75 #define SETS_LEN 10
76 #if (SIZEOF_VOID_P == 8)
77 #define SETS_WID 100
78 #else
79 #define SETS_WID 200
80 #endif
81 /* retune rpwid if the size of struct name changes */
82 #define SETS_ELT_SIZE (sizeof(struct Set))
83 #define SETS_MORE_ELTS 3
84 /*
85 * Number of slots filled if more elements needed.
86 * So if the pool grows, it grows by SETS_MORE_ELTS*SETS_WID elements at a time.
87 */
88 #define SETS_MORE_BARS 50
89 /* This is the number of pool bar slots to add during expansion.
90 not all the slots will be filled immediately. */
91
92 /* This function is called at compiler startup time and destroy at shutdown. */
93 void sets_init_pool(void) {
94 if (g_sets_pool != NULL ) {
95 Asc_Panic(2, NULL, "ERROR: sets_init_pool called twice.\n");
96 }
97 g_sets_pool = pool_create_store(SETS_LEN, SETS_WID, SETS_ELT_SIZE,
98 SETS_MORE_ELTS, SETS_MORE_BARS);
99 if (g_sets_pool == NULL) {
100 Asc_Panic(2, NULL, "ERROR: sets_init_pool unable to allocate pool.\n");
101 }
102 }
103
104 void sets_destroy_pool(void) {
105 if (g_sets_pool==NULL) return;
106 pool_clear_store(g_sets_pool);
107 pool_destroy_store(g_sets_pool);
108 g_sets_pool = NULL;
109 }
110
111 void sets_report_pool(void)
112 {
113 if (g_sets_pool==NULL) {
114 FPRINTF(ASCERR,"SetsPool is empty\n");
115 }
116 FPRINTF(ASCERR,"SetsPool ");
117 pool_print_store(ASCERR,g_sets_pool,0);
118 }
119
120 #define SMALLOC ((struct Set *)(pool_get_element(g_sets_pool)))
121 /* get a token. Token is the size of the struct struct Set */
122 #define SFREE(p) (pool_free_element(g_sets_pool,((void *)p)))
123 /* return a struct Set */
124
125 #endif
126
127 struct Set *CreateSingleSet(struct Expr *ex)
128 {
129 struct Set *result;
130 result = SMALLOC;
131 result->range = 0;
132 result->next = NULL;
133 result->val.e = ex;
134 result->ref_count = 1;
135 return result;
136 }
137
138 struct Set *CreateRangeSet(struct Expr *lower, struct Expr *upper)
139 {
140 struct Set *result;
141 result = SMALLOC;
142 result->range = 1;
143 result->next =NULL;
144 result->val.r.lower = lower;
145 result->val.r.upper = upper;
146 result->ref_count = 1;
147 return result;
148 }
149
150 void LinkSets(struct Set *cur, struct Set *next)
151 {
152 asc_assert(cur!=NULL);
153 cur->next = next;
154 }
155
156 int SetTypeF(CONST struct Set *s)
157 {
158 asc_assert(s!=NULL);
159 return s->range;
160 }
161
162 struct Set *NextSetF(CONST struct Set *s)
163 {
164 asc_assert(s!=NULL);
165 return s->next;
166 }
167
168 CONST struct Expr *GetSingleExprF(CONST struct Set *s)
169 {
170 asc_assert(s!=NULL);
171 asc_assert(!(s->range));
172 return s->val.e;
173 }
174
175 CONST struct Expr *GetLowerExprF(CONST struct Set *s)
176 {
177 asc_assert(s!=NULL);
178 asc_assert(s->range);
179 return s->val.r.lower;
180 }
181
182 CONST struct Expr *GetUpperExprF(CONST struct Set *s)
183 {
184 asc_assert(s!=NULL);
185 asc_assert(s->range);
186 return s->val.r.upper;
187 }
188
189 struct Set *CopySetNode(CONST struct Set *s)
190 {
191 register struct Set *result;
192 if (s==NULL) {
193 return NULL;
194 }
195 result = SMALLOC;
196 result->ref_count = 1;
197 if (s->range) {
198 result->val.r.lower = CopyExprList(s->val.r.lower);
199 result->val.r.upper = CopyExprList(s->val.r.upper);
200 } else {
201 result->val.e = CopyExprList(s->val.e);
202 }
203 result->range = s->range;
204 result->next = NULL;
205 return result;
206 }
207
208 struct Set *CopySetList(CONST struct Set *s)
209 {
210 register struct Set *result,*p;
211 if (s==NULL) {
212 return NULL;
213 }
214 result = CopySetNode(s); /* must keep the head node to return it */
215 p = result;
216 while (s->next!=NULL) {
217 s = s->next;
218 p->next = CopySetNode(s);
219 p = p->next;
220 }
221 p->next = NULL;
222 return result;
223 }
224
225
226 struct Set *CopySetByReference(struct Set *s)
227 { if (s==NULL) return s;
228 asc_assert(s->ref_count);
229 s->ref_count++;
230 return s;
231 }
232
233 void DestroySetNode(struct Set *s)
234 {
235 asc_assert(s!=NULL);
236 #if SETUSESPOOL
237 AssertMemory(s);
238 #else
239 AssertAllocatedMemory(s,sizeof(struct Set));
240 #endif
241 if (s->range) {
242 DestroyExprList(s->val.r.lower);
243 DestroyExprList(s->val.r.upper);
244 } else {
245 DestroyExprList(s->val.e);
246 }
247 SFREE(s);
248 }
249
250
251 void DestroySetList(struct Set *s)
252 {
253 register struct Set *next;
254 while (s!=NULL) {
255 #if SETUSESPOOL
256 AssertMemory(s);
257 #else
258 AssertAllocatedMemory(s,sizeof(struct Set));
259 #endif
260 next = s->next;
261 DestroySetNode(s);
262 s = next;
263 }
264 }
265
266 void DestroySetHead(struct Set *s)
267 {
268 asc_assert(s!=NULL);
269 #if SETUSESPOOL
270 AssertMemory(s);
271 #else
272 AssertAllocatedMemory(s,sizeof(struct Set));
273 #endif
274 SFREE(s);
275 }
276
277 void DestroySetListByReference(struct Set *s)
278 {
279 register struct Set *next;
280 if (--s->ref_count == 0){
281 while (s!=NULL) {
282 #if SETUSESPOOL
283 AssertMemory(s);
284 #else
285 AssertAllocatedMemory(s,sizeof(struct Set));
286 #endif
287 next = s->next;
288 if (s->range) {
289 DestroyExprList(s->val.r.lower);
290 DestroyExprList(s->val.r.upper);
291 }
292 else DestroyExprList(s->val.e);
293 SFREE(s);
294 s = next;
295 }
296 }
297 }
298
299 void DestroySetNodeByReference(struct Set *s)
300 {
301 asc_assert(s!=NULL);
302 #if SETUSESPOOL
303 AssertMemory(s);
304 #else
305 AssertAllocatedMemory(s,sizeof(struct Set));
306 #endif
307 if (--s->ref_count == 0){
308 if (s->range) {
309 DestroyExprList(s->val.r.lower);
310 DestroyExprList(s->val.r.upper);
311 }
312 else DestroyExprList(s->val.e);
313 SFREE(s);
314 }
315 }
316
317 struct Set *JoinSetLists(struct Set *s1, struct Set *s2)
318 {
319 register struct Set *s;
320 if (s1==NULL) return s2;
321 s = s1;
322 /* find end of set list */
323 while(s->next) s = s->next;
324 /* link to s2 */
325 s->next = s2;
326 return s1;
327 }
328
329 struct Set *ReverseSetList(register struct Set *s)
330 {
331 register struct Set *next,*previous=NULL;
332 if (s==NULL) return s;
333 while (TRUE) { /* loop until broken */
334 next = s->next;
335 s->next = previous;
336 if (next==NULL) return s;
337 previous = s;
338 s = next;
339 }
340 }
341
342 int SetStructuresEqual(CONST struct Set *s1, CONST struct Set *s2)
343 {
344 if (s1==s2) return 1;
345 while ((s1!=NULL)&&(s2!=NULL)){
346 if (SetType(s1)!=SetType(s2)) return 0;
347 if (SetType(s1)){ /* range */
348 if ((!ExprsEqual(GetLowerExpr(s1),GetLowerExpr(s2)))||
349 (!ExprsEqual(GetUpperExpr(s1),GetUpperExpr(s2))))
350 return 0;
351 }
352 else
353 if (!ExprsEqual(GetSingleExpr(s1),GetSingleExpr(s2))) return 0;
354 s1 = NextSet(s1);
355 s2 = NextSet(s2);
356 }
357 return ((s1==NULL)&&(s2==NULL));
358 }
359
360 int CompareSetStructures(CONST struct Set *s1, CONST struct Set *s2)
361 {
362 int ctmp;
363 if (s1==s2) return 0;
364 if (s1 == NULL) {
365 return 1;
366 }
367 if (s2 == NULL) {
368 return -1;
369 }
370 while ((s1!=NULL)&&(s2!=NULL)){
371 if (SetType(s1)!=SetType(s2)) {
372 /* 1 is a range and 1 an expr */
373 if (SetType(s1)) {
374 return 1;
375 } else {
376 return -1;
377 }
378 }
379 /* both of same type */
380 if (SetType(s1)){ /* range */
381 ctmp = CompareExprs(GetLowerExpr(s1),GetLowerExpr(s2));
382 if (ctmp != 0) {
383 return ctmp;
384 }
385 ctmp = CompareExprs(GetUpperExpr(s1),GetUpperExpr(s2));
386 if (ctmp != 0) {
387 return ctmp;
388 }
389 } else { /* exprs */
390 ctmp = CompareExprs(GetSingleExpr(s1),GetSingleExpr(s2));
391 if (ctmp != 0) {
392 return ctmp;
393 }
394 }
395 s1 = NextSet(s1);
396 s2 = NextSet(s2);
397 }
398 if (s1 != NULL) {
399 return 1;
400 }
401 if (s2 != NULL) {
402 return -1;
403 }
404 return 0;
405 }
406
407 unsigned long SetLength(CONST struct Set *set)
408 {
409 register unsigned long l=0;
410 while (set!=NULL){
411 l++;
412 set = NextSet(set);
413 }
414 return l;
415 }
416
417 struct gl_list_t *SetNameList(CONST struct Set *set)
418 {
419 struct gl_list_t *list;
420 list = gl_create(3L);
421 asc_assert(list!=NULL);
422 while (set!=NULL){
423 if (SetType(set)) { /*range*/
424 list = EvaluateNamesNeeded(GetLowerExpr(set),NULL,list);
425 list = EvaluateNamesNeeded(GetUpperExpr(set),NULL,list);
426 } else {
427 list = EvaluateNamesNeeded(GetSingleExpr(set),NULL,list);
428 }
429 set = NextSet(set);
430 }
431 return list;
432 }
433
434 char *CreateStrFromSet(CONST struct Set *set)
435 {
436 char *result;
437 Asc_DString dstring;
438 Asc_DStringInit(&dstring);
439 WriteSet2Str(&dstring,set);
440 result = Asc_DStringResult(&dstring);
441 return result;
442 }
443

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22