source: git/Singular/lists.cc @ c4bbf1f

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