source: git/Singular/attrib.cc @ e6d9a32

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