source: git/Singular/attrib.cc @ 1c35568

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