source: git/Singular/ipid.cc @ dfc6b54

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