source: git/Singular/lists.cc @ 457d8d6

spielwiese
Last change on this file since 457d8d6 was dd01bf0, checked in by Olaf Bachmann <obachman@…>, 23 years ago
* fixed memory leaks (longalg, NF, syz) * eliminated OPT_MOREPAIS, CANCELUNIT, REDBEST * prepared for tailRings in local case git-svn-id: file:///usr/local/Singular/svn/trunk@4723 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.23 2000-11-14 16:04:58 obachman 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#ifdef HAVE_NAMESPACES_N
37  N->src_packhdl = L->src_packhdl;
38#endif
39  //Print("copy list with %d -> %d elems\n",L->nr,N->nr);
40  return N;
41}
42
43/*2
44* concat 2 lists
45*/
46BOOLEAN lAdd(leftv res, leftv u, leftv v)
47{
48  lists l=(lists) omAllocBin(slists_bin);
49  lists ul=(lists)u->CopyD();
50  lists vl=(lists)v->CopyD();
51  l->Init(ul->nr+vl->nr+2);
52  int i;
53
54  for(i=0;i<=ul->nr;i++)
55  {
56    //Print("u[%d]->r[%d]\n",i,i);
57    l->m[i].rtyp=ul->m[i].rtyp;
58    l->m[i].data=ul->m[i].data;
59  }
60  for(i=0;i<=vl->nr;i++)
61  {
62    //Print("v[%d]->r[%d]\n",i,i+ul->nr+1);
63    l->m[i+ul->nr+1].rtyp=vl->m[i].rtyp;
64    l->m[i+ul->nr+1].data=vl->m[i].data;
65  }
66  if (ul->m != NULL) // OB: ???
67    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
68  omFreeBin((ADDRESS)ul, slists_bin);
69  if (vl->m != NULL) // OB: ????
70    omFreeSize((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv));
71  omFreeBin((ADDRESS)vl, slists_bin);
72  memset(u,0,sizeof(*u));
73  memset(v,0,sizeof(*v));
74  res->data = (char *)l;
75  //res->Print();
76  return FALSE;
77}
78
79/*2
80* insert v into list u, destroys u
81*/
82lists lInsert0(lists ul, leftv v, int pos)
83{
84  if ((pos<0)||(v->rtyp==NONE))
85    return NULL;
86  lists l=(lists) omAllocBin(slists_bin);
87  l->Init(max(ul->nr+2,pos+1));
88  int i,j;
89
90  for(i=j=0;i<=ul->nr;i++,j++)
91  {
92    if(j==pos) j++;
93    l->m[j].Copy(&ul->m[i]);
94  }
95  for(j=ul->nr+1;j<pos;j++)
96    l->m[j].rtyp=DEF_CMD;
97  // memset(&(l->m[pos]),0,sizeof(sleftv)); - done by Init
98  l->m[pos].rtyp=v->Typ();
99  l->m[pos].data=v->CopyD();
100  l->m[pos].flag=v->flag;
101  l->m[pos].attribute=v->CopyA();
102  if (ul->m != NULL) // OB: ?????
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)w->Data());
130  if (res->data==NULL)
131  {
132    Werror("cannot insert type `%s` at pos. %d",
133      Tok2Cmdname(v->Typ()),(int)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)v->Data()-1;
156
157  if((0<=VIndex)&&(VIndex<=ul->nr))
158  {
159    int i,j;
160    lists l=(lists) omAllocBin(slists_bin);
161    l->Init(ul->nr);
162
163    ul=(lists)u->CopyD();
164    for(i=j=0;i<=ul->nr;i++,j++)
165    {
166      if (i!=VIndex)
167      {
168        l->m[j].Copy(&(ul->m[i]));
169      }
170      else
171      {
172        j--;
173        ul->m[i].CleanUp();
174      }
175    }
176    ul->Clean();
177    res->data = (char *)l;
178    return FALSE;
179  }
180  Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
181  return TRUE;
182}
183
184/*2
185* check, if a list contains any ring dependend data
186*/
187BOOLEAN lRingDependend(lists L)
188{
189  if (L==NULL) return TRUE;
190  int i=0;
191  while (i<=L->nr)
192  {
193    if ((L->m[i].rtyp!=QRING_CMD)
194    && (BEGIN_RING<L->m[i].rtyp)
195    && (L->m[i].rtyp<END_RING))
196      return TRUE;
197    if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data))
198      return TRUE;
199    i++;
200  }
201  return FALSE;
202}
203
204lists liMakeResolv(resolvente r, int length, int reallen,
205  int typ0, intvec ** weights)
206{
207  lists L=(lists)omAlloc0Bin(slists_bin);
208  if (length<=0)
209  {
210    // handle "empty" resolutions
211    L->Init(0);
212  }
213  else
214  {
215    int oldlength=length;
216    while (r[length-1]==NULL) length--;
217    if (reallen<=0) reallen=pVariables;
218    reallen=max(reallen,length);
219    L->Init(reallen);
220    int i=0;
221
222    while (i<length)
223    {
224      if (r[i]!=NULL)
225      {
226        if (i==0)
227        {
228          L->m[i].rtyp=typ0;
229          int j=IDELEMS(r[0])-1;
230          while ((j>0) && (r[0]->m[j]==NULL)) j--;
231          j++;
232          if (j!=IDELEMS(r[0]))
233          {
234            pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0]));
235            IDELEMS(r[0])=j;
236          }
237        }
238        else
239        {
240          L->m[i].rtyp=MODUL_CMD;
241          int rank=IDELEMS(r[i-1]);
242          if (idIs0(r[i-1]))
243          {
244            idDelete(&(r[i]));
245            r[i]=idFreeModule(rank);
246          }
247          else
248          {
249            r[i]->rank=max(rank,idRankFreeModule(r[i]));
250          }
251          idSkipZeroes(r[i]);
252        }
253        L->m[i].data=(void *)r[i];
254        if ((weights!=NULL) && (weights[i]!=NULL))
255        {
256          atSet((idhdl)&L->m[i],omStrDup("isHomog"),weights[i],INTVEC_CMD);
257          weights[i] = NULL;
258        }
259      }
260      #ifdef TEST
261      else
262      {
263        // should not happen:
264        Warn("internal NULL in resolvente");
265        L->m[i].data=(void *)idInit(1,1);
266      }
267      #endif
268      i++;
269    }
270    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
271    if (i==0)
272    {
273      L->m[0].rtyp=typ0;
274      L->m[0].data=(char *)idInit(1,1);
275      i=1;
276    }
277    while (i<reallen)
278    {
279      L->m[i].rtyp=MODUL_CMD;
280      ideal I=(ideal)L->m[i-1].data;
281      ideal J;
282      int rank=IDELEMS(I);
283      if (idIs0(I))
284      {
285        J=idFreeModule(rank);
286      }
287      else
288      {
289        J=idInit(1,rank);
290      }
291      L->m[i].data=(void *)J;
292      i++;
293    }
294    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
295  }
296  return L;
297}
298
299resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
300{
301  resolvente r;
302  intvec ** w=NULL,*tw=NULL;
303
304  *len=L->nr+1;
305  if (*len<=0)
306  {
307    WerrorS("empty list");
308    return NULL;
309  }
310  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
311  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
312  int i=0;
313  *typ0=MODUL_CMD;
314  while (i<(*len))
315  {
316    if (L->m[i].rtyp != MODUL_CMD)
317    {
318      if (L->m[i].rtyp!=IDEAL_CMD)
319      {
320        Werror("element %d is not of type module",i+1);
321        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
322        return NULL;
323      }
324      *typ0=IDEAL_CMD;
325    }
326    if ((i>0) && (idIs0(r[i-1])))
327    {
328      //*len=i-1;
329      break;
330    }
331    r[i]=(ideal)L->m[i].data;
332    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog");
333    if (tw!=NULL)
334    {
335      w[i]=ivCopy(tw);
336    }
337    tw = NULL;
338    i++;
339  }
340  BOOLEAN hom_complex=TRUE;
341  int j=0;
342  while ((j<i) && hom_complex)
343  {
344    hom_complex = hom_complex && (w[i]!=NULL);
345    j++;
346  }
347  if ((!hom_complex) || (weights==NULL))
348  {
349    for (j=0;j<i;j++)
350    {
351      if (w[j]!=NULL) delete w[j];
352    }
353    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
354  }
355  else
356  {
357    *weights = w;
358  }
359  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
360  return r;
361}
362
363char* lString(lists l, BOOLEAN typed, int dim)
364{
365  if (l->nr == -1)
366  {
367    if (typed) return omStrDup("list()");
368    return omStrDup("");
369  }
370
371  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
372  int i, j, k;
373  char *s;
374  for (i=0, j = 0, k = 0; i<=l->nr; i++)
375  {
376    slist[i] = l->m[i].String(NULL, typed, dim);
377    assume(slist[i] != NULL);
378    omCheckAddr(slist[i]);
379    if (*(slist[i]) != '\0')
380    {
381      j += strlen(slist[i]);
382      k++;
383    }
384  }
385  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
386
387  if (typed)
388    sprintf(s, "list(");
389  else
390    *s = '\0';
391
392  for (i=0; i<=l->nr; i++)
393  {
394    if (*(slist[i]) != '\0')
395    {
396      strcat(s, slist[i]);
397      strcat(s, ",");
398      if (dim == 2) strcat(s, "\n");
399    }
400    omCheckAddr(s);
401    omFree(slist[i]);
402  }
403  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
404  if (typed) strcat(s, ")");
405  omCheckAddr(s);
406  omFreeSize(slist, (l->nr+1) * sizeof(char*));
407  return s;
408}
409
410#endif /* LISTS_CC */
Note: See TracBrowser for help on using the repository browser.