ROSE 0.11.145.192
BiMap.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_BiMap_H
9#define Sawyer_BiMap_H
10
11#include <Sawyer/Map.h>
12#include <Sawyer/Sawyer.h>
13#include <boost/foreach.hpp>
14
15namespace Sawyer {
16namespace Container {
17
25template<class S, class T>
26class BiMap {
27public:
28 typedef S Source;
29 typedef T Target;
32private:
33 Forward forward_;
34 Reverse reverse_;
35public:
39 BiMap() {}
40
42 BiMap(const BiMap &other);
43
49 template<class U>
51 BOOST_FOREACH (const typename Forward::Node &anode, a.forward_.nodes()) {
52 if (b.forward_.exists(anode.value())) {
53 const Target &target = b.forward_[anode.value()];
54 forward_.insert(anode.key(), target);
55 reverse_.insert(target, anode.key());
56 }
57 }
58 }
59
63 void clear() {
64 forward_.clear();
65 reverse_.clear();
66 }
67
72 bool eraseSource(const Source &source) {
73 if (!forward_.exists(source))
74 return false;
75 reverse_.erase(forward_[source]);
76 forward_.erase(source);
77 return true;
78 }
79
84 bool eraseTarget(const Target &target) {
85 if (!reverse_.exists(target))
86 return false;
87 forward_.erase(reverse_[target]);
88 reverse_.erase(target);
89 return true;
90 }
91
97 bool erase(const Source &source, const Target &target) {
98 if (!forward_.exists(source) || forward_[source]!=target)
99 return false;
100 forward_.erase(source);
101 reverse_.erase(target);
102 return true;
103 }
104
110 void insert(const Source &source, const Target &target) {
111 eraseSource(source);
112 eraseTarget(target);
113 forward_.insert(source, target);
114 reverse_.insert(target, source);
115 }
116
120 const Forward& forward() const {
121 return forward_;
122 }
123
127 const Reverse& reverse() const {
128 return reverse_;
129 }
130};
131
132} // namespace
133} // namespace
134
135#endif
One-to-one mapping between source and target values.
Definition BiMap.h:26
Sawyer::Container::Map< Target, Source > Reverse
Type for range-to-domain map.
Definition BiMap.h:31
BiMap(const BiMap< Source, U > &a, const BiMap< U, Target > &b)
Construct a new map by composition of two maps.
Definition BiMap.h:50
Sawyer::Container::Map< Source, Target > Forward
Type for domain-to-range map.
Definition BiMap.h:30
T Target
Type of values in the range.
Definition BiMap.h:29
BiMap()
Default constructor.
Definition BiMap.h:39
bool eraseSource(const Source &source)
Erase domain value and its associated range value.
Definition BiMap.h:72
void clear()
Erase all mappings.
Definition BiMap.h:63
S Source
Type of values in the domain.
Definition BiMap.h:28
bool eraseTarget(const Target &target)
Erase range value and its associated domain value.
Definition BiMap.h:84
const Reverse & reverse() const
Return reverse mapping.
Definition BiMap.h:127
BiMap(const BiMap &other)
Copy constructor.
void insert(const Source &source, const Target &target)
Insert a new mapping.
Definition BiMap.h:110
bool erase(const Source &source, const Target &target)
Erase a specific map entry.
Definition BiMap.h:97
const Forward & forward() const
Return forward mapping.
Definition BiMap.h:120
Container associating values with keys.
Definition Sawyer/Map.h:72
bool exists(const Key &key) const
Determine if a key exists.
Definition Sawyer/Map.h:493
Map & erase(const Key &key)
Remove a node with specified key.
Definition Sawyer/Map.h:744
boost::iterator_range< NodeIterator > nodes()
Iterators for container nodes.
Definition Sawyer/Map.h:387
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition Sawyer/Map.h:646
Map & clear()
Remove all nodes.
Definition Sawyer/Map.h:732
Sawyer support library.