ModErn Text Analysis
META Enumerates Textual Applications
invertible_map.h
Go to the documentation of this file.
1 
10 #ifndef META_INVERTIBLE_MAP_H_
11 #define META_INVERTIBLE_MAP_H_
12 
13 #include <fstream>
14 #include <iterator>
15 #include <utility>
16 #include <map>
17 #include <unordered_map>
18 #include <string>
19 
20 namespace meta
21 {
22 namespace util
23 {
24 
29 template <class Key, class Value>
31 {
32  public:
37 
41  bool empty() const;
42 
46  size_t size() const;
47 
52  Key get_key(const Value& value) const;
53 
58  Value get_value(const Key& key) const;
59 
64  bool contains_key(const Key& key) const;
65 
70  bool contains_value(const Value& value) const;
71 
77  void insert(const Key& key, const Value& value);
78 
84  void insert(const std::pair<Key, Value>& pair);
85 
89  void clear();
90 
94  typedef typename std::unordered_map
96 
102  class Iterator : public std::iterator
103  <std::bidirectional_iterator_tag, InnerIterator>
104  {
105  private:
107  InnerIterator iter;
108 
109  public:
112  {
113  /* nothing */
114  }
115 
117  Iterator(const InnerIterator& other) : iter{other}
118  {
119  /* nothing */
120  }
121 
124  {
125  ++iter;
126  return *this;
127  }
128 
131  {
132  InnerIterator save = iter;
133  ++iter;
134  return Iterator{save};
135  }
136 
139  {
140  --iter;
141  return *this;
142  }
143 
146  {
147  InnerIterator save = iter;
148  --iter;
149  return Iterator{save};
150  }
151 
153  bool operator==(const Iterator& other)
154  {
155  return iter == other.iter;
156  }
157 
159  bool operator!=(const Iterator& other)
160  {
161  return iter != other.iter;
162  }
163 
169  const typename InnerIterator::value_type& operator*()
170  {
171  return *iter;
172  }
173 
179  const typename InnerIterator::value_type* operator->()
180  {
181  return &(*iter);
182  }
183  };
184 
189  typedef Iterator iterator;
190 
192  typedef Iterator const_iterator;
193 
197  const_iterator begin() const;
198 
202  const_iterator end() const;
203 
204  private:
206  std::unordered_map<Key, Value> forward_;
207 
209  std::unordered_map<Value, Key> backward_;
210 
211  public:
215  class invertible_map_exception : public std::runtime_error
216  {
217  public:
218  using std::runtime_error::runtime_error;
219  };
220 };
221 }
222 }
223 
224 #include "util/invertible_map.tcc"
225 #endif
Iterator(const InnerIterator &other)
Copy constructor.
Definition: invertible_map.h:117
bool contains_key(const Key &key) const
Definition: invertible_map.tcc:58
Iterator const_iterator
Lets const_iterator be interchangeable with "iterator".
Definition: invertible_map.h:192
Iterator & operator--()
Pre-decrement.
Definition: invertible_map.h:138
Iterator iterator
Easier typename to deal with if capital, also lets const_iterator share same name.
Definition: invertible_map.h:189
InnerIterator iter
The iterator of the underlying unordered_map.
Definition: invertible_map.h:107
Basic exception for invertible_map interactions.
Definition: invertible_map.h:215
std::unordered_map< Key, Value >::const_iterator InnerIterator
The "inner" iterator representation of the invertible_map.
Definition: invertible_map.h:95
bool operator!=(const Iterator &other)
Iterator inequality.
Definition: invertible_map.h:159
const_iterator end() const
Definition: invertible_map.tcc:92
bool operator==(const Iterator &other)
Iterator equality.
Definition: invertible_map.h:153
Iterator & operator++()
Pre-Increment.
Definition: invertible_map.h:123
std::unordered_map< Value, Key > backward_
The internal map representing Value -> Key pairs.
Definition: invertible_map.h:209
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
void clear()
Frees all keys from this object.
Definition: invertible_map.tcc:27
const_iterator begin() const
Definition: invertible_map.tcc:85
const InnerIterator::value_type & operator*()
Dereference operator.
Definition: invertible_map.h:169
size_t size() const
Definition: invertible_map.tcc:34
Iterator()
Constructor.
Definition: invertible_map.h:111
Key get_key(const Value &value) const
Definition: invertible_map.tcc:40
Iterator operator--(int)
Post-decrement.
Definition: invertible_map.h:145
std::unordered_map< Key, Value > forward_
The internal map representing Key -> Value pairs.
Definition: invertible_map.h:206
void insert(const Key &key, const Value &value)
Inserts a (key, value) pair into the invertible map.
Definition: invertible_map.tcc:70
const InnerIterator::value_type * operator->()
Arrow operator.
Definition: invertible_map.h:179
bool empty() const
Definition: invertible_map.tcc:21
The invertible_map iterator is really just a wrapper for the forward (key -> value) unordered_map ite...
Definition: invertible_map.h:102
Value get_value(const Key &key) const
Definition: invertible_map.tcc:49
invertible_map()
Constructor.
Definition: invertible_map.tcc:15
This data structure indexes by keys as well as values, allowing constant amortized lookup time by key...
Definition: invertible_map.h:30
bool contains_value(const Value &value) const
Definition: invertible_map.tcc:64
Iterator operator++(int)
Post-increment.
Definition: invertible_map.h:130