OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
BiMap.h
Go to the documentation of this file.
1//
2// Simple bidirectional map to replace boost::bimap
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef OPAL_BIMAP_HH
19#define OPAL_BIMAP_HH
20
21#include <map>
22#include <stdexcept>
23
27template <typename Left, typename Right>
28class BiMap {
29public:
30 using left_map = std::map<Left, Right>;
31 using right_map = std::map<Right, Left>;
32
33private:
34 // Declare maps first so they're initialized before left/right references
37
38public:
40 struct relation {
41 Left left;
42 Right right;
46 relation(const Left& l, const Right& r) : left(l), right(r) {}
47 };
48
49 // Access to underlying maps for compatibility (using references)
51 struct left_view {
59 typename left_map::iterator find(const Left& key) const { return map_.find(key); }
62 typename left_map::iterator end() const { return map_.end(); }
66 const Right& at(const Left& key) const { return map_.at(key); }
67 };
68
70 struct right_view {
78 typename right_map::iterator find(const Right& key) const { return map_.find(key); }
81 typename right_map::iterator end() const { return map_.end(); }
85 const Left& at(const Right& key) const { return map_.at(key); }
86 };
87
90
95
99 void insert(const Left& left, const Right& right) {
102 }
103
107 typename left_map::iterator left_find(const Left& key) { return left_map_.find(key); }
108
112 typename right_map::iterator right_find(const Right& key) { return right_map_.find(key); }
113
116 typename left_map::iterator left_end() { return left_map_.end(); }
119 typename right_map::iterator right_end() { return right_map_.end(); }
120
124 const Right& left_at(const Left& key) const {
125 auto it = left_map_.find(key);
126 if (it == left_map_.end()) {
127 throw std::out_of_range("Key not found in left map");
128 }
129 return it->second;
130 }
131
135 const Left& right_at(const Right& key) const {
136 auto it = right_map_.find(key);
137 if (it == right_map_.end()) {
138 throw std::out_of_range("Key not found in right map");
139 }
140 return it->second;
141 }
142};
143
147template <typename Left, typename Right>
149 std::initializer_list<typename BiMap<Left, Right>::relation> relations) {
150 BiMap<Left, Right> bimap;
151 for (const auto& rel : relations) {
152 bimap.insert(rel.left, rel.right);
153 }
154 return bimap;
155}
156
157#endif
BiMap< Left, Right > make_bimap(std::initializer_list< typename BiMap< Left, Right >::relation > relations)
Helper function to create a BiMap from an initializer list.
Definition BiMap.h:148
Simple bidirectional map with lookup in both directions.
Definition BiMap.h:28
void insert(const Left &left, const Right &right)
Insert or overwrite a left/right association.
Definition BiMap.h:99
right_map::iterator right_find(const Right &key)
Find an entry by right key.
Definition BiMap.h:112
left_map left_map_
Definition BiMap.h:35
right_view right
Right view accessor.
Definition BiMap.h:94
const Left & right_at(const Right &key) const
Get mapped left value by right key (throws if missing).
Definition BiMap.h:135
right_map right_map_
Definition BiMap.h:36
std::map< Left, Right > left_map
Definition BiMap.h:30
std::map< Right, Left > right_map
Definition BiMap.h:31
left_view left
Left view accessor.
Definition BiMap.h:92
BiMap()
Construct an empty bimap.
Definition BiMap.h:89
const Right & left_at(const Left &key) const
Get mapped right value by left key (throws if missing).
Definition BiMap.h:124
right_map::iterator right_end()
End iterator for right map.
Definition BiMap.h:119
left_map::iterator left_find(const Left &key)
Find an entry by left key.
Definition BiMap.h:107
left_map::iterator left_end()
End iterator for left map.
Definition BiMap.h:116
Left-map view with find/at helpers.
Definition BiMap.h:51
left_map & map_
Definition BiMap.h:52
left_map::iterator end() const
End iterator for the left map.
Definition BiMap.h:62
left_map::iterator find(const Left &key) const
Find iterator for key in the left map.
Definition BiMap.h:59
const Right & at(const Left &key) const
Get mapped value by key (throws if missing).
Definition BiMap.h:66
left_view(left_map &m)
Construct a left view from a map reference.
Definition BiMap.h:55
Pair relation used for initialization.
Definition BiMap.h:40
relation(const Left &l, const Right &r)
Construct a relation from left/right values.
Definition BiMap.h:46
Right right
Definition BiMap.h:42
Right-map view with find/at helpers.
Definition BiMap.h:70
right_map::iterator find(const Right &key) const
Find iterator for key in the right map.
Definition BiMap.h:78
right_view(right_map &m)
Construct a right view from a map reference.
Definition BiMap.h:74
right_map::iterator end() const
End iterator for the right map.
Definition BiMap.h:81
const Left & at(const Right &key) const
Get mapped value by key (throws if missing).
Definition BiMap.h:85
right_map & map_
Definition BiMap.h:71