Example #1
0
bool equalVectors(vector<T> a, vector<T> b) {
    a.shrink_to_fit();
    b.shrink_to_fit();
    if (a.size() != b.size()) return false;
    else {
        for (int i = 0; i < a.size(); i++) {
            if (a[i] != b[i]) return false;
        }

    }
    return true;
}
pair<uint32_t, uint32_t> algo(vector<pair<uint32_t, vector<pair<int32_t, int32_t>>>>& data)
{
	// like qsort.
	if (data.size() <= 1)
		return make_pair(UINT32_MAX, UINT32_MAX);
	
	// let [0] be separator.
	vector<pair<uint32_t, vector<pair<int32_t, int32_t>>>> less;
	vector<pair<uint32_t, vector<pair<int32_t, int32_t>>>> greater;
	
	for (uint32_t i = 1; i < data.size(); i++)
	{
		int32_t r = compare_two(data[0].second, data[i].second);
		if (r == 2)
			return make_pair(data[0].first, data[i].first);
		if (r == -1)
			less.push_back(std::move(data[i]));
		if (r == +1)
			greater.push_back(std::move(data[i]));
	}
	
	data.clear();
	data.shrink_to_fit();
	
	auto res = algo(less);
	if (res.first != UINT32_MAX)
		return res;
	res = algo(greater);
	if (res.first != UINT32_MAX)
		return res;
	return make_pair(UINT32_MAX, UINT32_MAX);
}
Example #3
0
	vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
		
		sort(input.begin(), input.end());
		auto iter = unique(input.begin(), input.end());

		input.erase(iter, input.end());

		input.shrink_to_fit();

		vector<int> vec;

		if (input.size() < k)
		{
			return vec;
		}
		
		int temp = 0;
		while (temp < k)
		{
			vec.push_back(input[temp]);
			temp++;
		}

		return vec;
	}
void heap_sort_parallel(vector<int>& a)
{
	auto numOfThreads = omp_get_max_threads();
	cout << "Sorting in " << numOfThreads << " threads" << endl;
	vector<vector<int>*> subMas;
	int elementsPerSubMassive = static_cast<int>(ceil(static_cast<float>(a.size()) / numOfThreads));
	auto it = a.begin();
	for (auto j = 0; j < numOfThreads; ++j)
	{
		subMas.push_back(new vector<int>());
		for (auto z = 0; z < elementsPerSubMassive; ++z)
		{
			if(*it)
			{
				subMas.at(j)->push_back(*it);
				++it;
			}
			else break;
		}
	}

	a.clear();
	a.shrink_to_fit();

	#pragma omp parallel num_threads(numOfThreads) shared(subMas)
	{
		int i;
		auto threadNumber = omp_get_thread_num();
		auto size = subMas.at(threadNumber)->size();
		
		for (i = size / 2 - 1; i >= 0; --i)
			shift_down(*subMas.at(threadNumber), i, size);

		for (i = size - 1; i >= 1; --i)
		{
			swap(subMas.at(threadNumber)->at(0), subMas.at(threadNumber)->at(i));
			shift_down(*subMas.at(threadNumber), 0, i);
		}
	}

	auto *m1 = new vector<int>(subMas.at(0)->size() + subMas.at(1)->size());
	auto *m2 = new vector<int>(subMas.at(2)->size() + subMas.at(3)->size());
	auto *m3 = new vector<int>(subMas.at(0)->size() + subMas.at(1)->size() + subMas.at(2)->size() + subMas.at(3)->size());

	mergeVectors(subMas.at(0), subMas.at(1), m1);
	mergeVectors(subMas.at(2), subMas.at(3), m2);
	mergeVectors(m1, m2, m3);
	a = *m3;
	subMas.clear();
	subMas.shrink_to_fit();
	delete m1;
	delete m2;
	delete m3;
}
Example #5
0
vector <Person> reduceVectorSize(vector <Person> people, int reduceAmount){
    
    //Check if the vector can be reduced by a certain size
    if (people.size() > reduceAmount){
        people.resize(people.size() - reduceAmount);
        people.shrink_to_fit();
        return people;
        
    }else{
        //Cannot reduce further. return the original vector
        return people;
    }
}
Example #6
0
bool Settings::list_dir(vector<wstring>& result) {
  FarSettingsEnum fse = {};
  fse.Root = dir_id;
  if (!control(SCTL_ENUM, &fse))
    return false;
  result.clear();
  result.reserve(fse.Count);
  for (size_t i = 0; i < fse.Count; i++) {
    if (fse.Items[i].Type == FST_SUBKEY)
      result.push_back(fse.Items[i].Name);
  }
  result.shrink_to_fit();
  return true;
}
Example #7
0
void initStlTable()
{
  int i,fac,j,n;
  if (stltable.size()!=216)
  {
    stltable.clear();
    for (i=1;i<=7776000;i++)
      if (7776000%i==0 && smooth5(i))
      {
	stltable.push_back(i);
	fac=0;
	n=i;
	for (j=0;j<9;j++)
	  if (n%2==0)
	  {
	    n/=2;
	    fac++;
	  }
	for (j=0;j<6;j++)
	  if (n%3==0)
	  {
	    n/=3;
	    fac+=16;
	  }
	for (j=0;j<4;j++)
	  if (n%5==0)
	  {
	    n/=5;
	    fac+=256;
	  }
	stlfac.push_back(fac);
      }
    stltable.shrink_to_fit();
    stlfac.shrink_to_fit();
  }
}
Example #8
0
void SQLQueryData::FillcomputerVector(QSqlQuery& query, vector<computersabstract> &temp)
{
    //Loop through and fill vector with ComputerScientists.
    while(query.next())
    {
        computersabstract tmp;

        tmp.setID(query.value("id").toInt());
        tmp.setName(query.value("name").toString().toStdString());
        tmp.setYear((query.value("year").toInt()));
        tmp.setType((query.value("type").toString().toStdString()));
        tmp.setBuilt((query.value("built").toBool()));

        temp.push_back(tmp);

    }
    temp.shrink_to_fit();
}
Example #9
0
void SQLQueryData::FillcsVector(QSqlQuery& query, vector<ComputerScientist> &temp)
{
    //Loop through and fill vector with ComputerScientists.
    while(query.next())
    {
        ComputerScientist tmp;

        tmp.setID(query.value("id").toInt());
        tmp.setFirst(query.value("first_name").toString().toStdString());
        tmp.setMid((query.value("middle_name").toString().toStdString()));
        tmp.setLast(query.value("last_name").toString().toStdString());
        tmp.setgender((query.value("gender").toString().toStdString()));
        tmp.setbday((query.value("birth_year").toInt()));
        tmp.setdday((query.value("death_year").toInt()));

        temp.push_back(tmp);
    }
    temp.shrink_to_fit();
}
//粒子を交換したクラスタ番号を確認
void CalcIteration::ConfirmExClusterIndx(const vector<vector<int>> exIndxList, vector<int>& exClstrIndxes)
{
	for(unsigned i = 0; i < exIndxList.size(); i++){

		////TODO: うまくいかない…
		////exIndxListにMAXINTより小さい値が存在するかどうか
		////存在するなら交換が行われている,存在しないなら交換が行われていないので戻る
		//vector<int> exList = exIndxList[i];
		//if( exList.end() != find(exList.begin(), exList.end(), bind2nd(less<int>(), MAXINT)) ){
		//	continue;
		//}

		//どう考えても冗長
		bool check = false;
		for(vector<int>::const_iterator it = exIndxList[i].begin(); it != exIndxList[i].end(); it++){
			if(*it != MAXINT){	check = true;	break;	}
		}
		if(!check){	continue;	}

		exClstrIndxes.push_back(i);
	}

	//何度もExchangeした場合に備えた処理
	////ソート
	//sort(exClstrIndxes.begin(), exClstrIndxes.end());
	//
	////重複削除
	//exClstrIndxes.erase( 
	//	unique(exClstrIndxes.begin(), exClstrIndxes.end()),
	//	exClstrIndxes.end()
	//);

	//サイズ調整
	exClstrIndxes.shrink_to_fit();

	//cout << __FUNCTION__ << "," << "exClstrIndxes.size:" << exClstrIndxes.size() << endl;
}
Example #11
0
void bns(int l,int r,vector<int> &pp){
    // cout<<"bns "<<l<<" "<<r<<endl;
    if(l>r || pp.empty())return;
    int mid=(l+r)>>1;
    vector<int> mids;
    for(int i=l;i<=mid;++i)mids.push_back(i);
    sort(mids.begin(),mids.end(),[&](const int &a,const int &b){return ms[a].r<ms[b].r;});

    vector<Q> qs;
    for(int i:pp){
        for(int j=1;j<app[i].size();++j){
            if(app[i][j-1]+1>app[i][j]-1)continue;
            qs.push_back({app[i][j-1]+1,app[i][j]-1,i,-1});
        }
        change[i]=0;
    }
    // for(Q q:qs)cout<<"["<<q.l<<" , "<<q.r<<" ] : "<<q.i<<" * "<<q.sgn<<endl;
    sort(qs.begin(),qs.end(),[](const Q &a,const Q &b){return a.r<b.r;});

    int qptr=0;
    long long rec=0;
    for(int mid:mids){
        M &m=ms[mid];
        while(qptr<qs.size() && qs[qptr].r<m.r){
            change[qs[qptr].i]-=rec-query(qs[qptr].l-1);
            // cout<<"run "<<"["<<qs[qptr].l<<" , "<<qs[qptr].r<<" ] : "<<qs[qptr].i<<" * "<<qs[qptr].sgn<<" , get "<<rec-query(qs[qptr].l-1)<<endl;
            ++qptr;
        }
        rec+=m.c;
        add(m.l,m.c);
        /* while(qptr<qs.size() && qs[qptr].r<=m.r){
            change[qs[qptr].i]-=rec-query(qs[qptr].l-1);
            cout<<"run "<<"["<<qs[qptr].l<<" , "<<qs[qptr].r<<" ] : "<<qs[qptr].i<<" * "<<qs[qptr].sgn<<" , query "<<query(qs[qptr].l-1)<<" get "<<rec-query(qs[qptr].l-1)<<endl;
            ++qptr;
        } */
    }
    while(qptr<qs.size()){
        change[qs[qptr].i]-=rec-query(qs[qptr].l-1);
        ++qptr;
    }
    for(int i:pp)change[i]+=rec;
    for(int mid:mids)add(ms[mid].l,-ms[mid].c);

    vector<int> lp,rp;
    for(int i:pp){
        // cout<<"for people "<<i<<" change "<<change[i]<<" need "<<v[i]<<endl;
        if(change[i]>=v[i]){
            ans[i]=mid;
            lp.push_back(i);
        }
        else{
            v[i]-=change[i];
            rp.push_back(i);
        }
    }

    if(l==r)return;
    qs.clear(); mids.clear(); pp.clear();
    qs.reserve(0); mids.reserve(0); pp.reserve(0);
    // vector<Q>().swap(qs);
    qs.shrink_to_fit();
    // qs.~vector<Q>();
    bns(l,mid,lp); bns(mid+1,r,rp);
}
void FindLargest_ProjectionVoxel(int ImageNum, vector<OctVoxel>& Octree, vector<cv::Mat>& Silhouette, Cpixel*** vertexII, CMatrix* ART){

	int thresh = 70;
	int max_thresh = 210;
	RNG rng(12345);

	Mat src_gray;
	Mat drawing;

	double scale(0.7);
	Size ssize;
	CVector M(4);		//Homogeneous coordinates of the vertices(x,y,z,1) world coordinate
	CVector m(4);		//That the image coordinates (normalized) expressed in homogeneous coordinates
	M[3] = 1.0;
	//8 vertices world coordinates of the voxel (x,y,z)
	CVector3d vertexW[8];

	ofstream fout("larget_boundingbox_contour.txt");

	int Boundingbox_line[12][2] = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 },
	{ 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 },
	{ 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 } };

	//---------------------------------------------------------------	
	for (auto h(0); h < ImageNum; h++){
		//src_gray = Silhouette[h];         	
		Silhouette[h].copyTo(src_gray);
		cout << "Silhouette_" << h << endl;

		for (auto j(0); j < Octree.size(); j++){

			Octree[j].SetVertexWorld_Rotated(vertexW);
			for (int k = 0; k < 8; ++k){	//8 vertices of the voxel
				M[0] = vertexW[k].x;
				M[1] = vertexW[k].y;
				M[2] = vertexW[k].z;
				m = ART[h] * M;
				vertexII[h][j][k].setPixel_u_v((int)(m[0] / m[2]), (int)(m[1] / m[2]));  // normalize
			}

			//-------------------------------------- bounding box ------------------------
			for (auto k(0); k < 12; k++){
				//Draw 12 lines of the voxel in img.
				Start_point.x = vertexII[h][j][Boundingbox_line[k][0]].getPixel_u();
				Start_point.y = vertexII[h][j][Boundingbox_line[k][0]].getPixel_v();
				PointStart.push_back(Start_point);
				End_point.x = vertexII[h][j][Boundingbox_line[k][1]].getPixel_u();
				End_point.y = vertexII[h][j][Boundingbox_line[k][1]].getPixel_v();
				PointEnd.push_back(End_point);

				//line(src_gray, Start_point, End_point, Scalar(225, 225,255), 2.0, CV_AA);
			}
		}
		

		Mat canny_output;
		vector<vector<Point> > contours;
		vector<Vec4i> hierarchy;

		double max_contour_area(0.0);
		int largest_contour_index(0);
		
		/// Detect edges using canny
		Canny(src_gray, canny_output, thresh, max_thresh, 3);
		/// Find contours
		//findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
		findContours(canny_output, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));

		/// Draw contours
		drawing = Mat::zeros(canny_output.size(), CV_8UC3);

		for (auto n(0); n < PointEnd.size(); n++){
			line(drawing, PointStart[n], PointEnd[n], Scalar(225, 225, 225), 1.0, 1, 0);
		}

		/// Get the moments
		vector<Moments> mu(contours.size());
		for (int i = 0; i < contours.size(); i++)
		{
			mu[i] = moments(contours[i], false);
			//cout << "# of contour points: " << contours[i].size() << endl;
			for (int j = 0; j < contours[i].size(); j++)
			{
				//cout << "Point(x,y)=" <<i<<" j "<<j<<" "<< contours[i][j] << endl;
			}
		}
		////  Get the mass centers:
		vector<Point2f> mc(contours.size());
		for (int i = 0; i < contours.size(); i++)
		{
			mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
		}
		//// ---------- - Find the convex hull object for each contour
			vector<vector<Point>>hull(contours.size());
		for (int i = 0; i < contours.size(); i++){
			convexHull(Mat(contours[i]), hull[i], false);
		}				
		
		// Calculate the area with the moments 00 and compare with the result of the OpenCV function
		//printf("\t Info: Area and Contour Length \n");

		//cout << "contours.size() " << contours.size() << endl;
		double countour_Area(0.0);
		double arc_Length(0.0);

		for (int i = 0; i < contours.size(); i++)
		{
			countour_Area = (double)contourArea(contours[i]);
			arc_Length = (double)arcLength(contours[i], true);

			//cout << "contourArea [" << i << "] " << ": Moment " << mu[i].m00 
			//	 << " OpenCV " << countour_Area << " arcLength " << arc_Length << endl;		
			//cout << "countour_Area "<< countour_Area << " " << endl;

			if (countour_Area > max_contour_area){
				max_contour_area = countour_Area;
				largest_contour_index = i;
			}

			//------- draw all contour ---------------
			//Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
			//drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
			//circle(drawing, mc[i], 4, color, -1, 8, 0);
			//drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
			//drawContours(drawing, contours, i, Scalar(255, 255, 255), 0.10, 8, hierarchy, 0, Point());

		}
		//------- draw largest contour ---------------
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		drawContours(drawing, contours, largest_contour_index, color, 2, 8, hierarchy, 0, Point());
		//circle(drawing, mc[largest_contour_index], 4, color, -1, 8, 0);		
		//drawContours(drawing, contours, largest_contour_index, Scalar(0, 255, 255), 2, 8, hierarchy, 0, Point());
		//drawContours(drawing, hull, largest_contour_index, color, 2, 8, vector<Vec4i>(), 0, Point());
		//drawContours(drawing, contours, largest_contour_index, Scalar(255, 255, 255), 1, 8, hierarchy, 0, Point());

		fout << max_contour_area << endl;
		cout << "max_contour_area " << max_contour_area << endl;	
		
		//----------------------- Show in a window --------------------------------------
		//resize(drawing, drawing, ssize, INTER_NEAREST);
		namedWindow("Contours", CV_WINDOW_AUTOSIZE);
		imshow("Contours", drawing);

		//output white boundary
		imwrite("../../data2016/input/newzebra/contour_voxel/contour_voxel" + to_string(h) + ".bmp", drawing);

		waitKey(0);
		destroyWindow("silhouette");

		PointStart.clear();
		PointStart.shrink_to_fit();
		PointEnd.clear();
		PointEnd.shrink_to_fit();
	}

	//getchar();
}