void createtable(){ char buf1[64],buf2[64],c; int i,r,pos1=0,pos2=0; for(i=0;i<256;i++) hash[i]=NULL; FILE* f1=fopen("table.txt","r"); c=getc(f1); while(c!=EOF){ pos1=pos2=0; //to push a new word into buf while(c!=' ' && c!='\n'&& c!= EOF){ //used to fetch a word from table file buf1[pos1++]=c; c=getc(f1); } buf1[pos1]='\0'; c=getc(f1); while(c!=' ' && c!='\n' && c!= EOF){ //used to fetch a word from table file buf2[pos2++]=c; c=getc(f1); } buf2[pos2]='\0'; int val1=calchash(buf1); int val2=calchash(buf2); addtotable(buf1,val1,buf2,val2); c=getc(f1); }//read until end of table file }//createtable() close
void impCubeTables::makeTriStripPatterns(){ int i, j, k; int currentvertex; int currentedge; bool vertices[8]; // true if on low side of gradient (outside of surface) bool edges[12]; bool edgesdone[12]; int edgelist[7]; // final list of egdes used in a triangle strip int edgecount; // Set cubetable values to zero // A zero will indicate that there are no more triangle strips to build for(i=0; i<256; i++){ for(j=0; j<17; j++){ triStripPatterns[i][j] = 0; } } // For each vertex combination for(i=0; i<256; i++){ // identify the vertices on the low side of the gradient int andbit; for(j=0; j<8; j++){ andbit = 1; for(k=0; k<j; k++) andbit *= 2; if(i & andbit) vertices[j] = 1; else vertices[j] = 0; } // Identify the edges that cross threshold value // These are edges that connect 1 turned-on and 1 turned-off vertex for(j=0; j<12; j++){ if((vertices[ec[j][0]] + vertices[ec[j][1]]) == 1) edges[j] = 1; else edges[j] = 0; edgesdone[j] = 0; // no edges have been used yet } // Construct lists of edges that form triangle strips // try starting from each edge (no need to try last 2 edges) for(j=0; j<10; j++){ currentedge = j; edgecount = 0; // if this edge contains a surface vertex and hasn't been used while(edges[currentedge] && !edgesdone[currentedge]){ // add edge to list edgelist[edgecount] = currentedge; edgecount ++; edgesdone[currentedge] = 1; // find that edge's vertex on low side of gradient if(vertices[ec[currentedge][0]]) currentvertex = ec[currentedge][0]; else currentvertex = ec[currentedge][1]; // move along gradiant boundary to find next edge currentedge = nextedge(currentvertex, currentedge); while(!edges[currentedge]){ if(currentvertex != ec[currentedge][0]) currentvertex = ec[currentedge][0]; else currentvertex = ec[currentedge][1]; currentedge = nextedge(currentvertex, currentedge); } } // if a surface has been created add it to the table // and start over to try to make another surface if(edgecount) addtotable(i, edgecount, edgelist); } } }
void genobj( void ) { value_size token_size; action_n *actions, *parent, *other; base_n *base; token_n *p, *q, *r, *s; token_n *tokens, *same, *diff, *test, *best; set_size *mp; token_n tokval, dtoken, ptoken, ntoken; action_n actval, error, redun, new_action; a_sym *sym; a_pro *pro; an_item *item; a_state *x; a_shift_action *tx; a_reduce_action *rx; index_n i, j; set_size savings, min, *size; set_size shift; token_n parent_base; unsigned num_default, num_parent; if( fastflag ) { GenFastTables(); return; } if( bigflag || compactflag ) { token_size = FITS_A_WORD; } else { token_size = FITS_A_BYTE; } num_default = 0; num_parent = 0; ntoken = FirstNonTerminalTokenValue(); dtoken = ntoken++; ptoken = ntoken++; for( i = nterm; i < nsym; ++i ) { symtab[i]->token = ntoken++; } actions = CALLOC( ntoken, action_n ); error = nstate + npro; for( i = 0; i < ntoken; ++i ) { actions[i] = error; } tokens = CALLOC( ntoken, token_n ); test = CALLOC( ntoken, token_n ); best = CALLOC( ntoken, token_n ); other = CALLOC( nstate, action_n ); parent = CALLOC( nstate, action_n ); size = CALLOC( nstate, set_size ); base = CALLOC( nstate, base_n ); same = NULL; r = NULL; diff = NULL; used = 0; avail = 0; table = NULL; shift = 0; parent_base = 0; for( i = nstate; i > 0; ) { --i; x = statetab[i]; q = tokens; for( tx = x->trans; (sym = tx->sym) != NULL; ++tx ) { *q++ = sym->token; actions[sym->token] = tx->state->sidx; } savings = 0; for( rx = x->redun; (pro = rx->pro) != NULL; ++rx ) { redun = pro->pidx + nstate; mp = Members( rx->follow ); if( (set_size)( mp - setmembers ) > savings ) { savings = (set_size)( mp - setmembers ); r = q; } while( mp != setmembers ) { --mp; tokval = symtab[*mp]->token; *q++ = tokval; actions[tokval] = redun; } } if( savings ) { actval = actions[*r]; other[i] = actval; *q++ = dtoken; actions[dtoken] = actval; p = r; while( savings-- > 0 ) actions[*p++] = error; while( p < q ) *r++ = *p++; q = r; ++num_default; } else { other[i] = error; } r = q; min = (set_size)( q - tokens ); size[i] = min; parent[i] = nstate; for( j = nstate; --j > i; ) { if( abs( size[j] - size[i] ) < min ) { x = statetab[j]; p = test; q = test + ntoken; for( tx = x->trans; (sym = tx->sym) != NULL; ++tx ) { if( actions[sym->token] == tx->state->sidx ) { *p++ = sym->token; } else { *--q = sym->token; } } for( rx = x->redun; (pro = rx->pro) != NULL; ++rx ) { redun = pro->pidx + nstate; if( redun == other[j] ) redun = error; for( mp = Members( rx->follow ); mp != setmembers; ) { --mp; tokval = symtab[*mp]->token; if( actions[tokval] == redun ) { *p++ = tokval; } else { *--q = tokval; } } } if( other[j] != error ) { if( other[j] == other[i] ) { *p++ = dtoken; } else { *--q = dtoken; } } savings = (set_size)( size[i] + size[j] - 2 * ( p - test ) ); if( savings < min ) { min = savings; same = p; diff = q; s = test; test = best; best = s; parent[i] = j; } } } if( min >= size[i] ) { s = r; } else { ++num_parent; s = tokens; p = same; while( --p >= best ) actions[*p] = error; for( q = tokens; q < r; ++q ) { if( actions[*q] != error ) { *s++ = *q; } } p = best + ntoken; while( --p >= diff ) { if( actions[*p] == error ) { *s++ = *p; } } actval = parent[i]; *s++ = ptoken; actions[ptoken] = actval; } base[i] = addtotable( tokens, s, actions, dtoken, ptoken ); while( --s >= tokens ) { actions[*s] = error; } } FREE( actions ); FREE( tokens ); FREE( test ); FREE( best ); FREE( other ); FREE( size ); putambigs( base ); putnum( "YYNOACTION", error - nstate + used ); putnum( "YYEOFTOKEN", eofsym->token ); putnum( "YYERRTOKEN", errsym->token ); putnum( "YYETOKEN", errsym->token ); if( compactflag ) { parent_base = used + npro; putnum( "YYPARENT", parent_base ); shift = 8; for( i = 256; i < used; i <<= 1 ) { ++shift; } putnum( "YYPRODSIZE", shift ); } else { putnum( "YYPTOKEN", ptoken ); putnum( "YYDTOKEN", dtoken ); } putnum( "YYSTART", base[startstate->sidx] ); putnum( "YYSTOP", base[eofsym->enter->sidx] ); putnum( "YYERR", base[errstate->sidx] ); putnum( "YYUSED", used ); if( compactflag ) { begtab( "YYPACKTYPE", "yyacttab" ); j = nstate; for( i = 0; i < used; ++i ) { new_action = table[i].action; if( i == base[j - 1] ) { --j; // First element in each state is default/parent if( parent[j] == nstate ) { // No parent state tokval = used + parent_base; } else { tokval = base[parent[j]] + parent_base; } // 0 indicates no default if( new_action != 0 ) { if( new_action < nstate ) { // Shift new_action = base[new_action]; } else { // Reduce new_action -= nstate; // convert to 0 based new_action += used; // now convert to 'used' base } } } else { tokval = table[i].token; if( new_action < nstate ) { // Shift new_action = base[new_action]; } else { // Reduce new_action -= nstate; // convert to 0 based new_action += used; // now convert to 'used' base } } putcompact( tokval, new_action ); } endtab(); // Combine lengths & lhs into a single table begtab( "YYPRODTYPE", "yyprodtab" ); for( i = 0; i < npro; ++i ) { j = 0; for( item = protab[i]->items; item->p.sym != NULL; ++item ) { ++j; } puttab( FITS_A_WORD, (j << shift) + protab[i]->sym->token ); } endtab(); } else { begtab( "YYCHKTYPE", "yychktab" ); for( i = 0; i < used; ++i ) { puttab( token_size, Token( table + i ) ); } endtab(); begtab( "YYACTTYPE", "yyacttab" ); for( i = 0; i < used; ++i ) { j = Action( table + i ); if( j < nstate ) { puttab( FITS_A_WORD, base[j] ); } else { puttab( FITS_A_WORD, j - nstate + used ); } } endtab(); begtab( "YYPLENTYPE", "yyplentab" ); for( i = 0; i < npro; ++i ) { for( item = protab[i]->items; item->p.sym != NULL; ) { ++item; } puttab( FITS_A_BYTE, (unsigned)( item - protab[i]->items ) ); } endtab(); begtab( "YYPLHSTYPE", "yyplhstab" ); for( i = 0; i < npro; ++i ) { puttab( token_size, protab[i]->sym->token ); } endtab(); } FREE( table ); FREE( base ); FREE( parent ); dumpstatistic( "bytes used in tables", bytesused ); dumpstatistic( "states with defaults", num_default ); dumpstatistic( "states with parents", num_parent ); puttokennames( dtoken, token_size ); FREE( protab ); FREE( symtab ); }