1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <functional>
#include <cmath>
#include <boost/algorithm/string.hpp>
#include <map>
#include <tuple>
#include <cstdlib>
#include <cstdio>
#include <limits>
#include <omp.h>
using namespace std;
int main(int argc, char* argv[])
{
if(argc < 2)
{
cout << "Usage: ./renamer <input>" << endl;
return 0;
}
ifstream input1(argv[1], ifstream::in);
map<string, int> vertexmap;
int vertexid = 0;
string name = "Renamed_";
name += string(argv[1]);
ofstream out(name);
string locline;
int numlines = 0;
while(getline(input1,locline))
{
std::tuple<int64_t, int64_t, string> triple; // the third entry is a float, but that doesn't matter here
vector<string> strs;
boost::split(strs, locline, boost::is_any_of("\t "));
auto ret = vertexmap.insert(make_pair(strs[0], vertexid));
if (ret.second) // successfully inserted
++vertexid;
// map::insert returns a pair, with its member pair::first set to an
// iterator pointing to either the newly inserted element or to the element with an equivalent key in the map
get<0>(triple) = ret.first->second;
ret = vertexmap.insert(make_pair(strs[1], vertexid));
if (ret.second) ++vertexid;
get<1>(triple) = ret.first->second;
get<2>(triple) = strs[2];
numlines++;
out << get<0>(triple) << "\t" << get<1>(triple) << "\t" << get<2>(triple) << "\n";
}
out.close();
string dictname = "Vertex_Dict_";
dictname += string(argv[1]);
ofstream dictout(dictname);
int max = 0;
for (auto it = vertexmap.begin(); it != vertexmap.end(); ++it)
{
max = std::max(max, it->second);
dictout << it->second << "\t" << it ->first << endl;
}
cout << (max+1) << "\t" << (max+1) << "\t" << numlines << endl;
dictout.close();
}
|