[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.9

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

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