[BACK]Return to bsave.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2018 / io

Annotation of OpenXM_contrib2/asir2018/io/bsave.c, Revision 1.2

1.1       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
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     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.2     ! noro       47:  * $OpenXM: OpenXM_contrib2/asir2018/io/bsave.c,v 1.1 2018/09/19 05:45:08 noro Exp $
1.1       noro       48: */
                     49: /* saveXXX must not use GC_malloc(), GC_malloc_atomic(). */
                     50:
                     51: #include "ca.h"
                     52: #include "parse.h"
                     53: #include "com.h"
                     54: #include "ox.h"
                     55:
                     56: void savenbp(FILE *s,NBP p);
                     57:
                     58: void (*savef[])() = { 0, savenum, savep, saver, savelist, savevect,
                     59:   savemat, savestring, 0, savedp, saveui, saveerror,0,0,0,savegfmmat,
1.2     ! noro       60:   savebytearray, 0, 0, 0, 0, 0, 0, 0, 0,  savenbp, savedpm };
1.1       noro       61: #if defined(INTERVAL)
                     62: void saveitv();
                     63: void saveitvd();
                     64: void (*nsavef[])() = { saveq, savereal, 0, savebf, saveitv, saveitvd, 0, saveitv, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
                     65: #else
                     66: void (*nsavef[])() = { saveq, savereal, 0, savebf, savecplx ,savemi, savelm, savegf2n, savegfpn, savegfs, savegfsn,savedalg};
                     67: #endif
                     68:
                     69: static short zeroval = 0;
                     70:
                     71: void saveobj(FILE *s,Obj p)
                     72: {
                     73:   if ( !p )
                     74:     write_short(s,&zeroval);
                     75:   else if ( !savef[OID(p)] )
                     76:     error("saveobj : not implemented");
                     77:   else
                     78:     (*savef[OID(p)])(s,p);
                     79: }
                     80:
                     81: void savenum(FILE *s,Num p)
                     82: {
                     83:   char sgn;
                     84:
                     85:   if ( !nsavef[NID(p)] )
                     86:     error("savenum : not implemented");
                     87:   else {
                     88:     write_short(s,&OID(p)); write_char(s,&NID(p));
                     89:     if ( NID(p) == N_Q ) {
                     90:       sgn = sgnq((Q)p);
                     91:       write_char(s,&sgn);
                     92:     } else
                     93:       write_char(s,&(p->pad));
                     94:     (*nsavef[NID(p)])(s,p);
                     95:   }
                     96: }
                     97:
                     98: void saveq(FILE *s,Q p)
                     99: {
                    100:   size_t lnm,ldn;
                    101:   int size[2];
                    102:   int len = 2;
                    103:   int *bnm,*bdn;
                    104:   Z nm,dn;
                    105:
                    106:   if ( INT(p) ) {
                    107:     bnm = (int *)mpz_export(0,&lnm,-1,sizeof(int),0,0,BDY((Z)p));
                    108:     size[0] = lnm;
                    109:     size[1] = 0;
                    110:     write_intarray(s,size,len);
                    111:     write_intarray(s,bnm,size[0]);
                    112:   } else {
                    113:     nmq(p,&nm);
                    114:     bnm = (int *)mpz_export(0,&lnm,-1,sizeof(int),0,0,BDY(nm));
                    115:     dnq(p,&dn);
                    116:     bdn = (int *)mpz_export(0,&ldn,-1,sizeof(int),0,0,BDY(dn));
                    117:     size[0] = lnm;
                    118:     size[1] = ldn;
                    119:     write_intarray(s,size,len);
                    120:     write_intarray(s,bnm,size[0]);
                    121:     write_intarray(s,bdn,size[1]);
                    122:   }
                    123: }
                    124:
                    125: void savereal(FILE *s,Real p)
                    126: { write_double(s,&BDY(p)); }
                    127:
                    128: /*
                    129:  * BDY(p) = |  z[0]  |   z[1]  | z[2] | ... | z[lg(z)-1] |
                    130:  * -> | id(2) | nid(1) | sgn(1) | expo>>32 | expo&0xffffffff | len | ... |
                    131:  */
                    132:
                    133: void savebf(FILE *s,BF p)
                    134: {
                    135:   unsigned int zero = 0;
                    136:   unsigned int prec;
                    137:   L exp;
                    138:   int sgn,len,t;
                    139:
                    140:   prec = MPFR_PREC(p->body);
                    141:   exp = MPFR_EXP(p->body);
                    142:   sgn = MPFR_SIGN(p->body);
                    143:   len = MPFR_LIMB_SIZE(p->body);
                    144:
                    145:   write_int(s,&sgn);
                    146:   write_int(s,(int *)&prec);
                    147:   write_int64(s,(UL *)&exp);
                    148: #if defined(VISUAL)
                    149: #if !defined(_WIN64)
                    150:   write_int(s,&len);
                    151:   write_intarray(s,p->body->_mpfr_d,len);
                    152: #else
                    153:   t = (prec+31)/32;
                    154:   write_int(s,&t);
                    155:   write_longarray(s,(long long *)p->body->_mpfr_d,t);
                    156: #endif
                    157: #else
                    158: #if SIZEOF_LONG == 4
                    159:   write_int(s,&len);
                    160:   write_intarray(s,p->body->_mpfr_d,len);
                    161: #else /* SIZEOF_LONG == 8 */
                    162:   t = (prec+31)/32;
                    163:   write_int(s,&t);
                    164:   write_longarray(s,p->body->_mpfr_d,t);
                    165: #endif
                    166: #endif
                    167: }
                    168:
                    169: #if defined(INTERVAL)
                    170: void saveitv(FILE *s,Itv p)
                    171: {
                    172:   saveobj(s,(Obj)INF(p));
                    173:   saveobj(s,(Obj)SUP(p));
                    174: }
                    175:
                    176: void saveitvd(FILE *s,IntervalDouble p)
                    177: {
                    178:   write_double(s,&INF(p));
                    179:   write_double(s,&SUP(p));
                    180: }
                    181: #endif
                    182:
                    183: void savecplx(FILE *s,C p)
                    184: { saveobj(s,(Obj)p->r); saveobj(s,(Obj)p->i); }
                    185:
                    186: void savemi(FILE *s,MQ p)
                    187: { write_int(s,&CONT(p)); }
                    188:
                    189: void savelm(FILE *s,LM p)
                    190: {
                    191:   size_t l;
                    192:   int size;
                    193:   int *b;
                    194:
                    195:   b = (int *)mpz_export(0,&l,-1,sizeof(int),0,0,BDY(p));
                    196:   size = l;
                    197:   write_int(s,&size);
                    198:   write_intarray(s,b,size);
                    199: }
                    200:
                    201: void savegf2n(FILE *s,GF2N p)
                    202: {
                    203:   int len;
                    204:
                    205:   len = p->body->w;
                    206:   write_int(s,&len);
                    207:   write_intarray(s,p->body->b,len);
                    208: }
                    209:
                    210: void savegfpn(FILE *s,GFPN p)
                    211: {
                    212:   int d,i;
                    213:
                    214:   d = p->body->d;
                    215:   write_int(s,&d);
                    216:   for ( i = 0; i <= d; i++ )
                    217:     saveobj(s,(Obj)p->body->c[i]);
                    218: }
                    219:
                    220: void savegfs(FILE *s,GFS p)
                    221: { write_int(s,&CONT(p)); }
                    222:
                    223: void savegfsn(FILE *s,GFSN p)
                    224: {
                    225:   int d;
                    226:
                    227:   d = DEG(BDY(p));
                    228:   write_int(s,&d);
                    229:   write_intarray(s,COEF(BDY(p)),d+1);
                    230: }
                    231:
                    232: void savedalg(FILE *s,DAlg p)
                    233: {
                    234:   saveobj(s,(Obj)p->nm);
                    235:   saveobj(s,(Obj)p->dn);
                    236: }
                    237:
                    238: void savep(FILE *s,P p)
                    239: {
                    240:   DCP dc;
                    241:   int n;
                    242:   int vindex;
                    243:
                    244:   if ( NUM(p) )
                    245:     savenum(s,(Num)p);
                    246:   else {
                    247:     vindex = save_convv(VR(p));
                    248:     for ( dc = DC(p), n = 0; dc; dc = NEXT(dc), n++ );
                    249:     write_short(s,&OID(p));
                    250:     write_int(s,&vindex);
                    251:     if ( vindex < 0 )
                    252:       savepfins(s,VR(p));
                    253:     write_int(s,&n);
                    254:     for ( dc = DC(p); dc; dc = NEXT(dc) ) {
                    255:       saveobj(s,(Obj)DEG(dc)); saveobj(s,(Obj)COEF(dc));
                    256:     }
                    257:   }
                    258: }
                    259:
                    260: /* save a pure function (v->attr = V_PF) */
                    261: /* |name(str)|argc(int)|darray(intarray)|args| */
                    262:
                    263: void savepfins(FILE *s,V v)
                    264: {
                    265:   PFINS ins;
                    266:   PF pf;
                    267:   int argc,i;
                    268:   int *darray;
                    269:
                    270:   ins = (PFINS)v->priv;
                    271:   pf = ins->pf;
                    272:   savestr(s,NAME(pf));
                    273:   argc = pf->argc;
                    274:   write_int(s,&argc);
                    275:   darray = (int *)ALLOCA(argc*sizeof(int));
                    276:   for ( i = 0; i < argc; i++ )
                    277:     darray[i] = ins->ad[i].d;
                    278:   write_intarray(s,darray,argc);
                    279:   for ( i = 0; i < argc; i++ )
                    280:     saveobj(s,ins->ad[i].arg);
                    281: }
                    282:
                    283: void saver(FILE *s,R p)
                    284: {
                    285:   if ( !RAT(p) )
                    286:     savep(s,(P)p);
                    287:   else {
                    288:     write_short(s,&OID(p)); write_short(s,&p->reduced);
                    289:     savep(s,NM(p)); savep(s,DN(p));
                    290:   }
                    291: }
                    292:
                    293: void savelist(FILE *s,LIST p)
                    294: {
                    295:   int n;
                    296:   NODE tn;
                    297:
                    298:   for ( tn = BDY(p), n = 0; tn; tn = NEXT(tn), n++ );
                    299:   write_short(s,&OID(p)); write_int(s,&n);
                    300:   for ( tn = BDY(p); tn; tn = NEXT(tn) )
                    301:     saveobj(s,(Obj)BDY(tn));
                    302: }
                    303:
                    304: void savevect(FILE *s,VECT p)
                    305: {
                    306:   int i,len = 2;
                    307:
                    308:   write_short(s,&OID(p)); write_int(s,&p->len);
                    309:   for ( i = 0, len = p->len; i < len; i++ )
                    310:     saveobj(s,(Obj)BDY(p)[i]);
                    311: }
                    312:
                    313: void savemat(FILE *s,MAT p)
                    314: {
                    315:   int i,j,row,col;
                    316:   int len = 3;
                    317:
                    318:   write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
                    319:   for ( i = 0, row = p->row, col = p->col; i < row; i++ )
                    320:     for ( j = 0; j < col; j++ )
                    321:       saveobj(s,(Obj)BDY(p)[i][j]);
                    322: }
                    323:
                    324: void savestring(FILE *s,STRING p)
                    325: {
                    326:   write_short(s,&OID(p)); savestr(s,BDY(p));
                    327: }
                    328:
                    329: void savestr(FILE *s,char *p)
                    330: {
                    331:   int size;
                    332:
                    333:   size = p ? strlen(p) : 0; write_int(s,&size);
                    334:   if ( size )
                    335:     write_string(s,p,size);
                    336: }
                    337:
                    338: void savedp(FILE *s,DP p)
                    339: {
                    340:   int nv,n,i,sugar;
                    341:   MP m,t;
                    342:
                    343:   nv = p->nv; m = p->body; sugar = p->sugar;
                    344:   for ( n = 0, t = m; t; t = NEXT(t), n++ );
                    345:   write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
                    346:   for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
                    347:     saveobj(s,(Obj)t->c);
                    348:     write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
                    349:   }
                    350: }
                    351:
1.2     ! noro      352: void savedpm(FILE *s,DPM p)
        !           353: {
        !           354:   int nv,n,i,sugar;
        !           355:   DMM m,t;
        !           356:
        !           357:   nv = p->nv; m = p->body; sugar = p->sugar;
        !           358:   for ( n = 0, t = m; t; t = NEXT(t), n++ );
        !           359:   write_short(s,&OID(p)); write_int(s,&nv); write_int(s,&sugar); write_int(s,&n);
        !           360:   for ( i = 0, t = m; i < n; i++, t = NEXT(t) ) {
        !           361:     saveobj(s,(Obj)t->c);
        !           362:     write_int(s,&t->pos);
        !           363:     write_int(s,&t->dl->td); write_intarray(s,&(t->dl->d[0]),nv);
        !           364:   }
        !           365: }
        !           366:
1.1       noro      367: void saveui(FILE *s,USINT u)
                    368: {
                    369:   write_short(s,&OID(u)); write_int(s,&BDY(u));
                    370: }
                    371:
                    372: void saveerror(FILE *s,ERR e)
                    373: {
                    374:   write_short(s,&OID(e)); saveobj(s,(Obj)BDY(e));
                    375: }
                    376:
                    377: void savegfmmat(FILE *s,GFMMAT p)
                    378: {
                    379:   int i,row,col;
                    380:
                    381:   write_short(s,&OID(p)); write_int(s,&p->row); write_int(s,&p->col);
                    382:   for ( i = 0, row = p->row, col = p->col; i < row; i++ )
                    383:     write_intarray(s,p->body[i],col);
                    384: }
                    385:
                    386: void savebytearray(FILE *s,BYTEARRAY p)
                    387: {
                    388:   write_short(s,&OID(p)); write_int(s,&p->len);
                    389:   write_string(s,p->body,p->len);
                    390: }
                    391:
                    392: void savenbp(FILE *s,NBP p)
                    393: {
                    394:   int i,n;
                    395:   NODE t;
                    396:   NBM m;
                    397:
                    398:   write_short(s,&OID(p));
                    399:   for ( n = 0, t = BDY(p); t; t = NEXT(t), n++ );
                    400:   write_int(s,&n);
                    401:   for ( i = 0, t = BDY(p); i < n; t = NEXT(t), i++ ) {
                    402:     m = (NBM)BDY(t);
                    403:     saveobj(s,(Obj)m->c);
                    404:     write_int(s,&m->d);
                    405:     write_intarray(s,m->b,(m->d+31)/32);
                    406:   }
                    407: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>