source: git/Singular/attrib.cc @ 7626c5f

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