source: git/Singular/attrib.cc @ f6b8d2e

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