source: git/Singular/lists.cc @ b1dfaf

spielwiese
Last change on this file since b1dfaf 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
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: handling of the list type
7*/
8// to produce a non-inline version from lists.h
9#define LISTS_CC
10
11#include <kernel/mod2.h>
12#include <Singular/tok.h>
13#include <kernel/febase.h>
14//#include "ipid.h"
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>
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)
64    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
65  omFreeBin((ADDRESS)ul, slists_bin);
66  if (vl->m != NULL)
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 ul, 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(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]=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)
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)(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*/
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)(long)v->Data()-1;
153
154  if((0<=VIndex)&&(VIndex<=ul->nr))
155  {
156    ul=(lists)u->CopyD();
157    int i,j;
158    lists l=(lists) omAllocBin(slists_bin);
159    l->Init(ul->nr);
160
161    for(i=j=0;i<=ul->nr;i++,j++)
162    {
163      if (i!=VIndex)
164      {
165        l->m[j]=ul->m[i];
166        memset(&ul->m[i],0,sizeof(ul->m[i]));
167      }
168      else
169      {
170        j--;
171        ul->m[i].CleanUp();
172      }
173    }
174    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
175    omFreeBin((ADDRESS)ul, slists_bin);
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{
188  if (L==NULL) return FALSE;
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,
204  int typ0, intvec ** weights, int add_row_shift)
205{
206  lists L=(lists)omAlloc0Bin(slists_bin);
207  if (length<=0)
208  {
209    // handle "empty" resolutions
210    L->Init(0);
211  }
212  else
213  {
214    int oldlength=length;
215    while (r[length-1]==NULL) length--;
216    if (reallen<=0) reallen=pVariables;
217    reallen=si_max(reallen,length);
218    L->Init(reallen);
219    int i=0;
220
221    while (i<length)
222    {
223      if (r[i]!=NULL)
224      {
225        if (i==0)
226        {
227          L->m[i].rtyp=typ0;
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          }
236        }
237        else
238        {
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          {
248            r[i]->rank=si_max(rank,(int)idRankFreeModule(r[i]));
249          }
250          idSkipZeroes(r[i]);
251        }
252        L->m[i].data=(void *)r[i];
253        if ((weights!=NULL) && (weights[i]!=NULL))
254        {
255          intvec *w=ivCopy(weights[i]);
256          (*w) += add_row_shift;
257          atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD);
258          weights[i] = NULL;
259        }
260      }
261      #ifdef TEST
262      else
263      {
264        // should not happen:
265        Warn("internal NULL in resolvente");
266        L->m[i].data=(void *)idInit(1,1);
267      }
268      #endif
269      i++;
270    }
271    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
272    if (i==0)
273    {
274      L->m[0].rtyp=typ0;
275      L->m[0].data=(char *)idInit(1,1);
276      i=1;
277    }
278    while (i<reallen)
279    {
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++;
294    }
295    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
296  }
297  return L;
298}
299
300resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
301{
302  resolvente r;
303  intvec ** w=NULL,*tw=NULL;
304
305  *len=L->nr+1;
306  if (*len<=0)
307  {
308    WerrorS("empty list");
309    return NULL;
310  }
311  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
312  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
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);
322        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
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;
333    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD);
334    if (tw!=NULL)
335    {
336      w[i]=ivCopy(tw);
337    }
338    tw = NULL;
339    i++;
340  }
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++)
351    {
352      if (w[j]!=NULL) delete w[j];
353    }
354    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
355  }
356  else
357  {
358    *weights = w;
359  }
360  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
361  return r;
362}
363
364char* lString(lists l, BOOLEAN typed, int dim)
365{
366  if (l->nr == -1)
367  {
368    if (typed) return omStrDup("list()");
369    return omStrDup("");
370  }
371
372  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
373  int i, j, k;
374  char *s;
375  for (i=0, j = 0, k = 0; i<=l->nr; i++)
376  {
377    slist[i] = l->m[i].String(NULL, typed, dim);
378    assume(slist[i] != NULL);
379    omCheckAddr(slist[i]);
380    if (*(slist[i]) != '\0')
381    {
382      j += strlen(slist[i]);
383      k++;
384    }
385  }
386  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
387
388  if (typed)
389    sprintf(s, "list(");
390  else
391    *s = '\0';
392
393  for (i=0; i<=l->nr; i++)
394  {
395    if (*(slist[i]) != '\0')
396    {
397      strcat(s, slist[i]);
398      strcat(s, ",");
399      if (dim == 2) strcat(s, "\n");
400    }
401    omCheckAddr(s);
402    omFree(slist[i]);
403  }
404  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
405  if (typed) strcat(s, ")");
406  omCheckAddr(s);
407  omFreeSize(slist, (l->nr+1) * sizeof(char*));
408  return s;
409}
Note: See TracBrowser for help on using the repository browser.