source: git/Singular/ipid.cc @ e6969d

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