map2.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Copyright (c) 2001 - 2008 Marcus Boerger.  All rights reserved.
00003 *
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 2.1 of the License, or (at your option) any later version.
00008 *
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 * Lesser General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library; if not, write to the Free Software
00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 * ========================================================================= */
00018 
00019 /* ------------------------------------------------------------------------ */
00020 /* Name:      map2.h
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  * - list2.h
00025  * - vector2.h
00026  * - deque2.h
00027  */
00039 /* ------------------------------------------------------------------------ */
00040 
00041 #if defined(_MSC_VER) && _MSC_VER >= 1200
00042 # pragma once
00043 #endif
00044 
00045 #ifndef _MAP2_H_
00046 #define _MAP2_H_
00047 
00048 #include "Mbo.h"
00049 
00050 #include "list2.h"
00051 #include "vector2.h"
00052 #include "deque2.h"
00053 #include <map>
00054 #include <functional>
00055 #include <stdexcept>
00056 
00057 namespace std
00058 {
00059 
00071 template <class _Tp, class _Arg>
00072 class check_fun_t
00073     : public binary_function<_Tp,const _Arg&,bool> 
00074 {
00075 typedef bool (_Tp::*_fun_type)(const _Arg&) const; 
00076 public:
00079     explicit check_fun_t(_fun_type __pf) : _M_f(__pf) {}
00080 
00088     bool operator()(const _Tp& __r, const _Arg & __x) const 
00089     { 
00090         return (__r.*_M_f)(__x); 
00091     }
00092 private:
00093     _fun_type _M_f; 
00094 }; 
00095 
00105 template <class _Tp, class _Arg>
00106 check_fun_t<_Tp,_Arg> check_fun(bool (_Tp::*__f)(const _Arg&) const) 
00107 { 
00108     return check_fun_t<_Tp,_Arg>(__f); 
00109 };
00110 
00117 #define MAP_CONTAINER std::deque2
00118 
00143 template <class __Kty,
00144     class __Ty,
00145     class __Kty_container = MAP_CONTAINER<__Kty>,
00146     class __Ty_container  = MAP_CONTAINER<__Ty>,
00147     class __Pr            = less<__Kty>,
00148     class __Alloc         = allocator<pair<const __Kty, __Ty> > >
00149 class map2
00150     : public map<__Kty, __Ty, __Pr, __Alloc> // ms<_Ty>
00151 {
00152 public:
00153     typedef typename __Kty_container                     _Kty_container;       
00154     typedef typename __Ty_container                      _Ty_container;        
00155     typedef map2<__Kty, __Ty, _Kty_container, _Ty_container, __Pr, __Alloc> _Myt; 
00156     typedef map<__Kty, __Ty, __Pr, __Alloc>              _Mybase;              
00157     typedef pair<const __Kty, __Ty>                      _Element;             
00158     typedef typename __Kty                               _Kty;                 
00159     typedef typename __Ty                                _Ty;                  
00160     typedef typename check_fun_t<_Myt,_Element>          _Element_check_fun_t; 
00161     typedef typename check_fun_t<_Kty_container,__Kty>   _Kty_check_fun_t;     
00162     typedef typename check_fun_t<_Ty_container,__Ty>     _Ty_check_fun_t;      
00165     // inherit array operator
00166     _Ty& operator[] (const _Kty & key)
00167     {
00168         iterator it = insert_elem(key, _Ty()).first;
00169         return (*it).second;
00170     }
00171 
00187     _Kty_container keys() const
00188     {
00189         _Kty_container __container;
00190         for (const_iterator it=begin(); it!=end(); ++it)
00191         {
00192             __container.push_back(it->first);
00193         }
00194         return __container;
00195     }
00196 
00216     _Kty_container keys(_IN _Kty_check_fun_t check) const
00217     {
00218         _Kty_container container;
00219         for (const_iterator it=begin(); it!=end(); ++it)
00220         {
00221             if (check(container, it->first))
00222             {
00223                 container.push_back(it->first);
00224             }
00225         }
00226         return container;
00227     }
00228 
00244     _Ty_container values() const
00245     {
00246         _Ty_container container;
00247         for (const_iterator it=begin(); it!=end(); ++it)
00248         {
00249             container.push_back(it->second);
00250         }
00251         return container;
00252     }
00253 
00281     _Ty_container values(_IN _Ty_check_fun_t check) const
00282     {
00283         _Ty_container container;
00284         for (const_iterator it=begin(); it!=end(); ++it)
00285         {
00286             if (check(container, it->second))
00287             {
00288                 container.push_back(it->second);
00289             }
00290         }
00291         return container;
00292     }
00293 
00307     inline pair<iterator, bool> insert_elem(_IN const _Kty & key, _IN const _Ty& val)
00308     {
00309         return /*_Mybase::*/insert(make_pair(key, val));
00310     }
00311 
00318     _Myt select(_IN _Element_check_fun_t check) const
00319     {
00320         _Myt container;
00321         for (const_iterator it=begin(); it!=end(); ++it)
00322         {
00323             if (check(container, *it))
00324             {
00325                 container.insert(*it);
00326             }
00327         }
00328         return container;
00329     }
00330 
00335     inline bool contains_key(_IN const _Kty & key) const
00336     {
00337         return find(key) != end();
00338     }
00339 
00349     const _Kty& first_key(_IN const _Kty& if_empty) const
00350     {
00351         if (size())
00352         {
00353             return begin()->first;
00354         }
00355         else
00356         {
00357             return if_empty;
00358         }
00359     }
00360 
00370     _Ty& first_elem(_IN _Ty& if_empty)
00371     {
00372         if (size())
00373         {
00374             return begin()->second;
00375         }
00376         else
00377         {
00378             return if_empty;
00379         }
00380     }
00381 
00391     const _Ty& first_elem(_IN const _Ty& if_empty) const
00392     {
00393         if (size())
00394         {
00395             return begin()->second;
00396         }
00397         else
00398         {
00399             return if_empty;
00400         }
00401     }
00402 };
00403 
00404 }; // namespace std
00405 
00406 #endif // _MAP2_H_

  Hosted on code.google.com  
© Marcus Börger
Generated on Fri Jan 18 21:21:07 2008 for MBO-lib by doxygen 1.5.4