CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
unique_map.hh
Go to the documentation of this file.
1 #ifndef CASM_unique_map_HH
2 #define CASM_unique_map_HH
3 
4 #include <map>
5 #include <functional>
6 #include "casm/external/boost.hh"
7 #include "casm/misc/CASM_TMP.hh"
9 
10 namespace notstd {
11 
17  template<typename TransformFunc, typename MapIteratorType>
19 
20  public:
21 
22  typedef std::bidirectional_iterator_tag iterator_category;
23  typedef typename MapIteratorType::difference_type difference_type;
24  typedef typename std::result_of<TransformFunc(typename MapIteratorType::reference)>::type reference;
25  typedef typename std::remove_reference<reference>::type value_type;
26  typedef value_type *pointer;
27 
29 
30  explicit UniqueMapIterator(MapIteratorType map_it) :
31  m_it(map_it),
32  m_transform(TransformFunc()) {}
33 
34  template<typename T, typename M>
36  m_it(umap_it.base()),
37  m_transform(TransformFunc()) {}
38 
39  template<typename T, typename M>
40  bool operator==(const UniqueMapIterator<T, M> &B) const {
41  return (!operator!=(B));
42  }
43 
44  template<typename T, typename M>
45  bool operator!=(const UniqueMapIterator<T, M> &B) const {
46  return m_it != B.base();
47  }
48 
49  int type() const {
50  return m_it->first;
51  }
52 
53  reference operator*() const {
54  return m_transform(*m_it);
55  }
56 
57  pointer operator->() const {
58  return &(this->operator*());
59  }
60 
61  // prefix
63  ++m_it;
64  return *this;
65  }
66 
67  // postfix
69  UniqueMapIterator result(*this);
70  ++m_it;
71  return result;
72  }
73 
74  // prefix
76  --m_it;
77  return *this;
78  }
79 
80  // postfix
82  UniqueMapIterator result(*this);
83  --m_it;
84  return result;
85  }
86 
87  MapIteratorType base() const {
88  return m_it;
89  }
90 
91  private:
92 
93  MapIteratorType m_it;
94  TransformFunc m_transform;
95  };
96 
97 
98  template<typename MapType>
99  struct GetSecond {
100 
101  typedef typename MapType::iterator::reference reference;
102  typedef typename MapType::iterator::value_type::second_type &result_type;
103 
105 
106  result_type operator()(reference pair) const {
107  return pair.second;
108  }
109 
110  };
111 
112  template<typename MapType>
113  struct GetSecondConst {
114 
115  typedef typename MapType::const_iterator::reference reference;
116  typedef const typename MapType::const_iterator::value_type::second_type &result_type;
117 
119 
120  result_type operator()(reference pair) const {
121  return pair.second;
122  }
123 
124  };
125 
136  template<typename KeyType,
137  typename ValueType,
138  typename MapType = std::map<KeyType, ValueType>,
139  typename TransformFunction = GetSecond<MapType>,
140  typename ConstTransformFunction = GetSecondConst<MapType> >
141  class unique_map {
142 
143  public:
144 
145  typedef ValueType value_type;
146  typedef KeyType key_type;
147  typedef std::function<KeyType(const ValueType &)> KeyFuncType;
148  typedef std::function<typename MapType::mapped_type(const ValueType &)> ConvertType;
149  typedef ValueType &reference;
150  typedef ValueType *pointer;
151 
152  typedef typename MapType::iterator MapIterator;
153  typedef typename MapType::const_iterator ConstMapIterator;
154 
157 
158  typedef typename MapType::size_type size_type;
159 
160  explicit unique_map(KeyFuncType keyfunc, ConvertType _converter) :
161  m_keyfunc(keyfunc), m_converter(_converter) {}
162 
163 
164  key_type key(const value_type &value) const {
165  return m_keyfunc(value);
166  }
167 
169  std::pair<iterator, bool> insert(const value_type &value) {
170  return _insert(value);
171  }
172 
174  iterator insert(const_iterator hint, const value_type &value) {
175  return iterator(
176  m_map.insert(
177  hint.base(),
178  std::make_pair(key(value), std::move(m_converter(value)))
179  )
180  );
181  }
182 
184  iterator insert(iterator hint, const value_type &value) {
185  return iterator(
186  m_map.insert(
187  hint.base(),
188  std::make_pair(key(value), std::move(m_converter(value)))
189  )
190  );
191  }
192 
194  template<typename... ValuesOrMaps>
195  void insert(const ValuesOrMaps &... more) {
197  }
198 
200  template<typename Iterator>
201  void insert(Iterator begin, Iterator end, typename CASM::CASM_TMP::enable_if_iterator<Iterator>::type * = nullptr) {
202  for(auto it = begin; it != end; ++it) {
203  _insert(*it);
204  }
205  }
206 
207  value_type &operator[](const key_type &key) {
208  // insert if not existing
209  m_map[key];
210 
211  // return reference
212  return *find(key);
213  }
214 
215  iterator find(const key_type &key) {
216  return iterator(m_map.find(key));
217  }
218 
219  const_iterator find(const key_type &key) const {
220  return const_iterator(m_map.find(key));
221  }
222 
223  void clear() {
224  m_map.clear();
225  }
226 
227  iterator erase(const_iterator pos) {
228  return iterator(m_map.erase(pos.base()));
229  }
230 
231  iterator erase(const_iterator first, const_iterator last) {
232  return iterator(m_map.erase(first.base(), last.base()));
233  }
234 
235  size_type erase(const key_type &key) {
236  return m_map.erase(key);
237  }
238 
239  size_type size() const {
240  return m_map.size();
241  }
242 
243  bool empty() const {
244  return m_map.empty();
245  }
246 
247  iterator begin() {
248  return iterator(m_map.begin());
249  }
250 
251  const_iterator begin() const {
252  return const_iterator(m_map.begin());
253  }
254 
255  const_iterator cbegin() const {
256  return const_iterator(m_map.cbegin());
257  }
258 
259  iterator end() {
260  return iterator(m_map.end());
261  }
262 
263  const_iterator end() const {
264  return const_iterator(m_map.end());
265  }
266 
267  const_iterator cend() const {
268  return const_iterator(m_map.cend());
269  }
270 
271 
272  private:
273 
275  std::pair<iterator, bool> _insert(const value_type &value) {
276  auto result = m_map.insert(std::make_pair(key(value), std::move(m_converter(value))));
277  return std::make_pair(iterator(result.first), result.second);
278  }
279 
281  template<typename A, typename B, typename C, typename D, typename E>
282  int _insert(const unique_map<A, B, C, D, E> &value) {
283  insert(value.begin(), value.end());
284  return 0;
285  }
286 
287  MapType m_map;
288  KeyFuncType m_keyfunc;
289  ConvertType m_converter;
290  };
291 
292 }
293 
294 #endif
295 
std::function< KeyType(const ValueType &)> KeyFuncType
Definition: unique_map.hh:147
size_type erase(const key_type &key)
Definition: unique_map.hh:235
void insert(Iterator begin, Iterator end, typename CASM::CASM_TMP::enable_if_iterator< Iterator >::type *=nullptr)
Iterator range insert.
Definition: unique_map.hh:201
ValueType * pointer
Definition: unique_map.hh:150
int _insert(const unique_map< A, B, C, D, E > &value)
Dictionary insert.
Definition: unique_map.hh:282
const_iterator begin() const
Definition: unique_map.hh:251
MapIteratorType m_it
Definition: unique_map.hh:93
An iterator over a UniqueMap.
Definition: unique_map.hh:18
size_type size() const
Definition: unique_map.hh:239
UniqueMapIterator operator++(int)
Definition: unique_map.hh:68
std::remove_reference< reference >::type value_type
Definition: unique_map.hh:25
value_type & operator[](const key_type &key)
Definition: unique_map.hh:207
bool operator==(const UniqueMapIterator< T, M > &B) const
Definition: unique_map.hh:40
reference operator*() const
Definition: unique_map.hh:53
MapType::iterator::reference reference
Definition: unique_map.hh:101
UniqueMapIterator(MapIteratorType map_it)
Definition: unique_map.hh:30
pointer operator->() const
Definition: unique_map.hh:57
MapType::const_iterator ConstMapIterator
Definition: unique_map.hh:153
const_iterator find(const key_type &key) const
Definition: unique_map.hh:219
key_type key(const value_type &value) const
Definition: unique_map.hh:164
std::pair< iterator, bool > insert(const value_type &value)
Insert single value.
Definition: unique_map.hh:169
UniqueMapIterator< TransformFunction, MapIterator > iterator
Definition: unique_map.hh:155
iterator insert(iterator hint, const value_type &value)
Insert single value.
Definition: unique_map.hh:184
MapType::iterator::value_type::second_type & result_type
Definition: unique_map.hh:102
bool operator!=(const UniqueMapIterator< T, M > &B) const
Definition: unique_map.hh:45
std::function< typename MapType::mapped_type(const ValueType &)> ConvertType
Definition: unique_map.hh:148
iterator begin()
Definition: unique_map.hh:247
iterator erase(const_iterator pos)
Definition: unique_map.hh:227
result_type operator()(reference pair) const
Definition: unique_map.hh:106
UniqueMapIterator & operator++()
Definition: unique_map.hh:62
UniqueMapIterator< ConstTransformFunction, ConstMapIterator > const_iterator
Definition: unique_map.hh:156
ValueType & reference
Definition: unique_map.hh:149
iterator insert(const_iterator hint, const value_type &value)
Insert single value.
Definition: unique_map.hh:174
void insert(const ValuesOrMaps &...more)
Variadic insert accepts as const UniqueMap& or const ValueType&.
Definition: unique_map.hh:195
ValueType value_type
Definition: unique_map.hh:145
iterator find(const key_type &key)
Definition: unique_map.hh:215
MapIteratorType base() const
Definition: unique_map.hh:87
iterator erase(const_iterator first, const_iterator last)
Definition: unique_map.hh:231
Non-std smart pointer classes and functions.
MapType::iterator MapIterator
Definition: unique_map.hh:152
UniqueMapIterator(const UniqueMapIterator< T, M > &umap_it)
Definition: unique_map.hh:35
const_iterator end() const
Definition: unique_map.hh:263
const MapType::const_iterator::value_type::second_type & result_type
Definition: unique_map.hh:116
unique_map(KeyFuncType keyfunc, ConvertType _converter)
Definition: unique_map.hh:160
std::map wrapper to enforce a 1-1 ValueType->KeyType relationship
Definition: unique_map.hh:141
std::enable_if< is_iterator< T >::type::value, void > enable_if_iterator
Template alias to enable a template function via SFINAE for any iterator.
Definition: CASM_TMP.hh:53
UniqueMapIterator operator--(int)
Definition: unique_map.hh:81
ConvertType m_converter
Definition: unique_map.hh:289
KeyFuncType m_keyfunc
Definition: unique_map.hh:288
std::pair< iterator, bool > _insert(const value_type &value)
Copy insert.
Definition: unique_map.hh:275
void ignore_returnvalues(Args &&...)
Enables calling a function on each argument in a parameter pack.
Definition: CASM_TMP.hh:19
result_type operator()(reference pair) const
Definition: unique_map.hh:120
MapType::size_type size_type
Definition: unique_map.hh:158
std::bidirectional_iterator_tag iterator_category
Definition: unique_map.hh:22
UniqueMapIterator & operator--()
Definition: unique_map.hh:75
const_iterator cend() const
Definition: unique_map.hh:267
std::result_of< TransformFunc(typename MapIteratorType::reference)>::type reference
Definition: unique_map.hh:24
MapType::const_iterator::reference reference
Definition: unique_map.hh:115
MapIteratorType::difference_type difference_type
Definition: unique_map.hh:23
TransformFunc m_transform
Definition: unique_map.hh:94
bool empty() const
Definition: unique_map.hh:243
const_iterator cbegin() const
Definition: unique_map.hh:255