source: git/Singular/lists.cc @ 63be42

spielwiese
Last change on this file since 63be42 was 5812c69, checked in by Hans Schönemann <hannes@…>, 26 years ago
cosmetic changes git-svn-id: file:///usr/local/Singular/svn/trunk@2517 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: lists.cc,v 1.11 1998-09-24 09:59:46 Singular Exp $ */
5/*
6* ABSTRACT: handling of the list type
7*/
8#include "mod2.h"
9#include "tok.h"
10#include "febase.h"
11#include "ipid.h"
12#include "polys.h"
13#include "ideals.h"
14#include "attrib.h"
15#include "ipshell.h"
16#include "intvec.h"
17#include "lists.h"
18
19lists lCopy(lists L)
20{
21  lists N=(lists)Alloc0(sizeof(slists));
22  int n=L->nr;
23  if (L->nr>=0)
24    N->Init(n+1);
25  else
26    N->Init();
27  for(;n>=0;n--)
28  {
29    N->m[n].Copy(&L->m[n]);
30  }
31  //Print("copy list with %d -> %d elems\n",L->nr,N->nr);
32  return N;
33}
34
35/*2
36* concat 2 lists
37*/
38BOOLEAN lAdd(leftv res, leftv u, leftv v)
39{
40  lists l=(lists) Alloc(sizeof(slists));
41  lists ul=(lists)u->CopyD();
42  lists vl=(lists)v->CopyD();
43  l->Init(ul->nr+vl->nr+2);
44  int i;
45
46  for(i=0;i<=ul->nr;i++)
47  {
48    //Print("u[%d]->r[%d]\n",i,i);
49    l->m[i].rtyp=ul->m[i].rtyp;
50    l->m[i].data=ul->m[i].data;
51  }
52  for(i=0;i<=vl->nr;i++)
53  {
54    //Print("v[%d]->r[%d]\n",i,i+ul->nr+1);
55    l->m[i+ul->nr+1].rtyp=vl->m[i].rtyp;
56    l->m[i+ul->nr+1].data=vl->m[i].data;
57  }
58  Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
59  Free((ADDRESS)ul,sizeof(slists));
60  Free((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv));
61  Free((ADDRESS)vl,sizeof(slists));
62  memset(u,0,sizeof(*u));
63  memset(v,0,sizeof(*v));
64  res->data = (char *)l;
65  //res->Print();
66  return FALSE;
67}
68
69/*2
70* insert v into list u, destroys u
71*/
72lists lInsert0(lists ul, leftv v, int pos)
73{
74  if ((pos<0)||(v->rtyp==NONE))
75    return NULL;
76  lists l=(lists) Alloc(sizeof(slists));
77  l->Init(max(ul->nr+2,pos+1));
78  int i,j;
79
80  for(i=j=0;i<=ul->nr;i++,j++)
81  {
82    if(j==pos) j++;
83    l->m[j].Copy(&ul->m[i]);
84  }
85  for(j=ul->nr+1;j<pos;j++)
86    l->m[j].rtyp=DEF_CMD;
87  memset(&(l->m[pos]),0,sizeof(sleftv));
88  l->m[pos].rtyp=v->Typ();
89  l->m[pos].data=v->CopyD();
90  l->m[pos].flag=v->flag;
91  l->m[pos].attribute=v->CopyA();
92  Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
93  Free((ADDRESS)ul,sizeof(slists));
94  return l;
95}
96
97/*2
98* insert v into list u, at the beginning
99*/
100BOOLEAN lInsert(leftv res, leftv u, leftv v)
101{
102  lists ul=(lists)u->CopyD();
103  res->data=(char *)lInsert0(ul,v,0);
104  if (res->data==NULL)
105  {
106    Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ()));
107    return TRUE;
108  }
109  return FALSE;
110}
111
112/*2
113* insert v into list u at pos w
114*/
115BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
116{
117  lists ul=(lists)u->CopyD();
118  res->data=(char *)lInsert0(ul,v,(int)w->Data());
119  if (res->data==NULL)
120  {
121    Werror("cannot insert type `%s` at pos. %d",
122      Tok2Cmdname(v->Typ()),(int)w->Data());
123    return TRUE;
124  }
125  return FALSE;
126}
127
128/*2
129* append v to list u
130*/
131BOOLEAN lAppend(leftv res, leftv u, leftv v)
132{
133  lists ul=(lists)u->CopyD();
134  res->data=(char *)lInsert0(ul,v,ul->nr+1);
135  return (res->data==NULL);
136}
137
138/*2
139* delete v-th element from list u
140*/
141BOOLEAN lDelete(leftv res, leftv u, leftv v)
142{
143  lists ul=(lists)u->Data();
144  int VIndex=(int)v->Data()-1;
145
146  if((0<=VIndex)&&(VIndex<=ul->nr))
147  {
148    int i,j;
149    lists l=(lists) Alloc(sizeof(slists));
150    l->Init(ul->nr);
151
152    ul=(lists)u->CopyD();
153    for(i=j=0;i<=ul->nr;i++,j++)
154    {
155      if (i!=VIndex)
156      {
157        l->m[j].Copy(&(ul->m[i]));
158      }
159      else
160      {
161        j--;
162        ul->m[i].CleanUp();
163      }
164    }
165    Free((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
166    Free((ADDRESS)ul,sizeof(slists));
167    res->data = (char *)l;
168    return FALSE;
169  }
170  Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
171  return TRUE;
172}
173
174/*2
175* check, if a list contains any ring dependend data
176*/
177BOOLEAN lRingDependend(lists L)
178{
179  if (L==NULL) return TRUE;
180  int i=0;
181  while (i<=L->nr)
182  {
183    if ((L->m[i].rtyp!=QRING_CMD)
184    && (BEGIN_RING<L->m[i].rtyp)
185    && (L->m[i].rtyp<END_RING))
186      return TRUE;
187    if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data))
188      return TRUE;
189    i++;
190  }
191  return FALSE;
192}
193
194lists liMakeResolv(resolvente r, int length, int reallen,
195  int typ0, intvec ** weights)
196{
197  lists L=(lists)Alloc0(sizeof(slists));
198  if (length<=0)
199  {
200    // handle "empty" resolutions
201    L->Init(0);
202  }
203  else
204  {
205    int oldlength=length;
206    while (r[length-1]==NULL) length--;
207    if (reallen<=0) reallen=pVariables;
208    reallen=max(reallen,length);
209    L->Init(reallen);
210    int i=0;
211
212    while (i<length)
213    {
214      if (r[i]!=NULL)
215      {
216        if (i==0)
217        {
218          L->m[i].rtyp=typ0;
219          int j=IDELEMS(r[0])-1;
220          while ((j>0) && (r[0]->m[j]==NULL)) j--;
221          j++;
222          if (j!=IDELEMS(r[0]))
223          {
224            pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0]));
225            IDELEMS(r[0])=j;
226          }
227        }
228        else
229        {
230          L->m[i].rtyp=MODUL_CMD;
231          int rank=IDELEMS(r[i-1]);
232          if (idIs0(r[i-1]))
233          {
234            idDelete(&(r[i]));
235            r[i]=idFreeModule(rank);
236          }
237          else
238          {
239            r[i]->rank=max(rank,idRankFreeModule(r[i]));
240          }
241          idSkipZeroes(r[i]);
242        }
243        L->m[i].data=(void *)r[i];
244        if ((weights!=NULL) && (weights[i]!=NULL))
245        {
246          atSet((idhdl)&L->m[i],mstrdup("isHomog"),weights[i],INTVEC_CMD);
247          weights[i] = NULL;
248        }
249      }
250      #ifdef TEST
251      else
252      {
253        // should not happen:
254        Warn("internal NULL in resolvente");
255        L->m[i].data=(void *)idInit(1,1);
256      }
257      #endif
258      i++;
259    }
260    Free((ADDRESS)r,oldlength*sizeof(ideal));
261    if (i==0)
262    {
263      L->m[0].rtyp=typ0;
264      L->m[0].data=(char *)idInit(1,1);
265      i=1;
266    }
267    while (i<reallen)
268    {
269      L->m[i].rtyp=MODUL_CMD;
270      ideal I=(ideal)L->m[i-1].data;
271      ideal J;
272      int rank=IDELEMS(I);
273      if (idIs0(I))
274      {
275        J=idFreeModule(rank);
276      }
277      else
278      {
279        J=idInit(1,rank);
280      }
281      L->m[i].data=(void *)J;
282      i++;
283    }
284    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
285  }
286  return L;
287}
288
289resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
290{
291  resolvente r;
292  intvec ** w=NULL,*tw=NULL;
293
294  *len=L->nr+1;
295  if (*len<=0)
296  {
297    WerrorS("empty list");
298    return NULL;
299  }
300  r=(ideal *)Alloc0((*len)*sizeof(ideal));
301  w=(intvec**)Alloc0((*len)*sizeof(intvec*));
302  int i=0;
303  *typ0=MODUL_CMD;
304  while (i<(*len))
305  {
306    if (L->m[i].rtyp != MODUL_CMD)
307    {
308      if (L->m[i].rtyp!=IDEAL_CMD)
309      {
310        Werror("element %d is not of type module",i+1);
311        Free((ADDRESS)r,(*len)*sizeof(ideal));
312        return NULL;
313      }
314      *typ0=IDEAL_CMD;
315    }
316    if ((i>0) && (idIs0(r[i-1])))
317    {
318      //*len=i-1;
319      break;
320    }
321    r[i]=(ideal)L->m[i].data;
322    tw=(intvec*)atGet((idhdl)&L->m[i],mstrdup("isHomog"));
323    if (tw!=NULL)
324    {
325      w[i]=ivCopy(tw);
326    }
327    tw = NULL;
328    i++;
329  }
330  BOOLEAN hom_complex=TRUE;
331  int j=0;
332  while ((j<i) && hom_complex)
333  {
334    hom_complex = hom_complex && (w[i]!=NULL);
335    j++;
336  }
337  if ((!hom_complex) || (weights==NULL))
338  {
339    for (j=0;j<i;j++)
340    {
341      if (w[j]!=NULL) delete w[j];
342    }
343    Free((ADDRESS)w,(*len)*sizeof(intvec*));
344  }
345  else
346  {
347    *weights = w;
348  }
349  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
350  return r;
351}
352
Note: See TracBrowser for help on using the repository browser.