source: git/Singular/lists.cc @ 25003c

spielwiese
Last change on this file since 25003c was 25003c, checked in by Thomas Siebert <siebert@…>, 26 years ago
*** empty log message *** git-svn-id: file:///usr/local/Singular/svn/trunk@1532 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: lists.cc,v 1.7 1998-04-29 07:05:29 siebert 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        }
220        else
221        {
222          L->m[i].rtyp=MODUL_CMD;
223          int rank=IDELEMS(r[i-1]);
224          if (idIs0(r[i-1]))
225          {
226            idDelete(&(r[i]));
227            r[i]=idFreeModule(rank);
228          }
229          else
230          {
231            r[i]->rank=max(rank,idRankFreeModule(r[i]));
232          }
233          idSkipZeroes(r[i]);
234        }
235        L->m[i].data=(void *)r[i];
236        if ((weights!=NULL) && (weights[i]!=NULL))
237        {
238          atSet((idhdl)&L->m[i],mstrdup("isHomog"),weights[i],INTVEC_CMD);
239          weights[i] = NULL;
240        }
241      }
242      else
243      {
244        // should not happen:
245        Warn("internal NULL in resolvente");
246        L->m[i].data=(void *)idInit(1,1);
247      }
248      i++;
249    }
250    Free((ADDRESS)r,oldlength*sizeof(ideal));
251    if (i==0)
252    {
253      L->m[0].rtyp=typ0;
254      L->m[0].data=(char *)idInit(1,1);
255      i=1;
256    }
257    while (i<reallen)
258    {
259      L->m[i].rtyp=MODUL_CMD;
260      ideal I=(ideal)L->m[i-1].data;
261      ideal J;
262      int rank=IDELEMS(I);
263      if (idIs0(I))
264      {
265        J=idFreeModule(rank);
266      }
267      else
268      {
269        J=idInit(1,rank);
270      }
271      L->m[i].data=(void *)J;
272      i++;
273    }
274    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
275  }
276  return L;
277}
278
279resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
280{
281  resolvente r;
282  intvec ** w=NULL,*tw=NULL;
283
284  *len=L->nr+1;
285  if (*len<=0)
286  {
287    WerrorS("empty list");
288    return NULL;
289  }
290  r=(ideal *)Alloc0((*len)*sizeof(ideal));
291  w=(intvec**)Alloc0((*len)*sizeof(intvec*));
292  int i=0;
293  *typ0=MODUL_CMD;
294  while (i<(*len))
295  {
296    if (L->m[i].rtyp != MODUL_CMD)
297    {
298      if (L->m[i].rtyp!=IDEAL_CMD)
299      {
300        Werror("element %d is not of type module",i+1);
301        Free((ADDRESS)r,(*len)*sizeof(ideal));
302        return NULL;
303      }
304      *typ0=IDEAL_CMD;
305    }
306    if ((i>0) && (idIs0(r[i-1])))
307    {
308      //*len=i-1;
309      break;
310    }
311    r[i]=(ideal)L->m[i].data;
312    tw=(intvec*)atGet((idhdl)&L->m[i],mstrdup("isHomog"));
313    if (tw!=NULL)
314    {
315      w[i]=ivCopy(tw);
316    }
317    tw = NULL;
318    i++;
319  }
320  BOOLEAN hom_complex=TRUE;
321  int j=0;
322  while ((j<i) && hom_complex)
323  {
324    hom_complex = hom_complex && (w[i]!=NULL);
325    j++;
326  }
327  if ((!hom_complex) || (weights==NULL))
328  {
329    for (j=0;j<i;j++)
330      if (w[j]!=NULL) delete w[j];
331    Free((ADDRESS)w,(*len)*sizeof(intvec*));
332  }
333  else
334  {
335    *weights = w;
336  }
337  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
338  return r;
339}
340
Note: See TracBrowser for help on using the repository browser.