Newer
Older
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <set>
#include <vector>
#include <utility>
#include <iterator>
#include <unordered_set>
template <typename T>
struct bimap_left {
T val;
};
template <typename T>
struct bimap_right {
T val;
};
template <typename L, typename R>
struct CompareBijection {
bool
operator()(const std::pair<L, R> &lhs, const std::pair<L, R> &rhs) const
{
return lhs.first < rhs.first;
}
bool
operator()(const bimap_left<L> &lhs, const std::pair<L, R> &rhs) const
{
return lhs.val < rhs.first;
}
bool
operator()(const bimap_right<R> &lhs, const std::pair<L, R> &rhs) const
{
return lhs.val < rhs.second;
}
bool
operator()(const std::pair<L, R> &lhs, const bimap_left<L> &rhs) const
{
return lhs.first < rhs.val;
}
bool
operator()(const std::pair<L, R> &lhs, const bimap_right<R> &rhs) const
{
return lhs.second < rhs.val;
}
using is_transparent = L;
};
template <typename L, typename R>
struct bijection {
using left = bimap_left<L>;
using right = bimap_right<R>;
using relation = std::pair<L,R>;
using relation_set = std::set<relation, CompareBijection<L,R>>;
using relation_iterator = typename relation_set::iterator;
std::set<L> left_elements;
std::set<R> right_elements;
relation_set relations;
// Insert relations
void
insert(L l, R r)
{
relations.insert({l,r});
}
void
insert(relation rel)
{
relations.insert(rel);
}
// Erase elements
void
erase(left l_key)
{
relations.erase(l_key);
}
void
erase(right r_key)
{
relations.erase(r_key);
}
void
l_erase(L l)
{
relations.erase(left{l});
}
void
r_erase(R r)
{
relations.erase(right{r});
}
// Contains
template <typename Key>
bool contains(Key key)
{
return relations.contains(key);
}
// Count
template <typename Key>
size_t
count(Key key)
{
return relations.count(key);
}
size_t
l_count(L l)
{
return count(left{l});
}
size_t
r_count(R r)
{
return count(right{r});
}
relation_iterator
find(left l_key){
return relations.find(l_key);
}
relation_iterator
find(right r_key){
return relations.find(l_key);
}
//TODO: add operator [] for both left and right access
R
at(left l_key){
auto r = relations.find(l_key);
return r->second;
}
L
at(right r_key){
auto r = relations.find(l_key);
return r->first;
}
};