source: git/Singular/lists.cc @ 0a001f

fieker-DuValspielwiese
Last change on this file since 0a001f was b1dfaf, checked in by Frank Seelisch <seelisch@…>, 14 years ago
patch from Kai (checked for problems under Windows OS: no problems) git-svn-id: file:///usr/local/Singular/svn/trunk@13210 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[0e1846]5/*
6* ABSTRACT: handling of the list type
7*/
[ea0601]8// to produce a non-inline version from lists.h
[0f1252]9#define LISTS_CC
10
[b1dfaf]11#include <kernel/mod2.h>
[599326]12#include <Singular/tok.h>
13#include <kernel/febase.h>
[a9a7be]14//#include "ipid.h"
[599326]15#include <kernel/polys.h>
16#include <kernel/ideals.h>
17#include <Singular/attrib.h>
18#include <Singular/ipshell.h>
19#include <kernel/intvec.h>
20#include <Singular/lists.h>
[0e1846]21
[c232af]22omBin slists_bin = omGetSpecBin(sizeof(slists));
23
[0e1846]24lists 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*/
43BOOLEAN 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  }
[795c3f]63  if (ul->m != NULL)
[c232af]64    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
65  omFreeBin((ADDRESS)ul, slists_bin);
[795c3f]66  if (vl->m != NULL)
[c232af]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
[8af95a]77* insert v into list ul, destroys u
[0e1846]78*/
79lists 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++;
[8af95a]90    l->m[j]=ul->m[i];
[0e1846]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();
[795c3f]99  if (ul->m != NULL)
[c232af]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*/
108BOOLEAN 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*/
123BOOLEAN 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*/
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);
[6ae4f5]143  return (res->data==NULL);
[0e1846]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();
[7447d8]152  int VIndex=(int)(long)v->Data()-1;
[0e1846]153
154  if((0<=VIndex)&&(VIndex<=ul->nr))
155  {
[795c3f]156    ul=(lists)u->CopyD();
[0e1846]157    int i,j;
[c232af]158    lists l=(lists) omAllocBin(slists_bin);
[0e1846]159    l->Init(ul->nr);
160
161    for(i=j=0;i<=ul->nr;i++,j++)
162    {
163      if (i!=VIndex)
164      {
[795c3f]165        l->m[j]=ul->m[i];
166        memset(&ul->m[i],0,sizeof(ul->m[i]));
[0e1846]167      }
168      else
169      {
170        j--;
171        ul->m[i].CleanUp();
172      }
173    }
[795c3f]174    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
175    omFreeBin((ADDRESS)ul, slists_bin);
[0e1846]176    res->data = (char *)l;
177    return FALSE;
178  }
179  Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
180  return TRUE;
181}
182
183/*2
184* check, if a list contains any ring dependend data
185*/
186BOOLEAN lRingDependend(lists L)
187{
[9bc0b8]188  if (L==NULL) return FALSE;
[0e1846]189  int i=0;
190  while (i<=L->nr)
191  {
192    if ((L->m[i].rtyp!=QRING_CMD)
193    && (BEGIN_RING<L->m[i].rtyp)
194    && (L->m[i].rtyp<END_RING))
195      return TRUE;
196    if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data))
197      return TRUE;
198    i++;
199  }
200  return FALSE;
201}
202
203lists liMakeResolv(resolvente r, int length, int reallen,
[f43a74]204  int typ0, intvec ** weights, int add_row_shift)
[0e1846]205{
[c232af]206  lists L=(lists)omAlloc0Bin(slists_bin);
[7b5d0a]207  if (length<=0)
208  {
209    // handle "empty" resolutions
210    L->Init(0);
211  }
212  else
[0e1846]213  {
[7b5d0a]214    int oldlength=length;
215    while (r[length-1]==NULL) length--;
216    if (reallen<=0) reallen=pVariables;
[f43a74]217    reallen=si_max(reallen,length);
[7b5d0a]218    L->Init(reallen);
219    int i=0;
[25003c]220
[7b5d0a]221    while (i<length)
[0e1846]222    {
[7b5d0a]223      if (r[i]!=NULL)
[0e1846]224      {
[7b5d0a]225        if (i==0)
[0e1846]226        {
[7b5d0a]227          L->m[i].rtyp=typ0;
[5812c69]228          int j=IDELEMS(r[0])-1;
229          while ((j>0) && (r[0]->m[j]==NULL)) j--;
230          j++;
231          if (j!=IDELEMS(r[0]))
232          {
233            pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0]));
234            IDELEMS(r[0])=j;
235          }
[0e1846]236        }
237        else
238        {
[7b5d0a]239          L->m[i].rtyp=MODUL_CMD;
240          int rank=IDELEMS(r[i-1]);
241          if (idIs0(r[i-1]))
242          {
243            idDelete(&(r[i]));
244            r[i]=idFreeModule(rank);
245          }
246          else
247          {
[90fd58]248            r[i]->rank=si_max(rank,(int)idRankFreeModule(r[i]));
[7b5d0a]249          }
[b807b0b]250          idSkipZeroes(r[i]);
[7b5d0a]251        }
252        L->m[i].data=(void *)r[i];
253        if ((weights!=NULL) && (weights[i]!=NULL))
254        {
[f43a74]255          intvec *w=ivCopy(weights[i]);
[b794bd3]256          (*w) += add_row_shift;
[f43a74]257          atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD);
[7b5d0a]258          weights[i] = NULL;
[0e1846]259        }
260      }
[326a59]261      #ifdef TEST
[7b5d0a]262      else
[0e1846]263      {
[7b5d0a]264        // should not happen:
265        Warn("internal NULL in resolvente");
266        L->m[i].data=(void *)idInit(1,1);
[0e1846]267      }
[326a59]268      #endif
[7b5d0a]269      i++;
[0e1846]270    }
[c232af]271    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
[7b5d0a]272    if (i==0)
[0e1846]273    {
[7b5d0a]274      L->m[0].rtyp=typ0;
275      L->m[0].data=(char *)idInit(1,1);
276      i=1;
[0e1846]277    }
[7b5d0a]278    while (i<reallen)
[0e1846]279    {
[7b5d0a]280      L->m[i].rtyp=MODUL_CMD;
281      ideal I=(ideal)L->m[i-1].data;
282      ideal J;
283      int rank=IDELEMS(I);
284      if (idIs0(I))
285      {
286        J=idFreeModule(rank);
287      }
288      else
289      {
290        J=idInit(1,rank);
291      }
292      L->m[i].data=(void *)J;
293      i++;
[0e1846]294    }
[7b5d0a]295    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
[0e1846]296  }
297  return L;
298}
299
[25003c]300resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
[0e1846]301{
302  resolvente r;
[25003c]303  intvec ** w=NULL,*tw=NULL;
[0e1846]304
305  *len=L->nr+1;
306  if (*len<=0)
307  {
308    WerrorS("empty list");
309    return NULL;
310  }
[c232af]311  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
312  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
[0e1846]313  int i=0;
314  *typ0=MODUL_CMD;
315  while (i<(*len))
316  {
317    if (L->m[i].rtyp != MODUL_CMD)
318    {
319      if (L->m[i].rtyp!=IDEAL_CMD)
320      {
321        Werror("element %d is not of type module",i+1);
[c232af]322        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
[0e1846]323        return NULL;
324      }
325      *typ0=IDEAL_CMD;
326    }
327    if ((i>0) && (idIs0(r[i-1])))
328    {
329      //*len=i-1;
330      break;
331    }
332    r[i]=(ideal)L->m[i].data;
[74162eb]333    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD);
[25003c]334    if (tw!=NULL)
335    {
336      w[i]=ivCopy(tw);
337    }
338    tw = NULL;
[0e1846]339    i++;
340  }
[25003c]341  BOOLEAN hom_complex=TRUE;
342  int j=0;
343  while ((j<i) && hom_complex)
344  {
345    hom_complex = hom_complex && (w[i]!=NULL);
346    j++;
347  }
348  if ((!hom_complex) || (weights==NULL))
349  {
350    for (j=0;j<i;j++)
[c5f17b]351    {
[25003c]352      if (w[j]!=NULL) delete w[j];
[c5f17b]353    }
[c232af]354    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
[25003c]355  }
356  else
357  {
358    *weights = w;
359  }
[0e1846]360  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
361  return r;
362}
363
[a79a128]364char* lString(lists l, BOOLEAN typed, int dim)
[4b2155]365{
[0c1144]366  if (l->nr == -1)
[7604db]367  {
[c232af]368    if (typed) return omStrDup("list()");
369    return omStrDup("");
[7604db]370  }
[0c1144]371
[c232af]372  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
[4b2155]373  int i, j, k;
374  char *s;
375  for (i=0, j = 0, k = 0; i<=l->nr; i++)
376  {
[7604db]377    slist[i] = l->m[i].String(NULL, typed, dim);
[4b2155]378    assume(slist[i] != NULL);
[c232af]379    omCheckAddr(slist[i]);
[4b2155]380    if (*(slist[i]) != '\0')
381    {
382      j += strlen(slist[i]);
383      k++;
384    }
385  }
[c232af]386  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
[7604db]387
388  if (typed)
389    sprintf(s, "list(");
390  else
391    *s = '\0';
392
[4b2155]393  for (i=0; i<=l->nr; i++)
394  {
395    if (*(slist[i]) != '\0')
396    {
397      strcat(s, slist[i]);
398      strcat(s, ",");
[7604db]399      if (dim == 2) strcat(s, "\n");
[4b2155]400    }
[c232af]401    omCheckAddr(s);
402    omFree(slist[i]);
[4b2155]403  }
[7604db]404  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
405  if (typed) strcat(s, ")");
[c232af]406  omCheckAddr(s);
407  omFreeSize(slist, (l->nr+1) * sizeof(char*));
[4b2155]408  return s;
409}
Note: See TracBrowser for help on using the repository browser.