-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapping.cc
67 lines (57 loc) · 1.24 KB
/
mapping.cc
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
#include "mapping.hh"
#include <set>
#include <string>
#include <utility>
#include <assert.h>
using std::ostream;
using std::string;
TStringIndex Mapping::get(TStringIndex from) const
{
TMapping::const_iterator i = mapping.find(from);
return (i == mapping.end()) ? 0 : i->second;
}
void Mapping::set(TStringIndex from, TStringIndex to)
{
if (!to) {
mapping.erase(from);
} else {
std::pair<TMapping::iterator, bool> inres = mapping.insert(
TMapping::value_type(from, to));
if (!inres.second) {
inres.first->second = to;
}
}
}
void Mapping::ensure_no_cycle()
{
for (Mapping::TMapping::const_iterator i = begin();
i != end();
++i) {
check_from(i->first);
}
}
void Mapping::check_from(TStringIndex i)
{
typedef std::set<TStringIndex> TKnown;
assert(i);
TKnown known;
while (i) {
std::pair<TKnown::iterator, bool> inres = known.insert(i);
assert(inres.second);
i = get(i);
}
}
ostream &operator<<(ostream &os, const Mapping &m)
{
os << "{\n";
std::string delim = "\t";
for (Mapping::TMapping::const_iterator i = m.begin();
i != m.end();
++i) {
os << delim;
delim = ",\n\t";
os << i->first << " -> " << i->second;
}
os << "\n}";
return os;
}