18 #ifndef BIDIRECTIONAL_MAP_HH_
19 #define BIDIRECTIONAL_MAP_HH_
22 #include <unordered_map>
40 unsigned int size()
const;
48 void set_item(T val,
unsigned int pos);
60 void reserve(
unsigned int init_size = 0);
64 void resize(
unsigned int new_size);
82 return vals_map_.size();
87 ASSERT_LT_DBG( 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_DBG(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_DBG(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];
151 #endif // BIDIRECTIONAL_MAP_HH_