Beispiel #1
0
void Graph::read(const char * fn) {
    std::ifstream inf (fn);
    std::string line;
    int n;
    node_number = 0;
    std::vector<int> tmp;
    while (std::getline(inf, line)) {
	read_line(line, &tmp);
	if ( tmp.size() == 2 ) {
	    if( node_order.find(tmp.at(0)) == node_order.end() ) {
		node_order[tmp.at(0)] = node_number;
		node_number++;
	    };
	    if( node_order.find(tmp.at(1)) == node_order.end() ) {
		node_order[tmp.at(1)] = node_number;
		node_number++;
	    };
	};
	tmp.clear();
    };
    nodes.resize(node_number);
    times.resize(node_number);
    visited.insert(visited.begin(), node_number, false);
    inf.clear();
    inf.seekg(0);
    while (std::getline(inf, line)) {
	read_line(line, &tmp);
	if ( tmp.size() == 2 ) {
	    nodes.at(node_order[tmp.at(1)]).add_reverse_edge(node_order[tmp.at(0)]);
	    nodes.at(node_order[tmp.at(0)]).add_edge(node_order[tmp.at(1)]);
	    };
	tmp.clear();
    };
    node_order.clear();
};
std::string RippleAddress::humanAccountID () const
{
    switch (nVersion)
    {
    case VER_NONE:
        throw std::runtime_error ("unset source - humanAccountID");

    case VER_ACCOUNT_ID:
    {
        boost::mutex::scoped_lock sl (rncLock);
        boost::unordered_map< Blob , std::string >::iterator it = rncMap.find (vchData);

        if (it != rncMap.end ())
            return it->second;

        if (rncMap.size () > 10000)
            rncMap.clear ();

        return rncMap[vchData] = ToString ();
    }

    case VER_ACCOUNT_PUBLIC:
    {
        RippleAddress   accountID;

        (void) accountID.setAccountID (getAccountID ());

        return accountID.ToString ();
    }

    default:
        throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion)));
    }
}
void Streamer::processPickups(Player &player, const std::vector<SharedCell> &cells)
{
	static boost::unordered_map<int, Item::SharedPickup> discoveredPickups;
	for (std::vector<SharedCell>::const_iterator c = cells.begin(); c != cells.end(); ++c)
	{
		for (boost::unordered_map<int, Item::SharedPickup>::const_iterator p = (*c)->pickups.begin(); p != (*c)->pickups.end(); ++p)
		{
			boost::unordered_map<int, Item::SharedPickup>::iterator d = discoveredPickups.find(p->first);
			if (d == discoveredPickups.end())
			{
				if (checkPlayer(p->second->players, player.playerID, p->second->interiors, player.interiorID, p->second->worlds, player.worldID))
				{
					if (boost::geometry::comparable_distance(player.position, p->second->position) <= p->second->streamDistance)
					{
						boost::unordered_map<int, int>::iterator i = internalPickups.find(p->first);
						if (i == internalPickups.end())
						{
							p->second->worldID = !p->second->worlds.empty() ? player.worldID : -1;
						}
						discoveredPickups.insert(*p);
					}
				}
			}
		}
	}
	if (processingFinalPlayer)
	{
		boost::unordered_map<int, int>::iterator i = internalPickups.begin();
		while (i != internalPickups.end())
		{
			boost::unordered_map<int, Item::SharedPickup>::iterator d = discoveredPickups.find(i->first);
			if (d == discoveredPickups.end())
			{
				DestroyPickup(i->second);
				i = internalPickups.erase(i);
			}
			else
			{
				discoveredPickups.erase(d);
				++i;
			}
		}
		for (boost::unordered_map<int, Item::SharedPickup>::iterator d = discoveredPickups.begin(); d != discoveredPickups.end(); ++d)
		{
			if (internalPickups.size() == visiblePickups)
			{
				break;
			}
			int internalID = CreatePickup(d->second->modelID, d->second->type, d->second->position[0], d->second->position[1], d->second->position[2], d->second->worldID);
			if (internalID == INVALID_ALTERNATE_ID)
			{
				break;
			}
			internalPickups.insert(std::make_pair(d->second->pickupID, internalID));
		}
		discoveredPickups.clear();
	}
}
Beispiel #4
0
void read_db_users()
{
	m_read_db_users_time = srv_time();
	if (!m_use_sql)
		return;
	try
	{
		Csql_query q(m_database, "select @uid");
		if (m_read_users_can_leech)
			q += ", can_leech";
		if (m_read_users_peers_limit)
			q += ", peers_limit";
		if (m_read_users_torrent_pass)
			q += ", torrent_pass";
		q += ", torrent_pass_version";
		if (m_read_users_wait_time)
			q += ", wait_time";
		q += " from @users";
		Csql_result result = q.execute();
		// m_users.reserve(result.size());
		for (auto& i : m_users)
			i.second.marked = true;
		m_users_torrent_passes.clear();
		while (Csql_row row = result.fetch_row())
		{
			t_user& user = m_users[row[0].i()];
			user.marked = false;
			int c = 0;
			user.uid = row[c++].i();
			if (m_read_users_can_leech)
				user.can_leech = row[c++].i();
			if (m_read_users_peers_limit)
				user.peers_limit = row[c++].i();
			if (m_read_users_torrent_pass)
			{
				if (row[c].size() == 32)
					m_users_torrent_passes[to_array<char, 32>(row[c])] = &user;
				c++;
			}
			user.torrent_pass_version = row[c++].i();
			if (m_read_users_wait_time)
				user.wait_time = row[c++].i();
		}
		for (auto i = m_users.begin(); i != m_users.end(); )
		{
			if (i->second.marked)
				m_users.erase(i++);
			else
				i++;
		}
	}
	catch (bad_query&)
	{
	}
}
Beispiel #5
0
//-----------------------------------------------------------------------------
DofMap::DofMap(boost::unordered_map<std::size_t, std::size_t>& collapsed_map,
               const DofMap& dofmap_view, const Mesh& mesh)
   :  _ufc_dofmap(dofmap_view._ufc_dofmap), _global_dimension(0),
      _ufc_offset(0)
{
  dolfin_assert(_ufc_dofmap);

  // Check for dimensional consistency between the dofmap and mesh
  check_dimensional_consistency(*_ufc_dofmap, mesh);

  // Check that mesh has been ordered
  if (!mesh.ordered())
  {
     dolfin_error("DofMap.cpp",
                  "create mapping of degrees of freedom",
                  "Mesh is not ordered according to the UFC numbering convention. "
                  "Consider calling mesh.order()");
  }

  // Check dimensional consistency between UFC dofmap and the mesh
  check_provided_entities(*_ufc_dofmap, mesh);

  // Copy slave-master map (copy or share?)
  if (dofmap_view.slave_master_mesh_entities)
    slave_master_mesh_entities.reset(new std::map<unsigned int, std::map<unsigned int, std::pair<unsigned int, unsigned int> > >(*dofmap_view.slave_master_mesh_entities));

  // Build new dof map
  DofMapBuilder::build(*this, mesh, slave_master_mesh_entities, _restriction);

  // Dimension sanity checks
  dolfin_assert(dofmap_view._dofmap.size() == mesh.num_cells());
  dolfin_assert(global_dimension() == dofmap_view.global_dimension());
  dolfin_assert(_dofmap.size() == mesh.num_cells());

  // FIXME: Could we use a std::vector instead of std::map if the
  //        collapsed dof map is contiguous (0, . . . , n)?

  // Build map from collapsed dof index to original dof index
  collapsed_map.clear();
  for (std::size_t i = 0; i < mesh.num_cells(); ++i)
  {
    const std::vector<dolfin::la_index>& view_cell_dofs = dofmap_view._dofmap[i];
    const std::vector<dolfin::la_index>& cell_dofs = _dofmap[i];
    dolfin_assert(view_cell_dofs.size() == cell_dofs.size());

    for (std::size_t j = 0; j < view_cell_dofs.size(); ++j)
      collapsed_map[cell_dofs[j]] = view_cell_dofs[j];
  }
}
void load(Archive & ar, boost::unordered_map<T,U> & t, unsigned int version)
{
    // clear the map
    t.clear();
    // read the size
    typedef typename boost::unordered_map<T,U>::size_type size_type;
    size_type size;
    ar & BOOST_SERIALIZATION_NVP(size);
    for (size_type i = 0; i < size; i++) {
        // read a pair
        std::pair<T, U> pair;
        ar & boost::serialization::make_nvp("pair"+i, pair);
        // insert it into the map
        t.insert(pair);
    }
}
Beispiel #7
0
/***********************************************************************
 * Internal format to return the legals in a structure that is easy
 * to check if some move is legal.
 ***********************************************************************/
void State::get_legals(boost::unordered_map<Player, boost::unordered_set<Move> >& result) const
{
    typedef boost::unordered_map<Player, boost::unordered_set<Move> > tmp_t;

    result.clear();
    std::vector<PlayerMove> legalMoves;
    legals(std::back_inserter(legalMoves));

    for(std::vector<PlayerMove>::iterator pm = legalMoves.begin(); pm != legalMoves.end(); pm++) {
        tmp_t::iterator iter_moves(result.find(pm->first));
        if(iter_moves == result.end()) {
            std::pair<tmp_t::iterator, bool> ok = result.emplace(pm->first, boost::unordered_set<Move>());
            ok.first->second.emplace(pm->second);
        } else {
            iter_moves->second.emplace(pm->second);
        }
    }
}
int main() {
    std::cout << "Element size: " << sizeof(map_element) << std::endl;

    std::cout << "std::map: ";
    run_tests(packet_collector_thread_std_map);
    DataCounter.clear();

#ifndef __APPLE__
    std::cout << "tbb::concurrent_unordered_map: ";
    run_tests(packet_collector_thread_unordered_concurrent_map);
    DataCounterUnorderedConcurrent.clear();
#endif

    // Boost unordered map
    std::cout << "boost::unordered_map: ";
    run_tests(packet_collector_thread_boost_unordered_map);
    DataCounterBoostUnordered.clear();

    // Boost flat_map
    DataCounterBoostFlatMap.reserve( number_of_ips );
    std::cout << "boost::container::flat_map with preallocated elements: ";
    run_tests(packet_collector_thread_flat_map_preallocated);
    DataCounterBoostFlatMap.clear();

    std::cout << "std::unordered_map C++11: ";
    run_tests(packet_collector_thread_unordered_map);
    DataCounterUnordered.clear();

    // Preallocate hash buckets
    DataCounterUnorderedPreallocated.reserve( number_of_ips );
    std::cout << "std::unordered_map C++11 preallocated buckets: ";
    run_tests(packet_collector_thread_unordered_map_preallocated);
    DataCounterUnorderedPreallocated.clear();

    // Preallocate vector
    DataCounterVector.reserve( number_of_ips );
    std::cout << "std::vector preallocated: ";
    run_tests(packet_collector_thread_vector);
    DataCounterVector.clear();
}
Beispiel #9
0
std::string RippleAddress::humanAccountID () const
{
    switch (nVersion)
    {
    case VER_NONE:
        throw std::runtime_error ("unset source - humanAccountID");

    case VER_ACCOUNT_ID:
    {
        StaticScopedLockType sl (s_lock, __FILE__, __LINE__);
        boost::unordered_map< Blob , std::string >::iterator it = rncMap.find (vchData);

        if (it != rncMap.end ())
            return it->second;

        // VFALCO NOTE Why do we throw everything out? We could keep two maps
        //             here, switch back and forth keep one of them full and clear the
        //             other on a swap - but always check both maps for cache hits.
        //
        if (rncMap.size () > 250000)
            rncMap.clear ();

        return rncMap[vchData] = ToString ();
    }

    case VER_ACCOUNT_PUBLIC:
    {
        RippleAddress   accountID;

        (void) accountID.setAccountID (getAccountID ());

        return accountID.ToString ();
    }

    default:
        throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion)));
    }
}
int main(int ac, char* av[])
{
    //read and parse command line inputs (using boose::program_options)
    po::options_description desc("Allowed options");
    desc.add_options()
    ("help,h", "produce help message")
    //scenario parameters
    ("lambda,l", po::value<double>(&lambda_mean)->default_value(10), "mean demand (E[lambda])")
    ("beta,b", po::value<double>(&beta0)->default_value(1), "beta")
    ("cost,c", po::value<double>(&cost)->default_value(1), "unit cost")
    //file names input
    ("path,p", po::value<string>(&path), "path for temporary archive files")
    ("outfile,o", po::value<string>(&resultFile), "file to save result")
    ;
    
    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);
    po::notify(vm);
    
    
    if (vm.count("help")) {
        cout << "Usage: options_description [options]\n";
        cout << desc;
        return 0;
    }
    
    
    //initialize the file names by taking all parameters
    modelName = "NVLearning_Full";
    if (resultFile == "") resultFile = modelName + ".result.txt";
    
    
    //Open output file
    file.open(resultFile, fstream::app|fstream::out);
    
    if (! file)
    {
        //if fail to open the file
        cerr << "can't open output file NVLearning_Full_result.txt!" << endl;
        exit(EXIT_FAILURE);
    }
	
    file << setprecision(10);
    cout << setprecision(10);
    
    //omp_set_num_threads(omp_get_num_procs());
	
    //cout << "Num of Procs: " << omp_get_num_procs() << endl;
    //cout << "Max Num of Threads: " << omp_get_max_threads() << endl;
    cout << "Num of periods (N): " << N << endl;
    cout << "r\tc\talpha\tbeta\tQ_F\tPi_F\tTime_F\tCPUTime_F\tComp_F" << endl;
    
    //file << "Num of Procs: " << omp_get_num_procs() << endl;
    //file << "Max Num of Threads: " << omp_get_max_threads() << endl;
    //file << "Num of periods (N): " << N << endl;
    file << "r\tc\talpha\tbeta\tQ_F\tPi_F\tTime_F\tCPUTime_F\tComp_F" << endl;
    
    
    
    //initialize cost parameters
    price = 2;
    //lambda_mean = 10;
    //cost = 1;
    //beta0 = 1;
    
    
    for (lambda_mean=10; lambda_mean<=50; lambda_mean+=10)
    for (beta0=2; beta0>=0.05; beta0/=2)
    {
        alpha0 = beta0*lambda_mean;
        
        for (cost=1.8; cost>=0.15; cost-=0.1)
        {
            v_map.clear();
            
            
            cout << price << "\t" << cost << "\t" << alpha0 << "\t" << beta0 << "\t";
            file << price << "\t" << cost << "\t" << alpha0 << "\t" << beta0 << "\t";
            
            
            auto startTime = chrono::system_clock::now();
            clock_t cpu_start = clock();
            
            V_F(1, 0, 0);
            
            clock_t cpu_end = clock();
            auto endTime = chrono::system_clock::now();
            
            cout << chrono::duration_cast<chrono::milliseconds> (endTime-startTime).count() << "\t" << 1000.0*(cpu_end-cpu_start)/CLOCKS_PER_SEC << "\t";
            file << chrono::duration_cast<chrono::milliseconds> (endTime-startTime).count() << "\t" << 1000.0*(cpu_end-cpu_start)/CLOCKS_PER_SEC << "\t";
            
            
            scenarioName = ".l" + dbl_to_str(lambda_mean) +".b" + dbl_to_str(beta0) + ".c" + dbl_to_str(cost);
            archiveFile = path + modelName + scenarioName + ".oarchive.txt";
            
            archiveVMap(v_map, archiveFile);
            auto mapSize = v_map.size();
            cout << mapSize+1 << endl;
            file << mapSize+1 << endl;
        }
        
    }

    
    
    
    file.close();
    
    return 0;
}
void Broder86::computeNeighbours(const BipartiteMatching& s,
                                 boost::unordered_map<BipartiteMatching, Rational>& neighbors) const {

    // Variables
    const int n = g.getNumberOfNodes();
    const int k = s.k;
    int u, v;
    BipartiteMatching s2;
    edgelist edges;

    neighbors.clear();

    // Implementierung der Transitionen nach Vorlage JS89

    // Gehe über jede Kante
    g.getEdges(edges);

    //std::cout << "compute neighbors for s=" << s << std::endl;

    // Für jede Wahl einer Kante e=(u,v) wird Transition konstruiert
    for (typename edgelist::iterator it = edges.begin(); it != edges.end();
            ++it) {

        u = it->first;
        v = it->second;

        // create new state as copy of old one
        s2 = BipartiteMatching(s);

        // Transition 1
        if (2 * k == n && s.mates[u] == v) {
            // remove edge (u,v)
            s2.mates[u] = n;
            s2.mates[v] = n;
            s2.k = k - 1;
            s2.unmatched[0] = u;
            s2.unmatched[1] = v;
        } else if (2 * k + 2 == n) {

            // Transition 2
            if (s.mates[u] == n && s.mates[v] == n) {
                // add edge (u,v)
                s2.mates[u] = v;
                s2.mates[v] = u;
                s2.k = k + 1;
                s2.unmatched[0] = n;
                s2.unmatched[1] = n;
            }
            // Transition 3a
            else if (s.mates[u] != n && s.mates[v] == n) {
                // remove edge (u, mate[u])
                // add edge (u,v)
                int w = s.mates[u];
                s2.mates[w] = n;
                s2.mates[u] = v;
                s2.mates[v] = u;
                // w=mate[u] (>= n/2) becomes unmatched node in bipartition group 1
                s2.unmatched[1] = w;
            }
            // Transition 3b
            else if (s.mates[u] == n && s.mates[v] != n) {
                // remove edge (v, mate[v])
                // add edge (u,v)
                int w = s.mates[v];
                s2.mates[w] = n;
                s2.mates[u] = v;
                s2.mates[v] = u;
                // w=mate[v] (< n/2) becomes unmatched node in bipartition group 0
                s2.unmatched[0] = w;
            } else {
                // stay in s
            }
        }
        // sonst
        else {
            // Verbleibe im aktuellen Zustand
        }

        neighbors[s2] += Rational(1, edges.size());

        //std::cout << std::endl;
    }
}
Beispiel #12
0
//------------------------------------------------------------------------------
void CleanUp(bool exitvalue) {
  pout_values.clear();
  xercesc::XMLPlatformUtils::Terminate();
  exit(exitvalue);
  }