-
Notifications
You must be signed in to change notification settings - Fork 294
/
utils.cpp
58 lines (51 loc) · 1.23 KB
/
utils.cpp
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
#include <map>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
std::vector<std::string> split(const std::string &text, char sep) {
std::vector<std::string> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
tokens.push_back(text.substr(start, end - start));
start = end + 1;
}
tokens.push_back(text.substr(start));
return tokens;
}
vector<int> key_to_vector_int(const string& key){
vector<string> tokens=split(key,';');
vector<int> res;
for(auto f:tokens)
{
res.push_back(atoi(f.c_str()));
}
return res;
}
void save_tags_map(std::map<uint64_t, std::vector<int> > * index,const char * fileName)
{
ofstream out(fileName);
auto it=index->begin();
while( it != index->end() )
{
out<<it->first<<" ";
for(auto i:it->second)
{
out<<i<<";";
}
out<<endl;
it++;
}
out.close();
}
std::map<uint64_t, std::vector<int> >* load_tags_map(const char * fileName)
{
std::map<uint64_t, std::vector<int> >* res=new std::map<uint64_t, std::vector<int> >();
ifstream out(fileName);
string key;
uint64_t id;
while(out>>id>>key){
res->insert( make_pair(id,key_to_vector_int(key)) );
}
return res;
}