[BACK]Return to cmo.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/cmo.c, Revision 1.13

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.13    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/cmo.c,v 1.12 2003/06/03 16:06:48 ohara Exp $ */
1.1       ohara       3:
                      4: /*
                      5:    This module includes functions for sending/receiveng CMO's.
                      6:    Some commnets is written in Japanese by the EUC-JP coded
                      7:    character set.
                      8: */
                      9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
                     12: #include <stdarg.h>
                     13: #include <string.h>
                     14: #include "ox_toolkit.h"
                     15: #include "parse.h"
                     16:
1.12      ohara      17: static cell*        new_cell(cmo *ob, int e);
1.1       ohara      18: static char*        new_string_set_cmo_null();
                     19: static char*        new_string_set_cmo_int32(int integer);
                     20: static char*        new_string_set_cmo_list(cmo_list *c);
                     21: static char*        new_string_set_cmo_zz(cmo_zz *c);
1.7       ohara      22: static char*        new_string_set_cmo_double(cmo_double *m);
1.1       ohara      23:
                     24: /* functions for a cmo_list */
1.12      ohara      25: static cell* new_cell(cmo *ob, int e)
1.1       ohara      26: {
1.11      ohara      27:     cell* h = MALLOC(sizeof(cell));
1.1       ohara      28:     h->next = NULL;
                     29:     h->prev = NULL;
                     30:     h->cmo  = ob;
1.12      ohara      31:     h->exp  = e;
1.1       ohara      32:     return h;
                     33: }
                     34:
                     35: cell* list_next(cell *el)
                     36: {
                     37:     return el->next;
                     38: }
                     39:
                     40: cell* list_first(cmo_list *this)
                     41: {
                     42:     return this->head->next;
                     43: }
                     44:
                     45: cmo* list_first_cmo(cmo_list *this)
                     46: {
                     47:     return list_first(this)->cmo;
                     48: }
                     49:
                     50: int list_endof(cmo_list *this, cell *el)
                     51: {
                     52:     return (this->head == el);
                     53: }
                     54:
                     55: /* (prev, head) => (prev, new, head) */
                     56: static void list_cons(cell *head, cell *new)
                     57: {
                     58:     cell* prev = head->prev;
                     59:     new->prev  = prev;
                     60:     head->prev = new;
                     61:     prev->next = new;
                     62:     new->next  = head;
                     63: }
                     64:
                     65: cmo_list *list_append(cmo_list* this, cmo* ob)
                     66: {
1.12      ohara      67:     list_cons(this->head, new_cell(ob, 0));
                     68:     this->length++;
                     69:     return this;
                     70: }
                     71:
                     72: cmo_list *list_append_monomial(cmo_list* this, cmo* coef, int exp)
                     73: {
                     74:     list_cons(this->head, new_cell(coef, exp));
1.1       ohara      75:     this->length++;
                     76:     return this;
                     77: }
                     78:
                     79: /* call as list_appendl(List, ob1, ob2, ob3, NULL) */
                     80: cmo_list *list_appendl(cmo_list* this, ...)
                     81: {
                     82:     cmo *ob;
                     83:     va_list ap;
                     84:     va_start(ap, this);
1.3       ohara      85:        if (this == NULL) {
                     86:                this = new_cmo_list();
                     87:        }
1.1       ohara      88:     while((ob = va_arg(ap, cmo *)) != NULL) {
                     89:         list_append(this, ob);
                     90:     }
                     91:     va_end(ap);
                     92:     return this;
                     93: }
                     94:
                     95: int list_length(cmo_list* this)
                     96: {
                     97:     return this->length;
                     98: }
                     99:
                    100: cmo *list_nth(cmo_list* this, int n)
                    101: {
                    102:     cell* el;
                    103:     if(list_length(this) > n) {
                    104:         el = list_first(this);
                    105:         while(n-- > 0) {
                    106:             el = list_next(el);
                    107:         }
                    108:         return el->cmo;
                    109:     }
                    110:     return NULL;
                    111: }
                    112:
                    113: /* for GNU mpz */
                    114: void resize_mpz(mpz_ptr mpz, int size)
                    115: {
                    116:     _mpz_realloc(mpz, abs(size));
                    117:     mpz->_mp_size = size;
                    118: }
                    119:
                    120: /* functions named new_cmo_*. */
                    121: cmo_null* new_cmo_null()
                    122: {
1.13    ! ohara     123:     cmo_null* m = MALLOC_ATOMIC(sizeof(cmo_null));
1.1       ohara     124:     m->tag = CMO_NULL;
                    125:     return m;
                    126: }
                    127:
                    128: cmo_int32* new_cmo_int32(int i)
                    129: {
                    130:     cmo_int32* c;
1.13    ! ohara     131:     c = MALLOC_ATOMIC(sizeof(cmo_int32));
        !           132:     c->tag = CMO_INT32;
1.1       ohara     133:     c->i = i;
                    134:     return c;
                    135: }
                    136:
                    137: cmo_string* new_cmo_string(char* s)
                    138: {
1.11      ohara     139:     cmo_string* c = MALLOC(sizeof(cmo_string));
1.1       ohara     140:     c->tag = CMO_STRING;
                    141:     if (s != NULL) {
1.13    ! ohara     142:         c->s = MALLOC_ATOMIC(strlen(s)+1);
1.1       ohara     143:         strcpy(c->s, s);
                    144:     }else {
                    145:         c->s = NULL;
                    146:     }
                    147:     return c;
                    148: }
                    149:
                    150: cmo_mathcap* new_cmo_mathcap(cmo* ob)
                    151: {
1.11      ohara     152:     cmo_mathcap* c = MALLOC(sizeof(cmo_mathcap));
1.1       ohara     153:     c->tag = CMO_MATHCAP;
                    154:     c->ob  = ob;
                    155:     return c;
                    156: }
                    157:
                    158: cmo_list* new_cmo_list()
                    159: {
1.11      ohara     160:     cmo_list* c = MALLOC(sizeof(cmo_list));
1.1       ohara     161:     c->tag    = CMO_LIST;
                    162:     c->length = 0;
                    163:     c->head->next = c->head;
                    164:     c->head->prev = c->head;
                    165:     return c;
                    166: }
                    167:
                    168: cmo_monomial32* new_cmo_monomial32()
                    169: {
1.11      ohara     170:     cmo_monomial32* c = MALLOC(sizeof(cmo_monomial32));
1.1       ohara     171:     c->tag  = CMO_MONOMIAL32;
                    172:     return c;
                    173: }
                    174:
                    175: cmo_monomial32* new_cmo_monomial32_size(int size)
                    176: {
                    177:     cmo_monomial32* c = new_cmo_monomial32();
                    178:     if (size>0) {
                    179:         c->length = size;
1.11      ohara     180:         c->exps = MALLOC(sizeof(int)*size);
1.1       ohara     181:     }
                    182:     return c;
                    183: }
                    184:
                    185: cmo_zz* new_cmo_zz()
                    186: {
1.11      ohara     187:     cmo_zz* c = MALLOC(sizeof(cmo_zz));
1.1       ohara     188:     c->tag  = CMO_ZZ;
                    189:     mpz_init(c->mpz);
                    190:     return c;
                    191: }
                    192:
                    193: cmo_zz* new_cmo_zz_noinit()
                    194: {
1.11      ohara     195:     cmo_zz* c = MALLOC(sizeof(cmo_zz));
1.1       ohara     196:     c->tag  = CMO_ZZ;
                    197:     return c;
                    198: }
                    199:
                    200: cmo_zz* new_cmo_zz_set_si(int i)
                    201: {
                    202:     cmo_zz* c = new_cmo_zz();
                    203:     mpz_set_si(c->mpz, i);
                    204:     return c;
                    205: }
                    206:
                    207: cmo_zz* new_cmo_zz_set_mpz(mpz_ptr z)
                    208: {
                    209:     cmo_zz* c = new_cmo_zz();
                    210:     mpz_set(c->mpz, z);
                    211:     return c;
                    212: }
                    213:
                    214: cmo_zz *new_cmo_zz_set_string(char *s)
                    215: {
                    216:     cmo_zz* c = new_cmo_zz_noinit();
                    217:     mpz_init_set_str(c->mpz, s, 10);
                    218:     return c;
                    219: }
                    220:
                    221: cmo_zz* new_cmo_zz_size(int size)
                    222: {
                    223:     cmo_zz* c = new_cmo_zz();
                    224:     resize_mpz(c->mpz, size);
                    225:     return c;
                    226: }
                    227:
                    228: cmo_zero* new_cmo_zero()
                    229: {
1.13    ! ohara     230:     cmo_zero* m = MALLOC_ATOMIC(sizeof(cmo_zero));
1.1       ohara     231:     m->tag = CMO_ZERO;
1.6       ohara     232:     return m;
                    233: }
                    234:
                    235: cmo_double *new_cmo_double(double d)
                    236: {
1.13    ! ohara     237:     cmo_double* m = MALLOC_ATOMIC(sizeof(cmo_double));
1.6       ohara     238:     m->tag = CMO_64BIT_MACHINE_DOUBLE;
                    239:     m->d = d;
1.1       ohara     240:     return m;
                    241: }
                    242:
                    243: cmo_dms_generic* new_cmo_dms_generic()
                    244: {
1.13    ! ohara     245:     cmo_dms_generic* m = MALLOC_ATOMIC(sizeof(cmo_dms_generic));
1.1       ohara     246:     m->tag = CMO_DMS_GENERIC;
                    247:     return m;
                    248: }
                    249:
                    250: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
                    251: {
1.11      ohara     252:     cmo_ring_by_name* c = MALLOC(sizeof(cmo_ring_by_name));
1.1       ohara     253:     c->tag = CMO_RING_BY_NAME;
                    254:     c->ob  = ob;
                    255:     return c;
                    256: }
                    257:
                    258: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
                    259: {
1.11      ohara     260:     cmo_indeterminate* c = MALLOC(sizeof(cmo_indeterminate));
1.1       ohara     261:     c->tag = CMO_INDETERMINATE;
                    262:     c->ob  = ob;
                    263:     return c;
                    264: }
                    265:
                    266: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
                    267: {
1.11      ohara     268:     cmo_distributed_polynomial* c = MALLOC(sizeof(cmo_distributed_polynomial));
1.1       ohara     269:     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL;
                    270:     c->length  = 0;
                    271:     c->head->next = c->head;
                    272:     c->head->prev = c->head;
                    273:     c->ringdef = NULL;
                    274:     return c;
                    275: }
                    276:
                    277: cmo_error2* new_cmo_error2(cmo* ob)
                    278: {
1.11      ohara     279:     cmo_error2* c = MALLOC(sizeof(cmo_error2));
1.1       ohara     280:     c->tag = CMO_ERROR2;
                    281:     c->ob  = ob;
                    282:     return c;
                    283: }
                    284:
1.2       ohara     285:
                    286: /* Following functions translate cmo's to (asciiz) strings. */
1.1       ohara     287: static char *new_string_set_cmo_zz(cmo_zz *c)
                    288: {
                    289:     return mpz_get_str(NULL, 10, c->mpz);
                    290: }
                    291:
                    292: static char *new_string_set_cmo_null()
                    293: {
                    294:     static char* null_string = "";
                    295:     return null_string;
                    296: }
                    297:
                    298: static char *new_string_set_cmo_int32(int integer)
                    299: {
                    300:     char buff[1024];
                    301:     char *s;
                    302:
                    303:     sprintf(buff, "%d", integer);
1.11      ohara     304:     s = MALLOC(strlen(buff)+1);
1.1       ohara     305:     strcpy(s, buff);
                    306:
                    307:     return s;
                    308: }
                    309:
                    310: static char *new_string_set_cmo_list(cmo_list *m)
                    311: {
                    312:     char *s;
                    313:     int i;
                    314:     int size = 0;
                    315:     int len = list_length(m);
1.11      ohara     316:     char **sp = ALLOCA(len*sizeof(cmo *));
1.1       ohara     317:
                    318:     cell* cp = list_first(m);
                    319:     for(i = 0; i < len; i++) {
                    320:         sp[i] = new_string_set_cmo(cp->cmo);
                    321:         size += strlen(sp[i]) + 3;
                    322:         cp = list_next(cp);
                    323:     }
1.11      ohara     324:     s = MALLOC(size+2);
1.1       ohara     325:     strcpy(s, "[ ");
                    326:     for(i = 0; i < len - 1; i++) {
                    327:         strcat(s, sp[i]);
                    328:         strcat(s, " , ");
                    329:     }
                    330:     strcat(s, sp[len-1]);
                    331:     strcat(s, " ]");
                    332:     return s;
                    333: }
                    334:
1.7       ohara     335: static char *new_string_set_cmo_double(cmo_double *m)
                    336: {
                    337:     char buff[1024];
                    338:     char *s;
                    339:
1.13    ! ohara     340:     sprintf(buff, "%.20f", m->d);
        !           341:     s = MALLOC_ATOMIC(strlen(buff)+1);
1.7       ohara     342:     strcpy(s, buff);
                    343:
                    344:     return s;
                    345: }
                    346:
1.1       ohara     347: char *new_string_set_cmo(cmo *m)
                    348: {
                    349:     switch(m->tag) {
                    350:     case CMO_ZZ:
                    351:         return new_string_set_cmo_zz((cmo_zz *)m);
                    352:     case CMO_INT32:
                    353:         return new_string_set_cmo_int32(((cmo_int32 *)m)->i);
                    354:     case CMO_STRING:
                    355:         return ((cmo_string *)m)->s;
                    356:     case CMO_NULL:
                    357:         return new_string_set_cmo_null();
                    358:     case CMO_LIST:
                    359:         return new_string_set_cmo_list((cmo_list *)m);
1.7       ohara     360:        case CMO_64BIT_MACHINE_DOUBLE:
                    361:         return new_string_set_cmo_int32(m);
1.1       ohara     362:     default:
1.8       ohara     363:         ox_printf("unconvertible <%s>\n", get_symbol_by_tag(m->tag));
1.1       ohara     364:         /* yet not implemented. */
                    365:         return NULL;
                    366:     }
                    367: }

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