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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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