source: git/Singular/lists.cc @ 509ea41

fieker-DuValspielwiese
Last change on this file since 509ea41 was 9bc0b8, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes; mpr_* ->kernel git-svn-id: file:///usr/local/Singular/svn/trunk@7671 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: lists.cc,v 1.26 2005-01-18 15:42:00 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
22omBin slists_bin = omGetSpecBin(sizeof(slists));
23
24lists 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*/
43BOOLEAN 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*/
79lists 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(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*/
108BOOLEAN 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*/
123BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
124{
125  lists ul=(lists)u->CopyD();
126  res->data=(char *)lInsert0(ul,v,(int)w->Data());
127  if (res->data==NULL)
128  {
129    Werror("cannot insert type `%s` at pos. %d",
130      Tok2Cmdname(v->Typ()),(int)w->Data());
131    return TRUE;
132  }
133  return FALSE;
134}
135
136/*2
137* append v to list u
138*/
139BOOLEAN 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*/
149BOOLEAN lDelete(leftv res, leftv u, leftv v)
150{
151  lists ul=(lists)u->Data();
152  int VIndex=(int)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*/
184BOOLEAN 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
201lists liMakeResolv(resolvente r, int length, int reallen,
202  int typ0, intvec ** weights)
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=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=max(rank,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          atSet((idhdl)&L->m[i],omStrDup("isHomog"),weights[i],INTVEC_CMD);
254          weights[i] = NULL;
255        }
256      }
257      #ifdef TEST
258      else
259      {
260        // should not happen:
261        Warn("internal NULL in resolvente");
262        L->m[i].data=(void *)idInit(1,1);
263      }
264      #endif
265      i++;
266    }
267    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
268    if (i==0)
269    {
270      L->m[0].rtyp=typ0;
271      L->m[0].data=(char *)idInit(1,1);
272      i=1;
273    }
274    while (i<reallen)
275    {
276      L->m[i].rtyp=MODUL_CMD;
277      ideal I=(ideal)L->m[i-1].data;
278      ideal J;
279      int rank=IDELEMS(I);
280      if (idIs0(I))
281      {
282        J=idFreeModule(rank);
283      }
284      else
285      {
286        J=idInit(1,rank);
287      }
288      L->m[i].data=(void *)J;
289      i++;
290    }
291    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
292  }
293  return L;
294}
295
296resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
297{
298  resolvente r;
299  intvec ** w=NULL,*tw=NULL;
300
301  *len=L->nr+1;
302  if (*len<=0)
303  {
304    WerrorS("empty list");
305    return NULL;
306  }
307  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
308  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
309  int i=0;
310  *typ0=MODUL_CMD;
311  while (i<(*len))
312  {
313    if (L->m[i].rtyp != MODUL_CMD)
314    {
315      if (L->m[i].rtyp!=IDEAL_CMD)
316      {
317        Werror("element %d is not of type module",i+1);
318        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
319        return NULL;
320      }
321      *typ0=IDEAL_CMD;
322    }
323    if ((i>0) && (idIs0(r[i-1])))
324    {
325      //*len=i-1;
326      break;
327    }
328    r[i]=(ideal)L->m[i].data;
329    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD);
330    if (tw!=NULL)
331    {
332      w[i]=ivCopy(tw);
333    }
334    tw = NULL;
335    i++;
336  }
337  BOOLEAN hom_complex=TRUE;
338  int j=0;
339  while ((j<i) && hom_complex)
340  {
341    hom_complex = hom_complex && (w[i]!=NULL);
342    j++;
343  }
344  if ((!hom_complex) || (weights==NULL))
345  {
346    for (j=0;j<i;j++)
347    {
348      if (w[j]!=NULL) delete w[j];
349    }
350    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
351  }
352  else
353  {
354    *weights = w;
355  }
356  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
357  return r;
358}
359
360char* lString(lists l, BOOLEAN typed, int dim)
361{
362  if (l->nr == -1)
363  {
364    if (typed) return omStrDup("list()");
365    return omStrDup("");
366  }
367
368  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
369  int i, j, k;
370  char *s;
371  for (i=0, j = 0, k = 0; i<=l->nr; i++)
372  {
373    slist[i] = l->m[i].String(NULL, typed, dim);
374    assume(slist[i] != NULL);
375    omCheckAddr(slist[i]);
376    if (*(slist[i]) != '\0')
377    {
378      j += strlen(slist[i]);
379      k++;
380    }
381  }
382  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
383
384  if (typed)
385    sprintf(s, "list(");
386  else
387    *s = '\0';
388
389  for (i=0; i<=l->nr; i++)
390  {
391    if (*(slist[i]) != '\0')
392    {
393      strcat(s, slist[i]);
394      strcat(s, ",");
395      if (dim == 2) strcat(s, "\n");
396    }
397    omCheckAddr(s);
398    omFree(slist[i]);
399  }
400  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
401  if (typed) strcat(s, ")");
402  omCheckAddr(s);
403  omFreeSize(slist, (l->nr+1) * sizeof(char*));
404  return s;
405}
406
407#endif /* LISTS_CC */
Note: See TracBrowser for help on using the repository browser.