Flow123d  JS_before_hm-883-gc471082
bidirectional_map.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file bidirectional_map.hh
15  * @brief Implementation of bidirectional map.
16  */
17 
18 #ifndef BIDIRECTIONAL_MAP_HH_
19 #define BIDIRECTIONAL_MAP_HH_
20 
21 #include <vector>
22 #include <unordered_map>
23 #include "system/asserts.hh"
24 
25 
26 /**
27  * @brief Bidirectional map templated by <T, unsigned int>.
28  *
29  * Store pairs of value and its position (index) and allow bidirectional search.
30  * Items of both (values, positions) must be unique.
31  */
32 template<typename T>
34 {
35 public:
36  /// Constructor
38 
39  /// Return size of map.
40  unsigned int size() const;
41 
42  /**
43  * Set value of given position.
44  *
45  * Position must be in interval set in \p reinit method <0, init_size-1>.
46  * Element on every position can be set only once.
47  */
48  void set_item(T val, unsigned int pos);
49 
50  /// Add new item at the end position of map.
51  unsigned int add_item(T val);
52 
53  /// Return position of item of given value.
54  int get_position(T val) const;
55 
56  /// Clear the content. Do not release memory.
57  void clear();
58 
59  /// Reset data of map, reserve space for given size.
60  void reserve(unsigned int init_size = 0);
61 
62  /// Return value on given position.
63  T operator[](unsigned int pos) const;
64 
65 private:
66  std::vector<T> vals_vec_; ///< Space to save values.
67  std::unordered_map<T, unsigned int> vals_map_; ///< Maps values to indexes into vals_vec_.
68 };
69 
70 // --------------------------------------------------- BidirectionalMap INLINE implementation -----------
71 template<typename T>
73 {}
74 
75 template<typename T>
76 inline unsigned int BidirectionalMap<T>::size() const {
77  ASSERT_EQ_DBG(vals_map_.size(), vals_vec_.size());
78  return vals_map_.size();
79 }
80 
81 template<typename T>
82 inline void BidirectionalMap<T>::set_item(T val, unsigned int pos) {
83  ASSERT_LT_DBG( pos, vals_vec_.size() )(pos)(vals_vec_.size()).error("Value id is out of vector size.");
84  //ASSERT( vals_vec_[pos] == -1 )(pos).error("Repeated setting of item.");
85  auto it = vals_map_.find(vals_vec_[pos]);
86  if (it != vals_map_.end()) vals_map_.erase(it);
87  vals_map_[val] = pos;
88  vals_vec_[pos] = val;
89 }
90 
91 template<typename T>
92 inline unsigned int BidirectionalMap<T>::add_item(T val) {
93  ASSERT( vals_map_.find(val) == vals_map_.end() )(val).error("Can not add item since it already exists.");
94  vals_map_[val] = vals_vec_.size();
95  vals_vec_.push_back(val);
96  return vals_map_[val];
97 }
98 
99 template<typename T>
100 inline int BidirectionalMap<T>::get_position(T val) const {
101  typename std::unordered_map<T, unsigned int>::const_iterator iter = vals_map_.find(val);
102  if (iter == vals_map_.end()) return -1;
103  else return iter->second;
104 }
105 
106 template<typename T>
108  vals_map_.clear();
109  vals_vec_.clear();
110 }
111 
112 
113 template<typename T>
114 inline void BidirectionalMap<T>::reserve(unsigned int init_size) {
115  vals_map_.reserve(init_size);
116  vals_vec_.reserve(init_size);
117 }
118 
119 template<typename T>
120 inline T BidirectionalMap<T>::operator[](unsigned int pos) const {
121  ASSERT( pos < vals_vec_.size() )(pos)(vals_vec_.size());
122  return vals_vec_[pos];
123 }
124 
125 
126 #endif // BIDIRECTIONAL_MAP_HH_
BidirectionalMap()
Constructor.
#define ASSERT_EQ_DBG(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Definition: asserts.hh:332
unsigned int size() const
Return size of map.
void reserve(unsigned int init_size=0)
Reset data of map, reserve space for given size.
std::vector< T > vals_vec_
Space to save values.
Definitions of ASSERTS.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
unsigned int add_item(T val)
Add new item at the end position of map.
std::unordered_map< T, unsigned int > vals_map_
Maps values to indexes into vals_vec_.
void set_item(T val, unsigned int pos)
int get_position(T val) const
Return position of item of given value.
T operator[](unsigned int pos) const
Return value on given position.
Bidirectional map templated by <T, unsigned int>.
void clear()
Clear the content. Do not release memory.
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:300