Newer
Older
#include <set>
#include <vector>
#include <utility>
#include <iterator>
#include <unordered_set>
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
namespace ojl {
template <typename T>
struct bimap_left {
T val;
};
template <typename T>
struct bimap_right {
T val;
};
template <typename L, typename R>
struct CompareLeft {
bool
operator()(const std::pair<L, R> &lhs, const std::pair<L, R> &rhs) const
{
return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);
}
bool
operator()(const L &lhs, const std::pair<L, R> &rhs) const
{
return lhs < rhs.first;
}
bool
operator()(const std::pair<L, R> &lhs, const L &rhs) const
{
return lhs.first < rhs;
}
using is_transparent = L;
};
template <typename L, typename R>
struct CompareRight {
bool
operator()(const std::pair<L, R> &lhs, const std::pair<L, R> &rhs) const
{
return lhs.second < rhs.second || (lhs.second == rhs.second && lhs.first < rhs.first);
}
bool
operator()(const R &lhs, const std::pair<L, R> &rhs) const
{
return lhs < rhs.second;
}
bool
operator()(const std::pair<L, R> &lhs, const R &rhs) const
{
return lhs.second < rhs;
}
using is_transparent = L;
};
//TODO: maybe add unkeyed versions of member functions for when L != R
// (replacing left with L, right with R)
template <typename L, typename R>
struct bimap {
using left = bimap_left<L>;
using right = bimap_right<R>;
using relation = std::pair<L,R>;
using l_relation_set = std::set<relation, CompareLeft<L,R>>;
using r_relation_set = std::set<relation, CompareRight<L,R>>;
using relation_iterator = typename l_relation_set::iterator;
//TODO: see if the iterator types are the same
std::set<L> left_elements;
std::set<R> right_elements;
l_relation_set l_relations;
r_relation_set r_relations;
//TODO: badly named? Maybe should make the sets private if I have accessors...?
const l_relation_set&
relations_ref()
{
return l_relations;
}
std::vector<relation>
all_relations_vec()
{
std::vector<relation> rel;
for (auto it = l_relations.begin(); it != l_relations.end(); it++){
rel.push_back(*it);
}
return rel;
}
// Insert elements
void
insert(left l_key)
{
left_elements.insert(l_key.val);
}
void
insert(right r_key)
{
right_elements.insert(r_key.val);
}
void
l_insert(L l)
{
left_elements.insert(l);
}
void
r_insert(R r)
{
right_elements.insert(r);
}
// Insert relations
void
insert(L l, R r)
{
left_elements.insert(l);
right_elements.insert(r);
l_relations.insert({l,r});
r_relations.insert({l,r});
}
void
insert(relation rel)
{
left_elements.insert(rel.first);
right_elements.insert(rel.second);
l_relations.insert(rel);
r_relations.insert(rel);
}
// Erase relations
void
erase(const relation &rel)
{
r_relations.erase(rel);
l_relations.erase(rel);
}
// Erase elements
void
erase(left l_key)
{
left_elements.erase(l_key.val);
//Find all relations in l_relations and remove them from r_relations
auto &[lb, ub] = l_relations.equal_range(l_key.val);
for (auto it = lb; it < ub; it++){
r_relations.erase(*it);
}
l_relations.erase(l_key.val);
}
void
erase(right r_key)
{
right_elements.erase(r_key.val);
auto &[lb, ub] = r_relations.equal_range(r_key.val);
for (auto it = lb; it < ub; it++){
l_relations.erase(*it);
}
r_relations.erase(r_key.val);
}
void
l_erase(L l)
{
left_elements.erase(l);
auto &[lb, ub] = l_relations.equal_range(l);
for (auto it = lb; it < ub; it++){
r_relations.erase(*it);
}
l_relations.erase(l);
}
void
r_erase(R r)
{
right_elements.erase(r);
auto &[lb, ub] = l_relations.equal_range(r);
for (auto it = lb; it < ub; it++){
r_relations.erase(*it);
}
r_relations.erase(r);
}
// Contains
bool contains(relation rel)
{
return l_relations.contains(rel);
}
bool contains(left l_key)
{
return l_relations.contains(l_key.val);
}
bool contains(right r_key)
{
return r_relations.contains(r_key.val);
}
bool l_contains(L l)
{
return l_relations.contains(l);
}
bool r_contains(R r)
{
return r_relations.contains(r);
}
// Count
size_t
count(left l_key)
{
return l_relations.count(l_key.val);
}
size_t
count(right r_key)
{
return r_relations.count(r_key.val);
}
size_t
l_count(L l)
{
return l_relations.count(l);
}
size_t
r_count(R r)
{
return r_relations.count(r);
}
// Equal range
std::pair<relation_iterator, relation_iterator>
equal_range(left l_key)
{
return l_relations.equal_range(l_key.val);
}
std::pair<relation_iterator, relation_iterator>
equal_range(right r_key)
{
return r_relations.equal_range(r_key.val);
}
std::pair<relation_iterator, relation_iterator>
l_equal_range(L l)
{
return equal_range(left{l});
}
std::pair<relation_iterator, relation_iterator>
r_equal_range(R r)
{
return equal_range(right{r});
}
// Convenience function for getting a vector out
std::vector<R>
mapped_vector(left l_key)
{
std::vector<R> v;
auto [lb, ub] = equal_range(l_key);
v.reserve(std::distance(lb,ub));
for (auto it = lb; it != ub; it++){
v.push_back(it->second);
}
return v;
}
std::vector<L>
mapped_vector(right r_key)
{
std::vector<L> v;
auto [lb, ub] = equal_range(r_key);
v.reserve(std::distance(lb,ub));
for (auto it = lb; it != ub; it++){
v.push_back(it->first);
}
return v;
}
// Convenience function for getting a set
std::vector<R>
mapped_set(left l_key)
{
std::set<R> s;
auto [lb, ub] = equal_range(l_key);
for (auto it = lb; it != ub; it++){
s.insert(it->second);
}
return s;
}
std::vector<L>
mapped_set(right r_key)
{
std::set<L> s;
auto [lb, ub] = equal_range(r_key);
s.insert(ub - lb);
for (auto it = lb; it != ub; it++){
s.insert(it->first);
}
return s;
}
std::vector<R>
l_mapped_vector(L l)
{
return mapped_vector(left{l});
}
std::vector<L>
r_mapped_vector(R r)
{
return mapped_vector(right{r});
}
std::vector<R>
l_mapped_set(L l)
{
return mapped_set(left{l});
}
std::vector<L>
r_mapped_set(R r)
{
return mapped_set(right{r});
}
};
}