1 |
/* |
2 |
* Statement list procedures |
3 |
* by Tom Epperly |
4 |
* Part of Ascend |
5 |
* Version: $Revision: 1.10 $ |
6 |
* Version control file: $RCSfile: slist.c,v $ |
7 |
* Date last modified: $Date: 1997/07/18 12:34:56 $ |
8 |
* Last modified by: $Author: mthomas $ |
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 |
|
30 |
#include <ascend/utilities/ascConfig.h> |
31 |
#include <ascend/utilities/ascMalloc.h> |
32 |
|
33 |
#include <ascend/general/list.h> |
34 |
|
35 |
#include "functype.h" |
36 |
#include "expr_types.h" |
37 |
#include "stattypes.h" |
38 |
#include "statement.h" |
39 |
#include "statio.h" |
40 |
#include "slist.h" |
41 |
#include <ascend/general/mathmacros.h> |
42 |
|
43 |
#define SLMALLOC ASC_NEW(struct StatementList) |
44 |
|
45 |
#ifndef lint |
46 |
static CONST char StatementListID[] = "$Id: slist.c,v 1.10 1997/07/18 12:34:56 mthomas Exp $"; |
47 |
#endif |
48 |
|
49 |
struct StatementList *CreateStatementList(struct gl_list_t *l) |
50 |
{ |
51 |
struct StatementList *result; |
52 |
result = SLMALLOC; |
53 |
assert(l!=NULL); |
54 |
assert(result!=NULL); |
55 |
result->ref_count = 1; |
56 |
result->l = l; |
57 |
return result; |
58 |
} |
59 |
|
60 |
struct StatementList *EmptyStatementList(void) |
61 |
{ |
62 |
struct gl_list_t *list; |
63 |
list = gl_create(0L); |
64 |
return CreateStatementList(list); |
65 |
} |
66 |
|
67 |
struct gl_list_t *GetListF(CONST struct StatementList *sl, |
68 |
CONST char *fname, int li) |
69 |
{ |
70 |
if ( sl == NULL ) { |
71 |
FPRINTF(ASCERR,"GetListF called with NULL StatementList (%s:%d)\n", |
72 |
fname,li); |
73 |
return NULL; |
74 |
} |
75 |
if ( sl->ref_count == 0 ) { |
76 |
FPRINTF(ASCERR,"GetListF called with bad StatementList (%s:%d)\n", |
77 |
fname,li); |
78 |
} |
79 |
return sl->l; |
80 |
} |
81 |
|
82 |
unsigned long StatementInList(CONST struct StatementList *sl, |
83 |
CONST struct Statement *s) |
84 |
{ |
85 |
if (sl==NULL || s==NULL || sl->l ==NULL) return 0L; |
86 |
return gl_ptr_search(sl->l,s,0); |
87 |
} |
88 |
|
89 |
struct Statement *GetStatement(CONST struct StatementList *sl, |
90 |
unsigned long int j) |
91 |
{ |
92 |
if (sl==NULL || sl->l == NULL || gl_length(sl->l) < j || j == 0L) { |
93 |
return NULL; |
94 |
} |
95 |
return (struct Statement *)gl_fetch(sl->l,j); |
96 |
} |
97 |
|
98 |
struct StatementList *CopyStatementList(struct StatementList *sl) |
99 |
{ |
100 |
assert(sl!=NULL); |
101 |
assert(sl->ref_count); |
102 |
if (sl->ref_count < MAXREFCOUNT) sl->ref_count++; |
103 |
return sl; |
104 |
} |
105 |
|
106 |
struct StatementList *CopyListToModify(struct StatementList *sl) |
107 |
{ |
108 |
register struct StatementList *result; |
109 |
register struct Statement *s; |
110 |
register unsigned long c,len; |
111 |
assert(sl!=NULL); |
112 |
assert(sl->ref_count); |
113 |
result=SLMALLOC; |
114 |
assert(result!=NULL); |
115 |
result->ref_count = 1; |
116 |
result->l = gl_create(len=gl_length(sl->l)); |
117 |
for(c=1;c<=len;c++) { |
118 |
s = CopyToModify((struct Statement *)gl_fetch(sl->l,c)); |
119 |
gl_append_ptr(result->l,(VOIDPTR)s); |
120 |
} |
121 |
return result; |
122 |
} |
123 |
|
124 |
int CompareStatementLists(CONST struct StatementList *sl1, |
125 |
CONST struct StatementList *sl2, |
126 |
unsigned long int *diff) |
127 |
{ |
128 |
unsigned long int c,len1, len2, len; |
129 |
int ctmp = 0; |
130 |
CONST struct Statement *s1; |
131 |
CONST struct Statement *s2; |
132 |
|
133 |
*diff = 0L; |
134 |
if (sl1 == sl2) return 0; |
135 |
len1=StatementListLength(sl1); |
136 |
len2=StatementListLength(sl2); |
137 |
/* do not return early just because len1 != len2. want to check |
138 |
* equivalency up to the length of the shorter. |
139 |
*/ |
140 |
len = MIN(len1,len2); |
141 |
if (len==0) { |
142 |
/* curiously empty lists > lists with something */ |
143 |
if (len1) return -1; |
144 |
if (len2) return 1; |
145 |
return 0; |
146 |
} |
147 |
for (c=1; c <= len; c++) { /* we always enter this loop */ |
148 |
s1 = (CONST struct Statement *)gl_fetch(GetList(sl1),c); |
149 |
s2 = (CONST struct Statement *)gl_fetch(GetList(sl2),c); |
150 |
ctmp = CompareStatements(s1,s2); |
151 |
if (ctmp != 0) { |
152 |
break; |
153 |
} |
154 |
} |
155 |
*diff = c; |
156 |
if (c <= len) { |
157 |
/* finished before end of short list */ |
158 |
return ctmp; |
159 |
} |
160 |
if (len1 == len2) { |
161 |
/* same len. finished both lists */ |
162 |
*diff = 0; |
163 |
return 0; |
164 |
} |
165 |
if (len > len2) { |
166 |
/* identical up to len. list length decides */ |
167 |
return 1; |
168 |
} else { |
169 |
return -1; |
170 |
} |
171 |
} |
172 |
|
173 |
int CompareISLists(CONST struct StatementList *sl1, |
174 |
CONST struct StatementList *sl2, |
175 |
unsigned long int *diff) |
176 |
{ |
177 |
unsigned long int c,len1, len2, len; |
178 |
int ctmp = 0; |
179 |
CONST struct Statement *s1; |
180 |
CONST struct Statement *s2; |
181 |
|
182 |
*diff = 0L; |
183 |
if (sl1 == sl2) return 0; |
184 |
len1=StatementListLength(sl1); |
185 |
len2=StatementListLength(sl2); |
186 |
/* do not return early just because len1 != len2. want to check |
187 |
* equivalency up to the length of the shorter. |
188 |
*/ |
189 |
len = MIN(len1,len2); |
190 |
if (len==0) { |
191 |
/* curiously empty lists > lists with something */ |
192 |
*diff = 1L; /* if 1 not empty, they differ in 1st entry */ |
193 |
if (len1) return -1; |
194 |
if (len2) return 1; |
195 |
*diff = 0L; /* both empty. don't differ. */ |
196 |
return 0; |
197 |
} |
198 |
for (c=1; c <= len; c++) { /* we always enter this loop */ |
199 |
s1 = (CONST struct Statement *)gl_fetch(GetList(sl1),c); |
200 |
s2 = (CONST struct Statement *)gl_fetch(GetList(sl2),c); |
201 |
ctmp = CompareISStatements(s1,s2); |
202 |
if (ctmp != 0) { |
203 |
break; |
204 |
} |
205 |
} |
206 |
*diff = c; |
207 |
if (c <= len) { |
208 |
/* finished before end of short list */ |
209 |
return ctmp; |
210 |
} |
211 |
if (len1 == len2) { |
212 |
/* same len. finished both lists */ |
213 |
*diff = 0; |
214 |
return 0; |
215 |
} |
216 |
if (len > len2) { |
217 |
/* identical up to len. list length decides */ |
218 |
return 1; |
219 |
} else { |
220 |
return -1; |
221 |
} |
222 |
} |
223 |
|
224 |
void AppendStatement(struct StatementList *sl1, struct Statement *s) |
225 |
{ |
226 |
register struct gl_list_t *l1; |
227 |
|
228 |
assert(s!=NULL); |
229 |
if (sl1 ==NULL) { |
230 |
sl1 = EmptyStatementList(); |
231 |
} |
232 |
l1 = GetList(sl1); |
233 |
CopyStatement(s); |
234 |
gl_append_ptr(l1,(VOIDPTR)s); |
235 |
} |
236 |
|
237 |
struct StatementList *AppendStatementLists(CONST struct StatementList *sl1, |
238 |
struct StatementList *sl2) |
239 |
{ |
240 |
register CONST struct gl_list_t *l1; |
241 |
register struct gl_list_t *list,*l2; |
242 |
register struct Statement *stat; |
243 |
register unsigned long c,len; |
244 |
assert(sl1 && sl2); |
245 |
l1 = GetList(sl1); |
246 |
l2 = GetList(sl2); |
247 |
len = gl_length(l1); |
248 |
list = gl_create(len+gl_length(l2)); |
249 |
/* add elements from sl1 */ |
250 |
for(c=1;c<=len;c++){ |
251 |
stat = (struct Statement *)gl_fetch(l1,c); |
252 |
CopyStatement(stat); |
253 |
gl_append_ptr(list,(VOIDPTR)stat); |
254 |
} |
255 |
/* add elements from sl2 */ |
256 |
len = gl_length(l2); |
257 |
for(c=1;c<=len;c++){ |
258 |
stat = (struct Statement *)gl_fetch(l2,c); |
259 |
CopyStatement(stat); |
260 |
gl_append_ptr(list,(VOIDPTR)stat); |
261 |
} |
262 |
DestroyStatementList(sl2); |
263 |
return CreateStatementList(list); |
264 |
} |
265 |
|
266 |
|
267 |
void DestroyStatementList(struct StatementList *sl) |
268 |
{ |
269 |
register unsigned long c,len; |
270 |
if (sl == NULL) return; |
271 |
assert(sl->ref_count!=0); |
272 |
if (sl->ref_count < MAXREFCOUNT) sl->ref_count--; |
273 |
if (sl->ref_count==0) { |
274 |
len = gl_length(sl->l); |
275 |
for(c=1;c<=len;c++) |
276 |
DestroyStatement((struct Statement *)gl_fetch(sl->l,c)); |
277 |
gl_destroy(sl->l); |
278 |
ascfree((char *)sl); |
279 |
} |
280 |
} |