source: git/Singular/ipid.cc @ f6b5f0

spielwiese
Last change on this file since f6b5f0 was f6b5f0, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: change Log: and Header: to Id: git-svn-id: file:///usr/local/Singular/svn/trunk@73 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4static char rcsid[] = "$Id: ipid.cc,v 1.2 1997-03-24 14:24:56 Singular Exp $";
5/*
6* ABSTRACT: identfier handling
7*/
8
9#include <string.h>
10
11#include "mod2.h"
12#include "tok.h"
13#include "ipshell.h"
14#include "intvec.h"
15#include "febase.h"
16#include "mmemory.h"
17#include "numbers.h"
18#include "polys.h"
19#include "ring.h"
20#include "ideals.h"
21#include "matpol.h"
22#include "lists.h"
23#include "attrib.h"
24#include "silink.h"
25#include "ipid.h"
26
27idhdl idroot = NULL;
28idhdl currRingHdl = NULL;
29ring  currRing = NULL;
30ideal currQuotient = NULL;
31char* iiNoName="_";
32
33/*0 implementation*/
34
35idhdl idrec::get(const char * s, int lev)
36{
37  idhdl h = this;
38  idhdl found=NULL;
39  int l;
40  while (h!=NULL)
41  {
42    l=IDLEV(h);
43    mmTestLP(IDID(h));
44    if ((l==lev) && (0 == strcmp(s,IDID(h)))) return h;
45    if ((l==0) && (found==NULL) && (0 == strcmp(s,IDID(h))))
46    {
47      found=h;
48    }
49    h = IDNEXT(h);
50  }
51  return found;
52}
53
54//idrec::~idrec()
55//{
56//  if (id!=NULL)
57//  {
58//    FreeL((ADDRESS)id);
59//    id=NULL;
60//  }
61//  /* much more !! */
62//}
63
64idhdl idrec::set(char * s, int lev, idtyp t, BOOLEAN init)
65{
66  //printf("define %s, %x, lev: %d, typ: %d\n", s,s,lev,t);
67  idhdl h = (idrec *)Alloc0(sizeof(idrec));
68  int   len = 0;
69  IDID(h)   = s;
70  IDTYP(h)  = t;
71  IDLEV(h)  = lev;
72  IDNEXT(h) = this;
73  if (init)
74  {
75    switch (t)
76    {
77    //the type with init routines:
78      case INTVEC_CMD:
79      case INTMAT_CMD:
80        IDINTVEC(h) = new intvec();
81        break;
82      case NUMBER_CMD:
83        IDNUMBER(h) = nInit(0);
84        break;
85      case IDEAL_CMD:
86      case MODUL_CMD:
87        IDFLAG(h) = Sy_bit(FLAG_STD);
88      case MATRIX_CMD:
89        IDIDEAL(h) = idInit(1,1);
90        break;
91      case MAP_CMD:
92        IDIDEAL(h) = idInit(1,1);
93        IDMAP(h)->preimage = mstrdup(IDID(currRingHdl));
94        break;
95      case STRING_CMD:
96      #ifdef HAVE_DLD
97      case BINARY_CMD:
98      #endif
99        IDSTRING(h) = mstrdup("");
100        break;
101      case LIST_CMD:
102        IDLIST(h)=(lists)Alloc(sizeof(slists));
103        IDLIST(h)->Init();
104        break;
105    //the types with the standard init: set the struct to zero
106      case LINK_CMD:
107        len=sizeof(ip_link);
108        break;
109      case RING_CMD:
110      case QRING_CMD:
111        len = sizeof(ip_sring);
112        break;
113      case PACKAGE_CMD:
114        len = sizeof(ip_package);
115        break;
116    //other types: without init (int,proc,script,poly,def,package)
117    }
118    if (len!=0)
119    {
120      IDSTRING(h) = (char *)Alloc0(len);
121    }
122  }
123  return  h;
124}
125
126idhdl enterid(char * s, int lev, idtyp t, idhdl* root, BOOLEAN init)
127{
128  idhdl h;
129  // is it the currRing - id ?
130  if ((currRingHdl!=NULL)
131  &&(IDLEV(currRingHdl)!=lev)
132  &&(s==IDID(currRingHdl)))
133  {
134    s=mstrdup(s);
135  }
136  // is it already defined in root ?
137  else if ((h=(*root)->get(s,lev))!=NULL)
138  {
139    if (IDLEV(h)!=lev)
140    {
141      s=mstrdup(s);
142    }
143    else if ((IDTYP(h) == t)||(t==DEF_CMD))
144    {
145      if (BVERBOSE(V_REDEFINE))
146        Warn("redefining %s **",s);
147      if (s==IDID(h))
148        IDID(h)=NULL;
149      killhdl(h,root);
150    }
151    else
152      goto errlabel;
153  }
154  // is it already defined in idroot ?
155  else if (*root != idroot)
156  {
157    if ((h=idroot->get(s,lev))!=NULL)
158    {
159      if (IDLEV(h)!=lev)
160      {
161        s=mstrdup(s);
162      }
163      else if ((IDTYP(h) == t)||(t==DEF_CMD))
164      {
165        if (BVERBOSE(V_REDEFINE))
166          Warn("redefining %s **",s);
167        IDID(h)=NULL;
168        killhdl(h,&idroot);
169      }
170      else
171      {
172        goto errlabel;
173      }
174    }
175  }
176  // is it already defined in currRing->idroot ?
177  else if ((currRing!=NULL)&&((*root) != currRing->idroot))
178  {
179    if ((h=currRing->idroot->get(s,lev))!=NULL)
180    {
181      if (IDLEV(h)!=lev)
182      {
183        s=mstrdup(s);
184      }
185      else if ((IDTYP(h) == t)||(t==DEF_CMD))
186      {
187        if (BVERBOSE(V_REDEFINE))
188          Warn("redefining %s **",s);
189        IDID(h)=NULL;
190        killhdl(h,&currRing->idroot);
191      }
192      else
193      {
194        goto errlabel;
195      }
196    }
197  }
198  return *root = (*root)->set(s, lev, t, init);
199
200  errlabel:
201    Werror("identifier `%s` in use",s);
202    return NULL;
203}
204
205void killid(char * id, idhdl * ih)
206{
207  if (id!=NULL)
208  {
209    idhdl h = (*ih)->get(id,myynest);
210
211    // id not found in global list, is it defined in current ring ?
212    if (h==NULL)
213    {
214      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
215      {
216        h = currRing->idroot->get(id,myynest);
217        if (h!=NULL)
218        {
219          killhdl(h,&(currRing->idroot));
220          return;
221        }
222      }
223      Werror("`%s` is not defined",id);
224      return;
225    }
226    killhdl(h,ih);
227  }
228  else
229    Werror("kill what ?");
230}
231
232void killhdl(idhdl h)
233{
234  int t=IDTYP(h);
235  if ((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
236    killhdl(h,&currRing->idroot);
237  else
238  {
239    idhdl s=idroot;
240    while ((s!=h) && (s!=NULL)) s=s->next;
241    if (s==NULL) killhdl(h,&currRing->idroot);
242    else killhdl(h,&idroot);
243  }
244}
245
246void killhdl(idhdl h, idhdl * ih)
247{
248  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
249  idhdl hh;
250  BOOLEAN killOtherRing = TRUE;
251  BOOLEAN needResetRing = FALSE;
252
253  if (h->attribute!=NULL)
254  {
255    atKillAll(h);
256    //h->attribute=NULL;
257  }
258  // ring / qring  --------------------------------------------------------
259  // package  -------------------------------------------------------------
260  if ((IDTYP(h) == RING_CMD) || (IDTYP(h) == QRING_CMD)
261  || (IDTYP(h) == PACKAGE_CMD) )
262  {
263    idhdl savecurrRingHdl = currRingHdl;
264    ring  savecurrRing = currRing;
265
266    // any objects defined for this ring ?
267    if (((IDTYP(h)==PACKAGE_CMD) || (IDRING(h)->ref<=0))
268    &&  (IDRING(h)->idroot!=NULL))
269    {
270      idhdl * hd = &IDRING(h)->idroot;
271      idhdl  hdh = IDNEXT(*hd);
272      idhdl  temp;
273      killOtherRing=(IDTYP(h)!=PACKAGE_CMD) && (IDRING(h)!=currRing);
274      if (killOtherRing) //we are not killing the base ring, so switch
275      {
276        needResetRing=TRUE;
277        rSetHdl(h,FALSE);
278        /* no complete init*/
279      }
280      while (hdh!=NULL)
281      {
282        temp = IDNEXT(hdh);
283        killhdl(hdh,&(IDRING(h)->idroot));
284        hdh = temp;
285      }
286      killhdl(*hd,hd);
287    }
288    // reset currRing ?
289    if (needResetRing) // && (killOtherRing)
290    {
291      //we have to switch back to the base ring
292      //currRing = savecurrRing;
293      //currRingHdl = savecurrRingHdl;
294      if (savecurrRing!=NULL)
295      {
296        rSetHdl(savecurrRingHdl,TRUE);
297      }
298    }
299    rKill(h);
300  }
301  // poly / vector -------------------------------------------------------
302  else if ((IDTYP(h) == POLY_CMD) || (IDTYP(h) == VECTOR_CMD))
303  {
304    pDelete(&IDPOLY(h));
305  }
306  // ideal / module/ matrix / map ----------------------------------------
307  else if ((IDTYP(h) == IDEAL_CMD)
308           || (IDTYP(h) == MODUL_CMD)
309           || (IDTYP(h) == MATRIX_CMD)
310           || (IDTYP(h) == MAP_CMD))
311  {
312    ideal iid = IDIDEAL(h);
313    if (IDTYP(h) == MAP_CMD)
314    {
315      map im = IDMAP(h);
316      FreeL((ADDRESS)im->preimage);
317    }
318    idDelete(&iid);
319  }
320  // string / proc / binary ------------------------------------------------
321  else if ((IDTYP(h) == STRING_CMD)
322           ||(IDTYP(h) == PROC_CMD)
323      #ifdef HAVE_DLD
324           ||(IDTYP(h) == BINARY_CMD)
325      #endif
326      )
327  {
328    FreeL((ADDRESS)IDSTRING(h));
329    //IDSTRING(h)=NULL;
330  }
331  // number -------------------------------------------------------------
332  else if (IDTYP(h) == NUMBER_CMD)
333  {
334    nDelete(&IDNUMBER(h));
335  }
336  // intvec / intmat  ---------------------------------------------------
337  else if ((IDTYP(h) == INTVEC_CMD)||(IDTYP(h) == INTMAT_CMD))
338  {
339    delete IDINTVEC(h);
340  }
341  // list  -------------------------------------------------------------
342  else if (IDTYP(h)==LIST_CMD)
343  {
344    IDLIST(h)->Clean();
345    //Free((ADDRESS)IDLIST(h)->m, (IDLIST(h)->nr+1)*sizeof(sleftv));
346    //Free((ADDRESS)IDLIST(h), sizeof(slists));
347  }
348  // link  -------------------------------------------------------------
349  else if (IDTYP(h)==LINK_CMD)
350  {
351    slKill(IDLINK(h));
352    Free((ADDRESS)IDLINK(h),sizeof(ip_link));
353  }
354#ifdef TEST
355  else if ((IDTYP(h)!= INT_CMD)&&(IDTYP(h)!=DEF_CMD))
356    Warn("unknown type to kill: %s(%d)",Tok2Cmdname(IDTYP(h)),IDTYP(h));
357#endif
358
359  //  general  -------------------------------------------------------------
360  // now dechain it and delete idrec
361  FreeL((ADDRESS)IDID(h));
362  //IDID(h)=NULL;
363  if (h == (*ih))
364  {
365    // h is at the beginning of the list
366    *ih = IDNEXT(*ih);
367  }
368  else
369  {
370    // h is somethere in the list:
371    hh = *ih;
372    loop
373    {
374      idhdl hhh = IDNEXT(hh);
375      if (hhh == h)
376      {
377        IDNEXT(hh) = IDNEXT(hhh);
378        break;
379      }
380      hh = hhh;
381    }
382  }
383  Free((ADDRESS)h,sizeof(idrec));
384}
385
386idhdl ggetid(const char *n)
387{
388  idhdl h = idroot->get(n,myynest);
389  idhdl h2=NULL;
390  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
391  {
392    h2 = currRing->idroot->get(n,myynest);
393  }
394  if (h2==NULL) return h;
395  return h2;
396}
397
398void ipListFlag(idhdl h)
399{
400  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
401  if (hasFlag(h,FLAG_DRING)) PrintS(" (D)");
402}
403
404lists ipNameList(idhdl root)
405{
406  idhdl h=root;
407  /* compute the length */
408  int l=0;
409  while (h!=NULL) { l++; h=IDNEXT(h); }
410  /* allocate list */
411  lists L=(lists)Alloc(sizeof(slists));
412  L->Init(l);
413  /* copy names */
414  h=root;
415  l=0;
416  while (h!=NULL)
417  {
418    /* list is initialized with 0 => no need to clear anything */
419    L->m[l].rtyp=STRING_CMD;
420    L->m[l].data=mstrdup(IDID(h));
421    l++;
422    h=IDNEXT(h);
423  }
424  return L;
425}
426
427/*
428* move 'tomove' from root1 list to root2 list
429*/
430static void ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
431{
432  idhdl h;
433  /* search 'tomove' in root2 : if found -> do nothing */
434  h=root2;
435  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
436  if (h!=NULL) return;
437  /* search predecessor of h in root1, remove 'tomove' */
438  h=root1;
439  if (tomove==h)
440  {
441    root1=IDNEXT(h);
442  }
443  else
444  {
445    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
446    if (h==NULL) return; /* not in the list root1 -> do nothing */
447    IDNEXT(h)=IDNEXT(tomove);
448  }
449  /* add to root2 list */
450  IDNEXT(tomove)=root2;
451  root2=tomove;
452}
453
454void  ipMoveId(idhdl tomove)
455{
456  if ((currRing!=NULL)&&(tomove!=NULL))
457  {
458    if (((QRING_CMD!=IDTYP(tomove)) && (BEGIN_RING<IDTYP(tomove)) && (IDTYP(tomove)<END_RING))
459    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
460    {
461      /*move 'tomove' to ring id's*/
462      ipSwapId(tomove,idroot,currRing->idroot);
463    }
464    else
465    {
466      /*move 'tomove' to global id's*/
467      ipSwapId(tomove,currRing->idroot,idroot);
468    }
469  }
470}
Note: See TracBrowser for help on using the repository browser.