Esempio n. 1
0
int main ( int /*argc*/, char * /*argv*/ [] ) {
//  In search.hpp, there are generic implementations of three classic sequence search
//  algorithms. They all have the same (dual) interface.

//  There is a procedural interface, based on std::search:
    if ( ba::boyer_moore_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != haystack.end ())
        std::cout << "Found '" << needle1 << "'  in '" << haystack << "' (boyer-moore 1)" << std::endl;
    else
        std::cout << "Did NOT find '" << needle1 << "'  in '" << haystack << "' (boyer-moore 1)" << std::endl;

//  If you plan on searching for the same pattern in several different data sets,
//  you can create a search object and use that over and over again - amortizing the setup
//  costs across several searches
    ba::boyer_moore<std::string::const_iterator> search1 ( needle1.begin (), needle1.end ());
    if ( search1 ( haystack.begin (), haystack.end ()) != haystack.end ())
        std::cout << "Found '" << needle1 << "'  in '" << haystack << "' (boyer-moore 2)" << std::endl;
    else
        std::cout << "Did NOT find '" << needle1 << "'  in '" << haystack << "' (boyer-moore 2)" << std::endl;

//  There is also an implementation of boyer-moore-horspool searching
    if ( ba::boyer_moore_horspool_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != haystack.end ())
        std::cout << "Found '" << needle1 << "'  in '" << haystack << "' (boyer-moore-horspool)" << std::endl;
    else
        std::cout << "Did NOT find '" << needle1 << "'  in '" << haystack << "' (boyer-moore-horspool)" << std::endl;

//  And also the knuth-pratt-morris search algorithm
    if ( ba::knuth_morris_pratt_search ( haystack.begin (), haystack.end (), needle1.begin (), needle1.end ()) != haystack.end ())
        std::cout << "Found '" << needle1 << "'  in '" << haystack << "' (knuth_morris_pratt)" << std::endl;
    else
        std::cout << "Did NOT find '" << needle1 << "'  in '" << haystack << "' (knuth_morris_pratt)" << std::endl;

    return 0;
    }
Esempio n. 2
0
const int *index_search_item::search(const char *ptr, int length,
				     int **temp_listp)
{
  const char *end = ptr + length;
  if (*temp_listp) {
    a_delete *temp_listp;
    *temp_listp = 0;
  }
  const int *first_list = 0;
  while (ptr < end && (first_list = search1(&ptr, end)) == 0)
    ;
  if (!first_list)
    return 0;
  if (*first_list < 0)
    return first_list;
  const int *second_list = 0;
  while (ptr < end && (second_list = search1(&ptr, end)) == 0)
    ;
  if (!second_list)
    return first_list;
  if (*second_list < 0)
    return second_list;
  const int *p;
  for (p = first_list; *p >= 0; p++)
    ;
  int len = p - first_list;
  for (p = second_list; *p >= 0; p++)
    ;
  if (p - second_list < len)
    len = p - second_list;
  int *matches = new int[len + 1];
  merge(matches, first_list, second_list);
  while (ptr < end) {
    const int *list = search1(&ptr, end);
    if (list != 0) {
      if (*list < 0) {
	a_delete matches;
	return list;
      }
      merge(matches, matches, list);
      if (*matches < 0) {
	a_delete matches;
	return &minus_one;
      }
    }
  }
  *temp_listp = matches;
  return matches;
}
Esempio n. 3
0
void del(vector<Train*>* data, string des)
{
	int pos = search1(data, des, 0, (*data).size()-1);
	if (pos == -1) return;                          //要加入输入错误
	else if ((*data)[pos]->flag == 0) (*data).erase(data->begin() + pos);          //要加入 票已售出,无法删除
	return;
}
Esempio n. 4
0
int main(int argc, char** argv){
    std::string targetstr = "sony";
    if(argc>1)targetstr = argv[1];

    std::string codestr = "";
    if(argc>2){
        codestr = argv[2];
    }
    else{
        codestr = codegen();
    }


	std::clock_t s = std::clock();
	std::list<std::vector<long long> > anslist = search1(codestr, targetstr);
    anslist.sort();
	std::clock_t e = std::clock();

    for(std::list<std::vector<long long> >::iterator it = anslist.begin(); it != anslist.end(); it++){
      std::cout<<(*it)[0]<<","<<(*it)[1]<<std::endl;
    }

	std::cout<<"time="<<(double)(e-s)/CLOCKS_PER_SEC<<std::endl;
	std::cout<<"ansnum="<<anslist.size()<<std::endl;

}
Esempio n. 5
0
unsigned CInterpolation::findBox( const std::vector<double>& pos ){
  plumed_dbg_massert( pos.size()==np.size(), "position size does not match the size of the grid");

  unsigned jold, ccf_box, bnew=0;
  for(unsigned i=0;i<np.size();++i){
     jold=static_cast<int>( std::floor( double(bold)/double(stride[i]) ) );
     bold-=jold*stride[i];
     ccf_box=search1( i, pos[i], jold );
     bnew+=ccf_box; 
  }
  plumed_dbg_assert( bold==0 ); bold=bnew;
  for(unsigned i=0;i<np.size();++i){ lb[i]=splinepoints(bold,i); ub[i]=splinepoints(bold+stride[i],i); }
  return bold;
}
void search1(int cur)
{

	if(cur == MAX)
	{
		tot++;
	} else
	{	
		for(i = 0; i < MAX; i++)
		{
			if(!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+MAX])
			{	
				//利用二维数组直接判断,依次为列,主对角线,副对角线
				C[cur] = i;				//如果不用打印解,整个c数组都可以忽略
				vis[0][i] = vis[1][cur+i] = vis[2][cur-i+MAX] = 1;//修改全局变量
				search1(cur+1);
				vis[0][i] = vis[1][cur+i] = vis[2][cur-i+MAX] = 0;//切记,一定要改回来
			}
		}
	}
}
Esempio n. 7
0
void DEL::on_pushButton_clicked()
{
	QString a = ui.lineEdit->text();
	string b = a.toStdString();
	int pos = search1(&traindata, b, 0, (traindata).size()-1);
	if (pos == -1)
	{
		QMessageBox::information(this, QString(u8"错误"), QString(u8"<font size=5>该车次不存在,请重新输入</font>"));
		ui.lineEdit->clear();
		return;
	}
	else if (traindata[pos]->flag == 1)
	{
		QMessageBox::information(this, QString(u8"错误"), QString(u8"<font size=5>该车次票已售出,无法删除</font>"));
		ui.lineEdit->clear();
		return;
	}
	else
	{
		ui.label_3->setText(QString(u8"车次存在,是否删除?"));
		ui.okButton->show();
	}
	return;
}
Esempio n. 8
0
void adajoint_chr(char **R_file_prefix, int *R_method,
int *R_nperm, int *R_seed, 
int *R_nthread, int *R_nsnp, int *R_ngene, 
double *R_vU, double *R_score0, double *R_vV, 
int *R_vgene_idx, int *R_gene_start, int *R_gene_end, 
int *R_vgene_cutpoint, 
int *R_gene_cutpoint_start, int *R_gene_cutpoint_end, 
double *R_gene_pval, int *R_arr_rank, 
int *R_sel_id, int *R_marg_id){
  
  int len_file_prefix = strlen(*R_file_prefix);
  char *file_prefix = new char[len_file_prefix + 1];
  file_prefix[0] = '\0';
  strcat(file_prefix, *R_file_prefix);
  
  int method = *R_method;
  assert(method == 1 || method == 2);
  
  int nperm = *R_nperm;
  int seed = *R_seed;
  int nthread = *R_nthread;
  int nsnp = *R_nsnp;
  int ngene = *R_ngene;
  
  fvec score0;
  fmat V;
  fmat U;
  load_score0(R_score0, score0, nsnp);
  load_cov(R_vV, V, nsnp);
  load_U(R_vU, U, nsnp);
  
  imat gene_idx; // index of SNPs in a gene
  
  load_gene_idx(R_vgene_idx, R_gene_start, R_gene_end, 
  gene_idx, ngene);
  
  imat cutpoint;
  load_gene_cutpoint(R_vgene_cutpoint, R_gene_cutpoint_start, R_gene_cutpoint_end, 
  cutpoint, ngene);
  
  string fprefix (file_prefix);
  svec gene_out (ngene, fprefix);
  for(int g = 0; g < ngene; ++g){
    ostringstream gid;
    gid << g;
    gene_out[g] = gene_out[g] + string("GID.") + gid.str() + string(".bin");
  }
  
  // write obs statistics for all genes
  imat sel_id(ngene);
  ivec marg_id(ngene);
  for(int g = 0; g < ngene; ++g){
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
  	if(!gout){
  		error("Fail to write observed statistics to file");
  	}
  	
  	fvec S;
  	fmat Sigma;
  	extract_score(S, score0, gene_idx[g]);
  	extract_cov(Sigma, V, gene_idx[g]);
  	fvec s;
    int ncp = cutpoint[g].size();
    int mc = cutpoint[g][ncp - 1];
    
    if(method == 1){
      search1(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }else{//assert(method == 2)
      search2(s, sel_id[g], marg_id[g], S, Sigma, mc);
    }
    
    for(int k = 0; k < ncp; ++k){
      float u = s[cutpoint[g][k]];
      gout.write((char*)(&u), sizeof(u));
    }
  	gout.close();
  }
  
  int i_sel_id = -1;
  for(int g = 0; g < ngene; ++g){
    R_marg_id[g] = gene_idx[g][marg_id[g]] + 1;
    for(int k = 0; k < sel_id[g].size(); ++k){
      ++i_sel_id;
      R_sel_id[i_sel_id] = gene_idx[g][sel_id[g][k]] + 1;
    }
    int nn = gene_idx[g].size() - sel_id[g].size();
    while(nn){
      ++i_sel_id;
      R_sel_id[i_sel_id] = -1;
      --nn;
    }
  }
  
  int ngap = min(10000, nperm);
  int nblock = nperm / ngap;
  
  for(int b = 0; b < nblock; ++b){
  	fmat null(ngap, fvec (nsnp, .0f));
  	drand48_data buf;
  	// compute null score
  	#pragma omp parallel num_threads(nthread) private(buf)
  	{
  		srand48_r(seed + b * nthread + omp_get_thread_num(), &buf);
  		#pragma omp for
	  	for(int i = 0; i < ngap; ++i){
	  		fvec rn;
	  		rnorm(buf, nsnp, rn);
	  		for(int j = 0; j < nsnp; ++j){
	  			null[i][j] = .0f;
	  			for(int k = 0; k < nsnp; ++k){
	  				null[i][j] += rn[k] * U[k][j];
	  			}
	  		}
	  	}
	  }
	  
	  // write null statistics to local files (per gene)
	  #pragma omp parallel num_threads(min(nthread, ngene))
	  {
	  	#pragma omp for
	  	for(int g = 0; g < ngene; ++g){
	  		ofstream gout;
	  		gout.open(gene_out[g].c_str(), ios::out | ios::binary | ios::app);
	  		if(!gout){
	  			error("Fail to write null statistics to file");
	  		}
	  		
	  		fmat Sigma;
	  		extract_cov(Sigma, V, gene_idx[g]);
	  		
        int ns = gene_idx[g].size();
        int ncp = cutpoint[g].size();
        int mc = cutpoint[g][ncp - 1];
	  		for(int i = 0; i < ngap; ++i){
	  			fvec S;
	  			extract_score(S, null[i], gene_idx[g]);
	  			
	  			fvec s;
          ivec sel_id;
          int marg_id;
          if(method == 1){
            search1(s, sel_id, marg_id, S, Sigma, mc);
          }else{
            search2(s, sel_id, marg_id, S, Sigma, mc);
          }
          
          for(int k = 0; k < ncp; ++k){
            float u = s[cutpoint[g][k]];
            gout.write((char*)(&u), sizeof(u));
          }
	  		}
	  		gout.close();
	  	}
	  }
	  //fmat().swap(null);
  }
  
  // read null statistics (per gene)
  int irk = -1;
  for(int g = 0; g < ngene; ++g){
  	int ncp = cutpoint[g].size();
  	vector<VecStat> stat(ncp, VecStat (nperm + 1, STAT0));
    fstream gin(gene_out[g].c_str(), ios::in | ios::binary);
    
  	for(int i = 0; i < nperm + 1; ++i){
  		for(int j = 0; j < ncp; ++j){
  			float s = .0f;
  			gin.read((char*)(&s), sizeof(s));
  			stat[j][i].stat = s;
  			stat[j][i].id = i;
  		}
  	}
  	gin.close();
    
    if(remove(gene_out[g].c_str())){
      error("Cannot delete gene output file");
    }
  	
  	imat arr_rank(ncp, ivec (nperm + 1, 0));
  	#pragma omp parallel num_threads(min(ncp, nthread))
  	{
      #pragma omp for
  		for(int j = 0; j < ncp; ++j){
  			sort(stat[j].begin(), stat[j].end(), descending);
  			for(int i = 0; i < nperm + 1; ++i){
  				int id = stat[j][i].id;
  				arr_rank[j][id] = i;
  			}
  		}
  	}
  	
  	vector<VecStat>().swap(stat);
    
    ivec gene_min_p (nperm + 1, -1);
    ivec subsum(nthread, 0);
    ivec subtie(nthread, 0);
    int m = nperm + 1;
    for(int j = 0; j < ncp; ++j){
      ++irk;
      R_arr_rank[irk] = arr_rank[j][0];
      if(arr_rank[j][0] < m){
        m = arr_rank[j][0];
      }
    }
    gene_min_p[0] = m;
    
    #pragma omp parallel num_threads(nthread)
    {
      #pragma omp for
      for(int i = 1; i < nperm + 1; ++i){
        int tid = omp_get_thread_num();
        int m = nperm + 1;
        for(int j = 0; j < ncp; ++j){
          if(arr_rank[j][i] < m){
            m = arr_rank[j][i];
          }
        }
        gene_min_p[i] = m;
        if(gene_min_p[i] < gene_min_p[0]){
          subsum[tid] += 1;
        }else if(gene_min_p[i] == gene_min_p[0]){
          subtie[tid] += 1;
        }else{
          ;
        }
      }
    }
    
    R_gene_pval[g] = 1.0;
    int rep = 0;
    for(int t = 0; t < nthread; ++t){
      R_gene_pval[g] += subsum[t];
      rep += subtie[t];
    }
    R_gene_pval[g] += rep / 2.0;
    R_gene_pval[g] /= nperm + 1;
    
    fstream gout(gene_out[g].c_str(), ios::out | ios::binary);
    if(!gout){
      error("Fail to write gene statistics to file");
    }
    for(int i = 0; i < nperm + 1; ++i){
      gout.write((char*)(&(gene_min_p[i])), sizeof(gene_min_p[i]));
    }
    gout.close();
    
  }
  
  
  delete[] file_prefix;
  
}
int search(int A[], int n, int target) {
    if (random()%2){
        return search1(A, n, target);
    }
    return search2(A, n, target);
}
Esempio n. 10
0
void preoptim(int nv)
{
int answ,k,i,s,gdrive=VGA,gmode=2;
long n[3];
double c[16],pv[16];
extern int npexp;
extern double pexpx[],pexpy[],pexpz[];
extern char namesch[],statpexp[];
int grafcond(void);
void grafica(int, double *pv);
void inspect(double *pv);
int search(long nsteps,long npoints,int nv,double *pv,double *c);
int search1(long nsteps,long npoints,double *a,double *c);
void searchpar(long *n);
void eliminate(void);
void initial(int nv,double *pv,double *c);
void initial1(int nv,double *pv,double *c);
int npgr;
double correlation(double *pv);
	for(i=0;i<npexp;i++)
		statpexp[i]=1;
	npgr=grafcond();
	initgraph(&gdrive,&gmode,"");
	answ=5993;
	do
	{
		switch(answ)
		{
			case 5993:
				initial(nv,pv,c);
			case 6512:
				n[2]=long(nv);
				searchpar(n);
			case 11875:
				if(nv==1)
					s=search1(n[1],n[0],pv,c);
				else
					s=search(n[1],n[0],nv,pv,c);
				switch(s)
				{
					case 1:
						puts("This is what you are searching for!");
					break;
					case 2:
						puts("Continue ! Modify parameters with <p> if you want.");
					break;
					case 0:
						if(nv==1)
						{
							puts("Raise npoints from <p> !");
							break;
						}
					default:
						puts("Something is bad !");
						return;
				}
				break;
			case 12654:
				inspect(pv);
				break;
			case 561:
				initial1(nv,pv,c);
				break;
			case 4978:
				printf("correlation coefficient = %f\n",correlation(pv));
				break;
			case 7777:
				npgr=grafcond();
			case 8807:
				puts("YOU MUST WAIT A LITTLE !");
				grafica(npgr,pv);
				break;
			case 4709:
				eliminate();
				break;
			default:
				puts("	Initialization        search Parameters      Continue");
				puts("	Eliminate/insert      iNspect                coRrelation");
				puts("	Graphics              graph quAlity          Stop");
				printf("	(i/p/c\n	 e/n/r\n	 g/a/s)\n ");
		}
		flushall();
		answ=bioskey(0);
	}
	while(answ!=8051);
	closegraph();
	puts("FINAL VALUES :");
	for(k=0;k<nv;k++)
	{
		printf("%2d  ",k);
		for(i=0;i<10;i++)
			printf("%c",namesch[10*k+i]);
		printf("		%12.7e\n",pv[k]);
	}
	return;
}