source: git/Singular/attrib.cc @ 7447d8

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