source: git/Singular/bigintm.cc @ 09830b6

spielwiese
Last change on this file since 09830b6 was 16f511, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Fixed the usage of "config.h" (if defined HAVE_CONFIG_H)
  • Property mode set to 100644
File size: 4.3 KB
Line 
1// as this file provides only example code,
2// no trace of it must appear in Singular and Singularg
3//
4#if 0
5//
6#ifdef HAVE_CONFIG_H
7#include "config.h"
8#endif /* HAVE_CONFIG_H */
9#include <kernel/mod2.h>
10#include <Singular/ipid.h>
11#include <Singular/blackbox.h>
12#include <omalloc/omalloc.h>
13#include <kernel/febase.h>
14#include <kernel/longrat.h>
15#include <Singular/subexpr.h>
16#include <Singular/bigintm.h>
17
18
19static char * bigintm_String(blackbox *b, void *d)
20{ if (d==NULL) return omStrDup("oo");
21   else
22   {
23     StringSetS("");
24     number n=(number)d; nlWrite(n,NULL); d=(void*)n;
25     return StringEndS();
26    }
27}
28static void * bigintm_Copy(blackbox*b, void *d)
29{  number n=(number)d; return nlCopy(n); }
30
31static BOOLEAN bigintm_Assign(leftv l, leftv r)
32{
33  blackbox *ll=getBlackboxStuff(l->Typ());
34 
35  if (r->Typ()>MAX_TOK)
36  {
37    if (l->Typ() == r->Typ())
38    {
39      blackbox *rr=getBlackboxStuff(r->Typ());
40     
41      if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
42      number n2=(number)r->CopyD();
43      if (l->rtyp==IDHDL)
44      {
45        IDDATA((idhdl)l->data)=(char *)n2;
46      }
47      else
48      {
49        l->data=(void *)n2;
50      }
51      return FALSE;
52    }
53    else
54    {
55      Werror("bigintm_Assign: assign %s (%d) = %s (%d)",
56             getBlackboxName(l->Typ()), l->Typ(),
57             getBlackboxName(r->Typ()), r->Typ());
58      return TRUE;
59    }
60  }
61  else if (r->Typ()==INT_CMD)
62  {
63    if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); }
64    number n2=nlInit((int)(long)r->Data(),NULL);
65    if (l->rtyp==IDHDL)
66    {
67      IDDATA((idhdl)l->data)=(char *)n2;
68    }
69    else
70    {
71      l->data=(void *)n2;
72    }
73    return FALSE;
74  }
75  else
76    Werror("assign %d = %d",l->Typ(),r->Typ());
77  return TRUE;
78}
79static BOOLEAN bigintm_OpM(int op, leftv res, leftv args);
80
81
82static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2)
83{
84  // interpreter: a1 is ist bigintm
85 
86  blackbox *a=getBlackboxStuff(a1->Typ());
87  number n1=(number)a1->Data();
88  switch(op)
89  {
90    case '+':
91    {
92      if (a2->Typ()==INT_CMD)
93      {
94        number n2=nlInit((int)(long)a2->Data(),NULL);
95        number n=nlAdd(n1,n2);
96        res->data=(void *)n;
97        res->rtyp=a1->Typ();
98        return FALSE;
99      }
100      else if (a2->Typ()==a1->Typ())
101      {
102        number n2=(number)a2->Data();
103        number n=nlAdd(n1,n2);
104        res->data=(void *)n;
105        res->rtyp=a1->Typ();
106        return FALSE;
107      }
108      return TRUE;
109    }
110    default:
111      return blackboxDefaultOp2(op,res,a1,a2);
112  }
113  return blackboxDefaultOp2(op,res,a1,a2);
114}
115// BOOLEAN opM(int op, leftv res, leftv args)
116static BOOLEAN bigintm_OpM(int op, leftv res, leftv args)
117{
118  // interpreter: args->1. arg is ist bigintm
119  blackbox *a=getBlackboxStuff(args->Typ());
120  switch(op)
121  {
122    case STRING_CMD:
123    {
124      res->data=(void *)a->blackbox_String(a,args->Data());
125      res->rtyp=STRING_CMD;
126      return FALSE;
127    }
128    default:
129      return blackboxDefaultOpM(op,res,args);
130      break;
131  }
132  return blackboxDefaultOpM(op,res,args);
133}
134static void bigintm_destroy(blackbox *b, void *d)
135{
136  if (d!=NULL)
137  {
138    number n=(number)d;
139    nlDelete(&n,NULL);
140  }
141}
142
143
144BOOLEAN bigintm_setup()
145{
146  // it is not very usefull to call this setup routine twice,
147  // but (as this is an example) it is possible.
148  // Other blackbox type really require multiple setup calls
149  // (with different names).
150  // Also the format of this setup routine is not fixed,
151  // it may take additional parameters, it may return a value or not
152  // - it really depends.
153
154  blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox));
155  // all undefined entries will be set to default in setBlackboxStuff
156  // the default Print is quite usefule,
157  // all other are simply error messages
158  b->blackbox_destroy=bigintm_destroy;
159  b->blackbox_String=bigintm_String;
160  //b->blackbox_Print=blackbox_default_Print;
161  //b->blackbox_Init=blackbox_default_Init;
162  b->blackbox_Copy=bigintm_Copy;
163  b->blackbox_Assign=bigintm_Assign;
164  //b->blackbox_Op1=blackboxDefaultOp1;
165  b->blackbox_Op2=bigintm_Op2;
166  //b->blackbox_Op3=blackbox_default_Op3;
167  b->blackbox_OpM=bigintm_OpM;
168  int rt=setBlackboxStuff(b,"bigintm");
169
170  Print("bigintm_setup: create type %d (%s)\n",rt, getBlackboxName(rt));
171
172  return FALSE; // ok, TRUE = error!: this example never fails
173}
174
175#endif
Note: See TracBrowser for help on using the repository browser.