Exemple #1
0
void process() {
	set<long> trackElements;
	for (multimap<long, long>::iterator itr = trackElement_sensors.begin();
			itr != trackElement_sensors.end();) {
		multimap<long, long>::iterator curr = itr;
		while (itr != trackElement_sensors.end() && itr->first == curr->first) {
			trackElements.insert(itr->first);
			++itr;
		}
	}

	long n = 0;
	for (set<long>::iterator it = switches.begin(); it != switches.end(); it++) {
		if (trackElements.find(*it) == trackElements.end()) {
			n++;
		}
	}
	cout << "Results: " << n << endl;

}
    long sendResponse(istream& in, long status) {
        string hdr = string("Connection: close\r\n");
        for (multimap<string,string>::const_iterator i=m_response_headers.begin(); i!=m_response_headers.end(); ++i)
            hdr += i->first + ": " + i->second + "\r\n";

        // We can't return 200 OK here or else the filter is bypassed
        // so custom Shib errors will get turned into a generic page.
        const char* codestr="Status: 500 Server Error";
        switch (status) {
            case XMLTOOLING_HTTP_STATUS_UNAUTHORIZED:   codestr="Status: 401 Authorization Required"; break;
            case XMLTOOLING_HTTP_STATUS_FORBIDDEN:      codestr="Status: 403 Forbidden"; break;
            case XMLTOOLING_HTTP_STATUS_NOTFOUND:       codestr="Status: 404 Not Found"; break;
        }
        cout << codestr << "\r\n" << hdr << "\r\n";
        char buf[1024];
        while (in) {
            in.read(buf,1024);
            cout.write(buf, in.gcount());
        }
        return SHIB_RETURN_DONE;
    }
Exemple #3
0
    long sendResponse(istream& in, long status) {
        string hdr = string("Connection: close\r\n");
        for (multimap<string,string>::const_iterator i=m_headers.begin(); i!=m_headers.end(); ++i)
            hdr += i->first + ": " + i->second + "\r\n";

        const char* codestr="Status: 200 OK";
        switch (status) {
            case XMLTOOLING_HTTP_STATUS_ERROR:          codestr="Status: 500 Server Error"; break;
            case XMLTOOLING_HTTP_STATUS_UNAUTHORIZED:   codestr="Status: 401 Authorization Required"; break;
            case XMLTOOLING_HTTP_STATUS_FORBIDDEN:      codestr="Status: 403 Forbidden"; break;
            case XMLTOOLING_HTTP_STATUS_NOTFOUND:       codestr="Status: 404 Not Found"; break;
            case XMLTOOLING_HTTP_STATUS_NOTMODIFIED:    codestr="Status: 304 Not Modified"; break;
        }
        cout << codestr << "\r\n" << hdr << "\r\n";
        char buf[1024];
        while (in) {
            in.read(buf,1024);
            cout.write(buf, in.gcount());
        }
        return SHIB_RETURN_DONE;
    }
Exemple #4
0
/* Return mime handler to pool */
void returnMimeHandler(RecollFilter *handler)
{
    typedef multimap<string, RecollFilter*>::value_type value_type;

    if (handler == 0) {
	LOGERR(("returnMimeHandler: bad parameter\n"));
	return;
    }
    handler->clear();

    PTMutexLocker locker(o_handlers_mutex);

    LOGDEB(("returnMimeHandler: returning filter for %s cache size %d\n", 
	    handler->get_mime_type().c_str(), o_handlers.size()));

    // Limit pool size. The pool can grow quite big because there are
    // many filter types, each of which can be used in several copies
    // at the same time either because it occurs several times in a
    // stack (ie mail attachment to mail), or because several threads
    // are processing the same mime type at the same time.
    multimap<string, RecollFilter *>::iterator it;
    if (o_handlers.size() >= max_handlers_cache_size) {
	static int once = 1;
	if (once) {
	    once = 0;
	    for (it = o_handlers.begin(); it != o_handlers.end(); it++) {
		LOGDEB1(("Cache full. key: %s\n", it->first.c_str()));
	    }
	    LOGDEB1(("Cache LRU size: %u\n", o_hlru.size()));
	}
	if (o_hlru.size() > 0) {
	    it = o_hlru.back();
	    o_hlru.pop_back();
	    delete it->second;
	    o_handlers.erase(it);
	}
    }
    it = o_handlers.insert(value_type(handler->get_id(), handler));
    o_hlru.push_front(it);
}
Exemple #5
0
/*这个函数在我们强制中断程序的时候,将map_urls
中没有访问完的url放入pse_unvisited_url.txt文件中,
扩充了我们的种子URL库*/
void SaveUnvisitedUrl()
{
	ofstream ofs_unvisited_url;
	ofs_unvisited_url.open(UNVISITED_FILE.c_str(),
		ios::in | ios::out | ios::trunc | ios::binary);//以二进制可追加的方式打开文件

	if(!ofs_unvisited_url)//打开文件失败
	{
		cerr<<"can not open the file "<<UNVISITED_FILE<<endl;
		exit(-1);
	}

	//将map_urls中没有访问完的url放入pse_unvisited.url文件中,扩充了我们的URL种子库
	multimap<string,string> ::iterator it;
	for(it=map_urls.begin();it != map_urls.end();it++)
	{
		ofs_unvisited_url<<(*it).second.c_str()<<"\n";
	}

	ofs_unvisited_url<<endl;
	ofs_unvisited_url.close();
}
Exemple #6
0
 bool book(int start, int end) {
     auto p = make_pair(start, end);
     if(isAlreadyBookingTwice(p))
         return false;
     auto it = intervals.insert(make_pair(p, end));
     if(it != intervals.begin()) {
         auto prevIt = prev(it);
         if(prevIt->second > start) {
             auto newOverlap = make_pair(start, min(prevIt->second, end));
             overlaps.insert(newOverlap);
         }
         it->second = max(prevIt->second, end);
     }
     auto nextIt = next(it);
     while(nextIt != intervals.end() && nextIt->first.first < end) {
         auto newOverlap = make_pair(nextIt->first.first,
                                     min(nextIt->first.second, end));
         overlaps.insert(newOverlap);
         nextIt->second = max(nextIt->second, end);
         nextIt = next(nextIt);
     }
     return true;
 }
Exemple #7
0
void CCacheThread::CasValueProcess(const multimap<unsigned int, string> &mapRequestValue, multimap<unsigned int, string> &mapStoredValue)
{
	CVS_XLOG(XLOG_DEBUG, "CCacheThread::%s\n", __FUNCTION__);
	multimap<unsigned int, string>::iterator itr;
	multimap<unsigned int, string>::const_iterator citr;
	for (citr=mapRequestValue.begin(); citr!=mapRequestValue.end(); ++citr)
	{
		itr = mapStoredValue.find(citr->first);
		if(itr==mapStoredValue.end()) 
		{
			mapStoredValue.insert(make_pair(citr->first, citr->second));
		}
		else
		{
			unsigned int dwStoreValue = atoi(itr->second.c_str());
			unsigned int dwNeedAddValue = atoi(citr->second.c_str());
			dwStoreValue += dwNeedAddValue;

			char szTemp[32] = {0};
			snprintf(szTemp, sizeof(szTemp), "%d", dwStoreValue);
			itr->second = szTemp;
		}
	}
}
Exemple #8
0
int main(){
    scanf("%d %s",&n,str);
    pi x = pi(0,0);
    mp.insert(make_pair(x,0));
    for (int i=0; i<n; i++) {
        if(str[i] == 'J'){
            x.first--;
            x.second--;
        }
        else if(str[i] == 'O') x.first++;
        else x.second++;
        mp.insert(make_pair(x,i+1));
    }
    it = mp.begin();
    while (it != mp.end()) {
        x = (*it).first;
        int t = (*it).second;
        while ((*it).first == x && it != mp.end()) it++;
        it--;
        res = max(res,(*it).second - t);
        it++;
    }
    printf("%d",res);
}
Exemple #9
0
 multimap<Seg, Triangle*>::iterator prevNode(multimap<Seg, Triangle*>::iterator it) {
     return it == S.begin() ? it : --it;
 }
Exemple #10
0
void obtainOptimalStateValue(multimap<double,cEvent>& _event_multimap,vector<cServer>& _server_vec)
{
	multimap<double,cEvent>::iterator iter_event_multimap;
	counting = -1;

	//store the number of accepted requests corresponding to each type of requests
	map<requesttype,unsigned int> hosted_requests_type_num_map;
	map<requesttype,unsigned int>::iterator iter_find_hosted_request_num_map;
	
	map<ID,cRequest*>  hosted_request_map;
	map<ID,cRequest*>::iterator iter_find_request_map;

	
	for (iter_event_multimap = _event_multimap.begin();iter_event_multimap != _event_multimap.end();iter_event_multimap++)
	{		
		if (iter_event_multimap->second.getEventType() == DEPARTURE)
		{
			//set current time
			current_time = iter_event_multimap->first;
			
			//delete the request from the hosting list
			iter_find_request_map = hosted_request_map.find((iter_event_multimap->second.getRequest())->getID());

			if (iter_find_request_map == hosted_request_map.end())
			{
				cout<<"Error!!!Can not locate the request that should exist in the hosting list!"<<endl;
				exit(0);
			}

			hosted_request_map.erase(iter_find_request_map);

			//minus 1 from the entry corresponding the type of currently departing requests in the counting list of hosting requests
			iter_find_hosted_request_num_map = hosted_requests_type_num_map.find((iter_event_multimap->second.getRequest())->getRequestType());
			if (iter_find_hosted_request_num_map == hosted_requests_type_num_map.end())
			{
				cout<<"Error!!!Can not locate the request that should exist in the counting list of types of the hosting requests!"<<endl;
				exit(0);
			}
			else
				(iter_find_hosted_request_num_map->second)--;

			//release the resources allocated to the request
			releaseRequest(iter_event_multimap->second.getRequest());


			//update the value of current system state after any request departure
			//system_state.first = NONE;
			//updateStateValueRequDepar(_server_vec,hosted_request_map,hosted_requests_type_num_map);
			//system_state.second = calculateRequestDepartureStateIndicator(_server_vec,iter_event_multimap->second.getRequest());
		}
		else
		{
			//set current time
			current_time = iter_event_multimap->first;
			
			//a request is arriving
			counting++;
			//update the system state
			system_state.first = (iter_event_multimap->second.getRequest())->getRequestType();

			//we should determine which action should be take base on the purpose of maximizing the expected profits
			//if(obtainOptimalAction(&(iter_event_multimap->second),_server_vec,hosted_requests_type_num_map,hosted_request_map))
            if(obtainOptimalAction(&(iter_event_multimap->second),_server_vec,hosted_requests_type_num_map,hosted_request_map))
			{
				//if the arriving request is accepted, we should allocate physical resources for it and insert a departure event into the event list
				(iter_event_multimap->second.getRequest())->setAccepted(true);
				allocateRequest(iter_event_multimap->second.getRequest());
				insertDepartureEvent(iter_event_multimap->second.getRequest(),_event_multimap);
				accepted_requests_num++;

				hosted_request_map.insert(make_pair((iter_event_multimap->second.getRequest())->getID(),iter_event_multimap->second.getRequest()));

				iter_find_hosted_request_num_map = hosted_requests_type_num_map.find((iter_event_multimap->second.getRequest())->getRequestType());
				if (iter_find_hosted_request_num_map == hosted_requests_type_num_map.end())
				{
					//there is no request in the system which has the same type as currently arriving request
					hosted_requests_type_num_map.insert(make_pair((iter_event_multimap->second.getRequest())->getRequestType(),1));
				}
				else
					(iter_find_hosted_request_num_map->second)++;
				
			}
			else
			{
				//do nothing
				//the arriving request is rejected due to 1) having not enough residual resources, or 2) maximizing profits policy
				(iter_event_multimap->second.getRequest())->setAccepted(false);

				//update the value of current system state after any request departure
				system_state.first = NONE;
				updateStateValueRequDepar(_server_vec,hosted_request_map,hosted_requests_type_num_map);
			}

		}
	}

	return ;
}
Exemple #11
0
bool IsHighSchool(const string &record,string &category,multimap<string::size_type,string> &m_keys,string &last_key)
{
	int i;
	string::size_type pos3 = 0;
	for(i = 0; i < HIGHNUM; ++i)
	{
		if((pos3 = record.find(high_school[i])) != string::npos)
			break;
	}
	if(i == HIGHNUM)
		return false;

	// 若名称中包含的是"*大"
	if(i >= UABBNUM)
	{
		int j;
		for(j = 0; j < UABBNUM; ++j)
		{
			if(record.find(high_school[j],pos3+2) != string::npos) //"*大"后面是否出现大学类的词 
				break;
		}
		if(j >= UABBNUM)
			if(category.compare("教育学校:大学") != 0)			  // 当前分类不属于"大学"分类
				return false;
	}

	if((high_school[i]).compare("大学") == 0)
	{
		pos3 = record.find(high_school[i]);
		if(record.substr(pos3+2,4).compare("学堂") == 0)
			return false;
	}
	
/*	
	if(IsSpecialUniversity(record,last_key) == true)
	{
		category =	school_category[HIGH]["特殊学校"];
		return true;
	}
*/
	string::size_type pos4 = record.find("附属");
	if(pos4 != string::npos)
	{
		if(record.find("校",pos4) != string::npos)
		{
			last_key = "附属";
			category =  school_category[HIGH]["附属学校"];
			return true;
		}
	}
/*
	pos4 = record.find("学校");
	if(pos4 != string::npos && pos4 != pos3+4)
	{
		category =  school_category[HIGH]["附属学校"];
		ofile11 << record << "\t" << category << "\n";
		return true;
	}
*/	
	string::size_type pos1 = 0,pos2 = 0;
	//multimap<string::size_type,string> m_keys;
	vector<string>::iterator viter = (school_keys[HIGH]).begin();
	
	while(viter != (school_keys[HIGH]).end())
	{
		pos1 = 0,pos2 = 0;
		while((pos2 = record.find(*viter,pos1)) != string::npos)
		{
			m_keys.insert(make_pair(pos2,*viter));
			pos1 = pos2 + (*viter).size();
		}
		++viter;
	}

	string ukey;
	for(int i = UNUM; i < HIGHNUM; ++i)
	{
		pos1 = 0,pos2 = 0;
		while((pos2 = record.find(high_school[i],pos1)) != string::npos)
		{
			ukey = high_school[i];
			if(ukey.compare("大学") == 0)
			{
				if(record.substr(pos2+2,4).compare("学堂") == 0)
					ukey = "";
			}
			if(ukey.compare("大学") != 0 && ukey.find("大") != string::npos)
			{
				if(record.substr(pos2+2,4).compare("大学") == 0)
					ukey = "大学";
			}
			if(ukey.compare("师范") == 0)
			{
				string tempstr = record.substr(pos2+4,4);
				if(tempstr.compare("大学") == 0 || tempstr.compare("学院") == 0 || tempstr.compare("学校") == 0)
					ukey = record.substr(pos2,8);
			}
			if(ukey.compare("") != 0 && ukey.compare("大学") != 0)
				m_keys.insert(make_pair(pos2,ukey));
			pos1 = pos2 + (ukey.size());
		}
	}
	
	vector<string> v_keys;
	multimap<string::size_type,string>::iterator miter = m_keys.begin();
	
	while(miter != m_keys.end())
	{
		v_keys.push_back(miter->second);
		++miter;
	}

	last_key = v_keys[v_keys.size() - 1];
	category = last_key;

	if(SchoolStyle(record,last_key,category,high_school,HIGHNUM) == true)
	{
		last_key = category;
		for(int i = 0; i <HIGHNUM; ++i)
		{
			if(last_key.compare(high_school[i]) == 0)
			{
				if(v_keys.size() > 1)
					category = school_category[HIGH]["二级学院"];
				else
					category = school_category[HIGH]["大学"];
				return true;
			}
		}
	}

	for(int i = 0; i < HIGHNUM; ++i)
	{
		if(last_key.find("师范") != string::npos)
			last_key = "师范";
		if(last_key.compare(high_school[i]) == 0)
		{
			if(IsSecondaryCollege(record,category,v_keys) == true)
				return true;

			category = school_category[HIGH]["大学"];
			return true;
		}   
	}   
	

	if(TechDepartType(record,category,v_keys) == true)
		return true;
	
	if(CenterType(record,category,v_keys) == true)
		return true;


	for(int i = 0; i < HIGHNUM; ++i)
	{
		if(last_key.compare(high_school[i]) == 0)
		{
			category = school_category[HIGH]["大学"];
			return true;
		}
	}
	
	category = school_category[HIGH][last_key];
		
	return true;
}
Exemple #12
0
void STLRepository::filter( Resources& resources, const multimap< string, Bytes >& inclusive_filters, const multimap< string, Bytes >& exclusive_filters ) const
{
    for ( auto resource = resources.begin( ); resource not_eq resources.end( ); )
    {
        bool failed = true;
        
        for ( const auto filter : exclusive_filters )
        {
            failed = true;
            
            const auto properties = resource->equal_range( filter.first );
            
            for ( auto property = properties.first; property not_eq properties.second; property++ )
            {
                if ( property->second == filter.second )
                {
                    failed = false;
                    break;
                }
            }
            
            if ( failed )
            {
                break;
            }
        }
        
        if ( failed and not exclusive_filters.empty( ) )
        {
            resource = resources.erase( resource );
            continue;
        }
        
        
        
        if ( inclusive_filters.empty( ) )
        {
            resource++;
            continue;
        }
        
        failed = true;
        
        for ( auto filter_iterator = inclusive_filters.begin( ); filter_iterator not_eq inclusive_filters.end( ); filter_iterator++ )
        {
            failed = true;
            
            const auto properties = resource->equal_range( filter_iterator->first );
            const auto filters = inclusive_filters.equal_range( filter_iterator->first );
            
            for ( auto filter = filters.first; filter not_eq filters.second; filter++ )
            {
                for ( auto property = properties.first; property not_eq properties.second; property++ )
                {
                    if ( property->second == filter->second )
                    {
                        failed = false;
                        break;
                    }
                }
                
                if ( not failed )
                {
                    break;
                }
            }
            
            if ( failed )
            {
                break;
            }
            
            if ( filters.second not_eq inclusive_filters.end( ) )
            {
                filter_iterator = filters.second;
            }
        }
        
        if ( failed )
        {
            resource = resources.erase( resource );
        }
        else
        {
            resource++;
        }
    }
}
Exemple #13
0
//LL PARSER PROGRAM
bool LLParser( string b, multimap<char,parseEntry>& ptable)
{
    string stackinput;      //to display stack content
    string input2,isymbol,top;
    input2 = b + "$";       //input symbol
    cout << "Input: " << input2 << endl;

    stack<char> pstack;               //stack of parser
    int i=0;                          // i is iterator of input string
    isymbol = input2[i];              //isymbol is current input symbol
    top =  startsymbol;
                            //put start symbol on top of stack
    pstack.push('$');
    stackinput += '$';
    pstack.push(startsymbol[0]);
    stackinput += startsymbol[0];

    //working
    cout << "STACK\t\tInput\t\tOutput\n";
    cout << "$" << top << "\t\t" << input2 << "\t" <<   endl ;

    while( top[0] != '$' )
    {
        bool p1 = false;
        string production,dummy ;        //from parse table.searching the parse table
        for (multimap<char,parseEntry>::iterator pit = ptable.begin(); pit != ptable.end(); ++pit)
        {
             parseEntry temp2 = pit->second;

             if ( (pit->first == top[0]) && ((temp2.input) == isymbol))
             {
                production = temp2.production;        //extracting production to be used in rule 3,4
             }
        }

        if ( top == isymbol)
        {
            //cout << "1\n";
        //    cout << "\t\tMatch " << top << endl;
            dummy = top;
            pstack.pop();
            int l = stackinput.length()-1;
            stackinput.resize(l);
            p1 = true;
            i++;
            isymbol = input2[i];
        }

        else if ( (terminals.find(top[0])) != string::npos)  //top is terminal
        {
          // cout << "2\n";
            return false;
        }

        else if (production == "")
        {
          //  cout << "3\n";
            return false;
        }

        else if ( production != "")
        {
            pstack.pop();
            int l = stackinput.length()-1;
            stackinput.resize(l);

            //pushing the production rhs on stack
            string p = rhs_cfg(production);
        //    cout << "RHS OF PRODUCTION: " << p << endl;
            for( int i= (p.length()-1); i >=0 ;i--)
            {
       //         cout << "PUSH: " << p[i] << endl;
                if ( p[i] != 'e')
                {
                    pstack.push(p[i]);
                    stackinput += p[i];
                }

            }
        }

        top = "";
        top = pstack.top();

        //display
        string inp = input2.substr(i);

        if ( p1 )
        cout << stackinput << "\t\t" << inp << "\t\t" << "Match " << dummy << endl ;

        else
        cout << stackinput << "\t\t" << inp << "\t\t" << production << endl ;

    }
    return true;
}
Exemple #14
0
/// convert infix string into postfix token list
postfix_t infix2postfix(string in)
{
	postfix_t out;
	stack<token_t> s;

	token_t lasttok;
	for (auto it = in.cbegin(); it != in.cend();)
	{
		const unsigned int i = it - in.cbegin(); // index of current character for parse_error purposes

		static const string spaces = " \t\r\n";
		if (spaces.find(*it) != string::npos)
		{
			++it;
			continue;
		}

		/*cout << string(it, in.cend()) << endl;
		cout << lasttok.first << "/" << lasttok.second << endl;
		if (!s.empty())
			cout << s.top().first << "/" << s.top().second << endl;*/

		// try to parse number
		static const string numbers = "0123456789.";
		auto it2 = it;
		for (; it2 != in.cend() && numbers.find(*it2) != string::npos; ++it2); // TODO: find_first_not_of
		if (it2 != it)
		{
			if (lasttok.first == ")" || (opers.find(lasttok.first) == opers.end() && funcs.find(lasttok.first) != funcs.end()) || lasttok.second == -1)
				throw parse_error("Missing operator", i);

			out.push_back(lasttok = token_t(string(it, it2), -1));
			it = it2;
			continue;
		}


		// try to parse operator
		auto lastoper = opers.find(lasttok.first);
		bool lunary = lasttok.first == "" || lasttok.first == "(" || lasttok.first == "," || (lastoper != opers.end() && !(lastoper->second.unary && lastoper->second.right)); // true if operator at current location would be left unary
		/*cout << unary << endl;
		cout << endl;*/

		auto oit = opers.begin();
		for (; oit != opers.end(); ++oit)
		{
			if (equal(oit->first.begin(), oit->first.end(), it) && (oit->second.unary == lunary || (oit->second.unary && oit->second.right)))
				break;
		}
		if (oit != opers.end())
		{
			if (lunary)
			{
				s.push(lasttok = token_t(oit->first, 1));
			}
			else if (oit->second.unary && oit->second.right) // right unary operator
			{
				// allow right unary operators to be used on constants and apply higher prec operators before
				while (!s.empty())
				{
					token_t tok = s.top();

					auto oit2 = find_oper(tok.first, true);
					if ((oit2 != opers.end() && oit2->second.prec > oit->second.prec) || (oit2 == opers.end() && funcs.find(tok.first) != funcs.end()))
						out.push_back(pop(s));
					else
						break;
				}
				out.push_back(lasttok = token_t(oit->first, 1)); // needs stack popping before?
			}
			else
			{
				insert_binaryoper(out, s, oit);
				lasttok = token_t(oit->first, 2);
			}
			it += oit->first.size();
			continue;
		}


		// try to parse function
		auto fit = funcs.begin();
		for (; fit != funcs.end(); ++fit)
		{
			if (opers.find(fit->first) == opers.end() && equal(fit->first.begin(), fit->first.end(), it))
				break;
		}
		if (fit != funcs.end())
		{
			if (lasttok.first == ")" || (opers.find(lasttok.first) == opers.end() && funcs.find(lasttok.first) != funcs.end()))
				throw parse_error("Missing operator", i);
			else if (lasttok.second == -1)
				insert_implicitmult(out, s);

			s.push(lasttok = token_t(fit->first, 0));
			it += fit->first.size();
			continue;
		}

		// try to parse function argument separator
		if (*it == ',')
		{
			if (lasttok.first == "(" || lasttok.first == ",")
				throw parse_error("Missing argument", i);

			bool found = false;
			while (!s.empty())
			{
				token_t tok = s.top();

				if (tok.first == "(")
				{
					found = true;
					break;
				}
				else
				{
					out.push_back(tok);
					s.pop();
				}
			}

			if (!found)
				throw parse_error("Found ',' not inside function arguments", i);

			s.top().second++; // increase number of arguments in current parenthesis
			lasttok = token_t(",", 0);
			++it;
			continue;
		}

		if (*it == '(')
		{
			if (lasttok.second == -1 || lasttok.first == ")")
				insert_implicitmult(out, s);

			s.push(lasttok = token_t("(", 1));
			++it;
			continue;
		}

		if (*it == ')')
		{
			if (lasttok.first == "(" || lasttok.first == ",")
				throw parse_error("Missing argument", i);

			bool found = false;
			while (!s.empty())
			{
				token_t tok = s.top();
				if (tok.first == "(")
				{
					found = true;
					break;
				}
				else
				{
					out.push_back(tok);
					s.pop();
				}
			}

			if (!found)
				throw parse_error("Found excess '('", i);

			token_t tok = pop(s); // pop '('

			if (!s.empty() && opers.find(s.top().first) == opers.end() && funcs.find(s.top().first) != funcs.end()) // if parenthesis part of function arguments
				out.push_back(token_t(pop(s).first, tok.second));

			lasttok = token_t(")", 0);
			++it;
			continue;
		}

		throw parse_error("Unknown token found", i);
	}

	while (!s.empty())
	{
		token_t tok = pop(s);
		if (tok.first == "(")
			throw parse_error("Found unclosed '('", in.size());
		out.push_back(tok);
	}

	return out;
}
Exemple #15
0
void CCrawl::Fetch(void *arg)
{
	string str_url,host;

	int nGsock = -1;//之前的套接字文件描述符
	string strGHost;//之前的主机号

	//生成一个PSE file来存放网页数据
	//string ofs_name = DATA_PSE_FILE + "." + CStrFunction::itos(GetCurrentThreadId());//PSE.raw+当前线程号

	string ofs_name = DATA_PSE_FILE + CStrFunction::itos(GetCurrentThreadId())+ ".txt";//PSE+当前线程号+.txt
	CPSEFile pse_file(ofs_name);//创建一个PSE格式的文件,保存为原始网页库

	//生成一个link_for_pse file来存放链接数据
	ofs_name = DATA_LINK_FOR_PSE_FILE  + CStrFunction::itos(GetCurrentThreadId())+ ".txt";//PSE+当前线程号+.txt
	CLinkForPSEFile link_for_pse_file(ofs_name);//创建一个网页结构库

	int isleep_cnt = 0;//线程运行控制参数

	for(;;)
	{
		WaitForSingleObject(mutex_collection,INFINITE);//互斥锁

		int cnt = map_urls.size();
		if(cnt > 0)
		{
			//已经收集的没有访问的url
			cout<<"collection has "<<cnt<<" unvisited urls"<<endl;
			multimap<string,string>::iterator it = map_urls.begin();
			if(it != map_urls.end())
			{
				//从带访问的url队列中得到一个url进行访问
				str_url = (*it).second;
				map_urls.erase(it);

				ReleaseMutex(mutex_collection);

				//分解url
				CUrl iurl;
				//看看url是否有http://,没有则返回

				if(iurl.ParseUrl(str_url) == false)
				{
					cout<<"parse url false in Fetch"<<str_url<<endl;
					continue;
				}

				//表明现在抓取的网页所在的主机,同之前抓取的网页所在的主机不同
				//我们不能利用之前的套接字文件描述符进行CS通信,必须创建新的
				//套接字文件描述符进行通信,这是由于循环导致的
				if(strGHost != iurl.host_name)
				{
					closesocket(nGsock);
					nGsock = -1;
					strGHost = iurl.host_name;
				}

				//根据URL以及套接字文件描述符抓取URL对应的网页,并保存为原始网页库和网页结构库
				((CCrawl *)arg)->DownroadFile(&pse_file,&link_for_pse_file,iurl,nGsock);

				cnt = 0;
			}else	
			{
				ReleaseMutex(mutex_collection);
				
			}
		}else
		{
			//等待访问的url队列map_urls已经没有url了,这是我们需要挂起线程进行等待
			ReleaseMutex(mutex_collection);
			Sleep(1000);
			isleep_cnt++;
		}

		if(b_f_over == true && isleep_cnt == 200)//当线程挂起的次数达到两百的时候,结束调用fetch
		{
			break;
		}
	}

	pse_file.Close();
	link_for_pse_file.Close();

}
 static inline int find_priority(multimap<unsigned,node*> &m, node* value){
     multimap<unsigned,node*>::iterator it;
     for (it = m.begin(); it != m.end(); it++)
         if (it->second == value) return (int)it->first;
     return -1;
 }
Exemple #17
0
int main() {
	srand(time(NULL));
	vector<string> names{ "Aragorn", "Andrev", "Chashbr", "Kostya", "Max", "Ilya", "Thireon", "Kirkorov", "Frodo" };
	//создаем овтобусный парк, 10 автобусов, первый мы инициализируем временным объектом Bus, все последующие инициализируем оператором копирования для временного Bus
	vector<Bus> park(10, Bus(route.begin()));

	for (size_t i = 0; i < park.size(); i++) {//инициализируем массив событий прихода автобусов-по сти расписание
		int t = i * 30;
		events.insert(pair<int, Event*>(t, new BusEvent(t, park[i])));
	}

	Passenger** p = new Passenger*[3];//Массив указателей на пассажиров
	if (p){
		for (int i = 0; i < 3; ++i){//создали 3 пассажиров и расставили их рандомно на остановках, присвоили рандомные имена из массива имен
			p[i] = new Passenger(route.begin() + rand() % (route.size() - 1), names[rand() % (names.size() - 1)], rand() % route.size() + 1);
			cout << "passenger " << p[i]->name << " come to " << p[i]->position->name << endl;
		}
	}
	//собственно на данный момент на карусели вертятся одновременно3 пассажира, они меняют свои имена и станции но их всегда трое
	while (events.size() > 0) {

		// get event
		Event *ev = events.begin()->second;
		int Time = events.begin()->first;
		//мы удаляем совершившиеся событие из очереди событий
		events.erase(events.begin());

		/*тут мы как-то обрабатываем свершившееся событие, кстати в момент обработки текущго события
		в очередь бобавляется следующшее планируемое событие. Т.е одно событие произошло автобус пришел на оcтановку, мы удалили
		событие из очереди, а тут же автобус запланировал себе приход на следующую остановку, и мы добавлили новое событие в очередь*/
		ev->process();


		/*В этих двух циклах выполняется следующее: мы перебираем массив пассажиров, для каждого отдельного пассажира мы проверяем, не сидит 
		ли он в автобусе p[i]->currentBus==NULL
		если не сидит мы перебираем автобусы и смотрим какой автобус в данный момент стоит на сотановк, после чего садим пассажира в него*/
		for (int i = 0; i < 3; ++i){
			if (p[i] == NULL){// т.к пассажиры будут удаляться из массива, нам нужно проверять есть ли в текущем указателе пассажир, и если его нет мы должны егшо сделать
				p[i] = new Passenger(route.begin() + rand() % (route.size() - 1), names[rand() % (names.size() - 1)], rand() % route.size() + 1);
				cout << "passenger " << p[i]->name << " come to " << p[i]->position->name << endl;
			}

			if (p[i]->currentBus == NULL){
				for (size_t j = 0; j < park.size(); ++j){//посадили пассажира на автобус
					if (park[j].position == p[i]->position){
						p[i]->currentBus = &park[j];
						p[i]->position = p[i]->currentBus->position;
						cout << "Passenger " << p[i]->name << " take a bus number " << park[j].number << " on station " << p[i]->position->name <<
						" and need to pass " << p[i]->positionCounter << " stations" << endl;
						break;
					}
				}
			}
		}

		/*Мы проверяем каждого пассажира, если он сидит в автобусе, и его(пассажира) позиция не равняется текущей позиции автобуса, 
		значит автобус с остановки уже уехал, прихватив пассажира,
		и мы меняем положение позиции пассажира, после чего делаем декремент от количеств остановок которое пассажир должен проехать*/
		for (int i = 0; i < 3; ++i){
			if (p[i]->currentBus != NULL && p[i]->position != p[i]->currentBus->position){
				p[i]->position = p[i]->currentBus->position;
				(p[i]->positionCounter)--;
				if (p[i]->positionCounter == 0){//пассажир приехал - удаляем его из массива, и делаем указатель равным 0
					cout << "Passenger " << p[i]->name << " was gone from bus number " << p[i]->currentBus->number << endl;
					delete p[i];
					p[i] = NULL;
				}
			}
		}

		// удаляем указатель на событие
		delete ev;

		int s = time(0);
		while (s == time(0));
	}

}
Exemple #18
0
/*
 * Opcje programu:
 * -d - uruchamia w trybie demona
 * -l <nazwa_pliku> - logowanie do pliku
 * -h wyswietla pomoc programu
 */
int main(int argc, char* argv[]) {
	int c;
	bool daemon = false;
	bool log = false;
	char *log_filename = NULL;

	while (argc != 1 && (c = getopt(argc, argv, "hdl:")) != -1) {
		switch (c) {
		case 'd':
			daemon = true;
			break;
		case 'l':
			log = true;
			log_filename = optarg;
			break;
		default:
			cout << "Opcje programu:" << endl;
			cout << "-d - uruchamia w trybie demona" << endl;
			cout << "-l <nazwa_pliku> - logowanie do pliku" << endl;
			cout << "-h wyswietla pomoc programu" << endl;
			exit(EXIT_FAILURE);
			break;
		}
	}

	if (daemon && !log) {
		cerr << "Nie mozna uzyc DLM w trybie demona bez opcji logowania"
				<< endl;
		exit(EXIT_FAILURE);
	}

	// logging
	int fd;
	if (log) {
		if ((fd = open(log_filename, O_WRONLY | O_CREAT | O_APPEND)) < 0) {
			cerr << "Nie moge otworzyc pliku do logowania o nazwie: "
					<< log_filename << endl;
			exit(EXIT_FAILURE);
		}
	}

	// demon
	if (daemon) {
		// zamkniecie deskryptorow cin cout cerr
		for (int i = 0; i < 3; ++i)
			close(i);

		// zmiana katalogu na katalog glowny
		chdir("/");

		// zerowanie maski trybu dostepu
		umask(0);

		// przejscie do pracy drugoplanowej
		if (0 != fork()) {
			exit(EXIT_SUCCESS);
		}

		// odlaczanie sie od grupy
		setpgrp();

		// ignorowanie sygnalow terminala
#ifdef SIGTTOU	/* SIGTTIN, SIDTSTP */
		signal(SIGTTOU, SIG_IGN);
#endif

		// odlaczenie sie od terminala
		int fdesc;
		if ((fdesc = open("/dev/tty", O_RDWR)) >= 0) {
			ioctl(fdesc, TIOCNOTTY, (char*) 0);
			close(fdesc);
		}
	}

	// logging
	if (log) {
		dup2(fd, 1); // fd -> stdout
		dup2(fd, 2); // fd -> stderr
	}

	int dlmfifo;
	if (mkdir(DLM_PATH, 00777) != 0) {
		if (errno != EEXIST) {
			cerr << "Nie moge utworzyc katalogu o nazwie: " << DLM_PATH << endl;
			return EXIT_FAILURE;
		}
	}
	if (mkfifo(DLM_FIFO_PATH, 00666) < 0) {
		if (errno != EEXIST) {
			cerr << "Nie moge utworzyc kolejki fifo o nazwie: " << DLM_FIFO_PATH << endl;
			return EXIT_FAILURE;
		}
	}
	if ((dlmfifo = open(DLM_FIFO_PATH, O_RDWR)) < 0) {
		cerr << "Nie moge otworzyc kolejki fifo o nazwie: " << DLM_FIFO_PATH << endl;
		unlink(DLM_FIFO_PATH);
		return EXIT_FAILURE;
	}

	signal(SIGALRM, timeout_alarm);
	signal(SIGTERM, close_event);
	signal(SIGINT, close_event);
	sigset_t toblock;
	sigemptyset(&toblock);
	sigaddset(&toblock, SIGALRM);

	cout << "DLM start" << endl;
	while (1) {
		DLMrequest request;
		while (read(dlmfifo, &request, sizeof(request)) != sizeof(request))
			;
		cout << "żądanie od pid: " << request.pid << endl;
		//sekcja krytyczna
		sigprocmask(SIG_BLOCK, &toblock, NULL);
		map<int, resource_clients>::iterator iter = resource_map.find(
				request.resource_id);
		if (request.lock_type == -1) { // zwalnianie zasobu
			cout << "-żądanie zwolnienia zasobu" << endl;
			if (iter != resource_map.end()) { // znaleziono zasob
				list<client>::iterator ret = find_by_pid(
						iter->second.active_clients, request.pid);
				cout << "--znaleziono zasob" << endl;
				if (ret != iter->second.active_clients.end()) { // znaleziono pid w aktywnych
					cout << "---znaleziono pid w aktywnych" << endl;
					iter->second.active_clients.erase(ret); // usuniecie klienta z aktywnych (zwolnienie zasobu)
					send_response(request.pid, UNLOCKED);
					try_grant(iter); // proba przydzielenia zasobu
				} else { // nie znaleziono pidu w aktywnych
					cout << "---nie znaleziono pidu w aktywnych" << endl;
					ret = find_by_pid(iter->second.waiting_clients,
							request.pid);
					if (ret != iter->second.waiting_clients.end()) { // znaleziono pid w oczekujacych
						cout << "----znaleziono pid w oczekujacych" << endl;
						iter->second.waiting_clients.erase(ret); // usuniecie klienta z oczekujacych
						erase_from_timestamp_map(request.pid,
								request.resource_id); // usuniecie klienta z kolejki timeout
						send_response(request.pid, UNLOCKED);
						try_grant(iter); // proba przydzielenia zasobu
					} else { // nie znaleziono pidu w oczekujacych
						cout << "----nie znaleziono pidu w oczekujacych"
								<< endl;
						// ktos zwalnia cos czego nie zajal
						send_response(request.pid, ENOTLOCKED);
					}
				}
				if (iter->second.waiting_clients.empty()
						&& iter->second.active_clients.empty()) { // usuniecie nieuzywanego zasobu
					cout << "usuniecie nieuzywanego zasobu resource_id: " << iter->first << endl;
					resource_map.erase(iter);
				}
			} else { // nie znaleziono zasobu
				cout << "--nie znaleziono zasobu" << endl;
				// ktos zwalnia cos czego nie zajal i to cos nie istnieje wogole
				send_response(request.pid, ENOTLOCKED);
			}
		} else { // przydzielanie zasobu
			cout << "-żądanie przydzielenia zasobu" << endl;
			if (iter != resource_map.end()) { // znaleziono zasob
				cout << "--zasob juz istnieje" << endl;
				// sprawdzenie czy klient nie oczekuje juz na zasob lub z niego korzysta
				list<client>::iterator ret = find_by_pid(
						iter->second.active_clients, request.pid);
				if (ret == iter->second.active_clients.end()) { // nie znaleziono pidu w aktywnych
					cout << "---nie znaleziono pidu w aktywnych" << endl;
					ret = find_by_pid(iter->second.waiting_clients,
							request.pid);
					if (ret == iter->second.waiting_clients.end()) { // nie znaleziono pidu w oczekujacych
						cout << "----nie znaleziono pidu w oczekujacych"
								<< endl;
						client c(request.pid, request.lock_type);
						if (request.timeout < 0) { // ujemny timeout
							// sprawdzic, czy mozna przydzielic i od razu wyslac odpowiedz
							bool collision = false;
							for (list<client>::iterator i =
									iter->second.active_clients.begin();
									i != iter->second.active_clients.end();
									++i) {
								if (!lock_matrix[request.lock_type][i->lock_type]) { // jesli blokada koliduje z zalozona blokada
									collision = true;
									break;
								}
							}
							if (!collision) { // przydzielamy zasob
								if (request.timeout != -2) { // funkcja trylock nie przydziela zasobu
									iter->second.active_clients.push_back(c);
									send_response(c.pid, GRANTED);
								} else {
									send_response(c.pid, FREE);
								}
							} else { // nie przydzielamy zasobu
								send_response(c.pid, LOCKED);
							}
						} else { // nieujemny timeout
							iter->second.waiting_clients.push_back(c); // dodanie klienta na koniec kolejki oczekujacych
							if (request.timeout > 0) { // dodanie do kolejki timeout o ile timeout > 0
								client_timeout ct(request.pid,
										request.resource_id);
								struct timeval tv;
								struct timezone tz;
								gettimeofday(&tv, &tz); // pobranie aktualnego czasu
								long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000; // zamiana na ms
								multimap<long, client_timeout>::iterator current =
										timestamp_map.insert(
												make_pair(ts + request.timeout,
														ct));
								if (current == timestamp_map.begin())
									ualarm(
											(timestamp_map.begin()->first - ts)
													* 1000, 0); // przestawianie alarmu
							}
							try_grant(iter); // proba przydzielenia zasobu
						}
					} else { // znaleziono pid w oczekujacych
						cout << "----znaleziono pid w oczekujacych" << endl;
						send_response(request.pid, EAGAIN);
					}
				} else { // znaleziono pid w aktywnych
					cout << "---znaleziono pid w aktywnych" << endl;
					send_response(request.pid, EAGAIN);
				}
			} else { // nie znaleziono zasobu
				cout << "--zasob jeszcze nie istnieje" << endl;
				if (request.timeout != -2) { //funkcja try_lock nie przydziela zasobu
					// stworz id zasobu i przydziel zasob
					client c(request.pid, request.lock_type);
					resource_clients rc;
					rc.active_clients.push_back(c);
					resource_map[request.resource_id] = rc;
					send_response(c.pid, GRANTED);
				} else {
					send_response(request.pid, FREE);
				}
			}
		}
		// koniec sekcji krytycznej
		sigprocmask(SIG_UNBLOCK, &toblock, NULL);
	}

	return EXIT_SUCCESS;
}
void InputHandler::initParamsFromDOM(DOMNode *node, multimap<string, ParamWrapper*> registry)
{

     assert(node);
     char *tmp = XMLString::transcode(node->getNodeName());
     crusde_debug("%s, line: %d, initParamsFromDOM for node: %s.", __FILE__, __LINE__, tmp);
     XMLString::release(&tmp);

     DOMNode *child = node->getFirstChild();
     DOMNamedNodeMap *attributes = NULL;
     DOMNode *attr = NULL;

     multimap<string, ParamWrapper*>::iterator key_iter;
     pair< multimap<string, ParamWrapper*>::iterator, multimap<string, ParamWrapper*>::iterator > key_range;

     list<string> params_set;

     int count_keys(-1);
     unsigned int count_set(0);
     int empty_param(0);

     while (child)
     {
          if( child->getNodeType() == DOMNode::ELEMENT_NODE)
          {
               attributes = child->getAttributes();
               attr = attributes->getNamedItem(ATTR_name.xmlStr());

               char* name = XMLString::transcode(attr->getNodeValue());

               if( XMLString::compareIString(child->getNodeName(), TAG_parameter.xmlStr()) == 0 )
               {			
                    //get number of keys that equal name
                    count_keys = static_cast<int>( registry.count(string(name)) );
                    //if there is anything in the registry ... 
                    if( count_keys > 0 )
                    {		
                         //equal_range gives two results: an iterator to the first and last element with key==name
                         key_range = registry.equal_range(string(name));
                         //all keys have values that are adresses of double variables in the repsective plugins
                         //each of those variables now gets a value assigned. the same value.
                         for ( key_iter=key_range.first; key_iter != key_range.second; ++key_iter)
                         {
                              //get the value from the DOM
                              StrXML value(attributes->getNamedItem(ATTR_value.xmlStr())->getNodeValue());
                              //write it into the variable that's strored at key_iter->second
                              if( (key_iter->second)->isString() )
                              {
                                   (key_iter->second)->setValue( value.cppStr() );
                              }
                              else if( (key_iter->second)->isDouble() )
                              {
                                   (key_iter->second)->setValue( static_cast<double>( atof(value.cStr()) ) );
                              }

                              ++count_set;
                         }
                         //memorize the key that was set only once
                         params_set.push_back(key_range.first->first);
                    }
                    else //if not found there might be a spelling mistake either in input, or plugin, or both :)
                    {
                         StrXML paramName(attributes->getNamedItem(ATTR_name.xmlStr())->getNodeValue());
                         crusde_warning("Parameter %s (coming from the experiment definition) not registered! Misspelled in XML file or Plug-in definition?",paramName.cStr());
                    }
               }

               XMLString::release(&name);         	
          }
		
          child = child->getNextSibling();
     }
	
     //two cases are possible for leftover parameters: 
     //     i)  they are optional parameters - check registry for that
     //     ii) they are unitialized - oh well, we gotta stop there
     if(count_set < registry.size())
     {
          multimap<string, ParamWrapper*>::iterator map_iter = registry.begin();
          list<string>::iterator found_iter;
          string out_string("Error: Parameters that remain uninitialized: \n");		

          while (map_iter != registry.end())
          {
               if(!map_iter->second->isOptional())
               {
                    found_iter = params_set.begin();
                    //as long as we're not at the end and could not find the parameter in the list of set 
                    //parameters, continue looking.
                    while( found_iter != params_set.end() && (*found_iter).compare(map_iter->first) != 0  ) 
                    {
                         ++found_iter;
                    } 	
                    //if we're at the end, the parameter was not in out list ... tell the user.			
                    if(found_iter==params_set.end() ) 
                    {
                         ++empty_param;
                         out_string.append("\t");
                         out_string.append(map_iter->first);
                         out_string.append("\n");
                    }
               }
		
               //get to the next unique value in the registry ... avoid printing multiple keys twice.
               map_iter = ( registry.equal_range(map_iter->first) ).second;
          }

          if(empty_param > 0) 
          {
               crusde_error("%s, Aborting.", out_string.c_str());
          }
     }		
}	
Exemple #20
0
//FIRST SET
string first( string a,multimap<char,string>& map)
{
    string first_ans;

    for (multimap<char, string>::iterator it = map.begin();it != map.end();++it)
    {
        if ( it->first == a[0])
        {
            //rhs is non-terminal
            for( int k = 0 ; k < (it->second).length(); k++)   //searching rhs of production
            {
               // multimap<char,string> cfg;
                bool repeat = false;
                string rhs;
                rhs += (it->second)[k];

                string tmp ;
                tmp  = first(rhs, map);

               const int length = tmp.length();
                //searching tmp for epsilon
                for( int i=0;i<length;i++)
                {
                    if ( tmp[i] == 'e')
                    {
                        if ( k != (it->second).length())                     //last symbol on rhs dont remove
                        tmp.erase(i,1);

                        first_ans += tmp;
                        repeat = true;        //epsilon found go to next symbol
                        break;
                    }
                }

                //epsilon not found  leave the loop
                if ( repeat == false)
                {

                    first_ans += tmp;
                    break;
                }
            }
        }

        else              //terminal        is part of loop not necessary but keep this way
            {
                for( int j=0 ; j< terminals.length();j++)
                {
          //          cout << "terminal " << terminals[j] << endl;
                    if ( a[0] == terminals[j] )
                    {
                        first_ans += a[0];
                    //   cout << first_ans << endl;
                       break;
                    }
               }
            }
    }


   //
    return first_ans;
}
const bool Octree :: build(multimap<unsigned int, Vector3d> &points,
                           const unsigned int count,
                           const unsigned int maximumdepth,
                           const Bounds &bounds,
                           const unsigned int currentdepth)
{
//    cout << "\n ----------------------------------- " <<endl;
//    cout << "CURRENT DEPTH: " << currentdepth << endl;
//    cout << "Current Center: " << bounds.center << endl;
//    cout << "Current radius: " << bounds.radius << endl;

    // STOPPING condition of recursive
    if (currentdepth >= maximumdepth){

        // The node is now no longer subjected to sub-division.
        // At this point node is a leaf
        // Store final count of points
        _density = count;

        // Store the Node center, also known as feature point
        _center = bounds.center;

        // copy all points from multimap into _points of node
        multimap<unsigned int, Vector3d>::const_iterator iter;  //iterator to traverse the vector
        for ( iter = points.begin();  iter != points.end(); ++iter)
        {
            _points.push_back(iter->second);
        }

        //        cout << "\nHere we go" << endl;
        //        for(unsigned int i = 0; i < _points.size() ; i++){
        //            cout << _points[i] << endl;
        //        }

        // cout << "_Center: "  << _center << endl;
        // cout << "BREAKING CONDITION REACHED" << endl;
        // cout << " -----------------------------------------------------------" << endl;
        // cout << " -----------------------------------------------------------" << endl;

        return true;

    }


    //The variable below keeps count of points in 8 child nodes
    unsigned int childPointCounts[8] = {0,0,0,0,0,0,0,0};

    // CLASSIFY EACH POINT to a child node
    // This means 'count' is DIVIDED into 8 different 'childPointCounts'.
    multimap<unsigned int, Vector3d>::iterator it;    // iterator to traverse all the points of this node

    // create a new multimap whose key holds the information about the point's childnode location
    // we have to go through this trouble as we cannot edit key of multimap 'points' // i should change this later
    multimap<unsigned int, Vector3d> newpoint;

    for(it = points.begin(); it != points.end(); it++)
    {

        // center of this node
        Vector3d cur_c;
        cur_c = bounds.center;

        // determine the node current point, cur_p belongs to and store this information on cell location in "code" base on
        // relative position of the point to the center of the current node
        unsigned int key = 0;

        if (it->second[0] > cur_c[0]){ key |= 1; }
        if (it->second[1] > cur_c[1]){ key |= 2; }
        if (it->second[2] > cur_c[2]){ key |= 4; }

        // Keep track of number of points in each child.
        childPointCounts[key]++;

        // update points with key information
        newpoint.insert(pair< unsigned int, Vector3d>(key,it->second));     //cout << it->second; ok
    }

    // Recursively call build() for EACH OF 8 CHILD NODE

    for(unsigned int i= 0; i < 8; i++)
    {

        // If there aren't any more vector <Vector3d> in this child, do not subdivided
        if(!childPointCounts[i]) continue;

        // ALLOCATE the child
        _child[i] = new Octree;

        // create TEMPORARY list of points that were coded just for this child only
        // That is ,temporary list to hold points of i'th node
        multimap<unsigned int, Vector3d> newMMap;

        // Go through the input list of points and copy over the points that
        // were coded for this child,
        for (it = newpoint.begin(); it != newpoint.end(); it++)
        {
            if (it->first == i)
            {
                newMMap.insert(pair<unsigned int, Vector3d>(i, it->second));
            }
        }

        unsigned int newCount = childPointCounts[i];

        // At this vector <Vector3d>, we have a list of points that will belong to this child node.
        // We'll want to remove any vector <Vector3d> with a duplicate 'n' in them
        // This results 'childPointCounts' to be turned into 'newCount' with duplication removed

        // Generating a new bounding volume
        Vector3d boundOffsetTable[8] =
        {
            {-0.5, -0.5, -0.5},
            {+0.5, -0.5, -0.5},
            {-0.5, +0.5, -0.5},
            {+0.5, +0.5, -0.5},
            {-0.5, -0.5, +0.5},
            {+0.5, -0.5, +0.5},
            {-0.5, +0.5, +0.5},
            {+0.5, +0.5, +0.5}
        };



        // Vector3d boundOffsetTable[8];
        // boundOffsetTable[0](-0.5, -0.5, -0.5);
        // calculate offset from the center of the parent's node to the center of the child's node

        Vector3d offset;
        offset = boundOffsetTable[i] * bounds.radius;


        // create a new Bounds, with the center offset and half the radius
        Bounds newBounds;
        newBounds.radius = bounds.radius * 0.5;

        newBounds.center = bounds.center + offset;

        // Recurse
        _child[i]->build(newMMap, newCount, maximumdepth, newBounds, currentdepth+1 );

        // Clean Up the temporary multimap newMMap

        multimap<unsigned int, Vector3d> temp;
        temp.swap(newMMap);     // just swap the multimap newMMap with empty variable that goes out of scope.
    }
    return true;
}
Exemple #22
0
int main(int argc, char**argv)
{
/*
    if(argc > 4 || argc <= 1)
    {
        cerr << "Invalid Inputs!!" << endl;
        exit(1);
    }

    string fns = argv[1];
    string cns = argv[2];
*/

    DataAnalytics* Analysis = new DataAnalytics();
    string fns = "../../apriori/recode_reformat/FunctionalOP" ;
    string cns = "../../apriori/recode_reformat/CompositionOP";

	ifstream input_fns(fns.c_str(), ifstream::in);
	ifstream input_cns(cns.c_str(), ifstream::in);
	ofstream output_fns;
	ofstream output_cns;

	if(!input_fns || !input_cns)
	{
		cerr << "Input File not found !!!" << endl;
		exit(1);
	}
	output_fns.open("../../apriori/recode_reformat/Functional_list.csv", ofstream::out);
	output_cns.open("../../apriori/recode_reformat/Composational_list.csv", ofstream::out);

    //Print Header
    output_fns << "Frequency, Node Type , 1_1, 1_8, 1_16, 1_32, 8_1, 8_8, 8_16, 8_32, 16_1, 16_8, 16_16, 16_32, 32_1, 32_8, 32_16, 32_32" << endl;
    output_cns << "From Node Type , a , b, c, d, e, f, g, h, i, j, k , l, m , n, o , p , q, r, s, t, u, v, w, x, y, z" << endl;

    if(!getThelist(input_fns, output_fns, functionType, Analysis))
    {
        cerr << "Could not get the Functional List " << endl;
        exit(1);
    }
    if(!getThelist(input_cns, output_cns, compositionType, Analysis))
    {
        cerr << "Could not get the Compostional List " << endl;
        exit(1);
    }

    // Map to Array

    // FLush array - A bug modifies the values somewhere - so flush all
    for(int i = 0; i < 26*26; i++)
        compMAT[i] = 0;

   // cout << "Array MAP" << endl;
    for(multimap<pair<string, string>, int>::iterator it = compMap.begin(); it!= compMap.end(); it++)
    {
        compLine++;
        stringstream ss;
       if(debug)
         {
            ss << it->first.first << ", " << it->first.second << " , " << it->second << endl;
            cout << ss.str();
         }

        // get char value
        const char* fromType = it->first.first.c_str();
        const char* toType = it->first.second.c_str();

        if(debug)
            cout << "Before assign : " << compMAT[((int(fromType[0])-97) * 26)  + ((int(toType[0])-97))] << endl;

        compMAT[((int(fromType[0])-97) * 26)  + ((int(toType[0])-97))] += it->second;

        if(debug)
        {
            cout << compMAT[((int(fromType[0])-97) * 26)  + ((int(toType[0])-97))] << " @ " << int(fromType[0] - 97) << fromType[0]
            << "    " <<  int(toType[0]-97) << toType[0]
            << " while types " << it->second << endl;
        }

    }

    //Map all to Composition List - DataAnalytics
    for(int i = 0; i < 26; i++)
    {
        for(int  j = 0; j < 26; j++)
        {
            if(compMAT[i*26 + j]!= 0)
            {
                char from = i+97;
                char to = j+97;

                stringstream s1, s2;
                s1 << from;
                s2 << to;

                if(debug)
                {
                    cout << s1.str() << " : " << s2.str() << endl;
                }

                Analysis->addComposition(s1.str(), s2.str(), compMAT[i*26 + j], 0);
            }
        }
    }



  //  cout << "Array Map end" << endl;

    // Print COmp list in csv
    for(int i = 0; i < 26; i++)
    {
        stringstream ss;
        ss << char(i+97);
        for(int j = 0; j < 26; j++)
        {
            ss << "," << compMAT[(i*26) + (j)] ;
        }

        ss << endl;
    //    cout << ss.str();
        output_cns << ss.str();
    }

	input_fns.close();
	input_cns.close();
	output_fns.close();
	output_cns.close();


   // Analysis->printFunctionMap() ;

    cout << "------------------------------" << endl;

   // Analysis->printCompMap();

    /*
	int number_of_compo = 0;
	cout << " \n ---------------------------------------------------------------\n" ;
	cout << " Max_Freq Compositions :  ";
	cout << endl << "Enter the number of compostions : " ; cin  >> number_of_compo;
    */

    Analysis->linearWeightedSelection();

    cout << " Total size :: Functions - Compositions :: " << Analysis->getFuncMapCapacity() << " - " << Analysis->getCompMapCapacity();
    int num_of_C;
    cout << " Number of Function Compositions?? : " ;
    cin >> num_of_C;


    Analysis->report(num_of_C);

	return 0;
}
void InterCodeGenerator:: startGenerate(multimap<int, Node> parsingTree){


	bool in_Expr = false;  // is in Expr block
	bool unaryOp = false;
	int Expr_index = -1;   // record the Expr index
	string go_to;
	in_while = false;
	queue<Node> tmp;      // tmp stack push expr symbol    
	queue<Node> postfix_expr;

	/* ParsingTree ->  produce three address code by postfix */
	for(multimap<int, Node>::iterator itr = parsingTree.begin(); itr != parsingTree.end(); ++ itr){
		
		string token =  itr -> second.token;
		string symbol =  itr -> second.symbol;
		string catergory = itr -> second.catergory;
		int index = itr -> second.index;

		// Start ALU expression
		if( !in_Expr && token.compare("Expr") == 0 ){
			// set flag and record index
			in_Expr = true;                     
			Expr_index = index;
		}
		// End ALU expression
		else if( index == Expr_index ){

			// exit expr block unset flag and index
			in_Expr = false;
			Expr_index = -1;

			// converts to  postfix  format
			postfix_expr = postfix(tmp);
			
			// clear
			while( !tmp.empty() ){
				tmp.pop();
			}
			//create quadruples
			createQuadruples(postfix_expr);
		}
		// in exprssion block push all Identifier operator Number char
		else if( ( catergory.compare("Identifier") == 0 || catergory.compare("Operator")  == 0 \
					|| catergory.compare("Number")  == 0 || catergory.compare("Char") == 0)   && in_Expr ){
		
			// char to int
			if( catergory.compare("Char") == 0){
				char ch = itr -> second.symbol[1];
				itr -> second.symbol = 	toString( int(ch) );
			}

			Node push_item = (itr) -> second;
			// unaryOP
			++itr;
			if( unaryOp && symbol.compare("-") == 0 ){
				tmp.push(Node(0, "0", "0", "Number"));
				unaryOp = false;
			}
			// not op
			else if( unaryOp && catergory.compare("Operator") != 0 ){
				tmp.push( push_item  );
				unaryOp = false;
			}
			
			// array 
			if ( (itr++) -> second.symbol.compare("[") == 0 ){
				queue<Node> array_queue;
				array_queue.push( Node(0, "0", "0", "Number") );
				array_queue.push( Node(0, "+", "+", "Operator") );
				while( (++itr) -> second.symbol.compare("]") != 0 ){
					if ( islower( itr-> second.token[0] ) || itr-> second.catergory.compare("Operator") == 0 ){
						array_queue.push( itr-> second );		
					}
				}
				postfix_expr = postfix(array_queue);
				createQuadruples(postfix_expr);
				quadruples.push_back( Quadruples("[]=", "t" + toString(t_index-1), "",  push_item.symbol)  );
				tmp.push( push_item );
			}
			else{
				tmp.push( push_item );
				--itr;
			}
		}		
		else if( whileStmt(index, token, symbol) ){
		}
		// if statement
		else if( ifStmt(index, token, symbol) ){
		}
		else if ( token.compare("UnaryOp") == 0 ){
			unaryOp = true;
		}
	}

	outputQuadruples();
}
Exemple #24
0
//FOLLOW SET
string follow( string a , multimap<char,string>& map)    //enter cfg
{
    string follow_ans;

    for (multimap<char,string>::iterator it = map.begin();it != map.end();++it)
    {
                     //means it is the start symbol
        if ( it == map.begin() && a[0] == it->first)
        {
            follow_ans += "$";
        }

        //scanning rhs to apply 2 rule to find follow
        if ( (it->second).find(a[0]) != string::npos)   // we found a[0]
        {
            int length = (it->second).length();
            int pos = (it->second).find(a[0]);
            string e = "e";

            if ( (pos+1) < length )              //not the last symbol
            {
                string p,tmp;
                //p += (it->second)[pos+1];         //change this to accomaodate whole till end
                p = (it->second).substr(pos+1,length-pos-1);
                tmp = firstMain(p,map);

                while( tmp.find(e) != string::npos)
                tmp.erase((tmp.find(e)),1);               //removing epsilon from first

                follow_ans += tmp;
            }

            string tmp2;
            tmp2 = (it->second).substr(pos+1,length-pos-1);     //need to modify this rule

        //    cout << "POS  " << pos+2 << " length " << length << " FOLLOW OF " << a[0] << " rhs " << it->second << endl;
            //applying 3 rule
            if ( (pos+1) == length  )
            {
                 string tmp1;
                 tmp1 += it->first;

                 if ( tmp1[0] == a[0])
                 {
                    continue;
                 }

             //    cout << "FOLLOW  "  << follow(tmp1,map) << " to be added in " << a[0] << endl;
                 follow_ans += follow(tmp1,map);
              //   cout << "Which is " << follow_ans << endl;
            }

            else if ( firstMain(tmp2,map).find(e) != string::npos )   //first has epsilon
            {
                string a1;
                a1 += it->first;

                if ( a1[0] == a[0]) continue;
                follow_ans += follow(a1,map);
            }
        }


    }

    return follow_ans;
}
Exemple #25
0
int _tmain(int argc, _TCHAR* argv[])
{
	fetch_proxy_from_web("www.cnproxy.com", "http://www.cnproxy.com/proxyedu1.html");
	fetch_proxy_from_web("www.cnproxy.com", "http://www.cnproxy.com/proxyedu2.html");

	evaluate_proxy();

	//fstream f("proxy_list.txt", ios::out | ios::trunc);
	//for(multimap<double, std::string>::iterator it = final_proxy_list.begin(); it!=final_proxy_list.end(); ++it )
	//	f << it->second << endl;
	//f.close();

	/// write it into firefox switch proxy add-on
	std::string file_path = std::getenv ("APPDATA");
	file_path.append("\\Mozilla\\Firefox\\Profiles\\rx4zwdfi.default\\localstore.rdf");

	TiXmlDocument doc( file_path.c_str() );

	if(!doc.LoadFile())
	{
		std::cout << "file not exist" << endl;
		assert(0);
	}

	TiXmlElement * general_elem = 0;
	TiXmlElement * elem = 0;
	TiXmlElement * temp_elem = 0;

	general_elem = doc.FirstChildElement();

	///remove all old proxies items
	elem = general_elem->FirstChildElement();

	while(elem)
	{
		if(elem->Attribute("NS1:networkProxyType") != 0)
		{
			temp_elem = elem;
			elem = elem->NextSiblingElement();
			general_elem->RemoveChild(temp_elem);
		}
		else
			elem = elem->NextSiblingElement();
	}

	///clear general entry list for proxy items
	elem = general_elem->FirstChildElement();

	while(elem)
	{
		std::string elem_value = elem->Value();
		if(elem_value == "RDF:Seq")
		{
			elem->Clear();
			break;
		}
		else
			elem = elem->NextSiblingElement();
	}

	///now we have elem as RDF:Seq node, we add entry our own entry list into it
	int i = 0;
	for(multimap<double, std::string>::iterator it = final_proxy_list.begin(); it!=final_proxy_list.end(); ++it )
	{
		TiXmlElement entry_elem("RDF:li");
		entry_elem.SetAttribute("RDF:resource", i);
		elem->InsertEndChild(entry_elem);

		TiXmlElement actual_elem("RDF:Description");

		char buffer[256];
		sprintf(buffer, "cnproxy-%02d", i);
		actual_elem.SetAttribute("NS1:name", buffer);

		actual_elem.SetAttribute("RDF:about", i);
		actual_elem.SetAttribute("NS1:networkProxyType", "1");

		std::vector<std::string> ip_port;
		qtk::misc::qString::TokenizeString(it->second, ":", ip_port);
		actual_elem.SetAttribute("NS1:networkProxyHTTP", ip_port[0]);
		actual_elem.SetAttribute("NS1:networkProxyHTTP_Port", ip_port[1]);

		actual_elem.SetAttribute("NS1:networkProxySOCKSVersion", "4");
		actual_elem.SetAttribute("NS1:networkProxySSL", "");
		actual_elem.SetAttribute("NS1:networkProxySSL_Port", "");
		actual_elem.SetAttribute("NS1:networkProxyFTP", "");
		actual_elem.SetAttribute("NS1:networkProxyFTP_Port", "");
		actual_elem.SetAttribute("NS1:networkProxyGopher", "");
		actual_elem.SetAttribute("NS1:networkProxyGopher_Port", "");
		actual_elem.SetAttribute("NS1:networkProxySOCKS", "");
		actual_elem.SetAttribute("NS1:networkProxySOCKS_Port", "");
		actual_elem.SetAttribute("NS1:networkProxyNone", "");
		actual_elem.SetAttribute("NS1:networkProxyAutoconfigURL", "");
		general_elem->InsertEndChild(actual_elem);

		i++;
	}

	///finally, add JAP
	TiXmlElement entry_elem("RDF:li");
	entry_elem.SetAttribute("RDF:resource", "JAP");
	elem->InsertEndChild(entry_elem);

	TiXmlElement actual_elem("RDF:Description");
	actual_elem.SetAttribute("NS1:name", "JAP");
	actual_elem.SetAttribute("RDF:about", "JAP");
	actual_elem.SetAttribute("NS1:networkProxyType", "1");
	actual_elem.SetAttribute("NS1:networkProxyHTTP", "127.0.0.1");
	actual_elem.SetAttribute("NS1:networkProxyHTTP_Port", "4001");
	actual_elem.SetAttribute("NS1:networkProxySOCKSVersion", "4");
	actual_elem.SetAttribute("NS1:networkProxySSL", "");
	actual_elem.SetAttribute("NS1:networkProxySSL_Port", "");
	actual_elem.SetAttribute("NS1:networkProxyFTP", "");
	actual_elem.SetAttribute("NS1:networkProxyFTP_Port", "");
	actual_elem.SetAttribute("NS1:networkProxyGopher", "");
	actual_elem.SetAttribute("NS1:networkProxyGopher_Port", "");
	actual_elem.SetAttribute("NS1:networkProxySOCKS", "");
	actual_elem.SetAttribute("NS1:networkProxySOCKS_Port", "");
	actual_elem.SetAttribute("NS1:networkProxyNone", "");
	actual_elem.SetAttribute("NS1:networkProxyAutoconfigURL", "");
	general_elem->InsertEndChild(actual_elem);

	doc.SaveFile(file_path.c_str());

	return 0;
}
Exemple #26
0
void testOneFunction( std::string funcParamName, 
		      vector<string> argvList,
		      bool debug, int nrOfNodes, 
		      multimap <string, int> results,
		      multimap <string, int> useresults) {
  if (debug)
    cout <<"\n\n------------------------------------------\ntesting ... " << argvList[1] << endl;
  // Build the AST used by ROSE
  SgProject* project = frontend(argvList);
  // Call the Def-Use Analysis
  DFAnalysis* defuse = new DefUseAnalysis(project);
  int val = defuse->run(debug);
  if (debug)
    std::cout << "Analysis run is : " << (val ?  "failure" : "success" ) << " " << val << std::endl;
  if (val==1) exit(1);

  if (debug==false)
    defuse->dfaToDOT();

  //std::list<SgNode*> vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition);
  //std::list<SgNode*>::const_iterator i = vars.begin();
  NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition);
  NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin();
  for (; i!=vars.end();++i) {
    SgFunctionDefinition* func = isSgFunctionDefinition(*i);
    std::string name = func->class_name();
    string funcName = func->get_declaration()->get_qualified_name().str();
    int maxNodes = defuse->getDefSize();
    int nodeNr = defuse->getIntForSgNode(func);
    if (nodeNr == -1)
      continue;
    //cout << " checking function : " << funcName << endl;
    if (funcName!=funcParamName)
      continue;

    if (debug)
      cout << "\n------------------------\nchecking for " << name << " -- " << funcName << " -- " << nodeNr << endl;
    if (maxNodes!=nrOfNodes) {
	cerr << " Error: Test should have " << nrOfNodes << " nodes. found: " << maxNodes << endl;
      abort();
    }
    if (debug)
      cout << " Test has nodes:  " << nrOfNodes <<  endl;
    if (debug)
      cout <<"\nChecking all definitions ... " << endl;
    // check nodes in multimap
    std::vector <std::pair < SgInitializedName*, SgNode*> > map = defuse->getDefMultiMapFor(func);
    if (map.size()>0) {
      std::vector < std::pair <SgInitializedName*, SgNode*> >::const_iterator j = map.begin();
      unsigned int hit=0;
      SgNode* node = NULL;
      string name="";
      for (;j!=map.end();++j) {
	SgInitializedName* in_node = j->first;
	node = j->second;
	name= in_node->get_qualified_name().str();
	if (debug)
	  cout << " ... checking :  " << name << endl;
	multimap <string, int>::const_iterator k =results.begin();
	for (;k!=results.end();++k) {
	  string resName = k->first;
	  int resNr = k->second;
	  int tableNr = defuse->getIntForSgNode(node);
	  if (name==resName)
	    if (debug)
	      cout << " ... defNr: " << resNr << "  inTable: " << tableNr <<  endl; 
	  if (name==resName && tableNr==resNr) {
	    hit++;
	    if (debug)
	      cout << " Hit " << hit << "/" << results.size() << " - (" << name << "," << resNr << ")" << endl;
	  }
	}

      }
      if (hit!=results.size()) {
	cerr << " Error: No hit! ... DFA values of node " << nodeNr << " are not correct! " << endl;
	exit(1);
      }
    } else {
      if (results.size()!=0) {
	cerr << " Error: Test node " << defuse->getIntForSgNode(func) << " should have a multimap. " << endl;
	exit(1);
      }
    }
  
    if (debug)
      cout <<"\nChecking all uses ... " << endl;
    // check nodes in multimap
    map = defuse->getUseMultiMapFor(func);
    if (map.size()>0) {
      std::vector <std::pair <SgInitializedName*, SgNode*> >::const_iterator j = map.begin();
      size_t hit=0;
      for (;j!=map.end();++j) {
	SgInitializedName* in_node = j->first;
	SgNode* node = j->second;
	string name= in_node->get_qualified_name().str();
	if (debug)
	  cout << " ... checking :  " << name << endl;
	multimap <string, int>::const_iterator k =useresults.begin();
	for (;k!=useresults.end();++k) {
	  string resName = k->first;
	  int resNr = k->second;
	  int tableNr = defuse->getIntForSgNode(node);
	  if (name==resName)
	    if (debug)
	      cout << " ... defNr: " << resNr << "  inTable: " << tableNr <<  endl; 
	  if (name==resName && tableNr==resNr) {
	    hit++;
	    if (debug)
	      cout << " Hit " << hit << "/" << useresults.size() << " - (" << name << "," << resNr << ")" << endl;
	  }
	}

      }
      if (hit!=useresults.size()) {
	cerr << " Error: No hit! ... DFA values of node " << nrOfNodes << " are not correct! " << endl;
	exit(1);
      }
    } // if
  }
  if (debug)
    std::cout << "Analysis test is success." << std::endl;
}
Exemple #27
0
int main(int argc, char** argv)
{
    // Allow one argument (base image) or three arguments (base, extend, offset)
    if ((argc != 2) && (argc != 4))
    {
        print_usage();
    }

    // Get base image name.
    g_imageName = argv[1];
    add_image_subdir(g_imageName);

    // Get extended image name.
    if (argc > 3)
    {
        g_extImageName = argv[2];
        add_image_subdir(g_extImageName);

        // Read extended image offset from options.
        if (1 != sscanf(argv[3], "%lx", &g_extImageOffset))
        {
            print_usage();
        }
    }

    // Open base image.
    int base_fd = open(g_imageName.c_str(), O_RDONLY);
    if (-1 == base_fd)
    {
        fprintf(stderr, "Failed to open image file: %s.\n", g_imageName.c_str());
        exit(-1);
    }
    struct stat base_stat;
    if (0 != fstat(base_fd, &base_stat))
    {
        fprintf(stderr, "Failed to stat image file: %s.\n", g_imageName.c_str());
        exit(-1);
    }
    g_imageFileSize = base_stat.st_size;
    g_imageFile = (const char*) mmap(NULL, base_stat.st_size,
                                     PROT_READ, MAP_PRIVATE,
                                     base_fd, 0);

    // Open extended image.
    if (string() != g_extImageName.c_str())
    {
        int ext_fd = open(g_extImageName.c_str(), O_RDONLY);
        if (-1 == ext_fd)
        {
            fprintf(stderr, "Failed to open image file: %s.\n", g_extImageName.c_str());
            exit(-1);
        }
        struct stat ext_stat;
        if (0 != fstat(ext_fd, &ext_stat))
        {
            fprintf(stderr, "Failed to stat image file: %s.\n", g_extImageName.c_str());
            exit(-1);
        }
        g_extImageFileSize = ext_stat.st_size;
        g_extImageFile = (const char*) mmap(NULL, ext_stat.st_size,
                                            PROT_READ, MAP_PRIVATE,
                                            ext_fd, 0);
    }

    // Read CROSS_PREFIX environment variable.
    g_crossPrefix = getenv("CROSS_PREFIX");
    if (NULL == g_crossPrefix)
    {
        fprintf(stderr, "Environment variable CROSS_PREFIX not set.\n");
        exit(-1);
    }
    g_crossPrefix = strdup(g_crossPrefix);

    // Parse modinfo file for base image.
    parse_modinfo_file(g_imageName);

    // Create threads for each ELF object in the image(s) to get their symbol
    // information.
    vector<pthread_t*> threads;
    for(vector<pair<string, uint64_t> >::const_iterator i = g_modules.begin();
        i != g_modules.end(); ++i)
    {
        const string& m = i->first;
        // Filter out non-ELF files by filename.
        if (strstr(m.c_str(), ".o") || strstr(m.c_str(), ".elf") ||
            strstr(m.c_str(), ".so"))
        {
            pthread_t* thread = new pthread_t;
            pthread_create(thread, NULL, read_module_symbols,
                           new pair<string,uint64_t>(*i));
            threads.push_back(thread);
        }
    }

    // Wait for all threads to finish.
    for(vector<pthread_t*>::const_iterator i = threads.begin();
        i != threads.end(); ++i)
    {
        pthread_join(*(*i), NULL);
    }

    // Output (in order) each symbol information.
    for (multimap<uint64_t, string>::const_iterator i = g_symbols.begin();
         i != g_symbols.end(); ++i)
    {
        printf("%s", i->second.c_str());
    }

    return 0;
}
Exemple #28
0
//PREDICTIVE PARSING TABLE PART
void PrediciveParsingTable(multimap<char,parseEntry>& ptable ,multimap<char,string> cfg)
{
    //creating empty parsing table
    for( int i=0; i < nonterminals.length(); i++)
    {
        char lhs = nonterminals[i];

        for( int j=0; j< terminals.length();j++)
        {
            parseEntry temp;
            if( terminals[j] != 'e')
            {
                temp.input = terminals[j];
                ptable.insert(pair<char,parseEntry>(lhs,temp));
            }

        }
        //adding the dollar entry
        parseEntry dollar;
        dollar.input = "$";
        ptable.insert(pair<char,parseEntry>(lhs,dollar));
    }


    //computations
    for ( multimap<char, string>::iterator it = cfg.begin(); it != cfg.end(); ++it)
    {
        string t;
        string lhs1,rhs1 ;
        lhs1 += it->first;      //lhs
        rhs1 += it->second;     //rhs
        t += it->second;           //rhs
        string first1 = firstMain(t,cfg);
     //   cout << "\n ..............LHS............ " << it->first << endl;
      //  cout << "First of " << t << " is " << first1  << endl;   //checked right

        for( int k=0; k< first1.length(); k++)
        {
            if ( first1[k] == 'e')
            {
             //   cout << "taking e" << endl;
                string p1,follow1;
                p1 += it->first;
                follow1 = dupliRemove(follow(p1,cfg));
              //  cout << "Follow of " << p1 << " " << follow1  << endl;

                //for each terminal in follow
                for ( int l=0 ; l< follow1.length(); l++)
                {
                    for (multimap<char,parseEntry>::iterator pit = ptable.begin(); pit != ptable.end(); ++pit)
                    {
                        parseEntry temp = pit->second;
                   //     cout << "LHS: " << it->first << " INPUT: " << temp.input << " PRODUCTION: "  << temp.production << endl;

                        if ( (pit->first == it->first) && ((temp.input)[0] == follow1[l]))
                        {
                            string pro;
                            pro += lhs1 + " - " + rhs1;
                     //      cout << "CFG: " << it->first << " - " << it->second << endl;
                        //   cout << "CFG ACTUAL: " << pro << endl;
                            (pit->second).production += pro;
                        }
                    }
                }
            }

            else
            {
               //gives the range with same lhs in the map
                pair <multimap<char,parseEntry>::iterator, multimap<char,parseEntry>::iterator> range;
                range = ptable.equal_range(it->first);

                for (multimap<char,parseEntry>::iterator pit = ptable.begin(); pit != ptable.end(); ++pit)
                {
                    parseEntry temp = pit->second;
            //        cout << "LHS: " << it->first << " INPUT: " << temp.input << " PRODUCTION: "  << temp.production << endl;

                    if ( (pit->first == it->first) && ((temp.input)[0] == first1[k]))
                    {
                        string pro;
                        pro += lhs1 + " - " + rhs1;

                        (pit->second).production += pro;
                    }

                }

                //
            }
        }
    }
}
int main() {
    
    int n;
    
    while(cin >> n >> ws) {
        
        // reset.
        vexp.clear();
        vvar.clear();
        
        vals.clear();
        expr.clear();
        
        vreq.assign(n, set<string>());
        vidx.clear();
        
        q.clear();
        
        broken = false;
        ans = 0;
        result.clear();
        

        // input and calculation.
        for(int i = 0; i < n; ++i) {
            
            //cout << "i = " << i << endl;
            
            string s;
            getline(cin, s);
            int p = s.find(' ');
            
            vvar.push_back(s.substr(0, p));
            vexp.push_back(s.substr(p + 3));
            
            //cout << "vvar = |" << vvar[i] << "|" << endl;
            //cout << "vexp = |" << vexp[i] << "|" << endl;
            
            
            // scratch all the reference variables.
            // add sentienl to split.
            istringstream iss(vexp.back());
            string v;
            
            while(iss >> v) {
                
                // if variable gotcha.
                if(isalpha(v[0])) {
                    
                    // initialize variable list
                    vals[v] = 0;
                    expr[v] = "";
                    
                    // add requirement link
                    vreq[i].insert(v);
                    
                    // add index link, AVOID repeatly link.
                    if(vidx[v].size() == 0 || vidx[v].back() != i) { 
                        vidx[v].push_back(i);
                    }
                }
                
            }

            // if the value is clear.
            if(vreq[i].size() == 0) {
                
                i64 val = eval(vexp.back());
                
                q.insert(make_pair(val, i));
                
            }
            
        }
        
        // proccess
        while(!q.empty()) {
            
            // pop from priority queue.
            i64 val = q.begin()->first;
            int i = q.begin()->second;
            q.erase(q.begin());
            
            // get var.
            string v = vvar[i];
            
            if(vals[v] == 0) {
            
                result.push_back(i);
                
                vals[v] = val;
                expr[v] = vexp[i];
                
                for(int j = 0; j < vidx[v].size(); ++j) {
                    
                    int k = vidx[v][j];
                    
                    // kick the link.
                    vreq[k].erase(v);
                    
                    // if all requirement was calculated.
                    if(vreq[k].empty()) {
                
                        i64 val = eval(vexp[k]);
                        
                        if(val != 0) q.insert(make_pair(val, k));
                        
                    }
                }
                
            }
            
        }
        
        
        // reduce..
        if(result.size() < vals.size()) broken = true;
        
        for(map<string, i64>::iterator it = vals.begin(); it != vals.end(); ++it) {
            
            // if overflow
            if(ans + it->second < ans) {
                broken = true;
                break;
            }
            
            ans += it->second;
            
        }
        
        if(broken) {
            cout << "stupid expressions!" << endl;
        }
        else {
            cout << ans << endl;
            for(int i = 0; i < result.size(); ++i) {
                int j = result[i];
                cout << vvar[j] << " = " << vexp[j] << endl;
            }
        }
        
    }
    
    return 0;
    
}
Exemple #30
0
int CCacheThread::GetRequestCacheInfo( unsigned int dwServiceId, 
									  const void *pBuffer, 
									  unsigned int dwLen, 
									  string &strKey, 
									  string &strValue, 
									  multimap<unsigned int, string> &mapRequest,
									  multimap<unsigned int, string> &mapResponse,
									  unsigned int &dwKeepTime )
{
	
	TYPE_CODE_MAP mapTypeByCode = m_mapCodeTypeByService[dwServiceId];
	dwKeepTime = m_mapCacheKeepTime[dwServiceId];

	unsigned char *ptrLoc = (unsigned char *)pBuffer;
	while(ptrLoc < (unsigned char *)pBuffer + dwLen)
	{
		SSapMsgAttribute *pAtti=(SSapMsgAttribute *)ptrLoc;
		unsigned short nLen=ntohs(pAtti->wLength);
		int nFactLen=((nLen&0x03)!=0?((nLen>>2)+1)<<2:nLen);

		if(nLen==0||ptrLoc+nFactLen>(unsigned char *)pBuffer+dwLen)
		{
			return CVS_AVENUE_PACKET_ERROR;
		}

		unsigned short nType = ntohs(pAtti->wType);
		string strValue = string((char *)(ptrLoc+sizeof(SSapMsgAttribute)), nLen-sizeof(SSapMsgAttribute));

		if(mapTypeByCode[nType] == MSG_FIELD_INT)
		{
			char szTemp[16] = {0};
			snprintf(szTemp, sizeof(szTemp)-1, "%d", ntohl(*(int *)strValue.c_str()));
			strValue = szTemp;
		}

		if(nType < 10000)
		{
			mapRequest.insert(make_pair(nType, strValue));
		}
		else if(nType == 10000)
		{
			dwKeepTime = atoi(strValue.c_str());
		}
		else
		{
			mapResponse.insert(make_pair(nType, strValue));
		}

		ptrLoc+=nFactLen;
	}

	/*
	printf("############begin dump############\n");
	printf("KeepTime:\t%d\n", dwKeepTime);
	printf("Request:\t%d\n", mapRequest.size());
	map<unsigned int, string>::iterator iter;
	for (iter = mapRequest.begin(); iter != mapRequest.end(); iter++)
	{
		printf("\t%d:\t%s\n", iter->first, iter->second.c_str());
	}

	printf("Response:\t%d\n", mapResponse.size());
	for (iter = mapResponse.begin(); iter != mapResponse.end(); iter++)
	{
		printf("\t%d:\t%s\n", iter->first, iter->second.c_str());
	}

	printf("############end dump############\n");
	*/

	if(dwKeepTime > 2592000)
	{
		dwKeepTime = 2592000;
	}

	//获取key值
	multimap<unsigned int, string>::const_iterator iterRequest;
	bool isClear = isClearText(dwServiceId);
	if (isClear)
	{
		for (iterRequest = mapRequest.begin(); iterRequest != mapRequest.end(); iterRequest++)
		{
			strKey = iterRequest->second;
			break;
		}

		string strKeyPrefix = getKeyPrefix(dwServiceId);
		if (!strKeyPrefix.empty())
		{
			strKey = strKeyPrefix + strKey;
		}
	}
	else
	{
		//key前缀
		char szPrefix[16] = {0};
		snprintf(szPrefix, sizeof(szPrefix)-1, "BPE%d", dwServiceId);
		strKey = szPrefix;

		for (iterRequest = mapRequest.begin(); iterRequest != mapRequest.end(); iterRequest++)
		{
			strKey += "_" + iterRequest->second;
		}
		
		if(strKey.length() > 1024)
		{
			CVS_XLOG(XLOG_ERROR, "CCacheThread::%s, Key too long[%d] > 1024\n", __FUNCTION__, strKey.length());
			return CVS_KEY_TOO_LONG;
		}

		if(!IsCacheKeyValid(strKey))
		{
			strKey = MakeCacheKeyValid(strKey);
		}
	}
	//获取value值
	vector<CVSKeyValue> vecKeyValue;
	multimap<unsigned int, string>::const_iterator iterResponse;
	for (iterResponse = mapResponse.begin(); iterResponse != mapResponse.end(); iterResponse++)
	{
		CVSKeyValue tempKeyValue;
		tempKeyValue.dwKey = iterResponse->first;
		tempKeyValue.strValue = iterResponse->second;
		vecKeyValue.push_back(tempKeyValue);
	}

	strValue = CCvsArchiveAdapter::Oarchive(vecKeyValue, isClear);

	return 0;

}