ngscopeclient 0.1-dev+51fbda87c
Bijection.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef Bijection_h
37#define Bijection_h
38
46template<class T1, class T2, typename Compare1 = std::less<T1>, typename Compare2 = std::less<T2> >
48{
49public:
50
52 typedef std::map<T1, T2, Compare1> forwardType;
53
55 typedef std::map<T2, T1, Compare2> reverseType;
56
58 typename forwardType::const_iterator begin()
59 { return m_forwardMap.begin(); }
60
62 typename forwardType::const_iterator end()
63 { return m_forwardMap.end(); }
64
73 void emplace(T1 a, T2 b)
74 {
75 m_forwardMap[a] = b;
76 m_reverseMap[b] = a;
77 }
78
84 const T1& operator[](T2 key)
85 { return m_reverseMap[key]; }
86
92 const T2& operator[](T1 key)
93 { return m_forwardMap[key]; }
94
100 bool HasEntry(T1 key)
101 { return m_forwardMap.find(key) != m_forwardMap.end(); }
102
108 bool HasEntry(T2 key)
109 { return m_reverseMap.find(key) != m_reverseMap.end(); }
110
112 void clear()
113 {
114 m_forwardMap.clear();
115 m_reverseMap.clear();
116 }
117
123 void erase(T1 key)
124 {
125 auto value = m_forwardMap[key];
126 m_forwardMap.erase(key);
127 m_reverseMap.erase(value);
128 }
129
135 void erase(T2 key)
136 {
137 auto value = m_reverseMap[key];
138 m_reverseMap.erase(key);
139 m_forwardMap.erase(value);
140 }
141
145 void replace(T2 oldval, T2 newval)
146 {
147 T1 key = m_reverseMap[oldval];
148 m_reverseMap.erase(oldval);
149 m_forwardMap[key] = newval;
150 m_reverseMap[newval] = key;
151 }
152
154 size_t size()
155 { return m_forwardMap.size(); }
156
157protected:
158
161
164};
165
166#endif
A strict one-to-one mapping from objects of type T1 to type T2 (which must be different types).
Definition: Bijection.h:48
forwardType::const_iterator begin()
Get an iterator to the start of the forward map.
Definition: Bijection.h:58
void replace(T2 oldval, T2 newval)
Replaces one value with another, keeping the keys identical.
Definition: Bijection.h:145
forwardType m_forwardMap
Map of object-to-object in the forward direction.
Definition: Bijection.h:160
std::map< T2, T1, Compare2 > reverseType
Type of the reverse map.
Definition: Bijection.h:55
bool HasEntry(T1 key)
Determines if an object is present in the forward mapping.
Definition: Bijection.h:100
void emplace(T1 a, T2 b)
Adds a new entry to the bijection.
Definition: Bijection.h:73
void erase(T1 key)
Erase an entry given a forward key.
Definition: Bijection.h:123
const T2 & operator[](T1 key)
Looks up an object in the forward direction.
Definition: Bijection.h:92
forwardType::const_iterator end()
Get an iterator to the end of the forward map.
Definition: Bijection.h:62
const T1 & operator[](T2 key)
Looks up an object in the reverse direction.
Definition: Bijection.h:84
void clear()
Erase all entries in the bijection.
Definition: Bijection.h:112
bool HasEntry(T2 key)
Determines if an object is present in the reverse mapping.
Definition: Bijection.h:108
void erase(T2 key)
Erase an entry given a reverse key.
Definition: Bijection.h:135
reverseType m_reverseMap
Map of object-to-object in the reverse direction.
Definition: Bijection.h:163
std::map< T1, T2, Compare1 > forwardType
Type of the forward map.
Definition: Bijection.h:52
size_t size()
Return the number of entries in the bijection.
Definition: Bijection.h:154