- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/Programmation/Sources/Tensor2/Utils/include/frost_utils_multimap.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::multimap.
    00008     *   It features a few more finding functions and some renamed ones.
    00009     */
    00010     template<class Key, class Value>
    00011     class s_multimap
    00012     {
    00013     public :
    00014
    00015         typedef typename std::multimap<Key, Value>::iterator       iterator;
    00016         typedef typename std::multimap<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_multimap()
    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_multimap<s_int, s_str> v = {MakePair(1, "bar"), MakePair(4, "foo")};
    00031             */
    00032             s_multimap(std::initializer_list<pair> mList)
    00033             {
    00034                 for (const pair* p = mList.begin(); p != mList.end(); ++p)
    00035                     mMulMap_.insert(std::make_pair(p->First(), p->Second()));
    00036             }
    00037         #endif
    00038
    00039         /// Erases an element from the multimap.
    00040         /** \param mKey The key of the element to erase
    00041         */
    00042         void Erase(const Key& mKey)
    00043         {
    00044             mMulMap_.erase(mKey);
    00045         }
    00046
    00047         /// Erases an element from the multimap.
    00048         /** \param mIter An iterator pointing at the element to erase
    00049         */
    00050         void Erase(const iterator& mIter)
    00051         {
    00052             mMulMap_.erase(mIter);
    00053         }
    00054
    00055         /// Erases a range of elements from the multimap.
    00056         /** \param mBegin The start of the range
    00057         *   \param mEnd   The ending of the range
    00058         */
    00059         void Erase(const iterator& mBegin, const iterator& mEnd)
    00060         {
    00061             mMulMap_.erase(mBegin, mEnd);
    00062         }
    00063
    00064         /// Erases a range of elements from the multimap.
    00065         /** \param mRange The range to erase
    00066         */
    00067         void Erase(const range& mRange)
    00068         {
    00069             mMulMap_.erase(mRange.Begin(), mRange.End());
    00070         }
    00071
    00072         /// Searches the multimap for the provided key.
    00073         /** \param mKey The key to search for
    00074         *   \return 'true' if the key has been found
    00075         */
    00076         s_bool Find(const Key& mKey) const
    00077         {
    00078             return (mMulMap_.find(mKey) != End());
    00079         }
    00080
    00081         /// Searches the multimap for the provided key.
    00082         /** \param mKey The key to search for
    00083         *   \return The position of the key in the multimap
    00084         *   \note If the key is not present in the multimap, this function
    00085         *         returns s_uint::NaN.
    00086         */
    00087         s_uint_t<default_uint> FindPos(const Key& mKey) const
    00088         {
    00089             const_iterator mIter = mMulMap_.find(mKey);
    00090             if (mIter == End())
    00091                 return s_uint_t<default_uint>::NaN;
    00092             else
    00093                 return mIter - Begin();
    00094         }
    00095
    00096         /// Searches the multimap for the provided key.
    00097         /** \param mKey The key to search for
    00098         *   \return An iterator pointing to the associated pair
    00099         *   \note If the key is not present in the multimap, this function
    00100         *         returns End().
    00101         */
    00102         iterator Get(const Key& mKey)
    00103         {
    00104             return mMulMap_.find(mKey);
    00105         }
    00106
    00107         /// Searches the multimap for the provided key.
    00108         /** \param mKey The key to search for
    00109         *   \return An iterator pointing to the associated pair
    00110         *   \note If the key is not present in the multimap, this function
    00111         *         returns End().
    00112         */
    00113         const_iterator Get(const Key& mKey) const
    00114         {
    00115             return mMulMap_.find(mKey);
    00116         }
    00117
    00118         /// Returns one of the surrounding pairs.
    00119         /** \param mKey The key to search for
    00120         *   \return An iterator pointing to one of the surrounding pairs
    00121         *   \note You are searching for mKey, but it's proably not
    00122         *         in this multimap. But there are two keys in the multimap that
    00123         *         surround mKey : mLower and mUpper.<br>
    00124         *         This function will always return mUpper, unless mKey
    00125         *         equals mLower : it will then return mLower.
    00126         */
    00127         iterator LowerBound(const Key& mKey)
    00128         {
    00129             return mMulMap_.lower_bound(mKey);
    00130         }
    00131
    00132         /// Returns one of the surrounding pairs.
    00133         /** \param mKey The key to search for
    00134         *   \return An iterator pointing to one of the surrounding pairs
    00135         *   \note You are searching for mKey, but it's proably not
    00136         *         in this multimap. But there are two keys in the multimap that
    00137         *         surround mKey : mLower and mUpper.<br>
    00138         *         This function will always return mUpper, unless mKey
    00139         *         equals mLower : it will then return mLower.
    00140         */
    00141         const_iterator LowerBound(const Key& mKey) const
    00142         {
    00143             return mMulMap_.lower_bound(mKey);
    00144         }
    00145
    00146         /// Returns one of the surrounding pairs.
    00147         /** \param mKey The key to search for
    00148         *   \return An iterator pointing to one of the surrounding pairs
    00149         *   \note You are searching for mKey, but it's proably not
    00150         *         in this multimap. But there are two keys in the multimap that
    00151         *         surround mKey : mLower and mUpper.<br>
    00152         *         This function will always return mUpper, unless mKey
    00153         *         equals mUpper : it will then return mUpper+1.
    00154         */
    00155         iterator UpperBound(const Key& mKey)
    00156         {
    00157             return mMulMap_.upper_bound(mKey);
    00158         }
    00159
    00160         /// Returns one of the surrounding pairs.
    00161         /** \param mKey The key to search for
    00162         *   \return An iterator pointing to one of the surrounding pairs
    00163         *   \note You are searching for mKey, but it's proably not
    00164         *         in this multimap. But there are two keys in the multimap that
    00165         *         surround mKey : mLower and mUpper.<br>
    00166         *         This function will always return mUpper, unless mKey
    00167         *         equals mUpper : it will then return mUpper+1.
    00168         */
    00169         const_iterator UpperBound(const Key& mKey) const
    00170         {
    00171             return mMulMap_.upper_bound(mKey);
    00172         }
    00173
    00174         /// Returns the number of elements whith the provided key.
    00175         /** \param mKey The key to search for
    00176         *   \return The number of elements whith the provided key
    00177         */
    00178         s_uint_t<default_uint> Count(const Key& mKey) const
    00179         {
    00180             return mMulMap_.count(mKey);
    00181         }
    00182
    00183         /// Returns the range of all elements with the provided key.
    00184         /** \param mKey The key to search for
    00185         *   \return The range of all elements with the provided key
    00186         */
    00187         range EqualRange(const Key& mKey)
    00188         {
    00189             return mMulMap_.equal_range(mKey);
    00190         }
    00191
    00192         /// Returns the range of all elements with the provided key.
    00193         /** \param mKey The key to search for
    00194         *   \return The range of all elements with the provided key
    00195         */
    00196         const_range EqualRange(const Key& mKey) const
    00197         {
    00198             return mMulMap_.equal_range(mKey);
    00199         }
    00200
    00201         /// Inserts a pair in the multimap.
    00202         /** \param mPair The pair to insert
    00203         *   \return An iterator pointing to the inserted pair
    00204         *   \note The multimap automatically sorts itself after
    00205         *         this operation.
    00206         */
    00207         iterator Insert(const pair& mPair)
    00208         {
    00209             return mMulMap_.insert(make_pair(mPair.First(), mPair.Second()));
    00210         }
    00211
    00212         /// Inserts a pair in the multimap.
    00213         /** \param mKey   The access key
    00214         *   \param mValue The value to store
    00215         *   \return An iterator pointing to the inserted pair
    00216         *   \note The multimap automatically sorts itself after
    00217         *         this operation.
    00218         */
    00219         iterator Insert(const Key& mKey, const Value& mValue)
    00220         {
    00221             return mMulMap_.insert(make_pair(mKey, mValue));
    00222         }
    00223
    00224         /// Inserts a pair in the multimap (with hint).
    00225         /** \param mIter A hint to where it should be inserted
    00226         *   \param mPair The pair to insert
    00227         *   \return An iterator pointing to the inserted pair
    00228         *   \note The multimap automatically sorts itself after
    00229         *         this operation.
    00230         */
    00231         iterator Insert(const iterator& mIter, const pair& mPair)
    00232         {
    00233             return mMulMap_.insert(mIter, make_pair(mPair.First(), mPair.Second()));
    00234         }
    00235
    00236         /// Inserts a pair in the multimap (with hint).
    00237         /** \param mIter A hint to where it should be inserted
    00238         *   \param mKey   The access key
    00239         *   \param mValue The value to store
    00240         *   \return An iterator pointing to the inserted pair
    00241         *   \note The multimap automatically sorts itself after
    00242         *         this operation.
    00243         */
    00244         iterator Insert(const iterator& mIter, const Key& mKey, const Value& mValue)
    00245         {
    00246             return mMulMap_.insert(mIter, make_pair(mKey, mValue));
    00247         }
    00248
    00249         /// Removes all elements from this multimap.
    00250         void Clear()
    00251         {
    00252             mMulMap_.clear();
    00253         }
    00254
    00255         /// Returns the number of elements in this multimap.
    00256         /** \return The number of elements in this multimap
    00257         */
    00258         s_uint_t<default_uint> GetSize() const
    00259         {
    00260             return mMulMap_.size();
    00261         }
    00262
    00263         /// Checks if this multimap is empty.
    00264         /** \return 'true' if this multimap is empty
    00265         */
    00266         s_bool IsEmpty() const
    00267         {
    00268             return mMulMap_.empty();
    00269         }
    00270
    00271         s_ctnr<s_multimap> operator, (const s_multimap& lValue)
    00272         {
    00273             s_ctnr<s_multimap> mContainer;
    00274             mContainer.PushBack(*this);
    00275             mContainer.PushBack(lValue);
    00276             return mContainer;
    00277         }
    00278
    00279         iterator Begin()
    00280         {
    00281             return mMulMap_.begin();
    00282         }
    00283
    00284         const_iterator Begin() const
    00285         {
    00286             return mMulMap_.begin();
    00287         }
    00288
    00289         iterator End()
    00290         {
    00291             return mMulMap_.end();
    00292         }
    00293
    00294         const_iterator End() const
    00295         {
    00296             return mMulMap_.end();
    00297         }
    00298
    00299         iterator begin()
    00300         {
    00301             return mMulMap_.begin();
    00302         }
    00303
    00304         const_iterator begin() const
    00305         {
    00306             return mMulMap_.begin();
    00307         }
    00308
    00309         iterator end()
    00310         {
    00311             return mMulMap_.end();
    00312         }
    00313
    00314         const_iterator end() const
    00315         {
    00316             return mMulMap_.end();
    00317         }
    00318
    00319     private :
    00320
    00321         std::multimap<Key, Value> mMulMap_;
    00322
    00323     };
    00324
    00325     /** \cond NOT_REMOVE_FROM_DOC
    00326     */
    00327     template<class Key, class Value> class StringConverter< string_element, s_multimap<Key, Value> >
    00328     {
    00329     public :
    00330
    00331         typedef s_str::string string;
    00332
    00333         static string Convert(const s_multimap<Key, Value>& mMap)
    00334         {
    00335             string sTemp = "(";
    00336             typename s_multimap<Key, Value>::const_iterator iter;
    00337             for (iter = mMap.Begin(); iter != mMap.End(); ++iter)
    00338             {
    00339                 if (iter != mMap.Begin())
    00340                     sTemp += ", [" + StringConverter<string_element, Key>::Convert(iter->first)
    00341                           + "] = " + StringConverter<string_element, Value>::Convert(iter->second);
    00342                 else
    00343                     sTemp +=   "[" + StringConverter<string_element, Key>::Convert(iter->first)
    00344                           + "] = " + StringConverter<string_element, Value>::Convert(iter->second);
    00345             }
    00346             sTemp += ")";
    00347
    00348             return sTemp;
    00349         }
    00350     };
    00351     /** \endcond
    00352     */
    00353 }