- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_map.h

Go to the documentation of this file.

    00001 // Warning : If you need to use this file, include frost_utils_types.h
    00002 #include <map>
    00003
    00004 namespace Frost
    00005 {
    00006     /// Base type : container (sorted associative)
    00007     /** This class is basically a wrapper around std::map.
    00008     *   It features a few more finding functions and some renamed ones.
    00009     */
    00010     template<class Key, class Value>
    00011     class s_map
    00012     {
    00013     public :
    00014
    00015         typedef typename std::map<Key, Value>::iterator       iterator;
    00016         typedef typename std::map<Key, Value>::const_iterator const_iterator;
    00017         typedef          s_range<iterator>                    range;
    00018         typedef          s_range<const_iterator>              const_range;
    00019         typedef          s_pair<Key, Value>                   pair;
    00020
    00021         /// Default constructor.
    00022         s_map()
    00023         {
    00024         }
    00025
    00026         #ifdef CPP_0X
    00027             /// Initializer list constructor.
    00028             /** \param mList Brace enclosed pair list
    00029             *   \note This constructor uses a C++0x feature.<br>
    00030             *         It allows : s_map<s_int, s_str> v = {MakePair(1, "bar"), MakePair(4, "foo")};
    00031             */
    00032             s_map(std::initializer_list<pair> mList)
    00033             {
    00034                 for (const pair* p = mList.begin(); p != mList.end(); ++p)
    00035                     mMap_.insert(std::make_pair(p->First(), p->Second()));
    00036             }
    00037         #endif
    00038
    00039         /// Inserts the provided pair in the map.
    00040         /** \param mPair The pair to insert
    00041         *   \return An iterator pointing at the inserted value, and a bool
    00042         *           that equals 'true' if no element with the same key was
    00043         *           present in the map.
    00044         *   \note If the provided key is already present in the map, the value
    00045         *         is not inserted.
    00046         */
    00047         s_pair<iterator, s_bool> Insert(const pair& mPair)
    00048         {
    00049             std::pair<iterator, bool> mResult = mMap_.insert(std::make_pair(mPair.First(), mPair.Second()));
    00050             return s_pair<iterator, s_bool>(mResult.first, mResult.second);
    00051         }
    00052
    00053         /// Erases an element from the map.
    00054         /** \param mKey The key of the element to erase
    00055         */
    00056         void Erase(const Key& mKey)
    00057         {
    00058             mMap_.erase(mKey);
    00059         }
    00060
    00061         /// Erases an element from the map.
    00062         /** \param mIter An iterator pointing at the element to erase
    00063         */
    00064         void Erase(const iterator& mIter)
    00065         {
    00066             mMap_.erase(mIter);
    00067         }
    00068
    00069         /// Erases a range of elements from the map.
    00070         /** \param mBegin The start of the range
    00071         *   \param mEnd   The ending of the range
    00072         */
    00073         void Erase(const iterator& mBegin, const iterator& mEnd)
    00074         {
    00075             mMap_.erase(mBegin, mEnd);
    00076         }
    00077
    00078         /// Erases a range of elements from the map.
    00079         /** \param mRange The range to erase
    00080         */
    00081         void Erase(const range& mRange)
    00082         {
    00083             mMap_.erase(mRange.Begin(), mRange.End());
    00084         }
    00085
    00086         /// Searches the map for the provided key.
    00087         /** \param mKey The key to search for
    00088         *   \return 'true' if the key has been found
    00089         */
    00090         s_bool Find(const Key& mKey) const
    00091         {
    00092             return (mMap_.find(mKey) != End());
    00093         }
    00094
    00095         /// Searches the map for the provided key.
    00096         /** \param mKey The key to search for
    00097         *   \return The position of the key in the map
    00098         *   \note If the key is not present in the map, this function
    00099         *         returns s_uint::NaN.
    00100         */
    00101         s_uint_t<default_uint> FindPos(const Key& mKey) const
    00102         {
    00103             const_iterator mIter;
    00104             mIter = mMap_.find(mKey);
    00105             if (mIter == End())
    00106                 return s_uint_t<default_uint>::NaN;
    00107             else
    00108                 return mIter - Begin();
    00109         }
    00110
    00111         /// Searches the map for the provided key.
    00112         /** \param mKey The key to search for
    00113         *   \return An iterator pointing to the associated pair
    00114         *   \note If the key is not present in the map, this function
    00115         *         returns End().
    00116         */
    00117         iterator Get(const Key& mKey)
    00118         {
    00119             return mMap_.find(mKey);
    00120         }
    00121
    00122         /// Searches the map for the provided key.
    00123         /** \param mKey The key to search for
    00124         *   \return An iterator pointing to the associated pair
    00125         *   \note If the key is not present in the map, this function
    00126         *         returns End().
    00127         */
    00128         const_iterator Get(const Key& mKey) const
    00129         {
    00130             return mMap_.find(mKey);
    00131         }
    00132
    00133         /// Returns one of the surrounding pairs.
    00134         /** \param mKey The key to search for
    00135         *   \return An iterator pointing to one of the surrounding pairs
    00136         *   \note You are searching for mKey, but it's proably not
    00137         *         in this map. But there are two keys in the map that
    00138         *         surround mKey : mLower and mUpper.<br>
    00139         *         This function will always return mUpper, unless mKey
    00140         *         equals mLower : it will then return mLower.
    00141         */
    00142         iterator LowerBound(const Key& mKey)
    00143         {
    00144             return mMap_.lower_bound(mKey);
    00145         }
    00146
    00147         /// Returns one of the surrounding pairs.
    00148         /** \param mKey The key to search for
    00149         *   \return An iterator pointing to one of the surrounding pairs
    00150         *   \note You are searching for mKey, but it's proably not
    00151         *         in this map. But there are two keys in the map that
    00152         *         surround mKey : mLower and mUpper.<br>
    00153         *         This function will always return mUpper, unless mKey
    00154         *         equals mLower : it will then return mLower.
    00155         */
    00156         const_iterator LowerBound(const Key& mKey) const
    00157         {
    00158             return mMap_.lower_bound(mKey);
    00159         }
    00160
    00161         /// Returns one of the surrounding pairs.
    00162         /** \param mKey The key to search for
    00163         *   \return An iterator pointing to one of the surrounding pairs
    00164         *   \note You are searching for mKey, but it's proably not
    00165         *         in this map. But there are two keys in the map that
    00166         *         surround mKey : mLower and mUpper.<br>
    00167         *         This function will always return mUpper, unless mKey
    00168         *         equals mUpper : it will then return mUpper+1.
    00169         */
    00170         iterator UpperBound(const Key& mKey)
    00171         {
    00172             return mMap_.upper_bound(mKey);
    00173         }
    00174
    00175         /// Returns one of the surrounding pairs.
    00176         /** \param mKey The key to search for
    00177         *   \return An iterator pointing to one of the surrounding pairs
    00178         *   \note You are searching for mKey, but it's proably not
    00179         *         in this map. But there are two keys in the map that
    00180         *         surround mKey : mLower and mUpper.<br>
    00181         *         This function will always return mUpper, unless mKey
    00182         *         equals mUpper : it will then return mUpper+1.
    00183         */
    00184         const_iterator UpperBound(const Key& mKey) const
    00185         {
    00186             return mMap_.upper_bound(mKey);
    00187         }
    00188
    00189         /// Removes all elements from this map.
    00190         void Clear()
    00191         {
    00192             mMap_.clear();
    00193         }
    00194
    00195         /// Returns the number of elements in this map.
    00196         /** \return The number of elements in this map
    00197         */
    00198         s_uint_t<default_uint> GetSize() const
    00199         {
    00200             return mMap_.size();
    00201         }
    00202
    00203         /// Checks if this map is empty.
    00204         /** \return 'true' if this map is empty
    00205         */
    00206         s_bool IsEmpty() const
    00207         {
    00208             return mMap_.empty();
    00209         }
    00210
    00211         Value& operator [] (const Key& mKey)
    00212         {
    00213             return mMap_[mKey];
    00214         }
    00215
    00216         const Value& operator [] (const Key& mKey) const
    00217         {
    00218             return mMap_[mKey];
    00219         }
    00220
    00221         s_ctnr<s_map> operator, (const s_map& lValue)
    00222         {
    00223             s_ctnr<s_map> mContainer;
    00224             mContainer.PushBack(*this);
    00225             mContainer.PushBack(lValue);
    00226             return mContainer;
    00227         }
    00228
    00229         iterator Begin()
    00230         {
    00231             return mMap_.begin();
    00232         }
    00233
    00234         const_iterator Begin() const
    00235         {
    00236             return mMap_.begin();
    00237         }
    00238
    00239         iterator End()
    00240         {
    00241             return mMap_.end();
    00242         }
    00243
    00244         const_iterator End() const
    00245         {
    00246             return mMap_.end();
    00247         }
    00248
    00249         iterator begin()
    00250         {
    00251             return mMap_.begin();
    00252         }
    00253
    00254         const_iterator begin() const
    00255         {
    00256             return mMap_.begin();
    00257         }
    00258
    00259         iterator end()
    00260         {
    00261             return mMap_.end();
    00262         }
    00263
    00264         const_iterator end() const
    00265         {
    00266             return mMap_.end();
    00267         }
    00268
    00269     private :
    00270
    00271         std::map<Key, Value> mMap_;
    00272
    00273     };
    00274
    00275     /** \cond NOT_REMOVE_FROM_DOC
    00276     */
    00277     template<class Key, class Value> class StringConverter< string_element, s_map<Key, Value> >
    00278     {
    00279     public :
    00280
    00281         typedef s_str::string string;
    00282
    00283         static string Convert(const s_map<Key, Value>& mMap)
    00284         {
    00285             string sTemp = "(";
    00286             typename s_map<Key, Value>::const_iterator iter;
    00287             for (iter = mMap.Begin(); iter != mMap.End(); ++iter)
    00288             {
    00289                 if (iter != mMap.Begin())
    00290                     sTemp += ", [" + StringConverter<string_element, Key>::Convert(iter->first)
    00291                           + "] = " + StringConverter<string_element, Value>::Convert(iter->second);
    00292                 else
    00293                     sTemp +=   "[" + StringConverter<string_element, Key>::Convert(iter->first)
    00294                           + "] = " + StringConverter<string_element, Value>::Convert(iter->second);
    00295             }
    00296             sTemp += ")";
    00297
    00298             return sTemp;
    00299         }
    00300     };
    00301     /** \endcond
    00302     */
    00303 }