source: git/Singular/attrib.cc @ 7f5789

fieker-DuValspielwiese
Last change on this file since 7f5789 was 7f5789, checked in by Karim Abou Zeid <karim23697@…>, 4 years ago
Fix fpaprops for ncgen and add tests
  • Property mode set to 100644
File size: 10.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/*
6* ABSTRACT: attributes to leftv and idhdl
7*/
8
9#include "kernel/mod2.h"
10
11#include "misc/options.h"
12#include "misc/intvec.h"
13
14#include "polys/matpol.h"
15
16#include "kernel/polys.h"
17#include "kernel/ideals.h"
18
19#include "Singular/tok.h"
20#include "Singular/ipid.h"
21#include "Singular/ipshell.h"
22#include "Singular/attrib.h"
23
24STATIC_VAR omBin sattr_bin = omGetSpecBin(sizeof(sattr));
25
26void sattr::Print()
27{
28  omCheckAddrSize(this,sizeof(sattr));
29  ::Print("attr:%s, type %s \n",name,Tok2Cmdname(atyp));
30  if (next!=NULL) next->Print();
31}
32
33attr sattr::Copy()
34{
35  assume (this!=NULL);
36
37  omCheckAddrSize(this,sizeof(sattr));
38  attr n=(attr)omAlloc0Bin(sattr_bin);
39  n->atyp=atyp;
40  if (name!=NULL) n->name=omStrDup(name);
41  n->data=CopyA();
42  if (next!=NULL)
43  {
44    n->next=next->Copy();
45  }
46  return n;
47}
48
49// in subexr.cc:
50//void * sattr::CopyA()
51//{
52//  omCheckAddrSize(this,sizeof(sattr));
53//  return s_internalCopy(atyp,data);
54//}
55
56static void attr_free(attr h, const ring r=currRing)
57{
58  if (h->data!=NULL) /*avoid assume failure */
59  {
60    s_internalDelete(h->atyp,h->data,r);
61    h->data=NULL;
62    omFree(h->name);
63    h->name=NULL;
64  }
65}
66
67attr sattr::set(char * s, void * d, int t)
68{
69  attr h = get(s);
70  attr result=this;
71  if (h!=NULL)
72  {
73    attr_free(h);
74  }
75  else
76  {
77    h = (attr)omAlloc0Bin(sattr_bin);
78    h->next = this;
79    result=h;
80  }
81  h->name = s;
82  h->data = d;
83  h->atyp = t;
84#ifdef TEST
85  //::Print("set attr >>%s<< of type %s\n",h->name, Tok2Cmdname(t));
86#endif
87  return  result;
88}
89
90attr sattr::get(const char * s)
91{
92  attr h = this;
93  while (h!=NULL)
94  {
95    if (0 == strcmp(s,h->name))
96    {
97#ifdef TEST
98      //::Print("get attr >>%s<< of type %s\n",h->name, Tok2Cmdname(h->atyp));
99#endif
100      return h;
101    }
102    h = h->next;
103  }
104  return NULL;
105}
106
107#if 0
108void * atGet(idhdl root,const char * name)
109{
110  attr temp = root->attribute->get(name);
111  if (temp!=NULL)
112    return temp->data;
113  else
114    return NULL;
115}
116
117void * atGet(leftv root,const char * name)
118{
119  attr temp;
120  attr a=*(root->Attribute());
121  temp = a->get(name);
122  if (temp!=NULL)
123    return temp->data;
124  else
125    return NULL;
126}
127#endif
128
129void * atGet(idhdl root,const char * name, int t, void *defaultReturnValue)
130{
131  attr temp = root->attribute->get(name);
132  if ((temp!=NULL) && (temp->atyp==t))
133    return temp->data;
134  else
135    return defaultReturnValue;
136}
137
138void * atGet(leftv root,const char * name, int t)
139{
140  attr *a=(root->Attribute());
141  if (a!=NULL)
142  {
143    attr temp = (*a)->get(name);
144    if ((temp!=NULL) && (temp->atyp==t))
145      return temp->data;
146  }
147  return NULL;
148}
149
150void atSet(idhdl root, char * name,void * data,int typ)
151{
152  if (root!=NULL)
153  {
154    if ((IDTYP(root)!=RING_CMD)
155    && (!RingDependend(IDTYP(root)))&&(RingDependend(typ)))
156      WerrorS("cannot set ring-dependend objects at this type");
157    else
158      root->attribute=root->attribute->set(name,data,typ);
159  }
160}
161
162void atSet(leftv root, char * name,void * data,int typ)
163{
164  if (root!=NULL)
165  {
166    attr *a=root->Attribute();
167    int rt=root->Typ();
168    if (a==NULL)
169      WerrorS("cannot set attributes of this object");
170    else if ((rt!=RING_CMD)
171    && (!RingDependend(rt))&&(RingDependend(typ)))
172      WerrorS("cannot set ring-dependend objects at this type");
173    else
174    {
175      *a=(*a)->set(name,data,typ);
176    }
177  }
178}
179
180void sattr::kill(const ring r)
181{
182  attr_free(this,r);
183  omFreeBin((ADDRESS)this, sattr_bin);
184}
185
186void sattr::killAll(const ring r)
187{
188  attr temp = this,temp1;
189
190  while (temp!=NULL)
191  {
192    temp1 = temp->next;
193    omCheckAddr(temp);
194    temp->kill(r);
195    temp = temp1;
196  }
197}
198
199void at_Kill(idhdl root,const char * name, const ring r)
200{
201  attr temp = root->attribute->get(name);
202  if (temp!=NULL)
203  {
204    attr N = temp->next;
205    attr temp1 = root->attribute;
206    if (temp1==temp)
207    {
208      root->attribute = N;
209    }
210    else
211    {
212      while (temp1->next!=temp) temp1 = temp1->next;
213      temp1->next = N;
214    }
215    temp->kill(r);
216  }
217}
218
219void at_KillAll(idhdl root, const ring r)
220{
221  root->attribute->killAll(r);
222  root->attribute = NULL;
223}
224
225void at_KillAll(leftv root, const ring r)
226{
227  root->attribute->killAll(r);
228  root->attribute = NULL;
229}
230
231BOOLEAN atATTRIB1(leftv res,leftv v)
232{
233  attr *aa=(v->Attribute());
234  if (aa==NULL)
235  {
236    WerrorS("this object cannot have attributes");
237    return TRUE;
238  }
239  attr a=*aa;
240  BOOLEAN haveNoAttribute=TRUE;
241  if (v->e==NULL)
242  {
243    if (hasFlag(v,FLAG_STD))
244    {
245      PrintS("attr:isSB, type int\n");
246      haveNoAttribute=FALSE;
247    }
248    if (hasFlag(v,FLAG_QRING))
249    {
250      PrintS("attr:qringNF, type int\n");
251      haveNoAttribute=FALSE;
252    }
253    if (v->Typ()==RING_CMD)
254    {
255      PrintS("attr:cf_class, type int\n");
256      PrintS("attr:global, type int\n");
257      PrintS("attr:maxExp, type int\n");
258      PrintS("attr:ring_cf, type int\n");
259      #ifdef HAVE_SHIFTBBA
260      PrintS("attr:isLetterplaceRing, type int\n");
261      PrintS("attr:ncgenCount, type int\n");
262      #endif
263
264      haveNoAttribute=FALSE;
265    }
266  }
267  else
268  {
269    leftv at=v->LData();
270    return atATTRIB1(res,at);
271  }
272  if (a!=NULL)                    a->Print();
273  else  if(haveNoAttribute)       PrintS("no attributes\n");
274  return FALSE;
275}
276BOOLEAN atATTRIB2(leftv res,leftv v,leftv b)
277{
278  char *name=(char *)b->Data();
279  int t=v->Typ();
280  leftv at=NULL;
281  if (v->e!=NULL)
282    at=v->LData();
283  if (strcmp(name,"isSB")==0)
284  {
285    res->rtyp=INT_CMD;
286    res->data=(void *)(long)hasFlag(v,FLAG_STD);
287    if (at!=NULL) res->data=(void *)(long)(hasFlag(v,FLAG_STD)||(hasFlag(at,FLAG_STD)));
288  }
289  else if ((strcmp(name,"rank")==0)&&(/*v->Typ()*/t==MODUL_CMD))
290  {
291    res->rtyp=INT_CMD;
292    res->data=(void *)(((ideal)v->Data())->rank);
293  }
294  else if ((strcmp(name,"global")==0)
295  &&(/*v->Typ()*/t==RING_CMD))
296  {
297    res->rtyp=INT_CMD;
298    res->data=(void *)(((ring)v->Data())->OrdSgn==1);
299  }
300  else if ((strcmp(name,"maxExp")==0)
301  &&(/*v->Typ()*/t==RING_CMD))
302  {
303    res->rtyp=INT_CMD;
304    res->data=(void *)(long)(((ring)v->Data())->bitmask);
305  }
306  else if ((strcmp(name,"ring_cf")==0)
307  &&(/*v->Typ()*/t==RING_CMD))
308  {
309    res->rtyp=INT_CMD;
310    res->data=(void *)(long)(rField_is_Ring((ring)v->Data()));
311  }
312  else if ((strcmp(name,"cf_class")==0)
313  &&(/*v->Typ()*/t==RING_CMD))
314  {
315    res->rtyp=INT_CMD;
316    coeffs cf;
317    if (t==RING_CMD) cf=((ring)v->Data())->cf;
318    else             cf=(coeffs)v->Data();
319    res->data=(void *)(long)(cf->type);
320  }
321  else if (strcmp(name,"qringNF")==0)
322  {
323    res->rtyp=INT_CMD;
324    res->data=(void *)(long)hasFlag(v,FLAG_QRING);
325    if (at!=NULL) res->data=(void *)(long)(hasFlag(v,FLAG_QRING)||(hasFlag(at,FLAG_QRING)));
326  }
327#ifdef HAVE_SHIFTBBA
328  else if ((strcmp(name,"isLetterplaceRing")==0)
329  &&(/*v->Typ()*/t==RING_CMD))
330  {
331    res->rtyp=INT_CMD;
332    res->data=(void *)(long)(((ring)v->Data())->isLPring);
333  }
334  else if ((strcmp(name,"ncgenCount")==0)
335  &&(/*v->Typ()*/t==RING_CMD))
336  {
337    res->rtyp=INT_CMD;
338    res->data=(void *)(long)(((ring)v->Data())->LPncGenCount);
339  }
340#endif
341  else
342  {
343    attr *aa=v->Attribute();
344    if (aa==NULL)
345    {
346      WerrorS("this object cannot have attributes");
347      return TRUE;
348    }
349    attr a=*aa;
350    a=a->get(name);
351    if (a!=NULL)
352    {
353      res->rtyp=a->atyp;
354      res->data=a->CopyA();
355    }
356    else
357    {
358      res->rtyp=STRING_CMD;
359      res->data=omStrDup("");
360    }
361  }
362  return FALSE;
363}
364BOOLEAN atATTRIB3(leftv /*res*/,leftv v,leftv b,leftv c)
365{
366  idhdl h=(idhdl)v->data;
367  if (v->e!=NULL)
368  {
369    v=v->LData();
370    if (v==NULL) return TRUE;
371    h=NULL;
372  }
373  else if (v->rtyp!=IDHDL) h=NULL;
374  int t=v->Typ();
375
376  char *name=(char *)b->Data();
377  if (strcmp(name,"isSB")==0)
378  {
379    if (c->Typ()!=INT_CMD)
380    {
381      WerrorS("attribute isSB must be int");
382      return TRUE;
383    }
384    if (((long)c->Data())!=0L)
385    {
386      if (h!=NULL) setFlag(h,FLAG_STD);
387      setFlag(v,FLAG_STD);
388    }
389    else
390    {
391      if (h!=NULL) resetFlag(h,FLAG_STD);
392      resetFlag(v,FLAG_STD);
393    }
394  }
395  else if (strcmp(name,"qringNF")==0)
396  {
397    if (c->Typ()!=INT_CMD)
398    {
399      WerrorS("attribute qringNF must be int");
400      return TRUE;
401    }
402    if (((long)c->Data())!=0L)
403    {
404      if (h!=NULL) setFlag(h,FLAG_QRING);
405      setFlag(v,FLAG_QRING);
406    }
407    else
408    {
409      if (h!=NULL) resetFlag(h,FLAG_QRING);
410      resetFlag(v,FLAG_QRING);
411    }
412  }
413  else if ((strcmp(name,"rank")==0)&&(/*v->Typ()*/t==MODUL_CMD))
414  {
415    if (c->Typ()!=INT_CMD)
416    {
417      WerrorS("attribute `rank` must be int");
418      return TRUE;
419    }
420    ideal I=(ideal)v->Data();
421    int rk=id_RankFreeModule(I,currRing);
422    I->rank=si_max(rk,(int)((long)c->Data()));
423  }
424  else if (((strcmp(name,"global")==0)
425    || (strcmp(name,"cf_class")==0)
426    || (strcmp(name,"ring_cf")==0)
427    || (strcmp(name,"maxExp")==0))
428  &&(/*v->Typ()*/t==RING_CMD))
429  {
430    Werror("can not set attribute `%s`",name);
431    return TRUE;
432  }
433#ifdef HAVE_SHIFTBBA
434  else if ((strcmp(name,"isLetterplaceRing")==0)
435  &&(/*v->Typ()*/t==RING_CMD))
436  {
437    if (c->Typ()==INT_CMD)
438      ((ring)v->Data())->isLPring=(int)(long)c->Data();
439    else
440    {
441      WerrorS("attribute `isLetterplaceRing` must be int");
442      return TRUE;
443    }
444  }
445  else if ((strcmp(name,"ncgenCount")==0)
446  &&(/*v->Typ()*/t==RING_CMD))
447  {
448    if (c->Typ()==INT_CMD)
449      ((ring)v->Data())->LPncGenCount=(int)(long)c->Data();
450    else
451    {
452      WerrorS("attribute `ncgenCount` must be int");
453      return TRUE;
454    }
455  }
456#endif
457  else
458  {
459    int typ=c->Typ();
460    if (h!=NULL) atSet(h,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
461    else         atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
462  }
463  return FALSE;
464}
465
466BOOLEAN atKILLATTR1(leftv /*res*/,leftv a)
467{
468  idhdl h=NULL;
469  if ((a->rtyp==IDHDL)&&(a->e==NULL))
470  {
471    h=(idhdl)a->data;
472    resetFlag((idhdl)a->data,FLAG_STD);
473  }
474  resetFlag(a,FLAG_STD);
475  if (h->attribute!=NULL)
476  {
477    atKillAll(h);
478    a->attribute=NULL;
479  }
480  else atKillAll(a);
481  return FALSE;
482}
483BOOLEAN atKILLATTR2(leftv /*res*/,leftv a,leftv b)
484{
485  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
486  {
487    WerrorS("object must have a name");
488    return TRUE;
489  }
490  char *name=(char *)b->Data();
491  if (strcmp(name,"isSB")==0)
492  {
493    resetFlag(a,FLAG_STD);
494    resetFlag((idhdl)a->data,FLAG_STD);
495  }
496  else if (strcmp(name,"global")==0)
497  {
498    WerrorS("can not set attribut `global`");
499    return TRUE;
500  }
501  else
502  {
503    atKill((idhdl)a->data,name);
504  }
505  return FALSE;
506}
507
Note: See TracBrowser for help on using the repository browser.