torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SymTab.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifndef TORC_GENERIC_SYMTAB_HPP
17 #define TORC_GENERIC_SYMTAB_HPP
18 
19 #include <algorithm>
20 #include <map>
21 #include <vector>
22 
23 #ifdef GENOM_SERIALIZATION
24 #include <boost/serialization/access.hpp>
25 #include <boost/serialization/map.hpp>
26 #endif //GENOM_SERIALIZATION
27 #include "torc/generic/Error.hpp"
28 
29 namespace torc {
30 namespace generic {
31 
32 /**
33  * @brief A symbol table
34  *
35  * This class acts as a symbol table to store key-value pairs
36  */
37 
38 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting = false> class SymTab {
39 #ifdef GENOM_SERIALIZATION
40  friend class boost::serialization::access;
41 #endif
42 
43  struct Data {
44 #ifdef GENOM_SERIALIZATION
45  friend class boost::serialization::access;
46 #endif
47  size_t mIndex;
48  _ValueType mValue;
49 
50  bool operator <(const Data& inRhs) const {
51  return mIndex < inRhs.mIndex;
52  }
53 
54 #ifdef GENOM_SERIALIZATION
55  template <class Archive> void serialize(Archive& ar, unsigned int) {
56  ar & mIndex;
57  ar & mValue;
58  }
59 #endif //GENOM_SERIALIZATION
60  };
61 public:
62  typedef _KeyType KeyType;
63  typedef _ValueType ValueType;
64  typedef std::map<KeyType, Data> Map;
65  typedef std::map<KeyType, ValueType> UserMap;
66 
67  SymTab();
68 
69  ~SymTab() throw ();
70 
71  /**
72  * Get a value for a key
73  * @param[in] inKey key to look for
74  * @param[out] outValue resultant value
75  * @return true if object was found
76  */
77  inline bool get(const KeyType& inKey, ValueType& outValue) const;
78 
79  /**
80  * Set a value for a key. If cOverWriteExisting is true, Existing value will be replaced.
81  * @param[in] inKey key to look for
82  * @param[in] inValue value to be stored
83  * @return True if value was inserted
84  */
85  inline bool set(const KeyType& inKey, const ValueType& inValue);
86 
87  /**
88  * Remove a value for a key
89  * @param[in] inKey key to look for
90  * @return true if object was removed
91  */
92  inline bool remove(const KeyType& inKey);
93 
94  inline void getValues(std::vector<ValueType>& outValues) const;
95 
96  inline void getValueMap(UserMap& outMap) const;
97 
98  inline size_t getSize() const;
99 
100  inline void clear();
101 
102  template <typename _Action> void applyOnAll(const _Action& action) throw (Error);
103 
104 private:
105 #ifdef GENOM_SERIALIZATION
106  template <class Archive> void serialize(Archive& ar, unsigned int);
107 #endif //GENOM_SERIALIZATION
109  //For ordering
110  size_t mNextValue;
111 };
112 
113 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting>
115  mValues(), mNextValue(0) {}
116 
117 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting>
119 
120 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline bool SymTab<
121  _KeyType, _ValueType, cOverWriteExisting>::get(const KeyType& inKey,
122  ValueType& outValue) const {
123  typename Map::const_iterator it = mValues.find(inKey);
124  if(it == mValues.end()) {
125  return false;
126  }
127  outValue = (*it).second.mValue;
128  return true;
129 }
130 
131 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline bool SymTab<
132  _KeyType, _ValueType, cOverWriteExisting>::set(const KeyType& inKey, const ValueType& inValue) {
133  Data data;
134  data.mIndex = mNextValue;
135  data.mValue = inValue;
136  mNextValue++;
137  typename Map::value_type value = std::make_pair(inKey, data);
138 
139  std::pair<typename Map::iterator, bool> res = mValues.insert(value);
140  if(false == res.second) {
141  if(cOverWriteExisting) {
142  mValues.erase(res.first);
143  mValues.insert(value);
144  } else {
145  mNextValue--;
146  return false;
147  }
148  }
149  return true;
150 }
151 
152 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline bool SymTab<
153  _KeyType, _ValueType, cOverWriteExisting>::remove(const KeyType& inKey) {
154  return mValues.erase(inKey) > 0;
155 }
156 
157 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline void SymTab<
158  _KeyType, _ValueType, cOverWriteExisting>::getValues(std::vector<ValueType>& outValues) const {
159  std::vector<Data> values;
160  for(typename Map::const_iterator it = mValues.begin(); it != mValues.end(); ++it) {
161  values.push_back((*it).second);
162  }
163  sort(values.begin(), values.end(), std::less<Data>());
164  for(typename std::vector<Data>::iterator it = values.begin(); it != values.end(); ++it) {
165  outValues.push_back((*it).mValue);
166  }
167  return;
168 }
169 
170 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline void SymTab<
171  _KeyType, _ValueType, cOverWriteExisting>::getValueMap(
173 
174  for(typename Map::const_iterator it = mValues.begin(); it != mValues.end(); ++it) {
175  typename UserMap::value_type value = std::make_pair((*it).first, (*it).second.mValue);
176  outMap.insert(value);
177  }
178  return;
179 }
180 
181 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline size_t SymTab<
182  _KeyType, _ValueType, cOverWriteExisting>::getSize() const {
183  return mValues.size();
184 }
185 
186 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> inline void SymTab<
187  _KeyType, _ValueType, cOverWriteExisting>::clear() {
188  mValues.clear();
189 }
190 
191 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> template <
193  const _Action& action) throw (Error) {
194  std::vector<Data> values;
195  for(typename Map::const_iterator it = mValues.begin(); it != mValues.end(); ++it) {
196  values.push_back((*it).second);
197  }
198  sort(values.begin(), values.end(), std::less<Data>());
199  for(typename std::vector<Data>::iterator value = values.begin(); value != values.end();
200  ++value) {
201  try {
202  action((*value).mValue);
203  } catch(Error& e) {
204  e.setCurrentLocation(__FUNCTION__, __FILE__, __LINE__);
205  throw;
206  }
207  }
208 }
209 
210 #ifdef GENOM_SERIALIZATION
211 template <typename _KeyType, typename _ValueType, bool cOverWriteExisting> template <class Archive>
212  void SymTab<_KeyType,_ValueType,cOverWriteExisting>::serialize(Archive& ar, unsigned int) {
213  ar & mValues;
214 }
215 #endif //GENOM_SERIALIZATION
216 
217 } // namespace generic
218 } //namespace torc
219 
220 #endif // TORC_GENERIC_SYMTAB_HPP
void getValues(std::vector< ValueType > &outValues) const
Definition: SymTab.hpp:158
bool set(const KeyType &inKey, const ValueType &inValue)
Definition: SymTab.hpp:132
void applyOnAll(const _Action &action)
Definition: SymTab.hpp:192
bool remove(const KeyType &inKey)
Definition: SymTab.hpp:153
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
bool get(const KeyType &inKey, ValueType &outValue) const
Definition: SymTab.hpp:121
bool operator<(const Data &inRhs) const
Definition: SymTab.hpp:50
void getValueMap(UserMap &outMap) const
Definition: SymTab.hpp:171
_ValueType ValueType
Definition: SymTab.hpp:63
size_t getSize() const
Definition: SymTab.hpp:182
A symbol table.
Definition: SymTab.hpp:38
std::map< KeyType, Data > Map
Definition: SymTab.hpp:64
std::map< KeyType, ValueType > UserMap
Definition: SymTab.hpp:65
void setCurrentLocation(const std::string &inFunction, const std::string &inFile, uint32_t inLine)
Definition: Error.cpp:73