library of assembled shared sources

http://lass.cocamware.com

dictionary.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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 // --- public --------------------------------------------------------------------------------------
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 /** add a key and value to dictionary as new entry.
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 /** remove a key and value from the dictionary.
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 /** set key and value to be used as default ones.
00128  *  Once @e default key and value are set, these will be returned on occasions you try to look up a
00129  *  key or value but it can't be found.
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 /** clear default key and value.
00142  *  If no @e default key and value are set, and exception will be thrown on occasions you try to look
00143  *  up a key or value but it can't be found.
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 /** return true if dictionary has value
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 /** if enabled, suggestions of valid keys or values will be included in the exception that
00164  *  is thrown in case a key or value is not found and there's no default.
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 /** look up a value by key
00175  *  @return
00176  *      - the value that belongs to the key.
00177  *      - if such a value can not be found and an @e default value is set, then this @e default
00178  *        will be returned.
00179  *  @note
00180  *      if multiple entries with the same key are found, only one is returned.  It is not
00181  *      specified which one this will be.  If you want all entries with this key, use @c values(iKey).
00182  *  @throw
00183  *      if no value can be found that belongs to the key and no @e default value is set, then
00184  *      an exception will be thrown.
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 /** look up a key by value
00213  *  @return
00214  *      - the key to which the value belongs
00215  *      - if such a key can not be found and an @e default key is set, then this @e default will
00216  *        be returned.
00217  *  @note
00218  *      if multiple entries with the same value are found, only one is returned.  It is not
00219  *      specified which one this will be.  If you want all entries with this value, use @c keys(iValue).
00220  *  @throw if no key can be found to wich the value belongs and no @e default key is set, then an
00221  *         exception will be thrown.
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 /** return all keys in the dictionary
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 /** return all values in the dictionary
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 /** return all keys that have value @a iValue.
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 /** return all values in the dictionary
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 /** return true if @a iKey is a key of dictionary
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 /** return true if @a iValue is a value of dictionary
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 // --- protected -----------------------------------------------------------------------------------
00347 
00348 
00349 
00350 // --- private -------------------------------------------------------------------------------------
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 // --- free ----------------------------------------------------------------------------------------
00361 
00362 
00363 
00364 }
00365 
00366 }
00367 
00368 #endif
00369 
00370 // EOF

Generated on Mon Nov 10 14:20:03 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo