1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: lists.cc,v 1.15 1999-04-19 11:02:40 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: handling of the list type |
---|
7 | */ |
---|
8 | #include "mod2.h" |
---|
9 | #include "tok.h" |
---|
10 | #include "febase.h" |
---|
11 | #include "ipid.h" |
---|
12 | #include "polys.h" |
---|
13 | #include "ideals.h" |
---|
14 | #include "attrib.h" |
---|
15 | #include "ipshell.h" |
---|
16 | #include "intvec.h" |
---|
17 | #include "lists.h" |
---|
18 | |
---|
19 | lists lCopy(lists L) |
---|
20 | { |
---|
21 | lists N=(lists)Alloc0(sizeof(slists)); |
---|
22 | int n=L->nr; |
---|
23 | if (L->nr>=0) |
---|
24 | N->Init(n+1); |
---|
25 | else |
---|
26 | N->Init(); |
---|
27 | for(;n>=0;n--) |
---|
28 | { |
---|
29 | N->m[n].Copy(&L->m[n]); |
---|
30 | } |
---|
31 | #ifdef HAVE_NAMESPACES_N |
---|
32 | N->src_packhdl = L->src_packhdl; |
---|
33 | #endif |
---|
34 | //Print("copy list with %d -> %d elems\n",L->nr,N->nr); |
---|
35 | return N; |
---|
36 | } |
---|
37 | |
---|
38 | /*2 |
---|
39 | * concat 2 lists |
---|
40 | */ |
---|
41 | BOOLEAN lAdd(leftv res, leftv u, leftv v) |
---|
42 | { |
---|
43 | lists l=(lists) Alloc(sizeof(slists)); |
---|
44 | lists ul=(lists)u->CopyD(); |
---|
45 | lists vl=(lists)v->CopyD(); |
---|
46 | l->Init(ul->nr+vl->nr+2); |
---|
47 | int i; |
---|
48 | |
---|
49 | for(i=0;i<=ul->nr;i++) |
---|
50 | { |
---|
51 | //Print("u[%d]->r[%d]\n",i,i); |
---|
52 | l->m[i].rtyp=ul->m[i].rtyp; |
---|
53 | l->m[i].data=ul->m[i].data; |
---|
54 | } |
---|
55 | for(i=0;i<=vl->nr;i++) |
---|
56 | { |
---|
57 | //Print("v[%d]->r[%d]\n",i,i+ul->nr+1); |
---|
58 | l->m[i+ul->nr+1].rtyp=vl->m[i].rtyp; |
---|
59 | l->m[i+ul->nr+1].data=vl->m[i].data; |
---|
60 | } |
---|
61 | Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
62 | Free((ADDRESS)ul,sizeof(slists)); |
---|
63 | Free((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv)); |
---|
64 | Free((ADDRESS)vl,sizeof(slists)); |
---|
65 | memset(u,0,sizeof(*u)); |
---|
66 | memset(v,0,sizeof(*v)); |
---|
67 | res->data = (char *)l; |
---|
68 | //res->Print(); |
---|
69 | return FALSE; |
---|
70 | } |
---|
71 | |
---|
72 | /*2 |
---|
73 | * insert v into list u, destroys u |
---|
74 | */ |
---|
75 | lists lInsert0(lists ul, leftv v, int pos) |
---|
76 | { |
---|
77 | if ((pos<0)||(v->rtyp==NONE)) |
---|
78 | return NULL; |
---|
79 | lists l=(lists) Alloc(sizeof(slists)); |
---|
80 | l->Init(max(ul->nr+2,pos+1)); |
---|
81 | int i,j; |
---|
82 | |
---|
83 | for(i=j=0;i<=ul->nr;i++,j++) |
---|
84 | { |
---|
85 | if(j==pos) j++; |
---|
86 | l->m[j].Copy(&ul->m[i]); |
---|
87 | } |
---|
88 | for(j=ul->nr+1;j<pos;j++) |
---|
89 | l->m[j].rtyp=DEF_CMD; |
---|
90 | memset(&(l->m[pos]),0,sizeof(sleftv)); |
---|
91 | l->m[pos].rtyp=v->Typ(); |
---|
92 | l->m[pos].data=v->CopyD(); |
---|
93 | l->m[pos].flag=v->flag; |
---|
94 | l->m[pos].attribute=v->CopyA(); |
---|
95 | Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
96 | Free((ADDRESS)ul,sizeof(slists)); |
---|
97 | return l; |
---|
98 | } |
---|
99 | |
---|
100 | /*2 |
---|
101 | * insert v into list u, at the beginning |
---|
102 | */ |
---|
103 | BOOLEAN lInsert(leftv res, leftv u, leftv v) |
---|
104 | { |
---|
105 | lists ul=(lists)u->CopyD(); |
---|
106 | res->data=(char *)lInsert0(ul,v,0); |
---|
107 | if (res->data==NULL) |
---|
108 | { |
---|
109 | Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ())); |
---|
110 | return TRUE; |
---|
111 | } |
---|
112 | return FALSE; |
---|
113 | } |
---|
114 | |
---|
115 | /*2 |
---|
116 | * insert v into list u at pos w |
---|
117 | */ |
---|
118 | BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w) |
---|
119 | { |
---|
120 | lists ul=(lists)u->CopyD(); |
---|
121 | res->data=(char *)lInsert0(ul,v,(int)w->Data()); |
---|
122 | if (res->data==NULL) |
---|
123 | { |
---|
124 | Werror("cannot insert type `%s` at pos. %d", |
---|
125 | Tok2Cmdname(v->Typ()),(int)w->Data()); |
---|
126 | return TRUE; |
---|
127 | } |
---|
128 | return FALSE; |
---|
129 | } |
---|
130 | |
---|
131 | /*2 |
---|
132 | * append v to list u |
---|
133 | */ |
---|
134 | BOOLEAN lAppend(leftv res, leftv u, leftv v) |
---|
135 | { |
---|
136 | lists ul=(lists)u->CopyD(); |
---|
137 | res->data=(char *)lInsert0(ul,v,ul->nr+1); |
---|
138 | return (res->data==NULL); |
---|
139 | } |
---|
140 | |
---|
141 | /*2 |
---|
142 | * delete v-th element from list u |
---|
143 | */ |
---|
144 | BOOLEAN lDelete(leftv res, leftv u, leftv v) |
---|
145 | { |
---|
146 | lists ul=(lists)u->Data(); |
---|
147 | int VIndex=(int)v->Data()-1; |
---|
148 | |
---|
149 | if((0<=VIndex)&&(VIndex<=ul->nr)) |
---|
150 | { |
---|
151 | int i,j; |
---|
152 | lists l=(lists) Alloc(sizeof(slists)); |
---|
153 | l->Init(ul->nr); |
---|
154 | |
---|
155 | ul=(lists)u->CopyD(); |
---|
156 | for(i=j=0;i<=ul->nr;i++,j++) |
---|
157 | { |
---|
158 | if (i!=VIndex) |
---|
159 | { |
---|
160 | l->m[j].Copy(&(ul->m[i])); |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | j--; |
---|
165 | ul->m[i].CleanUp(); |
---|
166 | } |
---|
167 | } |
---|
168 | Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
169 | Free((ADDRESS)ul,sizeof(slists)); |
---|
170 | res->data = (char *)l; |
---|
171 | return FALSE; |
---|
172 | } |
---|
173 | Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1); |
---|
174 | return TRUE; |
---|
175 | } |
---|
176 | |
---|
177 | /*2 |
---|
178 | * check, if a list contains any ring dependend data |
---|
179 | */ |
---|
180 | BOOLEAN lRingDependend(lists L) |
---|
181 | { |
---|
182 | if (L==NULL) return TRUE; |
---|
183 | int i=0; |
---|
184 | while (i<=L->nr) |
---|
185 | { |
---|
186 | if ((L->m[i].rtyp!=QRING_CMD) |
---|
187 | && (BEGIN_RING<L->m[i].rtyp) |
---|
188 | && (L->m[i].rtyp<END_RING)) |
---|
189 | return TRUE; |
---|
190 | if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data)) |
---|
191 | return TRUE; |
---|
192 | i++; |
---|
193 | } |
---|
194 | return FALSE; |
---|
195 | } |
---|
196 | |
---|
197 | lists liMakeResolv(resolvente r, int length, int reallen, |
---|
198 | int typ0, intvec ** weights) |
---|
199 | { |
---|
200 | lists L=(lists)Alloc0(sizeof(slists)); |
---|
201 | if (length<=0) |
---|
202 | { |
---|
203 | // handle "empty" resolutions |
---|
204 | L->Init(0); |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | int oldlength=length; |
---|
209 | while (r[length-1]==NULL) length--; |
---|
210 | if (reallen<=0) reallen=pVariables; |
---|
211 | reallen=max(reallen,length); |
---|
212 | L->Init(reallen); |
---|
213 | int i=0; |
---|
214 | |
---|
215 | while (i<length) |
---|
216 | { |
---|
217 | if (r[i]!=NULL) |
---|
218 | { |
---|
219 | if (i==0) |
---|
220 | { |
---|
221 | L->m[i].rtyp=typ0; |
---|
222 | int j=IDELEMS(r[0])-1; |
---|
223 | while ((j>0) && (r[0]->m[j]==NULL)) j--; |
---|
224 | j++; |
---|
225 | if (j!=IDELEMS(r[0])) |
---|
226 | { |
---|
227 | pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0])); |
---|
228 | IDELEMS(r[0])=j; |
---|
229 | } |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | L->m[i].rtyp=MODUL_CMD; |
---|
234 | int rank=IDELEMS(r[i-1]); |
---|
235 | if (idIs0(r[i-1])) |
---|
236 | { |
---|
237 | idDelete(&(r[i])); |
---|
238 | r[i]=idFreeModule(rank); |
---|
239 | } |
---|
240 | else |
---|
241 | { |
---|
242 | r[i]->rank=max(rank,idRankFreeModule(r[i])); |
---|
243 | } |
---|
244 | idSkipZeroes(r[i]); |
---|
245 | } |
---|
246 | L->m[i].data=(void *)r[i]; |
---|
247 | if ((weights!=NULL) && (weights[i]!=NULL)) |
---|
248 | { |
---|
249 | atSet((idhdl)&L->m[i],mstrdup("isHomog"),weights[i],INTVEC_CMD); |
---|
250 | weights[i] = NULL; |
---|
251 | } |
---|
252 | } |
---|
253 | #ifdef TEST |
---|
254 | else |
---|
255 | { |
---|
256 | // should not happen: |
---|
257 | Warn("internal NULL in resolvente"); |
---|
258 | L->m[i].data=(void *)idInit(1,1); |
---|
259 | } |
---|
260 | #endif |
---|
261 | i++; |
---|
262 | } |
---|
263 | Free((ADDRESS)r,oldlength*sizeof(ideal)); |
---|
264 | if (i==0) |
---|
265 | { |
---|
266 | L->m[0].rtyp=typ0; |
---|
267 | L->m[0].data=(char *)idInit(1,1); |
---|
268 | i=1; |
---|
269 | } |
---|
270 | while (i<reallen) |
---|
271 | { |
---|
272 | L->m[i].rtyp=MODUL_CMD; |
---|
273 | ideal I=(ideal)L->m[i-1].data; |
---|
274 | ideal J; |
---|
275 | int rank=IDELEMS(I); |
---|
276 | if (idIs0(I)) |
---|
277 | { |
---|
278 | J=idFreeModule(rank); |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | J=idInit(1,rank); |
---|
283 | } |
---|
284 | L->m[i].data=(void *)J; |
---|
285 | i++; |
---|
286 | } |
---|
287 | //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr); |
---|
288 | } |
---|
289 | return L; |
---|
290 | } |
---|
291 | |
---|
292 | resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights) |
---|
293 | { |
---|
294 | resolvente r; |
---|
295 | intvec ** w=NULL,*tw=NULL; |
---|
296 | |
---|
297 | *len=L->nr+1; |
---|
298 | if (*len<=0) |
---|
299 | { |
---|
300 | WerrorS("empty list"); |
---|
301 | return NULL; |
---|
302 | } |
---|
303 | r=(ideal *)Alloc0((*len)*sizeof(ideal)); |
---|
304 | w=(intvec**)Alloc0((*len)*sizeof(intvec*)); |
---|
305 | int i=0; |
---|
306 | *typ0=MODUL_CMD; |
---|
307 | while (i<(*len)) |
---|
308 | { |
---|
309 | if (L->m[i].rtyp != MODUL_CMD) |
---|
310 | { |
---|
311 | if (L->m[i].rtyp!=IDEAL_CMD) |
---|
312 | { |
---|
313 | Werror("element %d is not of type module",i+1); |
---|
314 | Free((ADDRESS)r,(*len)*sizeof(ideal)); |
---|
315 | return NULL; |
---|
316 | } |
---|
317 | *typ0=IDEAL_CMD; |
---|
318 | } |
---|
319 | if ((i>0) && (idIs0(r[i-1]))) |
---|
320 | { |
---|
321 | //*len=i-1; |
---|
322 | break; |
---|
323 | } |
---|
324 | r[i]=(ideal)L->m[i].data; |
---|
325 | tw=(intvec*)atGet((idhdl)&L->m[i],mstrdup("isHomog")); |
---|
326 | if (tw!=NULL) |
---|
327 | { |
---|
328 | w[i]=ivCopy(tw); |
---|
329 | } |
---|
330 | tw = NULL; |
---|
331 | i++; |
---|
332 | } |
---|
333 | BOOLEAN hom_complex=TRUE; |
---|
334 | int j=0; |
---|
335 | while ((j<i) && hom_complex) |
---|
336 | { |
---|
337 | hom_complex = hom_complex && (w[i]!=NULL); |
---|
338 | j++; |
---|
339 | } |
---|
340 | if ((!hom_complex) || (weights==NULL)) |
---|
341 | { |
---|
342 | for (j=0;j<i;j++) |
---|
343 | { |
---|
344 | if (w[j]!=NULL) delete w[j]; |
---|
345 | } |
---|
346 | Free((ADDRESS)w,(*len)*sizeof(intvec*)); |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | *weights = w; |
---|
351 | } |
---|
352 | //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr); |
---|
353 | return r; |
---|
354 | } |
---|
355 | |
---|
356 | char* lString(lists l, BOOLEAN typed, int dim) |
---|
357 | { |
---|
358 | if (l->nr == -1) |
---|
359 | { |
---|
360 | if (typed) return mstrdup("list()"); |
---|
361 | return mstrdup(""); |
---|
362 | } |
---|
363 | |
---|
364 | char** slist = (char**) Alloc((l->nr+1) * sizeof(char*)); |
---|
365 | int i, j, k; |
---|
366 | char *s; |
---|
367 | for (i=0, j = 0, k = 0; i<=l->nr; i++) |
---|
368 | { |
---|
369 | slist[i] = l->m[i].String(NULL, typed, dim); |
---|
370 | assume(slist[i] != NULL); |
---|
371 | mmTestL(slist[i]); |
---|
372 | if (*(slist[i]) != '\0') |
---|
373 | { |
---|
374 | j += strlen(slist[i]); |
---|
375 | k++; |
---|
376 | } |
---|
377 | } |
---|
378 | s = (char*) AllocL(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0)); |
---|
379 | |
---|
380 | if (typed) |
---|
381 | sprintf(s, "list("); |
---|
382 | else |
---|
383 | *s = '\0'; |
---|
384 | |
---|
385 | for (i=0; i<=l->nr; i++) |
---|
386 | { |
---|
387 | if (*(slist[i]) != '\0') |
---|
388 | { |
---|
389 | strcat(s, slist[i]); |
---|
390 | strcat(s, ","); |
---|
391 | if (dim == 2) strcat(s, "\n"); |
---|
392 | } |
---|
393 | mmTestL(s); |
---|
394 | FreeL(slist[i]); |
---|
395 | } |
---|
396 | if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0'; |
---|
397 | if (typed) strcat(s, ")"); |
---|
398 | mmTestL(s); |
---|
399 | Free(slist, (l->nr+1) * sizeof(char*)); |
---|
400 | return s; |
---|
401 | } |
---|