Пример #1
0
Problem
Data::
select(const std::tr1::unordered_map<std::string,int>& pn_map) const
{
  uint n=0;
  for (uint i=0; i!=labels_.size(); ++i) {
    std::tr1::unordered_map<std::string,int>::const_iterator f;
    f=pn_map.find(labels_[i]);
    if (f!=pn_map.end() && f->second!=0) {
      n++;
    }
  }
  if (n==0) return Problem();

  std::vector<node_ary> x(n);
  std::vector<double> y(n);
  n=0;
  for (uint i=0; i!=labels_.size(); ++i) {
    std::tr1::unordered_map<std::string,int>::const_iterator f;
    f=pn_map.find(labels_[i]);
    if (f!=pn_map.end()) {
      if (f->second>0) {
	y[n] = +1;
	x[n] = vec_[i];
	n++;
      } else if (f->second<0) {
	y[n] = -1;
	x[n] = vec_[i];
	n++;
      }
    }
  }
  return Problem(x, y);
}
Пример #2
0
void ReadListGraphEdges(ListGraph &g,int nedges,ifstream & ifile,
#if __cplusplus >= 201103L
			std::unordered_map<string,Node> & string2node,
#else
			std::tr1::unordered_map<string,Node> & string2node,
#endif
			EdgeValueMap &weight)
{
  Node u,v;
  Edge a;
  string nomeu,nomev;
  double peso;
  for (int i=0;i<nedges;i++) {
    // format: <node_u>   <node_v>   <edge_weight>
    ifile >> nomeu;  ifile >> nomev; ifile >> peso;
    if (ifile.eof()) 
      {cout << "Reached unexpected end of file.\n"; exit(0);}
    auto test = string2node.find(nomeu);
    if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomeu<<endl;exit(0);}
    else u = string2node[nomeu];
    
    test = string2node.find(nomev);
    if (test == string2node.end()) {cout<<"ERROR: Unknown node: "<<nomev<<endl;exit(0);}
    else v = string2node[nomev];
    a = g.addEdge(u,v);
    weight[a] = peso;
  }
}
Пример #3
0
uint max_distance(TreeNode* root)
{
    if (root == NULL) {
        return 0;
    }

    static std::tr1::unordered_map<void*, uint> cache;

    typeof(cache.begin()) it = cache.find(root);
    if (it != cache.end()) {
        return it->second;
    }

    uint leftCost = max_distance(root->left);
    uint rightCost = max_distance(root->right);

    uint leftHeight = height(root->left);
    uint rightHeight = height(root->right);

    uint childCost = leftCost > rightCost ? leftCost : rightCost;
    uint throughRoot = leftHeight + rightHeight + root->cost;

    uint ret = childCost > throughRoot ? childCost : throughRoot;
    cache.insert(std::make_pair(root, ret));

    return ret;
}
Пример #4
0
// To read list of nodes in the format: <node_name>  <double1>  <double2>
void ReadListGraphNodes(ListGraph &g,int nnodes,ifstream & ifile,
#if __cplusplus >= 201103L
    std::unordered_map<string,Node> & string2node,
#else
    std::tr1::unordered_map<string,Node> & string2node,
#endif
			   NodeStringMap &vname,
			   NodePosMap  &posx,
			   NodePosMap  &posy)
{ string STR;  Node u,v;  string token;
  for (int i=0;i<nnodes;i++) {
    getline(ifile,STR);
    if (ifile.eof()) {cout<<"Reached unexpected end of file.\n";exit(0);}
    while (STR=="") getline(ifile,STR);
    { istringstream ins; // Declare an input string stream.
      ins.str(STR);        // Specify string to read.
      for (int p=0; getline(ins, token, ' ') ; p++) {
	if (p==0) {   // For example, to read:   node_name   posx   posy
	  auto test = string2node.find(token);
	  if (test!=string2node.end()){cout<<"ERROR: Repeated node: "<<token<<endl;exit(0);}
	  v = g.addNode(); string2node[token] = v; vname[v] = token;
	  posx[v]=DBL_MAX;  posy[v]=DBL_MAX; }
	else if (p==1) { posx[v] = atof(token.c_str());}
	else if (p==2) { posy[v] = atof(token.c_str());}
      }}
  }
}
Пример #5
0
bool m_dccchat::hookDCCMessage(std::string modName, std::string hookMsg) {
	if (moduleTriggers.find(hookMsg) == moduleTriggers.end()) {
		moduleTriggers.insert(std::pair<std::string, std::string> (hookMsg, modName));
		return true;
	}
	return false;
}
Пример #6
0
    int get(int key) {
    	if(key2node.find(key) == key2node.end())
    		return -1;
		// node to front
    	Node2front(key);
    	return key2node[key]->value;
    }
Пример #7
0
void m_dccchat::onNickChange(std::string server, std::string oldNick, std::string newNick) {
	std::tr1::unordered_map<std::string, Socket*>::iterator dccIter = activeConnections.find(server + "/" + oldNick);
	if (dccIter != activeConnections.end()) {
		Socket* thisSocket = dccIter->second;
		activeConnections.erase(dccIter);
		activeConnections.insert(std::pair<std::string, Socket*> (server + "/" + newNick, thisSocket));
	}
}
Пример #8
0
void m_dccchat::unhookDCCSession(std::string modName, std::string dccid) {
	std::tr1::unordered_map<std::string, std::vector<std::string> >::iterator reportingModIter = reportingModules.find(dccid);
	if (reportingModIter == reportingModules.end())
		return;
	for (unsigned int i = 0; i < reportingModIter->second.size(); i++) {
		if (reportingModIter->second[i] == modName)
			reportingModIter->second.erase(reportingModIter->second.begin()+i);
	}
}
Пример #9
0
void m_dccchat::onUserCTCP(std::string server, std::string nick, std::string message) {
	std::vector<std::string> messageParts = splitBySpace(message);
	if (messageParts[0] == "DCC" && messageParts[1] == "CHAT" && messageParts[2] == "chat") {
		if (activeConnections.find(server + "/" + nick) == activeConnections.end())
			dccConnect(server, nick, messageParts[3], messageParts[4]);
		else
			sendNotice(server, nick, "You already have an active DCC chat session!");
	}
}
Пример #10
0
 //a[index]=n
 //计算a[]的以a[index]为结尾的最长不重复子数组的长度
 int operator()(int n,int index){
   std::tr1::unordered_map<int,int>::iterator iter=all.find(n);
   if(iter==all.end()){
     //n没有出现过
     all[n]=index;
   } else{
     start=std::max(start,iter->second);
     iter->second=index;
   }
   return index-start;
 }
Пример #11
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;

    	}
    }
Пример #12
0
bool suisto_get_since( std::string stream_name, uint64_t ts, std::vector<entry_t*>& out ){	
	std::tr1::unordered_map< std::string, Stream  >::iterator it;

	it = streams.find( stream_name );

	if( it == streams.end() ){
		return false;
		}	
	
	out = (it->second).since( ts );
		
	return true;
	}
Пример #13
0
  float dotproduct(const fv_t& fv) {
    float m = 0.0;
    size_t fv_size = fv.size();
    for (size_t i = 0; i < fv_size; i++) {
	  int key = fv[i].first;
      float x_i = fv[i].second;
	  std::tr1::unordered_map<int, float>::iterator wit = w.find(key);
      if (wit != w.end()) {
        m +=  x_i * wit->second;
      }
    }
    return m;
  };
Пример #14
0
bool suisto_get_latest( std::string stream_name, size_t max_entries, std::vector<entry_t*>& out ){	
	std::tr1::unordered_map< std::string, Stream  >::iterator it;

	it = streams.find( stream_name );

	if( it == streams.end() ){
		return false;
		}	
		
	out = (it->second).latest( max_entries );
		
	return true;
	}
Пример #15
0
  void muladd(const fv_t &fv, const int y, float scale) {
    for (size_t i = 0; i < fv.size(); i++) {
	  int key = fv[i].first;
      float x_i = fv[i].second;

	  std::tr1::unordered_map<int, float>::iterator wit = w.find(key);
      if (wit != w.end()) {
		wit->second += y * x_i * scale;
      } else {
        w[key] = y * x_i * scale;
      }
    }
  };
Пример #16
0
static std::string encode(const std::string& s,const std::tr1::unordered_map<char,std::string>& charToAscii){
  if(s.empty()) return "";
  size_t i=s.length()-1;
  std::string ret;
  while(true){
    const std::tr1::unordered_map<char,std::string>::const_iterator iter=charToAscii.find(s[i]);
    if(iter!=charToAscii.end()) ret.append(iter->second);
    else throw std::runtime_error("char not found");
    if(i==0) break;
    i--;
  }
  return ret;
};
Пример #17
0
bool suisto_create_stream( std::string stream_name ){
	std::tr1::unordered_map< std::string, Stream  >::iterator it;

	it = streams.find( stream_name );

	if( it != streams.end() ){
		return false;
		}	
	
	
	streams[ stream_name ] = Stream();

	return true;
	}
Пример #18
0
void replace(std::string user, std::string command, std::deque<std::string> args)
{
  if (cuboidMap.find(user) != cuboidMap.end())
  {
    cuboidMap.erase(user);
  }
  if (args.size() == 2)
  {
    cuboidMap[user].active = 1;
    cuboidMap[user].state = 0;
    cuboidMap[user].action = REPLACE;

    int blockID = atoi(args[0].c_str());

    //If item was not a number, search the name from config
    if (blockID == 0)
    {
      blockID = mineserver->config.iData(args[0].c_str());
    }

    if(blockID < 1 || blockID > 255)
    {
      cuboidMap.erase(user);
      return;
    }

    cuboidMap[user].fromBlock = blockID;

    blockID = atoi(args[1].c_str());

    //If item was not a number, search the name from config
    if (blockID == 0 && args[1] != "0")
    {
      blockID = mineserver->config.iData(args[1].c_str());
    }

    if(blockID < 0 || blockID > 255)
    {
      cuboidMap.erase(user);
      return;
    }

    cuboidMap[user].toBlock = blockID;

    mineserver->chat.sendmsgTo(user.c_str(),"Cuboid replace start, hit first block");
  }
}
Пример #19
0
bool suisto_add_post( std::string stream_name, std::string post_content ){
	std::tr1::unordered_map< std::string, Stream  >::iterator it;

	it = streams.find( stream_name );

	if( it == streams.end() ){
		return false;
		}	
	
	entry_t *entry = new entry_t;
	entry->id = Clock::next();
	entry->timestamp = generate_timestamp( entry->id );

	posts[entry->id] = escape_string( post_content );

	(it->second).add( entry );

	return true;
	}
Пример #20
0
    double Modularity::getQualityScore(const Graph * graph, std::tr1::unordered_map<uint32_t, Community*>& communities, double gamma)
    {
     //	const uint32_t communityNum = communities.size();

	    double Q = 0.0;
	    uint32_t edge_num = graph->edge_num_;
        std::tr1::unordered_map<uint32_t, Community*>::iterator itr;
	    for(itr=communities.begin(); itr!=communities.end(); itr++)
	    {
            Community * community = itr->second;
		    uint32_t in_degree = community->in_degree_;
		    uint32_t total_degree = community->total_degree_;

		    Q += ((double)in_degree)/(2*(double)edge_num) - pow((double)total_degree/((double)(2*edge_num)), 2);
            //printf("Q: %f, indegree: %u, total: %u, edgenum: %u\n",Q,in_degree, total_degree, edge_num);
	    }
			std::cout << "Q: \t" << Q << "\n";

	    return Q;
    }
Пример #21
0
std::string resolve(size_t len,const std::string& s,std::tr1::unordered_map<std::string,int> count){
  count.clear();
  if(s.length()<len) throw std::runtime_error("err");
  if(s.length()==len) return s;
  const size_t maxIndex=s.length()-len;
  std::tr1::unordered_map<std::string,int>::iterator end=count.end();
  int maxCount=1;
  std::string maxStr=s.substr(0,3);
  for(size_t i=1;i!=maxIndex;++i){
    const std::string substr=s.substr(i,len);
    std::tr1::unordered_map<std::string,int>::iterator iter=count.find(substr);
    if(iter!=end){
      if(++iter->second > maxCount){
	maxCount=iter->second;
	maxStr=substr;
      }      
    }
    else count[substr]=1;
  }
  return maxStr;
}
Пример #22
0
uint height(TreeNode* root)
{
    if (root == NULL) {
        return 0;
    }

    static std::tr1::unordered_map<void*, uint> cache;

    typeof(cache.begin()) it = cache.find(root);
    if (it != cache.end()) {
        return it->second;
    }

    uint left = height(root->left);
    uint right = height(root->right);
    uint child = left > right ? left : right;

    uint ret = root->cost + child;
    cache.insert(std::make_pair(root, ret));

    return ret;
}
Пример #23
0
static std::string decode(const std::string& s,const std::tr1::unordered_map<std::string,char>& asciiToChar){
  if(!isDigit(s)) 
    throw std::runtime_error("err");
  std::string ret;
  for(size_t i=0;i+1<s.length();){
    std::string key;
    if(s[i+1]<='2'){
      if(i+2>=s.length()) throw std::runtime_error("err");
      key=s.substr(i,3);
      i+=3;
    } else{
      key=s.substr(i,2);
      i+=2;
    }
    const std::tr1::unordered_map<std::string,char>::const_iterator iter=asciiToChar.find(key);
    if(iter!=asciiToChar.end())
      ret.append(1,iter->second);
    else throw std::runtime_error("num not found");
  }
  std::reverse(ret.begin(),ret.end());
  return ret;
}
Пример #24
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 ;
}
Пример #25
0
std::vector<std::string> m_dccchat::getConnections() {
	std::vector<std::string> connections;
	for (std::tr1::unordered_map<std::string, Socket*>::iterator connIter = activeConnections.begin(); connIter != activeConnections.end(); ++connIter)
		connections.push_back(connIter->first);
	return connections;
}
Пример #26
0
void m_dccchat::dccSend(std::string recipient, std::string message) {
	std::tr1::unordered_map<std::string, Socket*>::iterator dccIter = activeConnections.find(recipient);
	if (dccIter == activeConnections.end())
		return;
	dccIter->second->sendData(message);
}
Пример #27
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);
}