Example #1
0
void bruteforce_helper(HittingSetData &data, int setNum, int level, vector<int> &set_antenna)
{
	//make copies for recursive calls
	vector<int> b1(data.antennas.at(setNum)), b2, a1(set_antenna), a2(set_antenna), n1(data.antennas.at(setNum)), n2, dc1, dc2;
	int nn1 = data.antennas.at(setNum).size(), nn2 = 0;
	a1.at(level) = setNum;
	
	cout << "brute force called 1"  << endl;
	bruteforce(data, setNum+1, level+1, a1, b1, n1, dc1, nn1);
	
	cout << "brute force called 2 " << endl;
	bruteforce(data, setNum+2, level+1, a2, b2, n2, dc2, nn2);
	
  //select which one did better
  //TODO: check this logic
	if(nn1 > nn2)
  {
    set_antenna.swap(a1);
  }
	else
  {
    set_antenna.swap(a2);
  }

	return;
}
//a method used to clean up any memory, may be unnecessary as vectors should deallocate its memory once it runs out of scope
//might be moved to a different location in the future
int endProgram() {
	
	//create a temporary, empty vector that can be swapped with the actual vector so to 'free' its memory
	vector<concessionItem> temp1;
	vector<starLightEvent> temp2;

	itemContainer.swap(temp1);
	eventContainer.swap(temp2);

	exit(0);
}
Example #3
0
int mergesort(vector<int> &data)
{
	helpArray.resize(data.size());
	int count = mergesortwork(data, 0, data.size() - 1);
	helpArray.swap(data);
	return count;
}
Example #4
0
void matrix::pivot(matrix &a, vector &b, vector &x, int h){
	int n = a.getrows();
	int g = h;
	double t = 0;
	for (int k = h; k<n; ++k)
	{
		double elm = fabs(a[k][h]);  //finds the largest value in the column, the "pivot".
		if (elm > t) { t = elm; g = k; }
	}
	if (g > h)
	{
		a.swap(h, g);
		b.swap(h, g);
		x.swap(h, g);  //this is to swap the entries of the X vector, so that we can keep track of the solution properly.  This may not be useful in this example because x2 to xN are all zero, but in general it would be useful.  
	}
}
Example #5
0
	void Load(char* f)
	{
		vector<standardRecord> emptyVector;

		//Load file in memory
		int size=0;
		char *ptr=0;
		char* cursor=0;
		size=load_file_to_memory(f,&ptr);
		cursor=ptr;

		if(size>0&&ptr!=0)
		{
			cursor=ptr+24+(*(int*)(ptr+4));
			while(cursor<(ptr+size))
				cursor=LoopGRUP(cursor,0,0);
		}

		ParseLoadedData();

		delete ptr;

		//Trick to clear used ram by the vector
		recordPointers.swap(emptyVector);
		recordPointers.clear();
	}
Example #6
0
		primitive_properties& operator=(primitive_properties&& props) noexcept {
			points.swap(props.points);
			extent = props.extent;
			primitive_type = props.primitive_type;
			flags = props.flags;
			return *this;
		}
Example #7
0
    int play(vector <string> costs) 
    {	
        cost = costs;
        N = size(cost);
        M = size(cost[0]);
        for(int i=0;i<size(cost);i++) {
            for(int j=0;j<size(cost[i]);j++) cost[i][j] -= '0';
        }
        cur.resize(M);
        for(int i=0;i<M;i++) {
            cur[i] = cost.back()[i];
        }

        for(int i=N-2;i>=0;i--) {
            calc(cost[i]);
            cur.swap(nex);
            for(int i=0;i<M;i++) {
                cout << cur[i] << " ";
            }
            cout << endl;
        }

        int ret = cur[0];
        for(int i=1;i<M;i++) {
            ret = min(ret, cur[i]);
        }
        return ret;
    }
Example #8
0
 double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
     
     if(nums1.empty()&&nums2.empty()) return 0;
     
     int len = nums1.size() + nums2.size();
     
     int big,small;
     
     if(nums1.size()>nums2.size()) {
         big = nums1.size();
         small = nums2.size();
         
     }
     
     else {
         big = nums2.size();
         small = nums1.size();
         nums1.swap(nums2);
     }
     
     if(len%2) {
         return findkstElements(nums1,nums2,0,big,0,small, (len+1)/2);
     }
     
     else {
         return (findkstElements(nums1,nums2,0,big,0,small, len/2) + findkstElements(nums1,nums2,0,big,0,small, len/2+1))/2.0;
     }
     
 }
Example #9
0
void quicksort( vector<int> &vec ) {

    if ( vec.size() < 2 ) {
        return;
    }

    size_t pivot = (vec.size()/2);

    vector<int> lefty;
    vector<int> righty;

    partition( vec, pivot, lefty, righty );

    quicksort( lefty );
    quicksort( righty );

    vector<int> sorted;

    sorted.insert( sorted.end(), lefty.begin(), lefty.end() );
    sorted.insert( sorted.end(), vec[pivot] );
    sorted.insert( sorted.end(), righty.begin(), righty.end() );

    vec.swap(sorted);
    return;
}
Example #10
0
void mergesortImproved(vector<Comparable> &a) 
{
	int counter = 2;
	int size = a.size();
	vector<Comparable> b(size);  // this is only one temporary array. 
	//splits each array into lengths of multiples of 2
	for (int i = 1; i < size; i *= 2)
    {
        for (int j = 0; j < size; j += 2 * i)
        {
			//counter switches roles of a and b
			if(counter % 2 == 0)
				MergeIt(a, b, j, min(j+i, size), min(j+2*i, size));
			if(counter % 2 != 0)
				MergeIt(b, a, j, min(j+i, size), min(j+2*i, size));
			
        }
		//increments to change the role of a and b 
		counter++;	
    }
	//if last iteration ends on b, moves contents to a
   if(counter % 2 != 0)
	   a.swap(b);
  // implement a nonrecursive mergesort only using vectors a and b.
}
Example #11
0
void splitwords(string s, string sep, vector<string>& resultat) {
    int j = 0, j0;
    while (s.find(sep) == 0) s = s.substr(1);
    int k = 0;
    s.append(sep);
    //cout<<"s="<<s<<"\n";
    vector<string> sb(0);
    string s0, s1;
    s1 = string();
    for (int i = 0; i < (int)s.length(); i++) {
        s0 = s.substr(i, 1);
        if (s0 == sep) {
            j++;
            if (j == 1) {
                s1.append(s0);
                if (j == 1) (k)++;
                //cout <<" j=1  k="<<*k<<"\n";
            }
        } else {
            s1.append(s0);
            j = 0;
        }
    }
    sb.resize(k);
    for (int i = 0; i < k; i++) {
        j0 = s1.find(sep);
        sb[i] = s1.substr(0, j0);
        s1 = s1.substr(j0 + 1, s.length());
    }
    //cout <<"k="<<*k<<"\n";
    resultat.swap(sb);
}
Example #12
0
void LfGrid::Query(vector<SiteValue>& result, int x1, int y1, int x2, int y2, int tq) {
	int cx1 = get_coordinate(x1);
	int cy1 = get_coordinate(y1);
	int cx2 = get_coordinate(x2);
	int cy2 = get_coordinate(y2);

	for (int ax = cx1 + 1; ax < cx2; ax++)
		for (int ay = cy1 + 1; ay < cy2; ay++)
			RetrieveAllSitesInCell(result, ax, ay);

	for (int x = cx1 + 1; x < cx2; x++) {
		RetrieveSitesInCell(result, x, cy1, x1, y1, x2, y2, tq);
		RetrieveSitesInCell(result, x, cy2, x1, y1, x2, y2, tq);
	}
	for (int y = cy1; y <= cy2; y++) {
		RetrieveSitesInCell(result, cx1, y, x1, y1, x2, y2, tq);
		RetrieveSitesInCell(result, cx2, y, x1, y1, x2, y2, tq);
	}

	vector<SiteValue> unique_result_sites;
	for (auto& r : result) {
		bool found_one = false;
		for (auto& u : unique_result_sites) {
			if (u.id == r.id) {
				if (u.tu < 0) u = r;
				found_one = true;
				break;
			}
		}
		if (!found_one)
			unique_result_sites.push_back(r);
	}

	result.swap(unique_result_sites);
}
Example #13
0
	int removeDuplicates(vector<int>& nums) 
    {
    	sort(nums.begin(),nums.end());
    	vector<bool> flags(nums.size(),1);
    	bool flag = 0;
    	for(int i=1; i<nums.size(); ++i)
    	{
    		if(nums[i]!=nums[i-1])
    		{
    			flag = 0;
    		}
    		else
    		{
    			if(flag == 0)
    			{
    				flag = 1;
    			}
    			else
    			{
    				flags[i] =0;
    			}
    		}
    		
    	}
    	vector<int> processed;
    	for(int i=0; i<nums.size(); ++i)
    	{
    		if(flags[i]) processed.push_back(nums[i]);
    	}
    	nums.swap(processed);
    	return nums.size();
    }
bool sensor_plugin_loader::load_module(const string &path, vector<void*> &sensors, void* &handle)
{
	void *_handle = dlopen(path.c_str(), RTLD_NOW);

	if (!_handle) {
		ERR("Failed to dlopen(%s), dlerror : %s", path.c_str(), dlerror());
		return false;
	}

	dlerror();

	create_t create_module = (create_t) dlsym(_handle, "create");

	if (!create_module) {
		ERR("Failed to find symbols in %s", path.c_str());
		dlclose(_handle);
		return false;
	}

	sensor_module *module = create_module();

	if (!module) {
		ERR("Failed to create module, path is %s\n", path.c_str());
		dlclose(_handle);
		return false;
	}

	sensors.clear();
	sensors.swap(module->sensors);

	delete module;
	handle = _handle;

	return true;
}
    vector<string> process(vector<string> &vecStr, int left, int right)
    {
        if (left == 0 && right == 0)
        {
            return vecStr;
        }

        vector<string> vecStrCopy(vecStr);
        vector<string> processResult;
        if (left > 0)
        {
            vector<string> leftBefore = process(vecStrCopy, left-1, right);
            for (int i = 0; i < leftBefore.size(); i++)
            {
                leftBefore[i] += "(";
                processResult.push_back(leftBefore[i]);
            }
        }
        if (right > left && right > 0)
        {
            vector<string> rightBefore = process(vecStrCopy, left, right-1);
            for (int i = 0; i < rightBefore.size(); i++)
            {
                rightBefore[i] += ")";
                processResult.push_back(rightBefore[i]);
            }
        }
        vecStr.swap(processResult);
        return processResult;
    }
Example #16
0
static void CreateGraph(TVertices &vertices, vector< vector<bool> > &adjMatrix) {
    vertices.clear();
    vertices.resize(6);

    vector< vector<bool> > am;
    vector<bool> tmp(6);
    for (int i = 0; i < 6; ++i)
        am.push_back(tmp);

    am[0][1] = true;
    am[0][3] = true;

    am[1][4] = true;

    am[2][4] = true;
    am[2][5] = true;

    am[3][1] = true;

    am[4][3] = true;

    am[5][5] = true;

    adjMatrix.swap(am);
}
	void swap(LongNum &b)
	{
		ilum.swap(b.ilum);
		int c = sign;
		sign = b.sign;
		b.sign = c;
	}
Example #18
0
void splitwordsR(string s, string sep, int m, vector<string>& resultat) {
    int j = 0, j0;
    while (s.find(sep) == 0) s = s.substr(1);
    int k = 0;
    string s0, s1;
    if (s.length() == 0) {
        resultat.resize(0);
        return;
    }
    s.append(sep);
    s1 = string();
    for (int i = 0; i < (int)s.length(); i++) {
        s0 = s.substr(i, 1);
        if (s0 == sep) {
            j++;
            if (j == 1) {
                s1.append(s0);
                if (j == 1) k++;
                //cout <<" j=1  k="<<*k<<"\n";
            }
        } else {
            s1.append(s0);
            j = 0;
        }
        if (k == m) break;
    }
    vector<string> sb(k);
    for (int i = 0; i < k; i++) {
        j0 = s1.find(sep);
        sb[i] = s1.substr(0, j0);
        s1 = s1.substr(j0 + 1, s.length());
    }
    resultat.swap(sb);
    //cout <<"k="<<*k<<"\n";
}
Example #19
0
void sparql_parser::remove_header(vector<string>& token_vec){
    vector<string> new_vec;
    int iter=0;
    while(token_vec.size()>iter && token_vec[iter]=="PREFIX"){
        if(token_vec.size()>iter+2){
            prefix_map[token_vec[iter+1]]=token_vec[iter+2];
            iter+=3;
        } else {
            valid=false;
            return ;
        }
    }
    /// TODO More Check!
    while(token_vec[iter]!="{"){
        iter++;
    }
    iter++;

    while(token_vec[iter]!="}"){
        if(token_vec[iter]=="join"){
            join_step=new_vec.size()/4;
            iter++;
            continue;
        }
        if(token_vec[iter]=="fork"){
            fork_step=new_vec.size()/4;
            iter++;
            continue;
        }
        new_vec.push_back(token_vec[iter]);
        iter++;
    }
    token_vec.swap(new_vec);
}
void apply_pi(vector<int> &vec) {
	int N = temp.size();
	for(int i = 0; i < N; i++) {
		temp[ Pi[i]-1 ] = vec[i];	
	}
	vec.swap(temp);
	return;
}
void DetectorTester::mergeInto(vector<pair<float, bool>>& scores, const vector<pair<float, bool>>& additionalScores) const {
	vector<pair<float, bool>> mergedScores;
	mergedScores.reserve(scores.size() + additionalScores.size());
	std::merge(scores.begin(), scores.end(),
			additionalScores.begin(), additionalScores.end(),
			std::back_inserter(mergedScores), std::greater<pair<float, bool>>());
	scores.swap(mergedScores);
}
 void generateNext(vector<int> &vec) {
     vector<int> next;
     next.push_back(1);
     for (int i = 1; i < vec.size(); ++i) 
         next.push_back(vec[i] + vec[i - 1]);
     next.push_back(1);
     vec.swap(next);
 }
Example #23
0
void compute_convex_hull(vector<PI>& P) {
    vector<PI> H;
    FORE(p, P) {
        while (SIZE(H) >= 2 && !ccw(H[SIZE(H) - 2], H[SIZE(H) - 1], *p))
            H.pop_back();
        H.push_back(*p);
    }
    P.swap(H);
}
static void spSort(vector<uint64>& ioWrittenTupleIDs, vector<ZTuple>& ioWrittenTuples)
{
    vector<uint64> newIDs;
    vector<ZTuple> newTuples;
    // Do a lower_bound insertion into newIDs, and
    // to equivalent offset of newTuples.
    for (vector<uint64>::iterator i = ioWrittenTupleIDs.begin(); i != ioWrittenTupleIDs.end(); ++i)
    {
        vector<uint64>::iterator pos = lower_bound(newIDs.begin(), newIDs.end(), *i);

        newTuples.insert(newTuples.begin() + (pos - newIDs.begin()),
                         ioWrittenTuples[i - ioWrittenTupleIDs.begin()]);

        newIDs.insert(pos, *i);
    }
    ioWrittenTupleIDs.swap(newIDs);
    ioWrittenTuples.swap(newTuples);
}
Example #25
0
void GenerateRandom(vector<int>& vi, set<int>::size_type size, int maxV)
{
  set<int> s;
  while(s.size() != size)
  {
    s.insert(rand() % maxV);
  }
  vector<int> temp(s.begin(), s.end());
  vi.swap(temp);
}
Example #26
0
 void MemoryCheck()
 {
     if (size > data.size())
         data.resize(data.size() * 2 + 1);
     if (size != 0 && size <= data.size() / 4)
     {
         data.resize(data.size() / 2);
         vector<int> temp = data;
         data.swap(temp);
     }
 }
Example #27
0
void shuffle(vector<T> &v){
	vector<T> temp;
	 T locations[v.size()];
	for (int i = 0; i < v.size(); i++){
		do{
			locations[i] = rand() % v.size();
		} while (i != 0 || include(locations, i, locations[i]));
		temp.push_back(v.at(locations[i]));
	}
	v.swap(temp);
}
Example #28
0
int actUpdatingAvg(ALEInterface& ale, RAMFeatures *ram, BPROFeatures *features, int nextAction, 
	vector<vector<vector<float> > > &w, Parameters param, int totalNumFrames, int gameId,
	vector<bool> &F, vector<bool> &Fprev){

	int reward = 0;

	//If the selected action was one of the primitive actions
	if(nextAction < NUM_ACTIONS){ 
		for(int i = 0; i < FRAME_SKIP && totalNumFrames + ale.getEpisodeFrameNumber() < MAX_NUM_FRAMES; i++){
			reward += ale.act((Action) nextAction);
			Fprev.swap(F);
			F.clear();
			ram->getCompleteFeatureVector(ale.getRAM(), F);
			F.pop_back();
			updateAverage(Fprev, F, ale.getEpisodeFrameNumber(), param, gameId);
		}
	}
	//If the selected action was one of the options
	else{
		int currentAction;
		vector<int> Fbpro;	                  //Set of features active
		vector<float> Q(NUM_ACTIONS, 0.0);    //Q(a) entries

		int option = nextAction - NUM_ACTIONS;
		while(rand()%1000 > 1000 * PROB_TERMINATION && !ale.game_over() && totalNumFrames + ale.getEpisodeFrameNumber() < MAX_NUM_FRAMES){
			//Get state and features active on that state:		
			Fbpro.clear();
			features->getActiveFeaturesIndices(ale.getScreen(), Fbpro);
			updateQValues(Fbpro, Q, w, option);       //Update Q-values for each possible action
			currentAction = epsilonGreedy(Q);
			//Take action, observe reward and next state:
			reward += ale.act((Action) currentAction);
			Fprev.swap(F);
			F.clear();
			ram->getCompleteFeatureVector(ale.getRAM(), F);
			F.pop_back();
			updateAverage(Fprev, F, ale.getEpisodeFrameNumber(), param, gameId);
		}
	}
	return reward;
}
Example #29
0
void StringUtil::split(vector<string>& output, const string& input, vector<string> delimiters)
{
	output.clear();
	output.push_back(input);
	for (int var = 0; var < (int)delimiters.size(); ++var) {
		vector<string> output1;
		for (int var1 = 0; var1 < (int)output.size(); ++var1) {
			splitInternal(output1, output.at(var1), delimiters.at(var));
		}
		output.swap(output1);
	}
}
Example #30
0
void reorder_nodeList_merge_bitNumber(const pair<vector<int>, vector<int> >  &iDx, vector<Node> &nodeList, int sz)
{
	#ifdef OPEN_ALL_STREAMS
	stringstream ss;
	ss << "graphs\\slashburn_graph_" << sz << ".txt";
	string fileName; ss >> fileName;
    fstream fs(fileName.c_str(), std::ios::out);
	#endif // OPEN_ALL_STREAMS
	for(vector<Node>::iterator sit = nodeList.begin(); sit != nodeList.end(); ++sit){
		if(SLASHBURN_GRAPH_WIDTH == SUB_CNT){
			sit->rowId = iDx.first[sit->rowId];
		}
		for(BitList::iterator bit = sit->bitList.begin(); bit != sit->bitList.end(); ++bit){
			bit->colId = iDx.second[bit->colId];
			#ifdef OPEN_ALL_STREAMS
			fs << sit->rowId + 1 << " " << bit->colId + 1 << " " << bit->weight << endl;
			#endif // OPEN_ALL_STREAMS
        }
	}
	sort(nodeList.begin(), nodeList.end(), cmp);
	int newsz = (int)ceil((int)nodeList.size() * 1.0 / BASE_NUM);
	vector<Node> tmp(newsz);
	for(int sit = 0; sit < (int)nodeList.size(); sit += BASE_NUM){
		BitList mergedList;
		map<int, BitNumber> bitNumberCollector;
		for(int offset = 0; offset < min(BASE_NUM, (int)nodeList.size() - sit); offset++){
			for(BitList::iterator bit = nodeList[sit + offset].bitList.begin(); bit != nodeList[sit + offset].bitList.end(); ++bit){
				int newColId = bit->colId / BASE_NUM;
				int gNum = bit->colId % BASE_NUM;
				if(bitNumberCollector.find(newColId) != bitNumberCollector.end()){
					bitNumberCollector[newColId].eid |= (1 << (BASE_NUM - 1 - gNum + BASE_NUM * (BASE_NUM - 1 - offset % BASE_NUM)));
				}
				else{
					BitNumber bNum;
					bNum.did = newColId * BASE_NUM;
					bNum.colId = newColId;
					bNum.eid = (1 << (BASE_NUM - 1 - gNum + BASE_NUM * (BASE_NUM - 1 - offset % BASE_NUM)));
					bitNumberCollector[newColId] = bNum;
				}
			}
		}
		for(map<int, BitNumber>::iterator mit = bitNumberCollector.begin(); mit != bitNumberCollector.end(); ++mit){
			mergedList.push_back(mit->second);
		}
		tmp[sit / BASE_NUM].rowId = sit / BASE_NUM;
		tmp[sit / BASE_NUM].wordId = sit / BASE_NUM;
		tmp[sit / BASE_NUM].bitList.swap(mergedList);
    }
	nodeList.swap(tmp);
	#ifdef OPEN_ALL_STREAMS
	fs.close();
	#endif // OPEN_ALL_STREAMS
}