00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef TORC_GENERIC_OM_SYMTAB_HPP
00017 #define TORC_GENERIC_OM_SYMTAB_HPP
00018
00019 #include "torc/generic/om/DumpRestoreConfig.hpp"
00020
00021 #include <algorithm>
00022 #include <map>
00023 #include <vector>
00024
00025 #ifdef GENOM_SERIALIZATION
00026 #include <boost/serialization/access.hpp>
00027 #include <boost/serialization/map.hpp>
00028 #endif //GENOM_SERIALIZATION
00029 #include "torc/generic/util/Error.hpp"
00030
00031 namespace torc {
00032
00033 namespace generic {
00034
00035
00036
00037
00038
00039
00040
00041 template<typename _KeyType,
00042 typename _ValueType,
00043 bool cOverWriteExisting = false>
00044 class SymTab {
00045 #ifdef GENOM_SERIALIZATION
00046 friend class boost::serialization::access;
00047 #endif
00048
00049 struct Data
00050 {
00051 #ifdef GENOM_SERIALIZATION
00052 friend class boost::serialization::access;
00053 #endif
00054 size_t mIndex;
00055 _ValueType mValue;
00056
00057 bool
00058 operator < ( const Data &inRhs ) const {
00059 return mIndex < inRhs.mIndex;
00060 }
00061
00062 #ifdef GENOM_SERIALIZATION
00063 template<class Archive> void
00064 serialize( Archive &ar, unsigned int ) {
00065 ar & mIndex;
00066 ar & mValue;
00067 }
00068 #endif //GENOM_SERIALIZATION
00069
00070 };
00071 public:
00072 typedef _KeyType KeyType;
00073 typedef _ValueType ValueType;
00074 typedef std::map<KeyType,Data> Map;
00075 typedef std::map<KeyType,ValueType> UserMap;
00076
00077 SymTab();
00078
00079 ~SymTab() throw();
00080
00081
00082
00083
00084
00085
00086
00087 inline bool
00088 get( const KeyType &inKey,
00089 ValueType &outValue ) const throw();
00090
00091
00092
00093
00094
00095
00096
00097 inline bool
00098 set( const KeyType &inKey,
00099 const ValueType &inValue ) throw();
00100
00101
00102
00103
00104
00105
00106 inline bool
00107 remove( const KeyType &inKey ) throw();
00108
00109 inline void
00110 getValues(std::vector<ValueType> &outValues) const throw();
00111
00112 inline void
00113 getValueMap( UserMap &outMap ) const throw();
00114
00115 inline size_t
00116 getSize() const throw();
00117
00118 inline void
00119 clear() throw();
00120
00121 template<typename _Action>
00122 void
00123 applyOnAll( const _Action &action ) throw(Error);
00124
00125 private:
00126 #ifdef GENOM_SERIALIZATION
00127 template<class Archive> void
00128 serialize( Archive &ar, unsigned int );
00129 #endif //GENOM_SERIALIZATION
00130
00131 Map mValues;
00132
00133 size_t mNextValue;
00134 };
00135
00136 template<typename _KeyType,
00137 typename _ValueType,
00138 bool cOverWriteExisting>
00139 SymTab<_KeyType,_ValueType,cOverWriteExisting>::SymTab()
00140 :mValues(),
00141 mNextValue(0) {
00142 }
00143
00144 template<typename _KeyType,
00145 typename _ValueType,
00146 bool cOverWriteExisting>
00147 SymTab<_KeyType,_ValueType,cOverWriteExisting>::~SymTab() throw() {
00148 }
00149
00150 template<typename _KeyType,
00151 typename _ValueType,
00152 bool cOverWriteExisting>
00153 inline bool
00154 SymTab<_KeyType,_ValueType,cOverWriteExisting>::get(
00155 const KeyType &inKey, ValueType &outValue ) const throw() {
00156 typename Map::const_iterator it = mValues.find( inKey );
00157 if( it == mValues.end() )
00158 {
00159 return false;
00160 }
00161 outValue = (*it).second.mValue;
00162 return true;
00163 }
00164
00165 template<typename _KeyType,
00166 typename _ValueType,
00167 bool cOverWriteExisting>
00168 inline bool
00169 SymTab<_KeyType,_ValueType,cOverWriteExisting>::set(
00170 const KeyType &inKey, const ValueType &inValue ) throw() {
00171 Data data;
00172 data.mIndex = mNextValue;
00173 data.mValue = inValue;
00174 mNextValue++;
00175 typename Map::value_type value
00176 = std::make_pair( inKey, data );
00177
00178 std::pair< typename Map::iterator, bool > res
00179 = mValues.insert( value );
00180 if( false == res.second )
00181 {
00182 if( cOverWriteExisting )
00183 {
00184 mValues.erase( res.first );
00185 mValues.insert( value );
00186 }
00187 else
00188 {
00189 mNextValue--;
00190 return false;
00191 }
00192 }
00193 return true;
00194 }
00195
00196 template<typename _KeyType,
00197 typename _ValueType,
00198 bool cOverWriteExisting>
00199 inline bool
00200 SymTab<_KeyType,_ValueType,cOverWriteExisting>::remove(
00201 const KeyType &inKey ) throw()
00202 {
00203 return mValues.erase( inKey ) > 0;
00204 }
00205
00206 template<typename _KeyType,
00207 typename _ValueType,
00208 bool cOverWriteExisting>
00209 inline void
00210 SymTab<_KeyType,_ValueType,cOverWriteExisting>::getValues(
00211 std::vector<ValueType> &outValues) const throw() {
00212 std::vector<Data> values;
00213 for( typename Map::const_iterator it = mValues.begin();
00214 it != mValues.end(); ++it )
00215 {
00216 values.push_back( (*it).second );
00217 }
00218 sort( values.begin(), values.end(), std::less<Data>());
00219 for( typename std::vector<Data>::iterator it = values.begin();
00220 it != values.end(); ++it )
00221 {
00222 outValues.push_back( (*it).mValue );
00223 }
00224 return;
00225 }
00226
00227 template<typename _KeyType,
00228 typename _ValueType,
00229 bool cOverWriteExisting>
00230 inline void
00231 SymTab<_KeyType,_ValueType,cOverWriteExisting>::getValueMap(
00232 typename SymTab<_KeyType,_ValueType,cOverWriteExisting>::UserMap &outMap ) const throw()
00233 {
00234
00235 for( typename Map::const_iterator it = mValues.begin();
00236 it != mValues.end(); ++it )
00237 {
00238 typename UserMap::value_type value
00239 = std::make_pair( (*it).first, (*it).second.mValue );
00240 outMap.insert( value );
00241 }
00242 return;
00243 }
00244
00245 template<typename _KeyType,
00246 typename _ValueType,
00247 bool cOverWriteExisting>
00248 inline size_t
00249 SymTab<_KeyType,_ValueType,cOverWriteExisting>::getSize() const throw()
00250 {
00251 return mValues.size();
00252 }
00253
00254 template<typename _KeyType,
00255 typename _ValueType,
00256 bool cOverWriteExisting>
00257 inline void
00258 SymTab<_KeyType,_ValueType,cOverWriteExisting>::clear() throw()
00259 {
00260 mValues.clear();
00261 }
00262
00263 template<typename _KeyType,
00264 typename _ValueType,
00265 bool cOverWriteExisting>
00266 template<typename _Action>
00267 inline void
00268 SymTab<_KeyType,_ValueType,cOverWriteExisting>::applyOnAll( const _Action &action ) throw(Error)
00269 {
00270 std::vector<Data> values;
00271 for( typename Map::const_iterator it = mValues.begin();
00272 it != mValues.end(); ++it )
00273 {
00274 values.push_back( (*it).second );
00275 }
00276 sort( values.begin(), values.end(), std::less<Data>());
00277 for( typename std::vector<Data>::iterator value = values.begin();
00278 value != values.end(); ++value ) {
00279 try
00280 {
00281 action( (*value).mValue );
00282 }
00283 catch(Error &e)
00284 {
00285 e.setCurrentLocation(
00286 __FUNCTION__, __FILE__, __LINE__ );
00287 throw;
00288 }
00289 }
00290 }
00291
00292 #ifdef GENOM_SERIALIZATION
00293 template<typename _KeyType,
00294 typename _ValueType,
00295 bool cOverWriteExisting>
00296 template<class Archive> void
00297 SymTab<_KeyType,_ValueType,cOverWriteExisting>::serialize( Archive &ar, unsigned int ) {
00298 ar & mValues;
00299 }
00300 #endif //GENOM_SERIALIZATION
00301
00302 }
00303
00304 }
00305
00306 #endif // TORC_GENERIC_OM_SYMTAB_HPP