source: git/Singular/attrib.cc @ 714312

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