source: git/Singular/attrib.cc @ f0f2fea

spielwiese
Last change on this file since f0f2fea was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.8 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 <Singular/mod2.h>
17#include <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(%d)\n",h->atyp);  /* 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  //::Print("set attr >>%s<< of type %d\n",h->name,t);
106  return  result;
107}
108
109attr sattr::get(const char * s)
110{
111  attr h = this;
112  while (h!=NULL)
113  {
114    if (0 == strcmp(s,h->name)) return h;
115    h = h->next;
116  }
117  return NULL;
118}
119
120void * atGet(idhdl root,const char * name)
121{
122  attr temp = root->attribute->get(name);
123  if (temp!=NULL)
124    return temp->data;
125  else
126    return NULL;
127}
128
129void * atGet(leftv root,const char * name)
130{
131  attr temp;
132  if (root->e==NULL)
133    temp = root->attribute->get(name);
134  else
135    temp = (root->LData())->attribute->get(name);
136  if ((temp==NULL) && (root->rtyp==IDHDL))
137  {
138    idhdl h=(idhdl)root->data;
139    temp=h->attribute->get(name);
140  }
141  if (temp!=NULL)
142    return temp->data;
143  else
144    return NULL;
145}
146
147void * atGet(idhdl root,const char * name, int t)
148{
149  attr temp = root->attribute->get(name);
150  if ((temp!=NULL) && (temp->atyp==t))
151    return temp->data;
152  else
153    return NULL;
154}
155
156void * atGet(leftv root,const char * name, int t)
157{
158  attr temp = root->attribute->get(name);
159  if ((temp==NULL) && (root->rtyp==IDHDL))
160  {
161    idhdl h=(idhdl)root->data;
162    temp=h->attribute->get(name);
163  }
164  if ((temp!=NULL) && (temp->atyp==t))
165    return temp->data;
166  else
167    return NULL;
168}
169
170void atSet(idhdl root,const char * name,void * data,int typ)
171{
172  if (root!=NULL)
173  {
174    root->attribute=root->attribute->set(name,data,typ);
175  }
176}
177
178void atSet(leftv root,const char * name,void * data,int typ)
179{
180  if (root!=NULL)
181  {
182    if (root->e!=NULL)
183    {
184      Werror("object must have a name for attrib %s",name);
185    }
186    else
187    {
188      if (root->rtyp==IDHDL)
189      {
190        idhdl h=(idhdl)root->data;
191        h->attribute=h->attribute->set(name,data,typ);
192        root->attribute=h->attribute;
193      }
194      else
195      {
196        root->attribute=root->attribute->set(name,data,typ);
197      }
198    }
199  }
200}
201
202void sattr::kill(const ring r)
203{
204  omFree((ADDRESS)name);
205  name=NULL;
206  attr_free(this,r);
207  omFreeBin((ADDRESS)this, sattr_bin);
208}
209
210void sattr::killAll(const ring r)
211{
212  attr temp = this,temp1;
213
214  while (temp!=NULL)
215  {
216    temp1 = temp->next;
217    temp->kill(r);
218    temp = temp1;
219  }
220}
221
222void at_Kill(idhdl root,const char * name, const ring r)
223{
224  attr temp = root->attribute->get(name);
225  if (temp!=NULL)
226  {
227    attr N = temp->next;
228    attr temp1 = root->attribute;
229    if (temp1==temp)
230    {
231      root->attribute = N;
232    }
233    else
234    {
235      while (temp1->next!=temp) temp1 = temp1->next;
236      temp1->next = N;
237    }
238    temp->kill(r);
239  }
240}
241
242void at_KillAll(idhdl root, const ring r)
243{
244  root->attribute->killAll(r);
245  root->attribute = NULL;
246}
247
248BOOLEAN atATTRIB1(leftv res,leftv a)
249{
250  leftv v=a;
251  int t;
252  if (a->e!=NULL)
253  {
254    v=a->LData();
255    if (v==NULL) return TRUE;
256  }
257  attr at=v->attribute;
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=v->attribute->get(name);
320    if (at!=NULL)
321    {
322      res->rtyp=at->atyp;
323      res->data=at->CopyA();
324    }
325    else
326    {
327      res->rtyp=STRING_CMD;
328      res->data=omStrDup("");
329    }
330  }
331  return FALSE;
332}
333BOOLEAN atATTRIB3(leftv res,leftv a,leftv b,leftv c)
334{
335  idhdl h=(idhdl)a->data;
336  int t;
337  leftv v=a;
338  if (a->e!=NULL)
339  {
340    v=a->LData();
341    if (v==NULL) return TRUE;
342    h=NULL;
343  }
344  attr *at=&(v->attribute);
345  char *name=(char *)b->Data();
346  if (strcmp(name,"isSB")==0)
347  {
348    if (c->Typ()!=INT_CMD)
349    {
350      WerrorS("attribute isSB must be int");
351      return TRUE;
352    }
353    if (((long)c->Data())!=0L)
354    {
355      if (h!=NULL) setFlag(h,FLAG_STD);
356      setFlag(v,FLAG_STD);
357    }
358    else
359    {
360      if (h!=NULL) resetFlag(h,FLAG_STD);
361      resetFlag(v,FLAG_STD);
362    }
363  }
364  else if (strcmp(name,"qringNF")==0)
365  {
366    if (c->Typ()!=INT_CMD)
367    {
368      WerrorS("attribute qringNF must be int");
369      return TRUE;
370    }
371    if (((long)c->Data())!=0L)
372    {
373      if (h!=NULL) setFlag(h,FLAG_QRING);
374      setFlag(v,FLAG_QRING);
375    }
376    else
377    {
378      if (h!=NULL) resetFlag(h,FLAG_QRING);
379      resetFlag(v,FLAG_QRING);
380    }
381  }
382  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
383  {
384    if (c->Typ()!=INT_CMD)
385    {
386      WerrorS("attribute `rank` must be int");
387      return TRUE;
388    }
389    ideal I=(ideal)v->Data();
390    I->rank=si_max((int)I->rank,(int)((long)c->Data()));
391  }
392  else if ((strcmp(name,"global")==0)
393  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
394  {
395    WerrorS("can not set attribute `global`");
396    return TRUE;
397  }
398#ifdef HAVE_SHIFTBBA
399  else if ((strcmp(name,"isLPring")==0)
400  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
401  {
402    if (c->Typ()==INT_CMD)
403      ((ring)v->Data())->isLPring=(int)(long)c->Data();
404    else
405    {
406      WerrorS("attribute `isLPring` must be int");
407      return TRUE;
408    }
409  }
410#endif
411  else
412  {
413    int typ=c->Typ();
414    atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
415    if (h!=NULL) IDATTR(h)=v->attribute;
416  }
417  return FALSE;
418}
419
420BOOLEAN atKILLATTR1(leftv res,leftv a)
421{
422  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
423  {
424    WerrorS("object must have a name");
425    return TRUE;
426  }
427  resetFlag(a,FLAG_STD);
428  resetFlag((idhdl)a->data,FLAG_STD);
429  if (a->attribute!=NULL)
430  {
431    atKillAll((idhdl)a->data);
432    a->attribute=NULL;
433  }
434  return FALSE;
435}
436BOOLEAN atKILLATTR2(leftv res,leftv a,leftv b)
437{
438  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
439  {
440    WerrorS("object must have a name");
441    return TRUE;
442  }
443  char *name=(char *)b->Data();
444  if (strcmp(name,"isSB")==0)
445  {
446    resetFlag(a,FLAG_STD);
447    resetFlag((idhdl)a->data,FLAG_STD);
448  }
449  else if (strcmp(name,"global")==0)
450  {
451    WerrorS("can not set attribut `global`");
452    return TRUE;
453  }
454  else
455  {
456    atKill((idhdl)a->data,name);
457  }
458  return FALSE;
459}
460
Note: See TracBrowser for help on using the repository browser.