vector<vector<int> > generateMatrix(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> oneline(n,0); vector<vector<int> > matrix( n, oneline); int i, j , k , l ,m; if (n == 0 ) return matrix; m = n; k = 0; l = 1; for ( k =0; k<n; k++){ for ( j = k ; j < n - k ; j ++ ){ matrix[k][j]= l; l ++; } for ( i = k+1; i < m - k ; i ++){ matrix[i][n-k-1] = l; l ++; } for ( j = n-k-2; j >= k; j --){ matrix[m-k-1][j] = l; l ++; } for ( i = m-k-2; i > k; i --){ matrix[i][k]= l; l ++; } } return matrix; }
char *actattr(const char *tty) { char *ret = (char*)0; char *path; if (!tty || *tty == '\0') goto err; if (asprintf(&path, "/sys/class/tty/%s/active", tty) < 0) goto err; if ((ret = oneline(path)) == (char*)0) goto out; out: free(path); err: return ret; }
int main(int argc, char ** argv) { long i; long num; if(argc < 2) { fprintf(stderr, "Number of lines required\n"); return 1; } srand(time(NULL)); num = atol(argv[1]); if(num <=0) { fprintf(stderr, "Bad number of lines specified\n"); return 1; } for(i=0; i<num; i++) { oneline(); } return 0; }
bool isInterleave(string s1, string s2, string s3) { // Start typing your C/C++ solution below // DO NOT write int main() function // for convenience ,add a space before string to // make sure the index starts at 1 rather than 0 s1 = ' ' + s1; s2 = ' ' + s2; s3 = ' ' + s3; int m,n, i , j; m = s1.length(); n = s2.length(); if ( (m+n-1) != s3.length() ) return false; vector <int> oneline(n+1,0); vector < vector<int> > dp(m+1,oneline); dp[0][0] = 1; for ( i= 0; i< m ; i++) for (j=0; j< n ; j++){ // s1[i] is at position i+j if (i>0) if (s1[i]==s3[i+j] && dp[i-1][j] == 1) dp[i][j] =1; //s2[j] is at position i+j if (j>0) if (s2[j]==s3[i+j] && dp[i][j-1] == 1) dp[i][j] =1; } return dp[m-1][n-1]; }
/* * Read and determine device attribute for tty below * /sys/class/tty. */ static dev_t devattr(const char *tty) { unsigned int maj, min; dev_t dev = 0; char *path, *value; if (!tty || *tty == '\0') goto err; if (asprintf(&path, "/sys/class/tty/%s/dev", tty) < 0) goto err; if ((value = oneline(path)) == (char*)0) goto out; if (sscanf(value, "%u:%u", &maj, &min) == 2) dev = makedev(maj, min); free(value); out: free(path); err: return dev; }
/* MAIN -------------------------------------------------------------------- */ int main(int argc, char **argv) { // For parallelization int rank, size, num_img_tot; int c,nn,imgno,opt_refno,iaux; double LL,sumw_allrefs,sumcorr; double aux,wsum_sigma_noise2, wsum_sigma_offset; std::vector<Matrix3D<double > > wsum_Mref; std::vector<Matrix3D<double > > wsum_Mwedge; std::vector<double> sumw; Matrix3D<double> Maux, Mauxbig; FileName fn_img,fn_tmp; Matrix1D<double> oneline(0); DocFile DFo,DFf; SelFile SFo,SFa; Prog_MLalign3D_prm prm; // Init Parallel interface MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); // Get input parameters try { // Read command line prm.read(argc,argv); // Select only relevant part of selfile for this rank mpiSelectPart(prm.SF, rank,size,num_img_tot); prm.produce_Side_info(); if (rank==0) prm.show(); else prm.verb=0; } catch (XmippError XE) {if (rank==0) {std::cout << XE; prm.usage();} MPI_Finalize(); exit(1);} try { Maux.resize(prm.dim,prm.dim,prm.dim); Maux.setXmippOrigin(); Mauxbig.resize(prm.bigdim,prm.bigdim,prm.bigdim); Mauxbig.setXmippOrigin(); DFo.reserve(2*prm.SF.ImgNo()+1); DFf.reserve(2*prm.SFr.ImgNo()+4); SFa.reserve(prm.Niter*prm.nr_ref); SFa.clear(); // Loop over all iterations for (int iter=prm.istart; iter<=prm.Niter; iter++) { if (prm.verb>0) std::cerr << " multi-reference refinement: iteration " << iter <<" of "<< prm.Niter<<std::endl; DFo.clear(); if (rank==0) DFo.append_comment("Headerinfo columns: rot (1), tilt (2), psi (3), Xoff (4), Yoff (5), Zoff (6), WedNo (7) Ref (8), Pmax/sumP (9)"); // Integrate over all images prm.ML_sum_over_all_images(prm.SF,prm.Iref,LL,sumcorr,DFo, wsum_Mref,wsum_Mwedge, wsum_sigma_noise2,wsum_sigma_offset,sumw); // Here MPI_allreduce of all wsums,LL and sumcorr !!! MPI_Allreduce(&LL,&aux,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); LL=aux; MPI_Allreduce(&sumcorr,&aux,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); sumcorr=aux; MPI_Allreduce(&wsum_sigma_offset,&aux,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); wsum_sigma_offset=aux; MPI_Allreduce(&wsum_sigma_noise2,&aux,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); wsum_sigma_noise2=aux; for (int refno=0;refno<prm.nr_ref; refno++) { MPI_Allreduce(MULTIDIM_ARRAY(wsum_Mref[refno]),MULTIDIM_ARRAY(Maux), MULTIDIM_SIZE(wsum_Mref[refno]),MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); wsum_Mref[refno]=Maux; MPI_Allreduce(MULTIDIM_ARRAY(wsum_Mwedge[refno]),MULTIDIM_ARRAY(Mauxbig), MULTIDIM_SIZE(wsum_Mwedge[refno]),MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); wsum_Mwedge[refno]=Mauxbig; MPI_Allreduce(&sumw[refno],&aux,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); sumw[refno]=aux; } // Update model parameters prm.update_parameters(wsum_Mref,wsum_Mwedge, wsum_sigma_noise2,wsum_sigma_offset, sumw,sumcorr,sumw_allrefs,iter); // All nodes write out temporary DFo fn_img.compose(prm.fn_root,rank,"tmpdoc"); DFo.write(fn_img); MPI_Barrier(MPI_COMM_WORLD); if (rank==0) { prm.write_output_files(iter,SFa,DFf,sumw_allrefs,sumw,LL,sumcorr); // Write out docfile with optimal transformation & references DFo.clear(); for (int rank2=0; rank2<size; rank2++) { fn_img.compose(prm.fn_root,rank2,"tmpdoc"); int ln=DFo.LineNo(); DFo.append(fn_img); DFo.locate(DFo.get_last_key()); DFo.next(); DFo.remove_current(); system(((std::string)"rm -f "+fn_img).c_str()); } fn_tmp=prm.fn_root+"_it"; fn_tmp.compose(fn_tmp,iter,"doc"); DFo.write(fn_tmp); } } // end loop iterations } catch (XmippError XE) {if (rank==0) {std::cout << XE; prm.usage();} MPI_Finalize(); exit(1);} MPI_Finalize(); return 0; }
int main() { // Read input image cv::Mat image= cv::imread("road.jpg",0); if (!image.data) return 0; // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); // Display the image cv::namedWindow("Original Image"); cv::imshow("Original Image",image); // Compute Sobel EdgeDetector ed; ed.computeSobel(image); // Display the Sobel orientation cv::namedWindow("Sobel (orientation)"); cv::imshow("Sobel (orientation)",ed.getSobelOrientationImage()); cv::imwrite("ori.bmp",ed.getSobelOrientationImage()); // Display the Sobel low threshold cv::namedWindow("Sobel (low threshold)"); cv::imshow("Sobel (low threshold)",ed.getBinaryMap(125)); // Display the Sobel high threshold cv::namedWindow("Sobel (high threshold)"); cv::imshow("Sobel (high threshold)",ed.getBinaryMap(350)); // Apply Canny algorithm cv::Mat contours; cv::Canny(image,contours,125,350); // Display the image of contours cv::namedWindow("Canny Contours"); cv::imshow("Canny Contours",255-contours); // Create a test image cv::Mat test(200,200,CV_8U,cv::Scalar(0)); cv::line(test,cv::Point(100,0),cv::Point(200,200),cv::Scalar(255)); cv::line(test,cv::Point(0,50),cv::Point(200,200),cv::Scalar(255)); cv::line(test,cv::Point(0,200),cv::Point(200,0),cv::Scalar(255)); cv::line(test,cv::Point(200,0),cv::Point(0,200),cv::Scalar(255)); cv::line(test,cv::Point(100,0),cv::Point(100,200),cv::Scalar(255)); cv::line(test,cv::Point(0,100),cv::Point(200,100),cv::Scalar(255)); // Display the test image cv::namedWindow("Test Image"); cv::imshow("Test Image",test); cv::imwrite("test.bmp",test); // Hough tranform for line detection std::vector<cv::Vec2f> lines; cv::HoughLines(contours,lines,1,PI/180,50); // Draw the lines cv::Mat result(contours.rows,contours.cols,CV_8U,cv::Scalar(255)); image.copyTo(result); std::cout << "Lines detected: " << lines.size() << std::endl; std::vector<cv::Vec2f>::const_iterator it= lines.begin(); while (it!=lines.end()) { float rho= (*it)[0]; // first element is distance rho float theta= (*it)[1]; // second element is angle theta if (theta < PI/4. || theta > 3.*PI/4.) { // ~vertical line // point of intersection of the line with first row cv::Point pt1(rho/cos(theta),0); // point of intersection of the line with last row cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows); // draw a white line cv::line( result, pt1, pt2, cv::Scalar(255), 1); } else { // ~horizontal line // point of intersection of the line with first column cv::Point pt1(0,rho/sin(theta)); // point of intersection of the line with last column cv::Point pt2(result.cols,(rho-result.cols*cos(theta))/sin(theta)); // draw a white line cv::line( result, pt1, pt2, cv::Scalar(255), 1); } std::cout << "line: (" << rho << "," << theta << ")\n"; ++it; } // Display the detected line image cv::namedWindow("Lines with Hough"); cv::imshow("Lines with Hough",result); // Create LineFinder instance LineFinder ld; // Set probabilistic Hough parameters ld.setLineLengthAndGap(100,20); ld.setMinVote(60); // Detect lines std::vector<cv::Vec4i> li= ld.findLines(contours); ld.drawDetectedLines(image); cv::namedWindow("Lines with HoughP"); cv::imshow("Lines with HoughP",image); std::vector<cv::Vec4i>::const_iterator it2= li.begin(); while (it2!=li.end()) { std::cout << "(" << (*it2)[0] << ","<< (*it2)[1]<< ")-(" << (*it2)[2]<< "," << (*it2)[3] << ")" <<std::endl; ++it2; } // Display one line image= cv::imread("road.jpg",0); // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); int n = 0; cv::line(image, cv::Point(li[n][0],li[n][1]),cv::Point(li[n][2],li[n][3]),cv::Scalar(255),5); cv::namedWindow("One line of the Image"); cv::imshow("One line of the Image",image); // Extract the contour pixels of the first detected line cv::Mat oneline(image.size(),CV_8U,cv::Scalar(0)); cv::line(oneline, cv::Point(li[n][0],li[n][1]),cv::Point(li[n][2],li[n][3]),cv::Scalar(255),3); cv::bitwise_and(contours,oneline,oneline); cv::namedWindow("One line"); cv::imshow("One line",255-oneline); std::vector<cv::Point> points; // Iterate over the pixels to obtain all point positions for( int y = 0; y < oneline.rows; y++ ) { uchar* rowPtr = oneline.ptr<uchar>(y); for( int x = 0; x < oneline.cols; x++ ) { // if on a contour if (rowPtr[x]) { points.push_back(cv::Point(x,y)); } } } // find the best fitting line cv::Vec4f line; cv::fitLine(points,line,CV_DIST_L2,0,0.01,0.01); std::cout << "line: (" << line[0] << "," << line[1] << ")(" << line[2] << "," << line[3] << ")\n"; int x0= line[2]; // a point on the line int y0= line[3]; int x1= x0+100*line[0]; // add a vector of length 100 int y1= y0+100*line[1]; image= cv::imread("road.jpg",0); // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); // draw the line cv::line(image,cv::Point(x0,y0),cv::Point(x1,y1),0,3); cv::namedWindow("Fitted line"); cv::imshow("Fitted line",image); // eliminate inconsistent lines ld.removeLinesOfInconsistentOrientations(ed.getOrientation(),0.4,0.1); // Display the detected line image image= cv::imread("road.jpg",0); // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); ld.drawDetectedLines(image); cv::namedWindow("Detected Lines (2)"); cv::imshow("Detected Lines (2)",image); // Create a Hough accumulator cv::Mat acc(200,180,CV_8U,cv::Scalar(0)); // Choose a point int x=50, y=30; // loop over all angles for (int i=0; i<180; i++) { double theta= i*PI/180.; // find corresponding rho value double rho= x*std::cos(theta)+y*std::sin(theta); int j= static_cast<int>(rho+100.5); std::cout << i << "," << j << std::endl; // increment accumulator acc.at<uchar>(j,i)++; } // draw the axes cv::line(acc, cv::Point(0,0), cv::Point(0,acc.rows-1), 255); cv::line(acc, cv::Point(acc.cols-1,acc.rows-1), cv::Point(0,acc.rows-1), 255); cv::imwrite("hough1.bmp",255-(acc*100)); // Choose a second point x=30, y=10; // loop over all angles for (int i=0; i<180; i++) { double theta= i*PI/180.; double rho= x*cos(theta)+y*sin(theta); int j= static_cast<int>(rho+100.5); acc.at<uchar>(j,i)++; } cv::namedWindow("Hough Accumulator"); cv::imshow("Hough Accumulator",acc*100); cv::imwrite("hough2.bmp",255-(acc*100)); // Detect circles image= cv::imread("chariot.jpg",0); // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); cv::GaussianBlur(image, image, cv::Size(5, 5), 1.5); std::vector<cv::Vec3f> circles; cv::HoughCircles(image, circles, CV_HOUGH_GRADIENT, 2, // accumulator resolution (size of the image / 2) 20, // minimum distance between two circles 200, // Canny high threshold 60, // minimum number of votes 15, 50); // min and max radius std::cout << "Circles: " << circles.size() << std::endl; // Draw the circles image= cv::imread("chariot.jpg",0); // image is resize for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6); std::vector<cv::Vec3f>::const_iterator itc = circles.begin(); while (itc!=circles.end()) { cv::circle(image, cv::Point((*itc)[0], (*itc)[1]), // circle centre (*itc)[2], // circle radius cv::Scalar(255), // color 2); // thickness ++itc; } cv::namedWindow("Detected Circles"); cv::imshow("Detected Circles",image); cv::waitKey(); return 0; }
//=================================== VERY DIRTY==COPY OF GRENG // but it knows to replace the specials;!! int64_t gr_engineX (const char* name, int rx, int ry, int rdx, int rdy) { double x[64000],y[64000],dy[64000],dx[64000], bu; int i,j; FILE * pFile; int MAXLINES=64000; // char mystring[1500];// one line TString oneline, title=name, token; // printf("gr_engineX: going to open filename=%s\n", name ); pFile=fopen( name ,"r" ); if (pFile==NULL) { printf("cannot open %s,STOPping\n", name ); return 0; } // error // printf("file opened\n%s",""); //.......................readout HERE.......i i=0; int lastlen;// remove spaces while ((i<MAXLINES)&&( feof(pFile)==0) ){ // printf(" reading line %d...\n", i ); if ( oneline.Gets (pFile, kTRUE) ){//chop true ... continue if not eof // printf(" string==%s\n", oneline.Data() ); //purify .................. start do { lastlen=oneline.Length(); if (oneline.Index(" ")==0){oneline=oneline(1,oneline.Length()-1);} lastlen=oneline.Length(); if (oneline.Index(" ")==lastlen){oneline=oneline(0,oneline.Length()-1);} oneline.ReplaceAll("\t"," "); oneline.ReplaceAll(" "," "); oneline.ReplaceAll(" "," "); // printf(" --- string==<%s> (%d)\n", oneline.Data(), lastlen ); }while( lastlen!=oneline.Length()); // printf(" string==<%s> (%d)\n", oneline.Data(), lastlen ); //purify .................. stop //printf("S=%s\n", oneline.Data() ); //........ parse oneline if ( (oneline.Index("#")==0) || (oneline.Index("@")==0) || (oneline.Index("END")==0) ){ // starts with # - COMMENT HERE // printf( "COMM:%s\n", oneline.Data() ); if (title.Length()==0){ title=oneline( 1,oneline.Length()-1 );} // title=oneline( oneline.Length(),oneline.Length()-1 ); }else{// DATA HERE TObjArray *tar; if (oneline.Length()>0){ // THAT WAS A HECK - 1digit! if (oneline.Index("#")>0){oneline.Remove(oneline.Index("#") );}// # if (oneline.Index("@")>0){oneline.Remove(oneline.Index("@") );}// # if (oneline.Index("END")>0){oneline.Remove(oneline.Index("END") );}// # // oneline.ReplaceAll(oneline.Index("\t"),1," "); oneline.ReplaceAll("\t"," "); x[i]=0; y[i] =0;dx[i]=0;dy[i]=0; j=0; // go through the columns // printf( "NotCM:%s\n", oneline.Data() ); /* * r?? contains a column to use for x,dx,y,dy: */ tar= oneline.Tokenize(" "); // printf("entries==%d\n", tar->GetEntries() ); while( (j<=rx && rx>=0) || (j<=ry && ry>=0) || (j<=rdy && rdy>=0)|| (j<=rdx && rdx>=0)){ if (j<tar->GetEntries()){ token= ((TObjString*)(tar->At(j)))->GetString(); //printf(" %3d %3d token <%s>\n", i,j, token.Data() ); bu= token.Atof(); // if (i<5){ printf(" token <%s> = %f\n", token.Data(), bu ); } if (rx==j) { x[i] =bu;} if (ry==j) { y[i] =bu;} if (rdx==j) { dx[i] =bu;} if (rdy==j) { dy[i] =bu;} }//j<entries j++; }//small while //tar->Delete(); // abandon tar i++;// skip to a next datum if point }// if oneline lenght>1 go thru all //back the loop }//DATA HERE end // printf("%s","data line ended\n"); }//if oneline.Gets false //........ parse oneline }//while //.......................readout HERE end....i //.......................close, create the TGraphErrors fclose( pFile); // TITLE HERE---------------- // title=name; DONE earlier title.ReplaceAll(".","_"); title.ReplaceAll(" ","_"); if (ry>1){title.Append( char(96+ry) ); } // multigraphs from mysql:columns printf("%d elements read. title= /%s/\n", i-1, title.Data() ); // // //IF ALREADY THE GRAPH EXISTS============+> if (gROOT->GetListOfSpecials()->FindObject(title.Data())!=NULL){ TGraphErrors *oldg=(TGraphErrors*)gROOT->GetListOfSpecials()->FindObject(title.Data()); printf(" ... same name already exists in specials tidis=%d\n", oldg->GetXaxis()->GetTimeDisplay() ); // printf("TIMEDISP %d\n",oldg->GetXaxis()->GetTimeDisplay() ); int timdis=oldg->GetXaxis()->GetTimeDisplay(); int lcol=oldg->GetLineColor(); char timc[100]; strcpy(timc,oldg->GetXaxis()->GetTimeFormat() ); for (int jj=0;jj<i;jj++){ //I believe that set 'new' point doesnot crash... oldg->SetPoint(jj,x[jj],y[jj]); oldg->SetPointError(jj,dx[jj],dy[jj]); }//for all jj //however if jj<graph points, it looks nasty while(i<oldg->GetN() ){ //printf("remove point %d < %d\n", i, oldg->GetN() ); oldg->RemovePoint(oldg->GetN()-1);//last? } // printf("TIMEDISP %d\n",oldg->GetXaxis()->GetTimeDisplay() ); oldg->GetXaxis()->SetTimeDisplay( timdis); oldg->GetXaxis()->SetTimeFormat(timc); //if this is already exists: oldg->SetLineColor( lcol) ; return (int64_t)oldg; }else{//if not exists in groot specials : TGraphErrors *g=new TGraphErrors(i,x,y,dx,dy); g->SetMarkerStyle(22); g->SetTitle( title.Data() ); g->SetName( title.Data() ); gROOT->GetListOfSpecials()->Add( g ); printf("added to GetListOfSpecials: %s \n", g->GetName() ); return (int64_t)g; }//if exists already in gROOT Specials // g->Print();// DONT PRINT TABLES //----------------------------------------------------- }// end of the new 201004 version of gr_engine
int64_t gr_engine (const char* name, int rx, int ry, int rdx, int rdy) { double x[64000],y[64000],dy[64000],dx[64000], bu; int i,j; FILE * pFile; int MAXLINES=64000; // char mystring[1500];// one line TString oneline, title=name, token; printf("i... gr_engine: going to open filename=%s\n", name ); pFile=fopen( name ,"r" ); if (pFile==NULL) { printf("cannot open %s,STOPping\n", name ); return 0; } // error // printf("file opened\n%s",""); //.......................readout HERE.......i i=0; int lastlen;// remove spaces while ((i<MAXLINES)&&( feof(pFile)==0) ){ // printf(" reading line %d...\n", i ); if ( oneline.Gets (pFile, kTRUE) ){//chop true ... continue if not eof // printf(" string==%s\n", oneline.Data() ); //purify .................. start do { lastlen=oneline.Length(); if (oneline.Index(" ")==0){oneline=oneline(1,oneline.Length()-1);} lastlen=oneline.Length(); if (oneline.Index(" ")==lastlen){oneline=oneline(0,oneline.Length()-1);} oneline.ReplaceAll("\t"," "); oneline.ReplaceAll(" "," "); oneline.ReplaceAll(" "," "); // printf(" --- string==<%s> (%d)\n", oneline.Data(), lastlen ); }while( lastlen!=oneline.Length()); // printf(" string==<%s> (%d)\n", oneline.Data(), lastlen ); //purify .................. stop //printf("S=%s\n", oneline.Data() ); //........ parse oneline if ( (oneline.Index("#")==0) || (oneline.Index("@")==0) || (oneline.Index("END")==0) ){ // starts with # - COMMENT HERE // printf( "COMM:%s\n", oneline.Data() ); if (title.Length()==0){ title=oneline( 1,oneline.Length()-1 );} // title=oneline( oneline.Length(),oneline.Length()-1 ); }else{// DATA HERE TObjArray *tar; if (oneline.Length()>0){ // 1digit also exist... if (oneline.Index("#")>0){oneline.Remove(oneline.Index("#") );}// # if (oneline.Index("@")>0){oneline.Remove(oneline.Index("@") );}// # if (oneline.Index("END")>0){oneline.Remove(oneline.Index("END") );}// # // oneline.ReplaceAll(oneline.Index("\t"),1," "); oneline.ReplaceAll("\t"," "); x[i]=0; y[i] =0;dx[i]=0;dy[i]=0; j=0; // go through the columns // printf( "NotCM:%s\n", oneline.Data() ); /* * r?? contains a column to use for x,dx,y,dy: */ tar= oneline.Tokenize(" "); // printf("entries==%d\n", tar->GetEntries() ); while( (j<=rx && rx>=0) || (j<=ry && ry>=0) || (j<=rdy && rdy>=0)|| (j<=rdx && rdx>=0)){ if (j<tar->GetEntries()){ token= ((TObjString*)(tar->At(j)))->GetString(); // printf(" %3d %3d token <%s>\n", i,j, token.Data() ); bu= token.Atof(); if (i<5){ printf("%3d. token <%s> = %f\n", i, token.Data(), bu ); } if (rx==j) { x[i] =bu;} if (ry==j) { y[i] =bu;} if (rdx==j) { dx[i] =bu;} if (rdy==j) { dy[i] =bu;} }//j<entries j++; }//small while //tar->Delete(); // abandon tar i++;// skip to a next datum if point }// if oneline lenght>1 go thru all //back the loop }//DATAH ERE end // printf("%s","data line ended\n"); }//if oneline.Gets false //........ parse oneline }//while //.......................readout HERE end....i //.......................close, create the TGraphErrors fclose( pFile); // TITLE HERE---------------- // title=name; DONE earlier title.ReplaceAll(".","_"); title.ReplaceAll(" ","_"); printf("i... %d elements read. title= /%s/\n", i-1, title.Data() ); TGraphErrors *g=new TGraphErrors(i,x,y,dx,dy); g->SetMarkerStyle(22); g->SetTitle( title.Data() ); g->SetName( title.Data() ); // g->Print();// DONT PRINT TABLES //========================ADDED 2013: AUTO!push to getlistofspecials (4multigraph) // if the name already exists: TObject *o=gROOT->GetListOfSpecials()->FindObject( g->GetName() ); if (o!=NULL){ printf("x... removing existing object named: /%s/\n", g->GetName() ); gROOT->GetListOfSpecials()->Remove( o ); } gROOT->GetListOfSpecials()->Add( g ); printf("added to GetListOfSpecials: %s \n", g->GetName() ); //========================= return (int64_t)g; //----------------------------------------------------- }// end of the new 201004 version of gr_engine
/* * Try to detect the real device(s) used for the system console * /dev/console if but only if /dev/console is used. On Linux * this can be more than one device, e.g. a serial line as well * as a virtual console as well as a simple printer. * * Returns 1 if stdout and stderr should be reconnected and 0 * otherwise. */ int detect_consoles(const char *device, int fallback) { int fd, ret = 0; #ifdef __linux__ char *attrib, *cmdline; FILE *fc; #endif if (!device || *device == '\0') fd = dup(fallback); else { fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); ret = 1; } if (fd >= 0) { DIR *dir; char *name; struct stat st; #ifdef TIOCGDEV unsigned int devnum; #endif if (fstat(fd, &st) < 0) { close(fd); goto fallback; } comparedev = st.st_rdev; if (ret && (fstat(fallback, &st) < 0 || comparedev != st.st_rdev)) dup2(fd, fallback); #ifdef __linux__ /* * Check if the device detection for Linux system console should be used. */ if (comparedev == makedev(TTYAUX_MAJOR, 0)) { /* /dev/tty */ close(fd); device = "/dev/tty"; goto fallback; } if (comparedev == makedev(TTYAUX_MAJOR, 1)) { /* /dev/console */ close(fd); goto console; } if (comparedev == makedev(TTYAUX_MAJOR, 2)) { /* /dev/ptmx */ close(fd); device = "/dev/tty"; goto fallback; } if (comparedev == makedev(TTY_MAJOR, 0)) { /* /dev/tty0 */ struct vt_stat vt; if (ioctl(fd, VT_GETSTATE, &vt) < 0) { close(fd); goto fallback; } comparedev = makedev(TTY_MAJOR, (int)vt.v_active); } #endif #ifdef TIOCGDEV if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); goto fallback; } comparedev = (dev_t)devnum; #endif close(fd); dir = opendir("/dev"); if (!dir) goto fallback; name = scandev(dir); if (name) consalloc(name); closedir(dir); if (!consoles) goto fallback; return ret; } #ifdef __linux__ console: /* * Detection of devices used for Linux system consolei using * the /proc/consoles API with kernel 2.6.38 and higher. */ if ((fc = fopen("/proc/consoles", "re"))) { char fbuf[16]; int maj, min; DIR *dir; dir = opendir("/dev"); if (!dir) { fclose(fc); goto fallback; } while ((fscanf(fc, "%*s %*s (%[^)]) %d:%d", &fbuf[0], &maj, &min) == 3)) { char * name; if (!strchr(fbuf, 'E')) continue; comparedev = makedev(maj, min); name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); fclose(fc); return ret; } /* * Detection of devices used for Linux system console using * the sysfs /sys/class/tty/ API with kernel 2.6.37 and higher. */ if ((attrib = actattr("console"))) { char *words = attrib, *token; DIR *dir; dir = opendir("/dev"); if (!dir) { free(attrib); goto fallback; } while ((token = strsep(&words, " \t\r\n"))) { char * name; if (*token == '\0') continue; comparedev = devattr(token); if (comparedev == makedev(TTY_MAJOR, 0)) { char *tmp = actattr(token); if (!tmp) continue; comparedev = devattr(tmp); free(tmp); } name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); free(attrib); if (!consoles) goto fallback; return ret; } /* * Detection of devices used for Linux system console using * kernel parameter on the kernels command line. */ if ((cmdline = oneline("/proc/cmdline"))) { char *words= cmdline, *token; DIR *dir; dir = opendir("/dev"); if (!dir) { free(cmdline); goto fallback; } while ((token = strsep(&words, " \t\r\n"))) { #ifdef TIOCGDEV unsigned int devnum; #else struct vt_stat vt; struct stat st; #endif char *colon, *name; if (*token != 'c') continue; if (strncmp(token, "console=", 8) != 0) continue; token += 8; if (strcmp(token, "brl") == 0) token += 4; if ((colon = strchr(token, ','))) *colon = '\0'; if (asprintf(&name, "/dev/%s", token) < 0) continue; if ((fd = open(name, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC)) < 0) { free(name); continue; } free(name); #ifdef TIOCGDEV if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); continue; } comparedev = (dev_t)devnum; #else if (fstat(fd, &st) < 0) { close(fd); continue; } comparedev = st.st_rdev; if (comparedev == makedev(TTY_MAJOR, 0)) { if (ioctl(fd, VT_GETSTATE, &vt) < 0) { close(fd); continue; } comparedev = makedev(TTY_MAJOR, (int)vt.v_active); } #endif close(fd); name = scandev(dir); if (!name) continue; consalloc(name); } closedir(dir); free(cmdline); /* * Detection of the device used for Linux system console using * the ioctl TIOCGDEV if available (e.g. official 2.6.38). */ if (!consoles) { #ifdef TIOCGDEV unsigned int devnum; const char *name; if (!device || *device == '\0') fd = dup(fallback); else fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); if (fd < 0) goto fallback; if (ioctl (fd, TIOCGDEV, &devnum) < 0) { close(fd); goto fallback; } comparedev = (dev_t)devnum; close(fd); if (device && *device != '\0') name = device; else name = ttyname(fallback); if (!name) name = "/dev/tty1"; consalloc(strdup(name)); if (consoles) { if (!device || *device == '\0') consoles->fd = fallback; return ret; } #endif goto fallback; } return ret; } #endif /* __linux __ */ fallback: if (fallback >= 0) { const char *name; if (device && *device != '\0') name = device; else name = ttyname(fallback); if (!name) name = "/dev/tty"; consalloc(strdup(name)); if (consoles) consoles->fd = fallback; } return ret; }
bool isMatch(const char *s, const char *p) { // Start typing your C/C++ solution below // DO NOT write int main() function //step: 1 compress string p ( string between stars could be compressed into one string) int m,n; int i , j , start, sp , pp,flag; m = 0; n = 0; while(s[m]!= NULL) m ++; while(p[n]!= NULL) n ++; //consider edge cases if ( m == 0 ){ for ( j = 0 ; j < n ; j ++) if (p[j]!= '*') return false; return true; } if ( n == 0) return false; //compress string p vector<int> position; vector<int> count; j = 0; //compress the string p while(p[j] != NULL){ start = j; while(p[j]!='*' && p[j]!=NULL){ j ++; } position.push_back(start); count.push_back(j-start); if ( count.back()== 0) j ++; } vector<int> oneline(position.size()+1, 0); vector<vector<int> > dp( m+1 , oneline ); //initialize dp[0][0] = 1; j = 0; while(p[j] == '*'){ dp[0][j+1] = 1; j ++; } for ( i = 1 ; i <= m; i ++)//the last NULL is taken into comparation for ( j = 1; j <= position.size(); j ++){ dp[i][j] = 0; if (p[position[j-1]] == '*'){ dp[i][j] = dp[i-1][j] || dp[i-1][j-1] || dp[i][j-1]; continue; } // now compare the string s[i-count[j]] to s[i] and p[position] to p[count+position] if (count[j-1] == 0) continue; sp = i-count[j-1]; pp = position[j-1]; if ( sp < 0 ) continue; if (dp[sp][j-1] == 0) continue; flag = 1; while(sp <= i-1){ if ( p[pp]!='?' && p[pp] !=s[sp] ){ flag = 0; break; } sp ++; pp ++; } dp[i][j] = flag; } return dp[m][position.size()]; }