Flow123d  release_3.0.0-916-g95df358
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 <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  /// Reset data of map, allow reserve size.
57  void reinit(unsigned int init_size = 0);
58 
59  /// Return value on given position.
60  T operator[](unsigned int pos) const;
61 
62 private:
63  std::vector<T> vals_vec_; ///< Space to save values.
64  std::map<T, unsigned int> vals_map_; ///< Maps values to indexes into vals_vec_.
65 };
66 
67 // --------------------------------------------------- BidirectionalMap INLINE implementation -----------
68 template<typename T>
70 {}
71 
72 template<typename T>
73 inline unsigned int BidirectionalMap<T>::size() const {
74  return vals_map_.size();
75 }
76 
77 template<typename T>
78 inline void BidirectionalMap<T>::set_item(T val, unsigned int pos) {
79  ASSERT_LT( pos, vals_vec_.size() )(pos)(vals_vec_.size()).error("Value id is out of vector size.");
80  ASSERT( vals_vec_[pos] == -1 )(pos).error("Repeated setting of item.");
81  vals_map_[val] = pos;
82  vals_vec_[pos] = val;
83 }
84 
85 template<typename T>
86 inline unsigned int BidirectionalMap<T>::add_item(T val) {
87  ASSERT( vals_map_.find(val) == vals_map_.end() )(val).error("Can not add item since it already exists.");
88  vals_map_[val] = vals_vec_.size();
89  vals_vec_.push_back(val);
90  return vals_map_[val];
91 }
92 
93 template<typename T>
94 inline int BidirectionalMap<T>::get_position(T val) const {
95  typename std::map<T, unsigned int>::const_iterator iter = vals_map_.find(val);
96  if (iter == vals_map_.end()) return -1;
97  else return iter->second;
98 }
99 
100 template<typename T>
101 inline void BidirectionalMap<T>::reinit(unsigned int init_size) {
102  vals_map_.clear();
103  vals_vec_.clear();
104  vals_vec_.resize(init_size, -1);
105 }
106 
107 template<typename T>
108 inline T BidirectionalMap<T>::operator[](unsigned int pos) const {
109  ASSERT( pos < vals_vec_.size() )(pos)(vals_vec_.size());
110  return vals_vec_[pos];
111 }
112 
113 
114 #endif // BIDIRECTIONAL_MAP_HH_
BidirectionalMap()
Constructor.
unsigned int size() const
Return size of map.
std::vector< T > vals_vec_
Space to save values.
Definitions of ASSERTS.
std::map< T, unsigned int > vals_map_
Maps values to indexes into vals_vec_.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
unsigned int add_item(T val)
Add new item at the end position of map.
void set_item(T val, unsigned int pos)
void reinit(unsigned int init_size=0)
Reset data of map, allow reserve size.
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:295
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>.