-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.h
66 lines (51 loc) · 1.35 KB
/
page.h
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
#ifndef PAGE
#define PAGE
#include <map>
#include <vector>
#include <iostream>
#include <regex>
using namespace std;
class Page
{
string name;
int id;
vector<int> links;
bool redirect;
public:
Page() {}
Page(const string& name) : name(name), redirect(false), id(obtainID(name)) {}
Page(const string& name, const string &redirected) : name(name), redirect(true), id(obtainID(name)) {
links.push_back(obtainID(redirected));
}
friend bool operator<(const Page &a, const Page &b) { return a.id < b.id; }
void addLink(const string& name) {
links.push_back(obtainID(name));
}
void addLink(int id) {
links.push_back(id);
}
int getId() const { return id; }
string getName() const { return name; }
string exportPage()
{
string ret;
ret += to_string(id) + "|" + name + "|" + to_string(redirect?1:0) + "|" + to_string(links.size()) + "|";
for(auto link : links)
ret += to_string(link) + "|";
ret += "\n";
return ret;
}
public:
static int nextIndex;
static map<string, int> indexer;
static int obtainID(string name)
{
transform(name.begin(), name.end(), name.begin(), ::tolower);
auto it = indexer.find(name);
if(it != indexer.end()) return (*it).second;
indexer[name] = nextIndex;
return nextIndex++;
}
friend ostream& operator<<(ostream& os, const Page& p);
};
#endif