source: git/Singular/lists.cc @ 60dcbbc

spielwiese
Last change on this file since 60dcbbc was 06ab6e5, checked in by Hans Schoenemann <hannes@…>, 13 years ago
remove sleftv::CopyA git-svn-id: file:///usr/local/Singular/svn/trunk@13929 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  if (v->attribute!=NULL)
99  {
100    l->m[pos].attribute=v->attribute->Copy();
101  }
102  if (ul->m != NULL)
103    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
104  omFreeBin((ADDRESS)ul, slists_bin);
105  return l;
106}
107
108/*2
109* insert v into list u, at the beginning
110*/
111BOOLEAN lInsert(leftv res, leftv u, leftv v)
112{
113  lists ul=(lists)u->CopyD();
114  res->data=(char *)lInsert0(ul,v,0);
115  if (res->data==NULL)
116  {
117    Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ()));
118    return TRUE;
119  }
120  return FALSE;
121}
122
123/*2
124* insert v into list u at pos w
125*/
126BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
127{
128  lists ul=(lists)u->CopyD();
129  res->data=(char *)lInsert0(ul,v,(int)(long)w->Data());
130  if (res->data==NULL)
131  {
132    Werror("cannot insert type `%s` at pos. %d",
133      Tok2Cmdname(v->Typ()),(int)(long)w->Data());
134    return TRUE;
135  }
136  return FALSE;
137}
138
139/*2
140* append v to list u
141*/
142BOOLEAN lAppend(leftv res, leftv u, leftv v)
143{
144  lists ul=(lists)u->CopyD();
145  res->data=(char *)lInsert0(ul,v,ul->nr+1);
146  return (res->data==NULL);
147}
148
149/*2
150* delete v-th element from list u
151*/
152BOOLEAN lDelete(leftv res, leftv u, leftv v)
153{
154  lists ul=(lists)u->Data();
155  int VIndex=(int)(long)v->Data()-1;
156
157  if((0<=VIndex)&&(VIndex<=ul->nr))
158  {
159    ul=(lists)u->CopyD();
160    int i,j;
161    lists l=(lists) omAllocBin(slists_bin);
162    l->Init(ul->nr);
163
164    for(i=j=0;i<=ul->nr;i++,j++)
165    {
166      if (i!=VIndex)
167      {
168        l->m[j]=ul->m[i];
169        memset(&ul->m[i],0,sizeof(ul->m[i]));
170      }
171      else
172      {
173        j--;
174        ul->m[i].CleanUp();
175      }
176    }
177    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
178    omFreeBin((ADDRESS)ul, slists_bin);
179    res->data = (char *)l;
180    return FALSE;
181  }
182  Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
183  return TRUE;
184}
185
186/*2
187* check, if a list contains any ring dependend data
188*/
189BOOLEAN lRingDependend(lists L)
190{
191  if (L==NULL) return FALSE;
192  int i=0;
193  while (i<=L->nr)
194  {
195    if ((L->m[i].rtyp!=QRING_CMD)
196    && (BEGIN_RING<L->m[i].rtyp)
197    && (L->m[i].rtyp<END_RING))
198      return TRUE;
199    if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data))
200      return TRUE;
201    i++;
202  }
203  return FALSE;
204}
205
206lists liMakeResolv(resolvente r, int length, int reallen,
207  int typ0, intvec ** weights, int add_row_shift)
208{
209  lists L=(lists)omAlloc0Bin(slists_bin);
210  if (length<=0)
211  {
212    // handle "empty" resolutions
213    L->Init(0);
214  }
215  else
216  {
217    int oldlength=length;
218    while (r[length-1]==NULL) length--;
219    if (reallen<=0) reallen=pVariables;
220    reallen=si_max(reallen,length);
221    L->Init(reallen);
222    int i=0;
223
224    while (i<length)
225    {
226      if (r[i]!=NULL)
227      {
228        if (i==0)
229        {
230          L->m[i].rtyp=typ0;
231          int j=IDELEMS(r[0])-1;
232          while ((j>0) && (r[0]->m[j]==NULL)) j--;
233          j++;
234          if (j!=IDELEMS(r[0]))
235          {
236            pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0]));
237            IDELEMS(r[0])=j;
238          }
239        }
240        else
241        {
242          L->m[i].rtyp=MODUL_CMD;
243          int rank=IDELEMS(r[i-1]);
244          if (idIs0(r[i-1]))
245          {
246            idDelete(&(r[i]));
247            r[i]=idFreeModule(rank);
248          }
249          else
250          {
251            r[i]->rank=si_max(rank,(int)idRankFreeModule(r[i]));
252          }
253          idSkipZeroes(r[i]);
254        }
255        L->m[i].data=(void *)r[i];
256        if ((weights!=NULL) && (weights[i]!=NULL))
257        {
258          intvec *w=ivCopy(weights[i]);
259          (*w) += add_row_shift;
260          atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD);
261          weights[i] = NULL;
262        }
263      }
264      #ifdef TEST
265      else
266      {
267        // should not happen:
268        Warn("internal NULL in resolvente");
269        L->m[i].data=(void *)idInit(1,1);
270      }
271      #endif
272      i++;
273    }
274    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
275    if (i==0)
276    {
277      L->m[0].rtyp=typ0;
278      L->m[0].data=(char *)idInit(1,1);
279      i=1;
280    }
281    while (i<reallen)
282    {
283      L->m[i].rtyp=MODUL_CMD;
284      ideal I=(ideal)L->m[i-1].data;
285      ideal J;
286      int rank=IDELEMS(I);
287      if (idIs0(I))
288      {
289        J=idFreeModule(rank);
290      }
291      else
292      {
293        J=idInit(1,rank);
294      }
295      L->m[i].data=(void *)J;
296      i++;
297    }
298    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
299  }
300  return L;
301}
302
303resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
304{
305  resolvente r;
306  intvec ** w=NULL,*tw=NULL;
307
308  *len=L->nr+1;
309  if (*len<=0)
310  {
311    WerrorS("empty list");
312    return NULL;
313  }
314  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
315  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
316  int i=0;
317  *typ0=MODUL_CMD;
318  while (i<(*len))
319  {
320    if (L->m[i].rtyp != MODUL_CMD)
321    {
322      if (L->m[i].rtyp!=IDEAL_CMD)
323      {
324        Werror("element %d is not of type module",i+1);
325        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
326        return NULL;
327      }
328      *typ0=IDEAL_CMD;
329    }
330    if ((i>0) && (idIs0(r[i-1])))
331    {
332      //*len=i-1;
333      break;
334    }
335    r[i]=(ideal)L->m[i].data;
336    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD);
337    if (tw!=NULL)
338    {
339      w[i]=ivCopy(tw);
340    }
341    tw = NULL;
342    i++;
343  }
344  BOOLEAN hom_complex=TRUE;
345  int j=0;
346  while ((j<i) && hom_complex)
347  {
348    hom_complex = hom_complex && (w[i]!=NULL);
349    j++;
350  }
351  if ((!hom_complex) || (weights==NULL))
352  {
353    for (j=0;j<i;j++)
354    {
355      if (w[j]!=NULL) delete w[j];
356    }
357    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
358  }
359  else
360  {
361    *weights = w;
362  }
363  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
364  return r;
365}
366
367char* lString(lists l, BOOLEAN typed, int dim)
368{
369  if (l->nr == -1)
370  {
371    if (typed) return omStrDup("list()");
372    return omStrDup("");
373  }
374
375  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
376  int i, j, k;
377  char *s;
378  for (i=0, j = 0, k = 0; i<=l->nr; i++)
379  {
380    slist[i] = l->m[i].String(NULL, typed, dim);
381    assume(slist[i] != NULL);
382    omCheckAddr(slist[i]);
383    if (*(slist[i]) != '\0')
384    {
385      j += strlen(slist[i]);
386      k++;
387    }
388  }
389  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
390
391  if (typed)
392    sprintf(s, "list(");
393  else
394    *s = '\0';
395
396  for (i=0; i<=l->nr; i++)
397  {
398    if (*(slist[i]) != '\0')
399    {
400      strcat(s, slist[i]);
401      strcat(s, ",");
402      if (dim == 2) strcat(s, "\n");
403    }
404    omCheckAddr(s);
405    omFree(slist[i]);
406  }
407  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
408  if (typed) strcat(s, ")");
409  omCheckAddr(s);
410  omFreeSize(slist, (l->nr+1) * sizeof(char*));
411  return s;
412}
Note: See TracBrowser for help on using the repository browser.