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

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.4     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/cmo.c,v 1.3 2000/12/14 01:39:31 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;
                    223:     return m;
                    224: }
                    225:
                    226: cmo_dms_generic* new_cmo_dms_generic()
                    227: {
                    228:     cmo_dms_generic* m = malloc(sizeof(cmo_dms_generic));
                    229:     m->tag = CMO_DMS_GENERIC;
                    230:     return m;
                    231: }
                    232:
                    233: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
                    234: {
                    235:     cmo_ring_by_name* c = malloc(sizeof(cmo_ring_by_name));
                    236:     c->tag = CMO_RING_BY_NAME;
                    237:     c->ob  = ob;
                    238:     return c;
                    239: }
                    240:
                    241: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
                    242: {
                    243:     cmo_indeterminate* c = malloc(sizeof(cmo_indeterminate));
                    244:     c->tag = CMO_INDETERMINATE;
                    245:     c->ob  = ob;
                    246:     return c;
                    247: }
                    248:
                    249: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
                    250: {
                    251:     cmo_distributed_polynomial* c = malloc(sizeof(cmo_distributed_polynomial));
                    252:     c->tag     = CMO_DISTRIBUTED_POLYNOMIAL;
                    253:     c->length  = 0;
                    254:     c->head->next = c->head;
                    255:     c->head->prev = c->head;
                    256:     c->ringdef = NULL;
                    257:     return c;
                    258: }
                    259:
                    260: cmo_error2* new_cmo_error2(cmo* ob)
                    261: {
                    262:     cmo_error2* c = malloc(sizeof(cmo_error2));
                    263:     c->tag = CMO_ERROR2;
                    264:     c->ob  = ob;
                    265:     return c;
                    266: }
                    267:
1.2       ohara     268:
                    269: /* Following functions translate cmo's to (asciiz) strings. */
1.1       ohara     270: static char *new_string_set_cmo_zz(cmo_zz *c)
                    271: {
                    272:     return mpz_get_str(NULL, 10, c->mpz);
                    273: }
                    274:
                    275: static char *new_string_set_cmo_null()
                    276: {
                    277:     static char* null_string = "";
                    278:     return null_string;
                    279: }
                    280:
                    281: static char *new_string_set_cmo_int32(int integer)
                    282: {
                    283:     char buff[1024];
                    284:     char *s;
                    285:
                    286:     sprintf(buff, "%d", integer);
                    287:     s = malloc(strlen(buff)+1);
                    288:     strcpy(s, buff);
                    289:
                    290:     return s;
                    291: }
                    292:
                    293: static char *new_string_set_cmo_list(cmo_list *m)
                    294: {
                    295:     char *s;
                    296:     int i;
                    297:     int size = 0;
                    298:     int len = list_length(m);
                    299:     char **sp = malloc(len*sizeof(cmo *));
                    300:
                    301:     cell* cp = list_first(m);
                    302:     for(i = 0; i < len; i++) {
                    303:         sp[i] = new_string_set_cmo(cp->cmo);
                    304:         size += strlen(sp[i]) + 3;
                    305:         cp = list_next(cp);
                    306:     }
                    307:     s = malloc(size+2);
                    308:     strcpy(s, "[ ");
                    309:     for(i = 0; i < len - 1; i++) {
                    310:         strcat(s, sp[i]);
                    311:         strcat(s, " , ");
                    312:     }
                    313:     strcat(s, sp[len-1]);
                    314:     strcat(s, " ]");
                    315:     free(sp);
                    316:     return s;
                    317: }
                    318:
                    319: char *new_string_set_cmo(cmo *m)
                    320: {
                    321:     symbol_t symp;
                    322:     switch(m->tag) {
                    323:     case CMO_ZZ:
                    324:         return new_string_set_cmo_zz((cmo_zz *)m);
                    325:     case CMO_INT32:
                    326:         return new_string_set_cmo_int32(((cmo_int32 *)m)->i);
                    327:     case CMO_STRING:
                    328:         return ((cmo_string *)m)->s;
                    329:     case CMO_NULL:
                    330:         return new_string_set_cmo_null();
                    331:     case CMO_LIST:
                    332:         return new_string_set_cmo_list((cmo_list *)m);
                    333:     default:
                    334: #ifdef DEBUG
                    335:         symp = lookup_by_tag(m->tag);
1.4     ! ohara     336:         fprintf(ox_stderr, "I do not know how to convert %s to a string.\n", symp->key);
1.1       ohara     337: #endif
                    338:         /* yet not implemented. */
                    339:         return NULL;
                    340:     }
                    341: }
                    342:

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