ROSE  0.11.96.11
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://github.com/matzke1/sawyer.
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 
15 namespace Sawyer {
16 namespace Container {
17 
25 template<class S, class T>
26 class BiMap {
27 public:
28  typedef S Source;
29  typedef T Target;
32 private:
33  Forward forward_;
34  Reverse reverse_;
35 public:
39  BiMap() {}
40 
42  BiMap(const BiMap &other);
43 
49  template<class U>
50  BiMap(const BiMap<Source, U> &a, const BiMap<U, Target> &b) {
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
Sawyer::Container::BiMap::forward
const Forward & forward() const
Return forward mapping.
Definition: BiMap.h:120
Sawyer::Container::BiMap::Forward
Sawyer::Container::Map< Source, Target > Forward
Type for domain-to-range map.
Definition: BiMap.h:30
Sawyer::Container::BiMap::BiMap
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::BiMap::erase
bool erase(const Source &source, const Target &target)
Erase a specific map entry.
Definition: BiMap.h:97
Sawyer::Container::BiMap::reverse
const Reverse & reverse() const
Return reverse mapping.
Definition: BiMap.h:127
Sawyer::Container::Map< Source, Target >
Sawyer::Container::BiMap::eraseTarget
bool eraseTarget(const Target &target)
Erase range value and its associated domain value.
Definition: BiMap.h:84
Sawyer::Container::BiMap::clear
void clear()
Erase all mappings.
Definition: BiMap.h:63
Sawyer::Container::Map::insert
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:603
Sawyer::Container::Map::nodes
boost::iterator_range< NodeIterator > nodes()
Iterators for container nodes.
Definition: Sawyer/Map.h:344
Sawyer::Container::BiMap
One-to-one mapping between source and target values.
Definition: BiMap.h:26
Sawyer::Container::BiMap::eraseSource
bool eraseSource(const Source &source)
Erase domain value and its associated range value.
Definition: BiMap.h:72
Sawyer::Container::BiMap::Target
T Target
Type of values in the range.
Definition: BiMap.h:29
Sawyer::Container::BiMap::Source
S Source
Type of values in the domain.
Definition: BiMap.h:28
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::Container::BiMap::insert
void insert(const Source &source, const Target &target)
Insert a new mapping.
Definition: BiMap.h:110
Sawyer::Container::BiMap::Reverse
Sawyer::Container::Map< Target, Source > Reverse
Type for range-to-domain map.
Definition: BiMap.h:31
Sawyer::Container::BiMap::BiMap
BiMap()
Default constructor.
Definition: BiMap.h:39
Sawyer::Container::Map::exists
bool exists(const Key &key) const
Determine if a key exists.
Definition: Sawyer/Map.h:450
Sawyer::Container::Map::erase
Map & erase(const Key &key)
Remove a node with specified key.
Definition: Sawyer/Map.h:701
Sawyer::Container::Map::clear
Map & clear()
Remove all nodes.
Definition: Sawyer/Map.h:689