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