00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_DICTIONARY_INL
00047
00048 #include "util_common.h"
00049 #include "dictionary.h"
00050
00051 namespace lass
00052 {
00053 namespace util
00054 {
00055
00056
00057
00058 template <typename K, typename V, typename KL, typename VL>
00059 Dictionary<K, V, KL, VL>::Dictionary():
00060 map_(),
00061 hasDefault_(false),
00062 enableSuggestions_(false)
00063 {
00064 }
00065
00066
00067
00068 template <typename K, typename V, typename KL, typename VL>
00069 Dictionary<K, V, KL, VL>::Dictionary(TKeyLess iKeyLess):
00070 map_(),
00071 keyLess_(iKeyLess),
00072 hasDefault_(false),
00073 enableSuggestions_(false)
00074 {
00075 }
00076
00077
00078
00079 template <typename K, typename V, typename KL, typename VL>
00080 Dictionary<K, V, KL, VL>::Dictionary(TKeyLess iKeyLess, TValueLess iValueLess):
00081 map_(),
00082 keyLess_(iKeyLess),
00083 valueLess_(iValueLess),
00084 hasDefault_(false),
00085 enableSuggestions_(false)
00086 {
00087 }
00088
00089
00090
00091
00092
00093 template <typename K, typename V, typename KL, typename VL>
00094 void Dictionary<K, V, KL, VL>::add(const TKey& iKey, const TValue& iValue)
00095 {
00096 std::pair<typename TMap::iterator, typename TMap::iterator> range = map_.equal_range(iKey);
00097 for (typename TMap::iterator i = range.first; i != range.second; ++i)
00098 {
00099 if (equalValues(i->second, iValue))
00100 {
00101 return;
00102 }
00103 }
00104 map_.insert(typename TMap::value_type(iKey, iValue));
00105 }
00106
00107
00108
00109
00110
00111 template <typename K, typename V, typename KL, typename VL>
00112 void Dictionary<K, V, KL, VL>::remove(const TKey& iKey, const TValue& iValue)
00113 {
00114 std::pair<typename TMap::iterator, typename TMap::iterator> range = map_.equal_range(iKey);
00115 for (typename TMap::iterator i = range.first; i != range.second; ++i)
00116 {
00117 if (equalValues(i->second, iValue))
00118 {
00119 map_.erase(i);
00120 return;
00121 }
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131 template <typename K, typename V, typename KL, typename VL>
00132 void Dictionary<K, V, KL, VL>::setDefault(const TKey& iKey, const TValue& iValue)
00133 {
00134 defaultKey_ = iKey;
00135 defaultValue_ = iValue;
00136 hasDefault_ = true;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 template <typename K, typename V, typename KL, typename VL>
00146 void Dictionary<K, V, KL, VL>::clearDefault()
00147 {
00148 hasDefault_ = false;
00149 }
00150
00151
00152
00153
00154
00155 template <typename K, typename V, typename KL, typename VL>
00156 bool Dictionary<K, V, KL, VL>::hasDefault() const
00157 {
00158 return hasDefault_;
00159 }
00160
00161
00162
00163
00164
00165
00166 template <typename K, typename V, typename KL, typename VL>
00167 void Dictionary<K, V, KL, VL>::enableSuggestions(bool enable)
00168 {
00169 enableSuggestions_ = enable;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 template <typename K, typename V, typename KL, typename VL>
00187 const typename Dictionary<K, V, KL, VL>::TValue&
00188 Dictionary<K, V, KL, VL>::operator[](TKeyParam iKey) const
00189 {
00190 typename TMap::const_iterator i = map_.find(iKey);
00191 if (i == map_.end())
00192 {
00193 if (!hasDefault_)
00194 {
00195 if (enableSuggestions_)
00196 {
00197 LASS_THROW("Key '" << iKey << "' is not found in dictionary, please try one "
00198 << "of these: " << keys());
00199 }
00200 else
00201 {
00202 LASS_THROW("Key '" << iKey << "' is not found in dictionary.");
00203 }
00204 }
00205 return defaultValue_;
00206 }
00207 return i->second;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 template <typename K, typename V, typename KL, typename VL>
00224 const typename Dictionary<K, V, KL, VL>::TKey&
00225 Dictionary<K, V, KL, VL>::key(TValueParam iValue) const
00226 {
00227 for (typename TMap::const_iterator i = map_.begin(); i != map_.end(); ++i)
00228 {
00229 if (equalValues(i->second, iValue))
00230 {
00231 return i->first;
00232 }
00233 }
00234 if (!hasDefault_)
00235 {
00236 if (enableSuggestions_)
00237 {
00238 LASS_THROW("Value '" << iValue << "' is not found in dictionary, please try one "
00239 << "of these: " << values());
00240 }
00241 else
00242 {
00243 LASS_THROW("Value '" << iValue << "' is not found in dictionary.");
00244 }
00245 }
00246 return defaultKey_;
00247 }
00248
00249
00250
00251
00252
00253 template <typename K, typename V, typename KL, typename VL>
00254 typename Dictionary<K, V, KL, VL>::TKeys
00255 Dictionary<K, V, KL, VL>::keys() const
00256 {
00257 TKeys result;
00258 for (typename TMap::const_iterator i = map_.begin(); i != map_.end(); ++i)
00259 {
00260 result.insert(i->first);
00261 }
00262 return result;
00263 }
00264
00265
00266
00267
00268
00269 template <typename K, typename V, typename KL, typename VL>
00270 typename Dictionary<K, V, KL, VL>::TValues
00271 Dictionary<K, V, KL, VL>::values() const
00272 {
00273 TValues result;
00274 for (typename TMap::const_iterator i = map_.begin(); i != map_.end(); ++i)
00275 {
00276 result.insert(i->second);
00277 }
00278 return result;
00279 }
00280
00281
00282
00283
00284
00285 template <typename K, typename V, typename KL, typename VL>
00286 typename Dictionary<K, V, KL, VL>::TKeys
00287 Dictionary<K, V, KL, VL>::keys(TValueParam iValue) const
00288 {
00289 TKeys result;
00290 for (typename TMap::const_iterator i = map_.begin(); i != map_.end(); ++i)
00291 {
00292 if (equalValues(i->second, iValue))
00293 {
00294 result.insert(i->first);
00295 }
00296 }
00297 return result;
00298 }
00299
00300
00301
00302
00303
00304 template <typename K, typename V, typename KL, typename VL>
00305 typename Dictionary<K, V, KL, VL>::TValues
00306 Dictionary<K, V, KL, VL>::values(TKeyParam iKey) const
00307 {
00308 TValues result;
00309 std::pair<typename TMap::const_iterator, typename TMap::const_iterator> range = map_.equal_range(iKey);
00310 for (typename TMap::const_iterator i = range.first; i != range.second; ++i)
00311 {
00312 result.insert(i->second);
00313 }
00314 return result;
00315 }
00316
00317
00318
00319
00320
00321 template <typename K, typename V, typename KL, typename VL>
00322 bool Dictionary<K, V, KL, VL>::isKey(TKeyParam iKey) const
00323 {
00324 return map_.find(iKey) != map_.end();
00325 }
00326
00327
00328
00329
00330
00331 template <typename K, typename V, typename KL, typename VL>
00332 bool Dictionary<K, V, KL, VL>::isValue(TValueParam iValue) const
00333 {
00334 for (typename TMap::const_iterator i = map_.begin(); i != map_.end(); ++i)
00335 {
00336 if (equalValues(i->second, iValue))
00337 {
00338 return true;
00339 }
00340 }
00341 return false;
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 template <typename K, typename V, typename KL, typename VL>
00353 bool Dictionary<K, V, KL, VL>::equalValues(TValueParam iA, TValueParam iB) const
00354 {
00355 return !valueLess_(iA, iB) && !valueLess_(iB, iA);
00356 }
00357
00358
00359
00360
00361
00362
00363
00364 }
00365
00366 }
00367
00368 #endif
00369
00370