예제 #1
0
inline void operator<< (object::with_zone& o, const std::tr1::unordered_map<K,V>& v)
{
	o.type = type::MAP;
	if(v.empty()) {
		o.via.map.ptr  = NULL;
		o.via.map.size = 0;
	} else {
		object_kv* p = (object_kv*)o.zone->malloc(sizeof(object_kv)*v.size());
		object_kv* const pend = p + v.size();
		o.via.map.ptr  = p;
		o.via.map.size = v.size();
		typename std::tr1::unordered_map<K,V>::const_iterator it(v.begin());
		do {
			p->key = object(it->first, o.zone);
			p->val = object(it->second, o.zone);
			++p;
			++it;
		} while(p < pend);
	}
}
예제 #2
0
    void set(int key, int value) {
    	if(key2node.find(key) != key2node.end())
    	{
    		// node to front
    		Node2front(key);
    		head->value = value;
    	}
    	else{
    		Node * n = new Node(key, value);
    		key2node[key] = n;
    		n->next = head;
    		n->pre = NULL;
    		if(head != NULL)
    			head->pre = n;
    		else
    			tail = n;
    		head = n;

//    		Node * p = head;
//    		cout << ":";
//    		while(p!= NULL){
//    			cout << p->key << " ";
//    			p = p->next;
//    		}cout<< " taiL:" << tail->key /*<< " tpre:" << tail->pre->key */<< endl;

//    		if(key2node.size() > 1)
//    			cout << "tpre:" << tail->pre->key << endl;
    		// delete last node
    		if(key2node.size() > capa)
    		{
    			int k = tail->key;
    			if(tail->pre != NULL){
//        			cout << "tail pre:" <<  tail->pre->key << endl;
    				tail->pre->next = NULL;
        			tail = tail->pre;
//        			cout << "tail:" << tail->key << endl;
    			}
    			else {
    				head = tail = NULL;
    			}
    			key2node.erase(k);
    		}
//    		cout << head->key << head->value << " " <<tail->key <<  tail->value << endl;

//    		p = head;
//    		cout << ":";
//    		while(p!= NULL){
//    			cout << p->key << " ";
//    			p = p->next;
//    		}cout << endl;

    	}
    }
예제 #3
0
void m_dccchat::dccListen(std::string id, Socket* listenSocket) {
	std::tr1::unordered_map<std::string, std::vector<std::string> >::iterator ourReportingModules = reportingModules.find(id);
	while (true) {
		if (!listenSocket->isConnected())
			break;
		std::string receivedMsg = listenSocket->receive();
		std::cout << "DCC " << id << ":" << receivedMsg << std::endl;
		std::tr1::unordered_map<std::string, Module*> modules = getModules(); // get a new one each time in case it is updated
		for (std::tr1::unordered_map<std::string, std::string>::iterator hookIter = moduleTriggers.begin(); hookIter != moduleTriggers.end(); ++hookIter) {
			if (hookIter->first == receivedMsg.substr(0, receivedMsg.find_first_of(' '))) {
				bool alreadyReporting = false;
				for (unsigned int i = 0; i < ourReportingModules->second.size(); i++) {
					if (ourReportingModules->second[i] == hookIter->second) {
						alreadyReporting = true;
						break;
					}
				}
				if (!alreadyReporting)
					ourReportingModules->second.push_back(hookIter->second);
			}
		}
		for (unsigned int i = 0; i < ourReportingModules->second.size(); i++) {
			std::tr1::unordered_map<std::string, Module*>::iterator modIter = modules.find(ourReportingModules->second[i]);
			if (modIter == modules.end())
				ourReportingModules->second.erase(ourReportingModules->second.begin()+i);
			else {
				std::vector<std::string> modSupports = modIter->second->supports();
				for (unsigned int i = 0; i < modSupports.size(); i++) {
					if (modSupports[i] == "DCC_CHAT") {
						dccChat* dccMod = (dccChat*)modIter->second;
						dccMod->onDCCReceive(id, receivedMsg);
						break;
					}
				}
			}
		}
	}
	std::tr1::unordered_map<std::string, Module*> modules = getModules();
	for (unsigned int i = 0; i < reportingModules.size(); i++) {
		std::tr1::unordered_map<std::string, Module*>::iterator modIter = modules.find(ourReportingModules->second[i]);
		dccChat* dccMod = (dccChat*) modIter->second;
		dccMod->onDCCEnd(id); // call the DCC end hook for each watching module as the DCC session ends
	}
	delete listenSocket;
	activeConnections.erase(id);
}
예제 #4
0
//Implementation of displayMulti()
void Engine::displayMulti(std::tr1::unordered_map<string, unsigned int> table) {

    // If the map is empty there is nothing to be done.
    if(table.empty()) {
        cout << "No movies matched your search. Sorry" << endl << endl ;
        return ;
    }
    
    std::tr1::unordered_map<string, unsigned int>::iterator it2 ;
    string big ;
    
    // Iterate three times to get the three movies with highest frequencies
    for(int i = 0 ; i < 3 ; i++) { 
    
    // If the intersection is empty the job is done
        if(table.size() == 0) {
            cout << "No more movies." << endl << endl ;
            return ;
        }
        
        // Assume the movie with highest frequency is the first one
        big = table.begin()->first ;
        
        // Iterate through the rest of the movies and if one with higher frequency
        // is found swap it with big
        for(it2 = table.begin() ; it2 != table.end() ; it2++)
            if(table[big] < it2->second)
                big = it2->first;
        
        // Print the movie with highest frequency
        cout << "Movie " << i+1 << ": " << big << endl ;
        
        // Erase that movie from the map
        table.erase(big) ;
    }
    cout << endl ;
}