18 #ifndef BIDIRECTIONAL_MAP_HH_
19 #define BIDIRECTIONAL_MAP_HH_
22 #include <unordered_map>
81 ASSERT_EQ(vals_map_.size(), vals_vec_.size());
82 return vals_map_.size();
87 ASSERT_LT( pos, vals_vec_.size() )(pos)(vals_vec_.size()).error(
"Value id is out of vector size.");
89 auto it = vals_map_.find(vals_vec_[pos]);
91 if (
it != vals_map_.end()) {
94 auto it_dupl = vals_map_.find(val);
95 if(it_dupl != vals_map_.end()){
96 ASSERT(vals_map_[val] == pos)(pos).error(
"'val' already used in different 'pos'.");
101 vals_map_[val] = pos;
102 vals_vec_[pos] = val;
107 ASSERT( vals_map_.find(val) == vals_map_.end() )(val).error(
"Can not add item since it already exists.");
108 vals_map_[val] = vals_vec_.size();
109 vals_vec_.push_back(val);
110 return vals_map_[val];
115 typename std::unordered_map<T, unsigned int>::const_iterator iter = vals_map_.find(val);
116 if (iter == vals_map_.end())
return -1;
117 else return iter->second;
131 for(
uint pos = new_size; pos < vals_vec_.size(); pos++){
132 vals_map_.erase(vals_vec_[pos]);
134 vals_vec_.resize(new_size);
135 ASSERT(vals_vec_.size() == vals_map_.size())(vals_vec_.size())(vals_map_.size());
140 vals_map_.reserve(init_size);
141 vals_vec_.reserve(init_size);
146 ASSERT( pos < vals_vec_.size() )(pos)(vals_vec_.size());
147 return vals_vec_[pos];
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
#define ASSERT_EQ(a, b)
Definition of comparative assert macro (EQual) only for debug mode.
Bidirectional map templated by <T, unsigned int>.
void resize(unsigned int new_size)
Reset data of map, reserve space for given size.
BidirectionalMap()
Constructor.
void clear()
Clear the content. Do not release memory.
std::vector< T > vals_vec_
Space to save values.
unsigned int add_item(T val)
Add new item at the end position of map.
T operator[](unsigned int pos) const
Return value on given position.
int get_position(T val) const
Return position of item of given value.
void reserve(unsigned int init_size=0)
Reset data of map, reserve space for given size.
void set_item(T val, unsigned int pos)
std::unordered_map< T, unsigned int > vals_map_
Maps values to indexes into vals_vec_.
unsigned int size() const
Return size of map.