// Check data from inode to see if it is in the buffer cache. int checki(struct inode *ip, int off) { if(off > ip->size) return -1; //cprintf("checki: calling bcheck\n"); return bcheck(ip->dev, bmap(ip, off/BSIZE, 0)); }
/* * Find a matching buffer in the block pool. As we have few buffers * we do a simple linear search. The buffer we return is locked so * that it can't vanish under the caller when we do sleeping block * devices. */ bufptr bfind(uint16_t dev, blkno_t blk) { bufptr bp; for (bp = bufpool; bp < bufpool_end; ++bp) { if (bp->bf_dev == dev && bp->bf_blk == blk) { /* FIXME: this check is only relevant for non sync stuff if it's sleeping then this is fine as we'll block here and sleep until the buffer is unlocked */ if (bcheck(bp)) panic(PANIC_WANTBSYB); block(bp); return bp; } } return NULL; }
string longestPalindrome(string s) { gs = ""; for(int i = 0; i < s.length(); i++) { gs += (i>0 ? "#" : ""); gs += s[i]; } build_hash(); int ansp = -1, ansl = -1; for(int i = 0; i < gs.length(); i++) { int tl = bcheck(i); if(tl > ansl || tl == ansl && i%2!=0) { ansp = i, ansl = tl; } } string ans = ""; for(int i = ansp-ansl; i <= ansp+ansl; i++) { if(i%2 == 0) ans += gs[i]; } return ans; }
void evali(const char *str, char *err, int *eval) { // Mathematical Expression Evaluation Function // ----------------------------------------------- // This functions solves mathematical equations. // When a complex equation is passed via *str, // the equation is broken into parts, and the // simplest part is passed on recursively onto // the function itself. This recurvise process is // repeated until the whole equation has been solved. // Results of the evaluation are stored in int eval. if(!bcheck(str)) { /* this is the core of eval where the calculations are done. at this level, the equation does not have any brackets. */ const char symbols[]="^*/%+-&"; const char se[]="Invalid Syntax"; char *tmp = NULL; if((tmp=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *tmp='\0'; /* check wether str has reached the absolute stage */ if(prechar(tmp,str,symbols)==0) { printf("\n[simple]"); if(!ncheck(str)) { printf("\nequation solved!"); *eval = strint(str); free(tmp); tmp = NULL; return; } else if(!id_check(str)) { printf("\nit's a variable!"); free(tmp); tmp = NULL; return; } else { strcpy(err,se); stradd(err,": "); stradd(err,str); free(tmp); tmp = NULL; return; } } else /* there are symbols in str */ { free(tmp); tmp = NULL; } /* now the real maths */ printf("\n[complex]"); char *pre = NULL; /* string preceding of operator */ char *pos = NULL; /* string succeding the operator */ /* now allocate the variables */ if((pre=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *pre='\0'; if((pos=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *pos='\0'; char symbol = 0; if(prechar(pre,str,"^")) { if(postchar(pos,str,"^")) symbol = '^'; } else if(prechar(pre,str,"*")) { if(postchar(pos,str,"*")) symbol = '*'; } else if(prechar(pre,str,"/")) { if(postchar(pos,str,"/")) symbol = '/'; } else if(prechar(pre,str,"%")) { if(postchar(pos,str,"%")) symbol = '%'; } else if(prechar(pre,str,"+")) { if(postchar(pos,str,"+")) symbol = '+'; } else if(prechar(pre,str,"-")) { if(postchar(pos,str,"-")) symbol = '-'; } else if(prechar(pre,str,"&")) { if(postchar(pos,str,"&")) symbol = '&'; } char *ax = NULL; /* value preceding of operator */ char *bx = NULL; /* value succeding the operator */ char *cx = NULL; /* value of ax and bx processed */ /* now allocate the variables */ if((ax=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *ax='\0'; if((bx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *bx='\0'; if((cx=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *cx='\0'; /* find out the contents of bx */ char *ebx = NULL; /* temp string to build bx */ if((ebx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *ebx='\0'; strcpy(bx,pos); strcpy(ebx,bx); for(;;) /* infinite loop */ { if(!prechar(bx,ebx,symbols)) { strcpy(bx,ebx); free(ebx); ebx = NULL; /* de-allocate ebx */ break; } else /* here ebx is build */ strcpy(ebx,bx); } /* find out the contents of ax */ char *eax = NULL; /* temp string to build ax */ if((eax=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *eax='\0'; strcpy(ax,pre); strcpy(eax,ax); for(;;) /* infinite loop */ { if(!postchar(ax,eax,symbols)) { strcpy(ax,eax); free(eax); eax = NULL; /* de-allocate eax */ break; } else /* here eax is build */ strcpy(eax,ax); } /* variables to store (pre-ax) and (pre-bx) */ char *prex = NULL; /* string of (pre-ax) */ char *posx = NULL; /* string of (pos-ax) */ /* now allocate prex and posx */ if((prex=(char *)(malloc(sizeof(char)* (strlen(pre)+1))))==NULL) allocerr(); *prex='\0'; if((posx=(char *)(malloc(sizeof(char)* (strlen(pos)+1))))==NULL) allocerr(); *posx='\0'; /* find prex and posx */ strlft(prex,pre,(strlen(pre)-strlen(ax))); strrht(posx,pos,(strlen(pos)-strlen(bx))); /* de-allocate pre & pos */ printf("\nsym: %c",symbol); printf("\npre: %s",pre); printf("\npos: %s",pos); free(pre); pre = NULL; free(pos); pos = NULL; /* process ax and bx to find cx */ // ***************** /* de-allocate ax & bx */ printf("\n*ax: %s",ax); printf("\n*bx: %s",bx); printf("\n*cx: %s",cx); printf("\nprx: %s",prex); printf("\npox: %s",posx); free(ax); ax = NULL; free(bx); bx = NULL; /* variable to store one-step solved equation */ char *ex = NULL; if((ex=(char *)(malloc(sizeof(char)* (strlen(str)+1))))==NULL) allocerr(); *ex='\0'; /* find ex using cx in prex and posx */ // ***************** /* now de-allocate cx, prex & posx */ free(cx); cx = NULL; free(prex); cx = NULL; free(posx); cx = NULL; /* recurse ex on eval for next-step solving */ // ***************** /* de-allocate ex & return */ free(ex); ex = NULL; return; } else { if(!bcount(str)) { printf("\nEquation has brackets."); return; } else { strcpy(err,"Illegal Equation, inequal number of brackets."); return; } } }
int PartialFactorizationOneStep( const char* AmesosClass, const Epetra_Comm &Comm, bool transpose, bool verbose, Teuchos::ParameterList ParamList, Epetra_CrsMatrix *& Amat, double Rcond, int Steps ) { assert( Steps >= 0 && Steps < MaxNumSteps ) ; int iam = Comm.MyPID() ; int errors = 0 ; const Epetra_Map *RangeMap = transpose?&Amat->OperatorDomainMap():&Amat->OperatorRangeMap() ; const Epetra_Map *DomainMap = transpose?&Amat->OperatorRangeMap():&Amat->OperatorDomainMap() ; Epetra_Vector xexact(*DomainMap); Epetra_Vector x(*DomainMap); Epetra_Vector b(*RangeMap); Epetra_Vector bcheck(*RangeMap); Epetra_Vector difference(*DomainMap); Epetra_LinearProblem Problem; Amesos_BaseSolver* Abase ; Amesos Afactory; Abase = Afactory.Create( AmesosClass, Problem ) ; std::string AC = AmesosClass ; if ( AC == "Amesos_Mumps" ) { ParamList.set( "NoDestroy", true ); Abase->SetParameters( ParamList ) ; } double relresidual = 0 ; if ( Steps > 0 ) { // // Phase 1: Compute b = A' A' A xexact // Problem.SetOperator( Amat ); // // We only set transpose if we have to - this allows valgrind to check // that transpose is set to a default value before it is used. // if ( transpose ) OUR_CHK_ERR( Abase->SetUseTranspose( transpose ) ); // if (verbose) ParamList.set( "DebugLevel", 1 ); // if (verbose) ParamList.set( "OutputLevel", 1 ); if ( Steps > 1 ) { OUR_CHK_ERR( Abase->SetParameters( ParamList ) ); if ( Steps > 2 ) { xexact.Random(); xexact.PutScalar(1.0); // // Compute cAx = A' xexact // Amat->Multiply( transpose, xexact, b ) ; // b = A x2 = A A' A'' xexact #if 0 std::cout << __FILE__ << "::" << __LINE__ << "b = " << std::endl ; b.Print( std::cout ) ; std::cout << __FILE__ << "::" << __LINE__ << "xexact = " << std::endl ; xexact.Print( std::cout ) ; std::cout << __FILE__ << "::" << __LINE__ << "x = " << std::endl ; x.Print( std::cout ) ; #endif // // Phase 2: Solve A' A' A x = b // // // Solve A sAAx = b // Problem.SetLHS( &x ); Problem.SetRHS( &b ); OUR_CHK_ERR( Abase->SymbolicFactorization( ) ); if ( Steps > 2 ) { OUR_CHK_ERR( Abase->SymbolicFactorization( ) ); if ( Steps > 3 ) { OUR_CHK_ERR( Abase->NumericFactorization( ) ); if ( Steps > 4 ) { OUR_CHK_ERR( Abase->NumericFactorization( ) ); if ( Steps > 5 ) { OUR_CHK_ERR( Abase->Solve( ) ); if ( Steps > 6 ) { OUR_CHK_ERR( Abase->Solve( ) ); Amat->Multiply( transpose, x, bcheck ) ; // temp = A" x2 double norm_diff ; double norm_one ; difference.Update( 1.0, x, -1.0, xexact, 0.0 ) ; difference.Norm2( &norm_diff ) ; x.Norm2( &norm_one ) ; relresidual = norm_diff / norm_one ; if (iam == 0 ) { if ( relresidual * Rcond > 1e-16 ) { if (verbose) std::cout << __FILE__ << "::"<< __LINE__ << " norm( x - xexact ) / norm(x) = " << norm_diff /norm_one << std::endl ; errors += 1 ; } } } } } } } } } } delete Abase; return errors; }
int main() { char txt[TamMaxTexto]; char auxTxt[TamMaxTexto]; char bits[TamMaxComprimido]; int tamTxt, tamAuxTxt, numBits; /* Lê o texto de entrada */ if (!LeTexto(txt, &tamTxt)) { printf("Problemas com o arquivo de entrada\n"); return 0; } printf("\nTexto original:\n"); printf("--------------------------------------------------\n"); Escreve(txt,tamTxt); printf("--------------------------------------------------\n"); /* Inicializa estruturas */ if(ConstroiHuffman(txt, tamTxt)) { printf("\nÁrvore de Huffman:\n\n"); ImprimeHuffman(); } else { printf("\nNão foi possível construir a árvore\n"); return 0; } if (!Comprime(txt, tamTxt, bits, &numBits, TamMaxComprimido)) { printf("Problemas na compressão\n"); return 0; } printf("\nTexto comprimido:\n"); EscreveBits(bits, numBits); printf("\n"); if (!Descomprime(auxTxt,&tamAuxTxt,bits,numBits,TamMaxTexto)) { printf("Problemas na descompressão\n"); return 1; } printf("\nTexto descomprimido:\n"); printf("--------------------------------------------------\n"); Escreve(auxTxt, tamAuxTxt); printf("--------------------------------------------------\n"); if (tamTxt == tamAuxTxt && strncmp(txt,auxTxt,tamTxt)==0) printf("\nTextos idênticos\n"); else { printf("\nTextos diferentes\n"); return 0; } printf("\nTexto original: %d bytes\n", tamTxt); printf("Cadeia comprimida: %d bits (%d bytes)\n", numBits, numBits % 8 == 0 ? numBits/8 : numBits/8 + 1); printf("Média: %2.1f bits/caractere\n", (float)numBits/(float)tamTxt); LiberaHuffman(); /* Finalização */ int bch = bcheck(); /* Verifica liberação correta de memória dinâmica */ if (bch!=0) { printf("\n%d blocos de memória dinâmica não foram liberados:\n",bch); bapply(bprint); } else{ printf("\nA memória dinâmica foi totalmente liberada\n" ); } printf("\nProcessamento terminado\n"); return 0; }