Skip to content

Instantly share code, notes, and snippets.

@floli
Created February 10, 2019 12:05
Show Gist options
  • Save floli/20b79bacccea3e5e22c801a44e3d14a0 to your computer and use it in GitHub Desktop.
Save floli/20b79bacccea3e5e22c801a44e3d14a0 to your computer and use it in GitHub Desktop.
#define BOOST_ALL_DYN_LINK
#include <string>
#include <functional>
#include <iostream>
#include <boost/uuid/name_generator.hpp>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/filesystem.hpp>
// g++ -std=c++11 precice-hash.cpp -lboost_system -lboost_filesystem
using namespace std;
size_t std_hash(std::string const & acceptorName,
std::string const & requesterName,
std::string const & mesh,
int rank)
{
std::string s = acceptorName + requesterName + std::to_string(rank);
std::hash<std::string> h;
return h(s);
}
std::string boost_hash(std::string const & acceptorName,
std::string const & requesterName,
std::string const & mesh,
int rank)
{
boost::uuids::string_generator ns_gen;
auto ns = ns_gen("af7ce8f2-a9ee-46cb-38ee-71c318aa3580"); // md5 hash of precice.org
boost::uuids::name_generator_latest gen{ns};
std::string s = acceptorName + requesterName + mesh + std::to_string(rank);
std::string h = boost::uuids::to_string(gen(s));
std::remove(h.begin(), h.end(), '-');
return h;
}
void write_connection_info(std::string const & acceptorName,
std::string const & requesterName,
std::string const & mesh,
int rank,
std::string addressDirectory,
std::string info)
{
using namespace boost::filesystem;
const int firstLevelLen = 2;
auto hash = boost_hash(acceptorName, requesterName, mesh, rank);
cout << "Hash = " << hash << endl;
path p = path(addressDirectory) / path(hash.substr(0, firstLevelLen));
create_directories(p);
p /= hash.substr(firstLevelLen);
cout << p << endl;
std::ofstream ofs(p.string());
ofs << info;
}
int main()
{
cout << std_hash("a", "b", "m", 0) << endl;
cout << std_hash("a", "b", "m", 1) << endl;
cout << std_hash("a", "b", "m", 2) << endl;
cout << std_hash("a", "b", "m", 3) << endl;
cout << endl;
cout << boost_hash("a", "b", "m", 0) << endl;
cout << boost_hash("a", "b", "m", 1) << endl;
cout << boost_hash("a", "b", "m", 2) << endl;
cout << boost_hash("a", "b", "m", 3) << endl;
cout << endl;
write_connection_info("a", "b", "m", 0, "precice-conns", "info");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment