source: git/Singular/attrib.cc @ 60dcbbc

spielwiese
Last change on this file since 60dcbbc was 93fdae, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fix tr. 319 git-svn-id: file:///usr/local/Singular/svn/trunk@13950 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;
337    at=at->get(name);
338    if (at!=NULL)
339    {
340      res->rtyp=at->atyp;
341      res->data=at->CopyA();
342    }
343    else
344    {
345      res->rtyp=STRING_CMD;
346      res->data=omStrDup("");
347    }
348  }
349  return FALSE;
350}
351BOOLEAN atATTRIB3(leftv res,leftv a,leftv b,leftv c)
352{
353  idhdl h=(idhdl)a->data;
354  int t;
355  leftv v=a;
356  if (a->e!=NULL)
357  {
358    v=a->LData();
359    if (v==NULL) return TRUE;
360    h=NULL;
361  }
362  if (a->rtyp!=IDHDL) h=NULL;
363
364  attr *at=&(v->attribute);
365  if (h!=NULL) at=&(IDATTR(h));
366  char *name=(char *)b->Data();
367  if (strcmp(name,"isSB")==0)
368  {
369    if (c->Typ()!=INT_CMD)
370    {
371      WerrorS("attribute isSB must be int");
372      return TRUE;
373    }
374    if (((long)c->Data())!=0L)
375    {
376      if (h!=NULL) setFlag(h,FLAG_STD);
377      setFlag(v,FLAG_STD);
378    }
379    else
380    {
381      if (h!=NULL) resetFlag(h,FLAG_STD);
382      resetFlag(v,FLAG_STD);
383    }
384  }
385  else if (strcmp(name,"qringNF")==0)
386  {
387    if (c->Typ()!=INT_CMD)
388    {
389      WerrorS("attribute qringNF must be int");
390      return TRUE;
391    }
392    if (((long)c->Data())!=0L)
393    {
394      if (h!=NULL) setFlag(h,FLAG_QRING);
395      setFlag(v,FLAG_QRING);
396    }
397    else
398    {
399      if (h!=NULL) resetFlag(h,FLAG_QRING);
400      resetFlag(v,FLAG_QRING);
401    }
402  }
403  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
404  {
405    if (c->Typ()!=INT_CMD)
406    {
407      WerrorS("attribute `rank` must be int");
408      return TRUE;
409    }
410    ideal I=(ideal)v->Data();
411    I->rank=si_max((int)I->rank,(int)((long)c->Data()));
412  }
413  else if ((strcmp(name,"global")==0)
414  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
415  {
416    WerrorS("can not set attribute `global`");
417    return TRUE;
418  }
419#ifdef HAVE_SHIFTBBA
420  else if ((strcmp(name,"isLPring")==0)
421  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
422  {
423    if (c->Typ()==INT_CMD)
424      ((ring)v->Data())->isLPring=(int)(long)c->Data();
425    else
426    {
427      WerrorS("attribute `isLPring` must be int");
428      return TRUE;
429    }
430  }
431#endif
432  else
433  {
434    int typ=c->Typ();
435    if (h!=NULL) atSet(h,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
436    else         atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
437  }
438  return FALSE;
439}
440
441BOOLEAN atKILLATTR1(leftv res,leftv a)
442{
443  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
444  {
445    WerrorS("object must have a name");
446    return TRUE;
447  }
448  resetFlag(a,FLAG_STD);
449  resetFlag((idhdl)a->data,FLAG_STD);
450  if (a->attribute!=NULL)
451  {
452    atKillAll((idhdl)a->data);
453    a->attribute=NULL;
454  }
455  return FALSE;
456}
457BOOLEAN atKILLATTR2(leftv res,leftv a,leftv b)
458{
459  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
460  {
461    WerrorS("object must have a name");
462    return TRUE;
463  }
464  char *name=(char *)b->Data();
465  if (strcmp(name,"isSB")==0)
466  {
467    resetFlag(a,FLAG_STD);
468    resetFlag((idhdl)a->data,FLAG_STD);
469  }
470  else if (strcmp(name,"global")==0)
471  {
472    WerrorS("can not set attribut `global`");
473    return TRUE;
474  }
475  else
476  {
477    atKill((idhdl)a->data,name);
478  }
479  return FALSE;
480}
481
Note: See TracBrowser for help on using the repository browser.