void Merge(const int& first, const int& second) {
		Tree<int>* first_tree;
		Tree<int>* second_tree;
		if (vertices_by_objects.count(first) == 0) {
			first_tree = new Tree<int>(first);
			trees_by_roots[first_tree->root] = first_tree;
			vertices_by_objects[first] = first_tree->root;
		} else {
			first_tree = trees_by_roots[Tree<int>::FindRoot(vertices_by_objects[first])];
		}
		if (vertices_by_objects.count(second) == 0) {
			second_tree = new Tree<int>(second);
			trees_by_roots[second_tree->root] = second_tree;
			vertices_by_objects[second] = second_tree->root;
		} else {
			second_tree = trees_by_roots[Tree<int>::FindRoot(vertices_by_objects[second])];
		}
		if (first_tree != second_tree) {
//			std::cout << "MERGE(" << first << ", " << second << ")\n";

			first_tree->Merge(second_tree);
			trees_by_roots.erase(first_tree->root);
			delete trees_by_roots[second_tree->root];
			trees_by_roots.erase(second_tree->root);
			trees_by_roots[first_tree->root] = first_tree;


//			Vertex<int>* a = second_tree->root;
//			first_tree->Merge(second_tree);
//			std::cout << "END MERGE\n";
//			trees_by_roots.erase(a);
		}

	}
Beispiel #2
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");
  }
}
Beispiel #3
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));
	}
}
    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;

    	}
    }
Beispiel #5
0
  void l1_regularize(const int iter) {
	float lambda_hat = get_eta(iter) * lambda;

	std::tr1::unordered_map<int, float> tmp = w;
	std::tr1::unordered_map<int, float>::iterator it = tmp.begin();
	for (; it != tmp.end(); it++) {
	  int key = it->first;
	  std::tr1::unordered_map<int, float>::iterator wit = w.find(key);
	  float aaa = wit->second;
	  wit->second = clip_by_zero(wit->second, lambda_hat);
	  if (fabsf(aaa) < lambda_hat) {
		w.erase(wit);
	  } 
	}
  };
Beispiel #6
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);
}
//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 ;
}
Beispiel #8
0
int main(int argc, char **argv)
{
  read_cmd_options(argc,argv);

  // sender stats and stop flags

  count = new uint64_t[nsenders];
  shut = new int[nsenders];
  for (int i = 0; i < nsenders; i++) count[i] = shut[i] = 0;

  // setup UDP port

  const int socket = ::socket(PF_INET6, SOCK_DGRAM, 0);
  if (socket == -1)
    throw std::runtime_error(std::string("socket(): ") + ::strerror(errno));

  struct sockaddr_in6 address;
  ::memset(&address, 0, sizeof(address));
  address.sin6_family = AF_INET6;
  address.sin6_addr = in6addr_any;
  address.sin6_port = htons(port);
  if (-1 == ::bind(socket, reinterpret_cast<sockaddr*>(&address),
                   sizeof(address)))
    throw std::runtime_error(std::string("bind(): ") + ::strerror(errno));

  // scale maxactive by number of generators
  // oset/iset = active key sets for outer/inner keys
  // olist/ilist = doubly-linked lists so can delete least recently used
  // mactive = # of keys in active set for inner keys
  // okv/ikv = outer/inner key-value hash tables
  // ofree = index of 1st free element in linked list of free oset elements

  maxactive *= nsenders;
  Kouter *oset = new Kouter[maxactive];
  Kinner *iset = new Kinner[maxactive];
  MyDoubleLinkedList<Kouter*> olist;
  MyDoubleLinkedList<Kinner*> ilist;
  int mactive = 0;

  Kouter *ofree = oset;
  for (int i = 0; i < maxactive; i++)
    oset[i].next = &oset[i+1];
  oset[maxactive-1].next = NULL;

  // pre-allocate to maxactive per generator, in agreement with generator #3
  
  okv.rehash(ceil(maxactive / okv.max_load_factor()));
  ikv.rehash(ceil(maxactive / ikv.max_load_factor()));

  // packet buffer length of 64 bytes per datum is ample

  int maxbuf = 64*perpacket;
  std::vector<char> buffer(maxbuf);
  int ipacket;

  // loop on reading packets

  Kouter *okey;
  Kinner *ikey;

  int max = 0;

  while (true) {

    // read a packet with Nbytes

    const int nbytes = ::recv(socket,&buffer[0],buffer.size()-1,0);
    buffer[nbytes] = '\0';

    // check if STOP packet
    // exit if have received STOP packet from every sender
    
    if (nbytes < 8) {
      if (shutdown(&buffer[0])) break;
      continue;
    }

    nrecv++;
    
    // tally stats on packets from each sender

    if (countflag) {
      sscanf(&buffer[0],"packet %d",&ipacket);
      count[ipacket % nsenders]++;
    }

    // scan past header line

    strtok(&buffer[0],"\n");

    // process perpacket datums in packet

    for (int i = 0; i < perpacket; i++) {
      uint64_t key = strtoul(strtok(NULL,",\n"),NULL,0);
      char *value = strtok(NULL,",\n");
      int count = atoi(strtok(NULL,",\n"));

      // store outer key in okv hash table
      // if new key, add to active set, deleting LRU key if necessary
      // count = # of times key has been seen
      // discard key if its count is not consistent with okv count
      // build up inner key 16-bits at a time

      if (!okv.count(key)) {
        if (count > 1) continue;
        nunique++;
        if (ofree) {
          okey = ofree;
          ofree = ofree->next;
          okv[key] = okey;
        } else {
          okey = olist.last;
          olist.remove(okey);
          okv.erase(okey->key);
          okv[key] = okey;
        }
        okey->key = key;
        okey->inner = atoi(value);
        okey->count = 1;
        olist.prepend(okey);

      } else {
        okey = okv[key];
        if (okey->count != count-1) {
          okv.erase(okey->key);
          olist.remove(okey);
          okey->next = ofree;
          ofree = okey;
          continue;
        }
        if (count <= 4) {
          olist.move2front(okey);
          uint64_t ivalue = atoi(value);
          ivalue = ivalue << (16*okey->count);
          okey->inner |= ivalue;
          okey->count++;
          continue;
        }

        // 5th occurrence of outer key, discard it
        // value of inner key = low-order digit of value
        // truth of inner key = hi-order digit of value

        okv.erase(okey->key);
        olist.remove(okey);
        okey->next = ofree;
        ofree = okey;

        key = okey->inner;
        uint32_t innervalue,truth;
        if (value[0] == '0') truth = 0;
        else truth = 1;
        if (value[1] == '0') innervalue = 0;
        else innervalue = 1;

        if (!ikv.count(key)) {
          munique++;
          if (mactive < maxactive) {
            ikey = &iset[mactive++];
            ikv[key] = ikey;
          } else {
            ikey = ilist.last;
            ilist.remove(ikey);
            ikv.erase(ikey->key);
            ikv[key] = ikey;
          }
          ikey->key = key;
          ikey->count = 1;
          ikey->value = innervalue;
          ilist.prepend(ikey);
          
        } else {
          ikey = ikv[key];
          ilist.move2front(ikey);

          if (ikey->value < 0) {
            ikey->count++;
            continue;
          }
        
          ikey->count++;
          ikey->value += innervalue;

          if (ikey->count > max) max = ikey->count;

          if (ikey->count == nthresh) {
            if (ikey->value > mthresh) {
              if (truth) {
                nfalse++;
                printf("false negative = %" PRIu64 "\n",key);
              } else ntrue++;
            } else {
              if (truth) {
                ptrue++;
                printf("true anomaly = %" PRIu64 "\n",key);
              } else {
                pfalse++;
                printf("false positive = %" PRIu64 "\n",key);
              }
            }
            ikey->value = -1;
          }
        }
      }
    }
  }

  //printf("IFLAG %d %d\n",olist.check(),ilist.check());

  // close UDP port and print stats

  ::close(socket);
  stats();
}