source: git/Singular/attrib.cc @ 8c94ba

fieker-DuValspielwiese
Last change on this file since 8c94ba was 57c64ff, checked in by Hans Schoenemann <hannes@…>, 14 years ago
Singularg and Singular shluld not behave differently git-svn-id: file:///usr/local/Singular/svn/trunk@13338 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 9.0 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  if (a->e!=NULL)
261  {
262    v=a->LData();
263    if (v==NULL) return TRUE;
264  }
265  attr at=v->attribute;
266  BOOLEAN haveNoAttribute=TRUE;
267  if (hasFlag(v,FLAG_STD))
268  {
269    PrintS("attr:isSB, type int\n");
270    haveNoAttribute=FALSE;
271  }
272  if (hasFlag(v,FLAG_QRING))
273  {
274    PrintS("attr:qringNF, type int\n");
275    haveNoAttribute=FALSE;
276  }
277  if (((t=v->Typ())==RING_CMD)||(t==QRING_CMD))
278  {
279    PrintS("attr:global, type int\n");
280    haveNoAttribute=FALSE;
281  }
282  if (at!=NULL)                    at->Print();
283  else  if(haveNoAttribute)        PrintS("no attributes\n");
284  return FALSE;
285}
286BOOLEAN atATTRIB2(leftv res,leftv a,leftv b)
287{
288  char *name=(char *)b->Data();
289  int t;
290  leftv v=a;
291  if (a->e!=NULL)
292  {
293    v=a->LData();
294    if (v==NULL) return TRUE;
295  }
296  if (strcmp(name,"isSB")==0)
297  {
298    res->rtyp=INT_CMD;
299    res->data=(void *)(long)hasFlag(v,FLAG_STD);
300  }
301  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
302  {
303    res->rtyp=INT_CMD;
304    res->data=(void *)(((ideal)v->Data())->rank);
305  }
306  else if ((strcmp(name,"global")==0)
307  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
308  {
309    res->rtyp=INT_CMD;
310    res->data=(void *)(((ring)v->Data())->OrdSgn==1);
311  }
312  else if (strcmp(name,"qringNF")==0)
313  {
314    res->rtyp=INT_CMD;
315    res->data=(void *)(long)hasFlag(v,FLAG_QRING);
316  }
317#ifdef HAVE_SHIFTBBA
318  else if ((strcmp(name,"isLPring")==0)
319  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
320  {
321    res->rtyp=INT_CMD;
322    res->data=(void *)(long)(((ring)v->Data())->isLPring);
323  }
324#endif
325  else
326  {
327    attr at=v->attribute->get(name);
328    if (at!=NULL)
329    {
330      res->rtyp=at->atyp;
331      res->data=at->CopyA();
332    }
333    else
334    {
335      res->rtyp=STRING_CMD;
336      res->data=omStrDup("");
337    }
338  }
339  return FALSE;
340}
341BOOLEAN atATTRIB3(leftv res,leftv a,leftv b,leftv c)
342{
343  idhdl h=(idhdl)a->data;
344  int t;
345  leftv v=a;
346  if (a->e!=NULL)
347  {
348    v=a->LData();
349    if (v==NULL) return TRUE;
350    h=NULL;
351  }
352  attr *at=&(v->attribute);
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    atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
423    if (h!=NULL) IDATTR(h)=v->attribute;
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.