Annotation of OpenXM_contrib2/asir2000/io/bsave.c, Revision 1.15
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.15 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/bsave.c,v 1.14 2004/12/10 07:36:35 noro Exp $
1.2 noro 48: */
1.1 noro 49: /* saveXXX must not use GC_malloc(), GC_malloc_atomic(). */
50:
51: #include "ca.h"
52: #include "parse.h"
53: #include "com.h"
54:
1.12 ohara 55: #if defined(PARI)
1.1 noro 56: #include "genpari.h"
57: int get_lg(GEN);
58: #endif
59:
1.15 ! noro 60: void savenbp(FILE *s,NBP p);
! 61:
1.1 noro 62: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
1.15 ! noro 63: savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat,
! 64: savebytearray, 0, 0, 0, 0, 0, 0, 0, 0, savenbp };
1.6 saito 65: #if defined(INTERVAL)
1.7 saito 66: void saveitv();
67: void saveitvd();
1.14 noro 68: void (*nsavef[])() = { saveq, savereal, 0, savebf, saveitv, saveitvd, 0, saveitv, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
1.6 saito 69: #else
1.14 noro 70: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
1.6 saito 71: #endif
1.1 noro 72:
73: static short zeroval = 0;
74:
1.10 noro 75: void saveobj(FILE *s,Obj p)
1.1 noro 76: {
77: if ( !p )
78: write_short(s,&zeroval);
79: else if ( !savef[OID(p)] )
80: error("saveobj : not implemented");
81: else
82: (*savef[OID(p)])(s,p);
83: }
84:
1.10 noro 85: void savenum(FILE *s,Num p)
1.1 noro 86: {
87: if ( !nsavef[NID(p)] )
88: error("savenum : not implemented");
89: else {
90: write_short(s,&OID(p)); write_char(s,&NID(p));
91: write_char(s,&(p->pad));
92: (*nsavef[NID(p)])(s,p);
93: }
94: }
95:
1.10 noro 96: void saveq(FILE *s,Q p)
1.1 noro 97: {
98: int size[2];
99: int len = 2;
100:
101: size[0] = PL(NM(p)); size[1] = DN(p) ? PL(DN(p)) : 0;
102: write_intarray(s,size,len);
103: write_intarray(s,BD(NM(p)),size[0]);
104: if ( size[1] )
105: write_intarray(s,BD(DN(p)),size[1]);
106: }
107:
1.10 noro 108: void savereal(FILE *s,Real p)
1.1 noro 109: { write_double(s,&BDY(p)); }
110:
111: /*
112: * BDY(p) = | z[0] | z[1] | z[2] | ... | z[lg(z)-1] |
113: * -> | id(2) | nid(1) | sgn(1) | expo>>32 | expo&0xffffffff | len | ... |
114: */
115:
1.10 noro 116: void savebf(FILE *s,BF p)
1.1 noro 117: {
1.12 ohara 118: #if defined(PARI)
1.1 noro 119: GEN z;
120: int sign;
121: unsigned long expo;
1.13 noro 122: unsigned int len,t;
1.1 noro 123:
124: z = (GEN)BDY(p);
125: sign = signe(z);
126: len = lg(z)-2;
127: expo = expo(z);
128:
129: write_int(s,&sign);
130:
131: #if defined(LONG_IS_32BIT)
132: write_int(s,(int *)&zeroval); /* expo>>32 is always 0 */
133: write_int(s,(int *)&expo);
134: write_int(s,&len);
135: write_intarray(s,(int *)&z[2],len);
136: #elif defined(LONG_IS_64BIT)
137: t = expo>>32; write_int(s,(int *)&t);
138: t = expo&0xffffffff; write_int(s,&t);
139: t = 2*len; write_int(s,&t);
140: write_longarray(s,&z[2],len);
141: #endif
142: #else
143: error("savebf : PARI is not combined");
144: #endif
145: }
1.6 saito 146:
147: #if defined(INTERVAL)
1.10 noro 148: void saveitv(FILE *s,Itv p)
1.6 saito 149: {
150: saveobj(s,(Obj)INF(p));
151: saveobj(s,(Obj)SUP(p));
152: }
153:
1.11 kondoh 154: void saveitvd(FILE *s,IntervalDouble p)
1.6 saito 155: {
156: write_double(s,&INF(p));
157: write_double(s,&SUP(p));
158: }
159: #endif
1.1 noro 160:
1.10 noro 161: void savecplx(FILE *s,C p)
1.1 noro 162: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
163:
1.10 noro 164: void savemi(FILE *s,MQ p)
1.1 noro 165: { write_int(s,&CONT(p)); }
166:
1.10 noro 167: void savelm(FILE *s,LM p)
1.1 noro 168: {
169: int size;
170:
171: size = PL(BDY(p));
172: write_int(s,&size);
173: write_intarray(s,BD(BDY(p)),size);
174: }
175:
1.10 noro 176: void savegf2n(FILE *s,GF2N p)
1.1 noro 177: {
178: int len;
179:
180: len = p->body->w;
181: write_int(s,&len);
182: write_intarray(s,p->body->b,len);
183: }
184:
1.10 noro 185: void savegfpn(FILE *s,GFPN p)
1.1 noro 186: {
187: int d,i;
188:
189: d = p->body->d;
190: write_int(s,&d);
191: for ( i = 0; i <= d; i++ )
192: saveobj(s,(Obj)p->body->c[i]);
193: }
1.8 noro 194:
1.10 noro 195: void savegfs(FILE *s,GFS p)
1.8 noro 196: { write_int(s,&CONT(p)); }
1.9 noro 197:
1.10 noro 198: void savegfsn(FILE *s,GFSN p)
1.9 noro 199: {
200: int d;
201:
202: d = DEG(BDY(p));
203: write_int(s,&d);
204: write_intarray(s,COEF(BDY(p)),d+1);
1.14 noro 205: }
206:
207: void savedalg(FILE *s,DAlg p)
208: {
1.15 ! noro 209: saveobj(s,(Obj)p->nm);
! 210: saveobj(s,(Obj)p->dn);
1.9 noro 211: }
1.1 noro 212:
1.10 noro 213: void savep(FILE *s,P p)
1.1 noro 214: {
215: DCP dc;
216: int n;
217: int vindex;
218:
219: if ( NUM(p) )
220: savenum(s,(Num)p);
221: else {
222: vindex = save_convv(VR(p));
223: for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
224: write_short(s,&OID(p));
225: write_int(s,&vindex);
226: if ( vindex < 0 )
227: savepfins(s,VR(p));
228: write_int(s,&n);
229: for ( dc = DC(p); dc; dc = NEXT(dc) ) {
230: saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
231: }
232: }
233: }
234:
235: /* save a pure function (v->attr = V_PF) */
236: /* |name(str)|argc(int)|darray(intarray)|args| */
237:
1.10 noro 238: void savepfins(FILE *s,V v)
1.1 noro 239: {
240: PFINS ins;
241: PF pf;
242: int argc,i;
243: int *darray;
244:
245: ins = (PFINS)v->priv;
246: pf = ins->pf;
247: savestr(s,NAME(pf));
248: argc = pf->argc;
249: write_int(s,&argc);
250: darray = (int *)ALLOCA(argc*sizeof(int));
251: for ( i = 0; i < argc; i++ )
252: darray[i] = ins->ad[i].d;
253: write_intarray(s,darray,argc);
254: for ( i = 0; i < argc; i++ )
255: saveobj(s,ins->ad[i].arg);
256: }
257:
1.10 noro 258: void saver(FILE *s,R p)
1.1 noro 259: {
260: if ( !RAT(p) )
261: savep(s,(P)p);
262: else {
263: write_short(s,&OID(p)); write_short(s,&p->reduced);
264: savep(s,NM(p)); savep(s,DN(p));
265: }
266: }
267:
1.10 noro 268: void savelist(FILE *s,LIST p)
1.1 noro 269: {
270: int n;
271: NODE tn;
272:
273: for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
274: write_short(s,&OID(p)); write_int(s,&n);
275: for ( tn = BDY(p); tn; tn = NEXT(tn) )
276: saveobj(s,(Obj)BDY(tn));
277: }
278:
1.10 noro 279: void savevect(FILE *s,VECT p)
1.1 noro 280: {
281: int i,len = 2;
282:
283: write_short(s,&OID(p)); write_int(s,&p->len);
284: for ( i = 0, len = p->len; i < len; i++ )
285: saveobj(s,(Obj)BDY(p)[i]);
286: }
287:
1.10 noro 288: void savemat(FILE *s,MAT p)
1.1 noro 289: {
290: int i,j,row,col;
291: int len = 3;
292:
293: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
294: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
295: for ( j = 0; j < col; j++ )
296: saveobj(s,(Obj)BDY(p)[i][j]);
297: }
298:
1.10 noro 299: void savestring(FILE *s,STRING p)
1.1 noro 300: {
301: write_short(s,&OID(p)); savestr(s,BDY(p));
302: }
303:
1.10 noro 304: void savestr(FILE *s,char *p)
1.1 noro 305: {
306: int size;
307:
308: size = p ? strlen(p) : 0; write_int(s,&size);
309: if ( size )
310: write_string(s,p,size);
311: }
312:
1.10 noro 313: void savedp(FILE *s,DP p)
1.1 noro 314: {
315: int nv,n,i,sugar;
316: MP m,t;
317:
318: nv = p->nv; m = p->body; sugar = p->sugar;
319: for ( n = 0, t = m; t; t = NEXT(t), n++ );
320: write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
321: for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
322: saveobj(s,(Obj)t->c);
323: write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
324: }
325: }
326:
1.10 noro 327: void saveui(FILE *s,USINT u)
1.1 noro 328: {
329: write_short(s,&OID(u)); write_int(s,&BDY(u));
330: }
331:
1.10 noro 332: void saveerror(FILE *s,ERR e)
1.1 noro 333: {
334: write_short(s,&OID(e)); saveobj(s,(Obj)BDY(e));
335: }
336:
1.10 noro 337: void savegfmmat(FILE *s,GFMMAT p)
1.1 noro 338: {
1.10 noro 339: int i,row,col;
1.1 noro 340:
341: write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
342: for ( i = 0, row = p->row, col = p->col; i < row; i++ )
343: write_intarray(s,p->body[i],col);
1.4 noro 344: }
345:
1.10 noro 346: void savebytearray(FILE *s,BYTEARRAY p)
1.4 noro 347: {
348: write_short(s,&OID(p)); write_int(s,&p->len);
349: write_string(s,p->body,p->len);
1.15 ! noro 350: }
! 351:
! 352: void savenbp(FILE *s,NBP p)
! 353: {
! 354: int i,n;
! 355: NODE t;
! 356: NBM m;
! 357:
! 358: write_short(s,&OID(p));
! 359: for ( n = 0, t = BDY(p); t; t = NEXT(t), n++ );
! 360: write_int(s,&n);
! 361: for ( i = 0, t = BDY(p); i < n; t = NEXT(t), i++ ) {
! 362: m = (NBM)BDY(t);
! 363: saveobj(s,(Obj)m->c);
! 364: write_int(s,&m->d);
! 365: write_intarray(s,m->b,(m->d+31)/32);
! 366: }
1.1 noro 367: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>