source: git/Singular/attrib.cc @ 71df1a

spielwiese
Last change on this file since 71df1a was 71df1a, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: attribute handling git-svn-id: file:///usr/local/Singular/svn/trunk@9116 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.25 2006-05-08 15:37:15 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;
146  if (e==NULL)
147    temp = root->attribute->get(name);
148  else
149    temp = (root->LData())->attribute->get(name);
150  if ((temp==NULL) && (root->rtyp==IDHDL))
151  {
152    idhdl h=(idhdl)root->data;
153    temp=h->attribute->get(name);
154  }
155  if (temp!=NULL)
156    return temp->data;
157  else
158    return NULL;
159}
160
161void * atGet(idhdl root,char * name, int t)
162{
163  attr temp = root->attribute->get(name);
164  if ((temp!=NULL) && (temp->atyp==t))
165    return temp->data;
166  else
167    return NULL;
168}
169
170void * atGet(leftv root,char * name, int t)
171{
172  attr temp = root->attribute->get(name);
173  if ((temp==NULL) && (root->rtyp==IDHDL))
174  {
175    idhdl h=(idhdl)root->data;
176    temp=h->attribute->get(name);
177  }
178  if ((temp!=NULL) && (temp->atyp==t))
179    return temp->data;
180  else
181    return NULL;
182}
183
184void atSet(idhdl root,char * name,void * data,int typ)
185{
186  if (root!=NULL)
187  {
188    root->attribute=root->attribute->set(name,data,typ);
189  }
190}
191
192void atSet(leftv root,char * name,void * data,int typ)
193{
194  if (root!=NULL)
195  {
196    if (root->e!=NULL)
197    {
198      Werror("object must have a name for attrib %s",name);
199    }
200    else
201    {
202      if (root->rtyp==IDHDL)
203      {
204        idhdl h=(idhdl)root->data;
205        h->attribute=h->attribute->set(name,data,typ);
206        root->attribute=h->attribute;
207      }
208      else
209      {
210        root->attribute=root->attribute->set(name,data,typ);
211      }
212    }
213  }
214}
215
216void sattr::kill()
217{
218  omFree((ADDRESS)name);
219  name=NULL;
220  switch (atyp)
221  {
222  case INTVEC_CMD:
223    delete (intvec *)data;
224    break;
225  case IDEAL_CMD:
226  case MODUL_CMD:
227  case MATRIX_CMD:
228    idDelete((ideal *)&data);
229    break;
230  case POLY_CMD:
231  case VECTOR_CMD:
232    pDelete((poly *)&data);
233    break;
234  case INT_CMD:
235    break;
236  case STRING_CMD:
237    omFree((ADDRESS)data);
238    break;
239#ifdef TEST
240  default:
241    ::Print("atKill: unknown type\n",atyp);  /* DEBUG */
242#endif
243  } /* end switch: (atyp) */
244  data=NULL;
245  omFreeBin((ADDRESS)this, sattr_bin);
246}
247
248void sattr::killAll()
249{
250  attr temp = this,temp1;
251
252  while (temp!=NULL)
253  {
254    temp1 = temp->next;
255    temp->kill();
256    temp = temp1;
257  }
258}
259
260void atKill(idhdl root,char * name)
261{
262  attr temp = root->attribute->get(name);
263  if (temp!=NULL)
264  {
265    attr N = temp->next;
266    attr temp1 = root->attribute;
267    if (temp1==temp)
268    {
269      root->attribute = N;
270    }
271    else
272    {
273      while (temp1->next!=temp) temp1 = temp1->next;
274      temp1->next = N;
275    }
276    temp->kill();
277  }
278}
279
280void atKillAll(idhdl root)
281{
282  root->attribute->killAll();
283  root->attribute = NULL;
284}
285
286BOOLEAN atATTRIB1(leftv res,leftv a)
287{
288  leftv v=a;
289  int t;
290  if (a->e!=NULL)
291  {
292    v=a->LData();
293    if (v==NULL) return TRUE;
294  }
295  attr at=v->attribute;
296  if (hasFlag(v,FLAG_STD))
297  {
298    PrintS("attr:isSB, type int\n");
299    if (at!=NULL) at->Print();
300  }
301  else if (((t=v->Typ())==RING_CMD)||(t==QRING_CMD))
302  {
303    PrintS("attr:global, type int\n");
304  }
305  else
306  {
307    if (at!=NULL) at->Print();
308    else          PrintS("no attributes\n");
309  }
310  return FALSE;
311}
312BOOLEAN atATTRIB2(leftv res,leftv a,leftv b)
313{
314  char *name=(char *)b->Data();
315  int t;
316  leftv v=a;
317  if (a->e!=NULL)
318  {
319    v=a->LData();
320    if (v==NULL) return TRUE;
321  }
322  if (strcmp(name,"isSB")==0)
323  {
324    res->rtyp=INT_CMD;
325    res->data=(void *)hasFlag(v,FLAG_STD);
326  }
327  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
328  {
329    res->rtyp=INT_CMD;
330    res->data=(void *)(((ideal)v->Data())->rank);
331  }
332  else if ((strcmp(name,"global")==0)
333  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
334  {
335    res->rtyp=INT_CMD;
336    res->data=(void *)(((ring)v->Data())->OrdSgn==1);
337  }
338  else
339  {
340    attr at=v->attribute->get(name);
341    if (at!=NULL)
342    {
343      res->rtyp=at->atyp;
344      res->data=at->CopyA();
345    }
346    else
347    {
348      res->rtyp=STRING_CMD;
349      res->data=omStrDup("");
350    }
351  }
352  return FALSE;
353}
354BOOLEAN atATTRIB3(leftv res,leftv a,leftv b,leftv c)
355{
356  idhdl h=(idhdl)a->data;
357  int t;
358  leftv v=a;
359  if (a->e!=NULL)
360  {
361    v=a->LData();
362    if (v==NULL) return TRUE;
363    h=NULL;
364  }
365  attr *at=&(v->attribute);
366  char *name=(char *)b->Data();
367  if (strcmp(name,"isSB")==0)
368  {
369    if (c->Typ()!=INT_CMD)
370    {
371      WerrorS("attrib isSB must be int");
372      return TRUE;
373    }
374    if (((long)c->Data())!=0L)
375    {
376      if (h!=NULL) setFlag(h,FLAG_STD);
377      setFlag(v,FLAG_STD);
378    }
379    else
380    {
381      if (h!=NULL) resetFlag(h,FLAG_STD);
382      resetFlag(v,FLAG_STD);
383    }
384  }
385  else if ((strcmp(name,"rank")==0)&&(v->Typ()==MODUL_CMD))
386  {
387    if (c->Typ()!=INT_CMD)
388    {
389      WerrorS("attrib `rank` must be int");
390      return TRUE;
391    }
392    ideal I=(ideal)v->Data();
393    I->rank=si_max((int)I->rank,(int)((long)c->Data()));
394  }
395  else if ((strcmp(name,"global")==0)
396  &&(((t=v->Typ())==RING_CMD)||(t==QRING_CMD)))
397  {
398    WerrorS("can not set attribut `global`");
399    return TRUE;
400  }
401  else
402  {
403    int typ=c->Typ();
404    atSet(v,omStrDup(name),c->CopyD(typ),typ/*c->T(yp()*/);
405    if (h!=NULL) IDATTR(h)=v->attribute;
406  }
407  return FALSE;
408}
409
410BOOLEAN atKILLATTR1(leftv res,leftv a)
411{
412  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
413  {
414    WerrorS("object must have a name");
415    return TRUE;
416  }
417  resetFlag(a,FLAG_STD);
418  resetFlag((idhdl)a->data,FLAG_STD);
419  if (a->attribute!=NULL)
420  {
421    atKillAll((idhdl)a->data);
422    a->attribute=NULL;
423  }
424  return FALSE;
425}
426BOOLEAN atKILLATTR2(leftv res,leftv a,leftv b)
427{
428  if ((a->rtyp!=IDHDL)||(a->e!=NULL))
429  {
430    WerrorS("object must have a name");
431    return TRUE;
432  }
433  char *name=(char *)b->Data();
434  if (strcmp(name,"isSB")==0)
435  {
436    resetFlag(a,FLAG_STD);
437    resetFlag((idhdl)a->data,FLAG_STD);
438  }
439  else if (strcmp(name,"global")==0)
440  {
441    WerrorS("can not set attribut `global`");
442    return TRUE;
443  }
444  else
445  {
446    atKill((idhdl)a->data,name);
447  }
448  return FALSE;
449}
Note: See TracBrowser for help on using the repository browser.