source: git/Singular/attrib.cc @ 117e00e

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