Exemplo n.º 1
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;
};
Exemplo n.º 2
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;
	}
Exemplo n.º 3
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;
      }
    }
  };
Exemplo n.º 4
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;
  };
Exemplo n.º 5
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;
	}
Exemplo n.º 6
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;
	}
Exemplo n.º 7
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);
	  } 
	}
  };
Exemplo n.º 8
0
std::tr1::shared_ptr<WordTagFreqs> ModelPrivate::readLexicon(
	std::istream &lexiconStream,
	std::tr1::unordered_map<std::string, size_t> const &tagNumbers)
{
	std::tr1::shared_ptr<WordTagFreqs> lexicon(
		new WordTagFreqs);

	std::string line;
	while (std::getline(lexiconStream, line)) {
		std::istringstream lineStream(line);

		// Read line items.
		std::vector<std::string> lineItems;
		copy(std::istream_iterator<std::string>(lineStream),
			std::istream_iterator<std::string>(),
			std::back_inserter(lineItems));

		if (lineItems.size() == 0 || (lineItems.size() - 1) % 2 != 0)
		  throw std::runtime_error(std::string("Invalid lexicon entry: ") + line);

		std::string word = lineItems[0];

		for (size_t i = 1; i < lineItems.size(); i += 2) {
			std::istringstream freqStream(lineItems[i + 1]);
			size_t freq;
			freqStream >> freq;
			size_t tag = tagNumbers.find(lineItems[i])->second;
			(*lexicon)[word][tag] = freq;
		}
	}

	return lexicon;
}
Exemplo n.º 9
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");
  }
}
Exemplo n.º 10
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);
}
Exemplo n.º 11
0
void m_dccchat::dccConnect(std::string server, std::string nick, std::string ip, std::string port) {
	Socket* dccSocket = new Socket();
	std::istringstream portNumber (port);
	unsigned short dccPort;
	portNumber >> dccPort;
	dccSocket->connectServer(ip, dccPort);
	activeConnections.insert(std::pair<std::string, Socket*> (server + "/" + nick, dccSocket));
	reportingModules.insert(std::pair<std::string, std::vector<std::string> > (server + "/" + nick, std::vector<std::string> ()));
	dccListenArg listenData;
	listenData.modPtr = this;
	listenData.id = server + "/" + nick;
	listenData.sockPtr = dccSocket;
	pthread_t newThread;
	threads.push_back(&newThread);
	void* listenArgs = (void*) &listenData;
	pthread_create(&newThread, NULL, &dccListen_thread, listenArgs);
	sleep(1); // make the arguments passed to the thread not die before the thread function can read them.
}
Exemplo n.º 12
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);
	}
}
Exemplo n.º 13
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;
	}
Exemplo n.º 14
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;
    }
Exemplo n.º 15
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);
	}
}
Exemplo n.º 16
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;
}
Exemplo n.º 17
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;
}
Exemplo n.º 18
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;
}
Exemplo n.º 19
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 ;
}
Exemplo n.º 20
0
 void reset(){
   start=-1;
   all.clear();
 }
Exemplo n.º 21
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();
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
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);
}