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

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

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