CoMMA 1.3.2
A geometric agglomerator for unstructured meshes
Loading...
Searching...
No Matches
Bimap.h
Go to the documentation of this file.
1#ifndef BIMAP_PROJECT_COMMA_H
2#define BIMAP_PROJECT_COMMA_H
3
4/*
5 * CoMMA
6 *
7 * Copyright © 2024 ONERA
8 *
9 * Authors: Nicolas Lantos, Alberto Remigi, and Riccardo Milani
10 * Contributors: Karim Anemiche
11 *
12 * This Source Code Form is subject to the terms of the Mozilla Public
13 * License, v. 2.0. If a copy of the MPL was not distributed with this
14 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
15 */
16
17#include <cassert>
18#include <iostream>
19#include <map>
20#include <type_traits>
21
22namespace comma {
23
27template<typename A, typename B>
28class Bimap {
29public:
31 Bimap() = default;
33 ~Bimap() = default;
34
35 // using container= std::map<A, const B*>;
36 // using iterator=typename container::iterator;
37 // inline iterator begin() noexcept { return _mapB.begin(); }
38 // inline iterator end() noexcept { return _mapB.end(); }
39
44 void insert(const A &a, const B &b) {
45 auto aitr = _mapA.insert({b, nullptr}).first; // creates first pair
46 const B *bp = &(aitr->first); // get pointer of our stored copy of b
47 auto bitr =
48 _mapB.insert({a, bp}).first; // insert second pair {a, pointer_to_b}
49 const A *ap = &(bitr->first); // update pointer in _mapA to point to a
50 _mapA.insert_or_assign(b, ap);
51 }
52
54 void print() {
55 for (const auto &[key, value] : _mapB) {
56 std::cout << '[' << key << "] = " << *value << "; ";
57 }
58 }
59
65 void update_nodeB(const A &a_old, const A &a_new) {
66 auto node = _mapB.extract(a_old);
67 if (!node.empty()) {
68 node.key() = a_new;
69 _mapB.insert(std::move(node));
70 }
71 }
72
78 void update_nodeA(const B &b_old, const B &b_new) {
79 auto node = _mapA.extract(b_old);
80 if (!node.empty()) {
81 node.key() = b_new;
82 _mapA.insert(std::move(node));
83 }
84 }
85
90 B get_B(const A &a) {
91 // We pass the associated value we want to search to the member
92 // variable
93 auto it = _mapB.find(a);
94 return (*(it->second));
95 }
96
101 A get_A(const B &b) {
102 // We pass the associated value we want to search to the member
103 // variable
104 auto it = _mapA.find(b);
105 return (*(it->second));
106 }
107
112 void erase_B(const A &a) {
113 // We pass the associated value we want to search to the member
114 // variable
115 auto itB = _mapB.find(a);
116 auto be = *(itB->second);
117 auto itA = _mapA.find(be);
118 _mapB.erase(itB);
119 _mapA.erase(itA);
120 }
121
125 inline bool empty() { return (_mapA.empty()); }
126
130 inline size_t lung() { return (_mapB.size()); }
131
132protected:
134 std::map<B, const A *> _mapA;
136 std::map<A, const B *> _mapB;
137};
138
139} // end namespace comma
140
141#endif
An easy and straight forward implementation of a Bimap.
Definition: Bimap.h:28
void update_nodeA(const B &b_old, const B &b_new)
Update of the key of the map A and hence the value of the node B.
Definition: Bimap.h:78
B get_B(const A &a)
Getter of the B value starting from a A value.
Definition: Bimap.h:90
~Bimap()=default
Destructor.
Bimap()=default
Constructor.
void insert(const A &a, const B &b)
Insertion function in the Bimap.
Definition: Bimap.h:44
bool empty()
Check if the Bimap is empty.
Definition: Bimap.h:125
void erase_B(const A &a)
Eraser of the value starting from a A value.
Definition: Bimap.h:112
std::map< A, const B * > _mapB
Right map.
Definition: Bimap.h:136
A get_A(const B &b)
Getter of the A value starting from a B value.
Definition: Bimap.h:101
void update_nodeB(const A &a_old, const A &a_new)
Update of the key of the map B and hence the value of the node A.
Definition: Bimap.h:65
std::map< B, const A * > _mapA
Left map
Definition: Bimap.h:134
void print()
Function to print the map.
Definition: Bimap.h:54
size_t lung()
Returns the size of the container.
Definition: Bimap.h:130
Definition: Agglomerator.h:37