[0e1846] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[7447d8] | 4 | /* $Id: lists.cc,v 1.30 2005-07-27 15:47:57 Singular Exp $ */ |
---|
[0e1846] | 5 | /* |
---|
| 6 | * ABSTRACT: handling of the list type |
---|
| 7 | */ |
---|
[0f1252] | 8 | #ifndef LISTS_CC |
---|
| 9 | #define LISTS_CC |
---|
| 10 | |
---|
[0e1846] | 11 | #include "mod2.h" |
---|
| 12 | #include "tok.h" |
---|
| 13 | #include "febase.h" |
---|
[a9a7be] | 14 | //#include "ipid.h" |
---|
[0e1846] | 15 | #include "polys.h" |
---|
| 16 | #include "ideals.h" |
---|
| 17 | #include "attrib.h" |
---|
[2d0ae3] | 18 | #include "ipshell.h" |
---|
[25003c] | 19 | #include "intvec.h" |
---|
[0e1846] | 20 | #include "lists.h" |
---|
| 21 | |
---|
[c232af] | 22 | omBin slists_bin = omGetSpecBin(sizeof(slists)); |
---|
| 23 | |
---|
[0e1846] | 24 | lists lCopy(lists L) |
---|
| 25 | { |
---|
[c232af] | 26 | lists N=(lists)omAlloc0Bin(slists_bin); |
---|
[0e1846] | 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 | { |
---|
[c232af] | 45 | lists l=(lists) omAllocBin(slists_bin); |
---|
[0e1846] | 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 | } |
---|
[c232af] | 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); |
---|
[0e1846] | 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 | { |
---|
[6ae4f5] | 81 | if ((pos<0)||(v->rtyp==NONE)) |
---|
| 82 | return NULL; |
---|
[c232af] | 83 | lists l=(lists) omAllocBin(slists_bin); |
---|
[f43a74] | 84 | l->Init(si_max(ul->nr+2,pos+1)); |
---|
[0e1846] | 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; |
---|
[0c1144] | 94 | // memset(&(l->m[pos]),0,sizeof(sleftv)); - done by Init |
---|
[0e1846] | 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(); |
---|
[c232af] | 99 | if (ul->m != NULL) // OB: ????? |
---|
| 100 | omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
| 101 | omFreeBin((ADDRESS)ul, slists_bin); |
---|
[0e1846] | 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); |
---|
[2d0ae3] | 112 | if (res->data==NULL) |
---|
| 113 | { |
---|
| 114 | Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ())); |
---|
| 115 | return TRUE; |
---|
| 116 | } |
---|
| 117 | return FALSE; |
---|
[0e1846] | 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(); |
---|
[7447d8] | 126 | res->data=(char *)lInsert0(ul,v,(int)(long)w->Data()); |
---|
[2d0ae3] | 127 | if (res->data==NULL) |
---|
| 128 | { |
---|
| 129 | Werror("cannot insert type `%s` at pos. %d", |
---|
[7447d8] | 130 | Tok2Cmdname(v->Typ()),(int)(long)w->Data()); |
---|
[2d0ae3] | 131 | return TRUE; |
---|
| 132 | } |
---|
| 133 | return FALSE; |
---|
[0e1846] | 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); |
---|
[6ae4f5] | 143 | return (res->data==NULL); |
---|
[0e1846] | 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(); |
---|
[7447d8] | 152 | int VIndex=(int)(long)v->Data()-1; |
---|
[0e1846] | 153 | |
---|
| 154 | if((0<=VIndex)&&(VIndex<=ul->nr)) |
---|
| 155 | { |
---|
| 156 | int i,j; |
---|
[c232af] | 157 | lists l=(lists) omAllocBin(slists_bin); |
---|
[0e1846] | 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 | } |
---|
[8a9e35] | 173 | ul->Clean(); |
---|
[0e1846] | 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 | { |
---|
[9bc0b8] | 186 | if (L==NULL) return FALSE; |
---|
[0e1846] | 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, |
---|
[f43a74] | 202 | int typ0, intvec ** weights, int add_row_shift) |
---|
[0e1846] | 203 | { |
---|
[c232af] | 204 | lists L=(lists)omAlloc0Bin(slists_bin); |
---|
[7b5d0a] | 205 | if (length<=0) |
---|
| 206 | { |
---|
| 207 | // handle "empty" resolutions |
---|
| 208 | L->Init(0); |
---|
| 209 | } |
---|
| 210 | else |
---|
[0e1846] | 211 | { |
---|
[7b5d0a] | 212 | int oldlength=length; |
---|
| 213 | while (r[length-1]==NULL) length--; |
---|
| 214 | if (reallen<=0) reallen=pVariables; |
---|
[f43a74] | 215 | reallen=si_max(reallen,length); |
---|
[7b5d0a] | 216 | L->Init(reallen); |
---|
| 217 | int i=0; |
---|
[25003c] | 218 | |
---|
[7b5d0a] | 219 | while (i<length) |
---|
[0e1846] | 220 | { |
---|
[7b5d0a] | 221 | if (r[i]!=NULL) |
---|
[0e1846] | 222 | { |
---|
[7b5d0a] | 223 | if (i==0) |
---|
[0e1846] | 224 | { |
---|
[7b5d0a] | 225 | L->m[i].rtyp=typ0; |
---|
[5812c69] | 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 | } |
---|
[0e1846] | 234 | } |
---|
| 235 | else |
---|
| 236 | { |
---|
[7b5d0a] | 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 | { |
---|
[90fd58] | 246 | r[i]->rank=si_max(rank,(int)idRankFreeModule(r[i])); |
---|
[7b5d0a] | 247 | } |
---|
[b807b0b] | 248 | idSkipZeroes(r[i]); |
---|
[7b5d0a] | 249 | } |
---|
| 250 | L->m[i].data=(void *)r[i]; |
---|
| 251 | if ((weights!=NULL) && (weights[i]!=NULL)) |
---|
| 252 | { |
---|
[f43a74] | 253 | intvec *w=ivCopy(weights[i]); |
---|
[b794bd3] | 254 | (*w) += add_row_shift; |
---|
[f43a74] | 255 | atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD); |
---|
[7b5d0a] | 256 | weights[i] = NULL; |
---|
[0e1846] | 257 | } |
---|
| 258 | } |
---|
[326a59] | 259 | #ifdef TEST |
---|
[7b5d0a] | 260 | else |
---|
[0e1846] | 261 | { |
---|
[7b5d0a] | 262 | // should not happen: |
---|
| 263 | Warn("internal NULL in resolvente"); |
---|
| 264 | L->m[i].data=(void *)idInit(1,1); |
---|
[0e1846] | 265 | } |
---|
[326a59] | 266 | #endif |
---|
[7b5d0a] | 267 | i++; |
---|
[0e1846] | 268 | } |
---|
[c232af] | 269 | omFreeSize((ADDRESS)r,oldlength*sizeof(ideal)); |
---|
[7b5d0a] | 270 | if (i==0) |
---|
[0e1846] | 271 | { |
---|
[7b5d0a] | 272 | L->m[0].rtyp=typ0; |
---|
| 273 | L->m[0].data=(char *)idInit(1,1); |
---|
| 274 | i=1; |
---|
[0e1846] | 275 | } |
---|
[7b5d0a] | 276 | while (i<reallen) |
---|
[0e1846] | 277 | { |
---|
[7b5d0a] | 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++; |
---|
[0e1846] | 292 | } |
---|
[7b5d0a] | 293 | //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr); |
---|
[0e1846] | 294 | } |
---|
| 295 | return L; |
---|
| 296 | } |
---|
| 297 | |
---|
[25003c] | 298 | resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights) |
---|
[0e1846] | 299 | { |
---|
| 300 | resolvente r; |
---|
[25003c] | 301 | intvec ** w=NULL,*tw=NULL; |
---|
[0e1846] | 302 | |
---|
| 303 | *len=L->nr+1; |
---|
| 304 | if (*len<=0) |
---|
| 305 | { |
---|
| 306 | WerrorS("empty list"); |
---|
| 307 | return NULL; |
---|
| 308 | } |
---|
[c232af] | 309 | r=(ideal *)omAlloc0((*len)*sizeof(ideal)); |
---|
| 310 | w=(intvec**)omAlloc0((*len)*sizeof(intvec*)); |
---|
[0e1846] | 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); |
---|
[c232af] | 320 | omFreeSize((ADDRESS)r,(*len)*sizeof(ideal)); |
---|
[0e1846] | 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; |
---|
[74162eb] | 331 | tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD); |
---|
[25003c] | 332 | if (tw!=NULL) |
---|
| 333 | { |
---|
| 334 | w[i]=ivCopy(tw); |
---|
| 335 | } |
---|
| 336 | tw = NULL; |
---|
[0e1846] | 337 | i++; |
---|
| 338 | } |
---|
[25003c] | 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++) |
---|
[c5f17b] | 349 | { |
---|
[25003c] | 350 | if (w[j]!=NULL) delete w[j]; |
---|
[c5f17b] | 351 | } |
---|
[c232af] | 352 | omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*)); |
---|
[25003c] | 353 | } |
---|
| 354 | else |
---|
| 355 | { |
---|
| 356 | *weights = w; |
---|
| 357 | } |
---|
[0e1846] | 358 | //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr); |
---|
| 359 | return r; |
---|
| 360 | } |
---|
| 361 | |
---|
[a79a128] | 362 | char* lString(lists l, BOOLEAN typed, int dim) |
---|
[4b2155] | 363 | { |
---|
[0c1144] | 364 | if (l->nr == -1) |
---|
[7604db] | 365 | { |
---|
[c232af] | 366 | if (typed) return omStrDup("list()"); |
---|
| 367 | return omStrDup(""); |
---|
[7604db] | 368 | } |
---|
[0c1144] | 369 | |
---|
[c232af] | 370 | char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*)); |
---|
[4b2155] | 371 | int i, j, k; |
---|
| 372 | char *s; |
---|
| 373 | for (i=0, j = 0, k = 0; i<=l->nr; i++) |
---|
| 374 | { |
---|
[7604db] | 375 | slist[i] = l->m[i].String(NULL, typed, dim); |
---|
[4b2155] | 376 | assume(slist[i] != NULL); |
---|
[c232af] | 377 | omCheckAddr(slist[i]); |
---|
[4b2155] | 378 | if (*(slist[i]) != '\0') |
---|
| 379 | { |
---|
| 380 | j += strlen(slist[i]); |
---|
| 381 | k++; |
---|
| 382 | } |
---|
| 383 | } |
---|
[c232af] | 384 | s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0)); |
---|
[7604db] | 385 | |
---|
| 386 | if (typed) |
---|
| 387 | sprintf(s, "list("); |
---|
| 388 | else |
---|
| 389 | *s = '\0'; |
---|
| 390 | |
---|
[4b2155] | 391 | for (i=0; i<=l->nr; i++) |
---|
| 392 | { |
---|
| 393 | if (*(slist[i]) != '\0') |
---|
| 394 | { |
---|
| 395 | strcat(s, slist[i]); |
---|
| 396 | strcat(s, ","); |
---|
[7604db] | 397 | if (dim == 2) strcat(s, "\n"); |
---|
[4b2155] | 398 | } |
---|
[c232af] | 399 | omCheckAddr(s); |
---|
| 400 | omFree(slist[i]); |
---|
[4b2155] | 401 | } |
---|
[7604db] | 402 | if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0'; |
---|
| 403 | if (typed) strcat(s, ")"); |
---|
[c232af] | 404 | omCheckAddr(s); |
---|
| 405 | omFreeSize(slist, (l->nr+1) * sizeof(char*)); |
---|
[4b2155] | 406 | return s; |
---|
| 407 | } |
---|
[0f1252] | 408 | |
---|
| 409 | #endif /* LISTS_CC */ |
---|