Newer
Older
#include <set>
#include <vector>
#include <utility>
#include <iterator>
#include <unordered_set>
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
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(relation rel)
{
left_elements.insert(rel.first);
right_elements.insert(rel.second);
l_relations.insert(rel);
r_relations.insert(rel);
}
void
insert(const L &l, const R &r)
{
left_elements.insert(l);
right_elements.insert(r);
l_relations.insert({l,r});
r_relations.insert({l,r});
}
r_relations.erase(rel);
l_relations.erase(rel);
}
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
// 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(L l, R r)
{
return l_relations.contains({l,r});
}
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
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);
}
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Min elements
std::optional<L>
l_min_element()
{
auto it = std::min_element(left_elements.begin(), left_elements.end());
if (it != left_elements.end()){
return *it;
}
return std::nullopt;
}
std::optional<R>
r_min_element()
{
auto it = std::min_element(right_elements.begin(), right_elements.end());
if (it != right_elements.end()){
return *it;
}
return std::nullopt;
}
// Max elements
std::optional<L>
l_max_element()
{
auto it = std::max_element(left_elements.begin(), left_elements.end());
if (it != left_elements.end()){
return *it;
}
return std::nullopt;
}
std::optional<R>
r_max_element()
{
auto it = std::max_element(right_elements.begin(), right_elements.end());
if (it != right_elements.end()){
return *it;
}
return std::nullopt;
}
// Min mapped elements
std::optional<R>
min_mapped_element(left l_key)
{
auto it = std::ranges::min_element(l_relations | std::views::filter([&l_key](relation rel){return rel.first == l_key.val;}));
if (it != l_relations.end()){
return it->second;
}
return std::nullopt;
}
std::optional<R>
min_mapped_element(right r_key)
{
auto it = std::ranges::min_element(l_relations | std::views::filter([&r_key](relation rel){return rel.second == r_key.val;}));
if (it != l_relations.end()){
return it->first;
}
return std::nullopt;
}
std::optional<R>
l_min_mapped_element(L l)
{
auto it = std::ranges::min_element(l_relations | std::views::filter([&l](relation rel){return rel.first == l;}));
if (it != l_relations.end()){
return it->second;
}
return std::nullopt;
}
std::optional<R>
r_min_mapped_element(R r)
{
auto it = std::ranges::min_element(l_relations | std::views::filter([&r](relation rel){return rel.second == r;}));
if (it != l_relations.end()){
return it->first;
}
return std::nullopt;
}
// Max mapped elements
std::optional<R>
max_mapped_element(left l_key)
{
auto it = std::ranges::max_element(l_relations | std::views::filter([&l_key](relation rel){return rel.first == l_key.val;}));
if (it != l_relations.end()){
return it->second;
}
return std::nullopt;
}
std::optional<R>
max_mapped_element(right r_key)
{
auto it = std::ranges::max_element(l_relations | std::views::filter([&r_key](relation rel){return rel.second == r_key.val;}));
if (it != l_relations.end()){
return it->first;
}
return std::nullopt;
}
std::optional<R>
l_max_mapped_element(L l)
{
auto it = std::ranges::max_element(l_relations | std::views::filter([&l](relation rel){return rel.first == l;}));
if (it != l_relations.end()){
return it->second;
}
return std::nullopt;
}
std::optional<R>
r_max_mapped_element(R r)
{
auto it = std::ranges::max_element(l_relations | std::views::filter([&r](relation rel){return rel.second == r;}));
if (it != l_relations.end()){
return it->first;
}
return std::nullopt;
}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// 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});
}
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// A complement of this bimap (in the set of possible relations between left_elements and right_elements)
bimap<L,R>
complement()
{
bimap<L,R> ret;
// Insert elements
for (const auto &l : left_elements){
ret.l_insert(l);
}
for (const auto &r : right_elements){
ret.r_insert(r);
}
// Insert relations
for (const auto &l : left_elements){
for (const auto &r : right_elements){
relation rel{l,r};
if (!this->contains(rel)){
ret.insert(rel);
}
}
}
return ret;
}
bimap<L,R>
complement(std::set<L> left_elems, std::set<R> right_elems)
{
bimap<L,R> ret;
// Insert elements
for (const auto &l : left_elems){
ret.l_insert(l);
}
for (const auto &r : right_elems){
ret.r_insert(r);
}
// Insert relations
for (const auto &l : left_elems){
for (const auto &r : right_elems){
relation rel{l,r};
if (!this->contains(rel)){
ret.insert(rel);
}
}
}
return ret;
}
bimap<L,R>
complement(std::vector<L> left_elems, std::vector<R> right_elems)
{
bimap<L,R> ret;
// Insert elements
for (const auto &l : left_elems){
ret.l_insert(l);
}
for (const auto &r : right_elems){
ret.r_insert(r);
}
// Insert relations
for (const auto &l : left_elems){
for (const auto &r : right_elems){
relation rel{l,r};
if (!this->contains(rel)){
ret.insert(rel);
}
}
}
return ret;
}
// Equality comparison
template<typename L, typename R>
bool
operator==(const bimap<L,R> &lhs, const bimap<L,R> &rhs)
{
return lhs.left_elements == rhs.left_elements
&& lhs.right_elements == rhs.right_elements
&& lhs.l_relations == rhs.l_relations;
if (lhs.left_elements != rhs.left_elements || lhs.right_elements != rhs.right_elements) {
return false;
}
return lhs.l_relations == rhs.l_relations;
}