source: git/Singular/attrib.cc @ 0df59c8

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