ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/storage/hidden_key_map.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_STORAGE_HIDDEN_KEY_MAP_HPP
00002 #define VIENNAGRID_STORAGE_HIDDEN_KEY_MAP_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2011-2014, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008 
00009                             -----------------
00010                      ViennaGrid - The Vienna Grid Library
00011                             -----------------
00012 
00013    License:      MIT (X11), see file LICENSE in the base directory
00014 ======================================================================= */
00015 
00016 #include <map>
00017 #include "viennagrid/storage/container.hpp"
00018 
00023 namespace viennagrid
00024 {
00026   template<typename HiddenKeyMapT>
00027   class hidden_key_map_iterator : public HiddenKeyMapT::container_type::iterator
00028   {
00029     typedef typename HiddenKeyMapT::container_type::iterator base;
00030   public:
00031     hidden_key_map_iterator() {}
00032     hidden_key_map_iterator(const base & foo) : base(foo) {}
00033 
00034     typedef typename HiddenKeyMapT::value_type value_type;
00035     typedef typename HiddenKeyMapT::reference reference;
00036     typedef typename HiddenKeyMapT::const_reference const_reference;
00037     typedef typename HiddenKeyMapT::pointer pointer;
00038 
00039     reference operator* () { return base::operator*().second; }
00040     const_reference operator* () const { return base::operator*().second; }
00041   };
00042 
00044   template<typename HiddenKeyMapT>
00045   class hidden_key_map_const_iterator : public HiddenKeyMapT::container_type::const_iterator
00046   {
00047     typedef typename HiddenKeyMapT::container_type::const_iterator base;
00048   public:
00049     hidden_key_map_const_iterator() {}
00050     hidden_key_map_const_iterator(base const & foo) : base(foo) {}
00051     hidden_key_map_const_iterator(hidden_key_map_iterator<HiddenKeyMapT> const & it) : base(it) {}
00052 
00053     typedef typename HiddenKeyMapT::value_type value_type;
00054     typedef typename HiddenKeyMapT::const_reference reference;
00055     typedef typename HiddenKeyMapT::const_reference const_reference;
00056     typedef typename HiddenKeyMapT::const_pointer pointer;
00057 
00058     const_reference operator* () const { return base::operator*().second; }
00059   };
00060 
00062   template<typename HiddenKeyMapT>
00063   class hidden_key_map_reverse_iterator : public HiddenKeyMapT::container_type::reverse_iterator
00064   {
00065     typedef typename HiddenKeyMapT::container_type::reverse_iterator base;
00066   public:
00067     hidden_key_map_reverse_iterator() {}
00068     hidden_key_map_reverse_iterator(base const & foo) : base(foo) {}
00069 
00070     typedef typename HiddenKeyMapT::value_type value_type;
00071     typedef typename HiddenKeyMapT::reference reference;
00072     typedef typename HiddenKeyMapT::const_reference const_reference;
00073     typedef typename HiddenKeyMapT::pointer pointer;
00074 
00075     reference operator* () { return base::operator*().second; }
00076     const_reference operator* () const { return base::operator*().second; }
00077   };
00078 
00080   template<typename HiddenKeyMapT>
00081   class hidden_key_map_const_reverse_iterator : public HiddenKeyMapT::container_type::const_reverse_iterator
00082   {
00083     typedef typename HiddenKeyMapT::container_type::const_reverse_iterator base;
00084   public:
00085     hidden_key_map_const_reverse_iterator() {}
00086     hidden_key_map_const_reverse_iterator(base const & foo) : base(foo) {}
00087     hidden_key_map_const_reverse_iterator(hidden_key_map_reverse_iterator<HiddenKeyMapT> const & it) : base(it) {}
00088 
00089     typedef typename HiddenKeyMapT::value_type value_type;
00090     typedef typename HiddenKeyMapT::const_reference reference;
00091     typedef typename HiddenKeyMapT::const_reference const_reference;
00092     typedef typename HiddenKeyMapT::const_pointer pointer;
00093 
00094     const_reference operator* () const { return base::operator*().second; }
00095   };
00096 
00097 
00103   template<typename KeyT, typename ValueT>
00104   class hidden_key_map
00105   {
00106     typedef hidden_key_map<KeyT, ValueT> SelfType;
00107 
00108     friend class hidden_key_map_iterator<SelfType>;
00109     friend class hidden_key_map_const_iterator<SelfType>;
00110     friend class hidden_key_map_reverse_iterator<SelfType>;
00111     friend class hidden_key_map_const_reverse_iterator<SelfType>;
00112 
00113   public:
00114 
00115     typedef std::map< KeyT, ValueT >           container_type;
00116     typedef KeyT                               key_type;
00117     typedef ValueT                             value_type;
00118     typedef typename container_type::size_type size_type;
00119     typedef value_type &                       reference;
00120     typedef const value_type &                 const_reference;
00121     typedef value_type *                       pointer;
00122     typedef const value_type *                 const_pointer;
00123 
00124     typedef hidden_key_map_iterator<SelfType>                             iterator;
00125     typedef hidden_key_map_const_iterator<SelfType>                 const_iterator;
00126 
00127     typedef hidden_key_map_reverse_iterator<SelfType>             reverse_iterator;
00128     typedef hidden_key_map_const_reverse_iterator<SelfType> const_reverse_iterator;
00129 
00130 
00131     iterator begin() { return iterator(container.begin()); }
00132     iterator end()   { return iterator(container.end()); }
00133 
00134     const_iterator begin() const { return const_iterator(container.begin()); }
00135     const_iterator end()   const { return const_iterator(container.end()); }
00136 
00137     iterator find( const value_type & element)
00138     {
00139       return iterator(container.find( key_type(element) ));
00140     }
00141 
00142     const_iterator find( const value_type & element) const
00143     {
00144       return const_iterator(container.find( key_type(element) ));
00145     }
00146 
00147     std::pair<iterator, bool> insert( const value_type & element )
00148     {
00149       std::pair<typename container_type::iterator, bool> ret = container.insert( std::make_pair( key_type(element), element ) );
00150       return std::make_pair( iterator(ret.first), ret.second );
00151     }
00152 
00153     iterator erase( iterator to_erase )
00154     {
00155       container.erase( to_erase++ );
00156       return to_erase;
00157     }
00158 
00159     void clear()
00160     {
00161       container.clear();
00162     }
00163 
00164 
00165     size_type size() const { return container.size(); }
00166     bool empty()     const { return container.empty(); }
00167 
00168   private:
00169     container_type container;
00170   };
00171 
00172 
00173 
00174   namespace detail
00175   {
00176     template<typename KeyT, typename ElementT, typename handle_tag>
00177     class container_base<hidden_key_map<KeyT, ElementT>, handle_tag> : public handled_container<hidden_key_map<KeyT, ElementT>, handle_tag>
00178     {
00179     public:
00180 
00181       typedef handled_container<hidden_key_map<KeyT, ElementT>, handle_tag> handled_container_type;
00182       typedef typename handled_container_type::container_type container_type;
00183 
00184       typedef typename handled_container_type::value_type value_type;
00185 
00186       typedef typename handled_container_type::pointer pointer;
00187       typedef typename handled_container_type::const_pointer const_pointer;
00188 
00189       typedef typename handled_container_type::reference reference;
00190       typedef typename handled_container_type::const_reference const_reference;
00191 
00192       typedef typename handled_container_type::iterator iterator;
00193       typedef typename handled_container_type::const_iterator const_iterator;
00194 
00195       typedef typename handled_container_type::handle_type handle_type;
00196       typedef typename handled_container_type::const_handle_type const_handle_type;
00197 
00198       typedef std::pair<handle_type, bool> return_type;
00199 
00200       bool is_present( const value_type & element ) const
00201       {
00202         return container_type::find(element) != container_type::end();
00203       }
00204 
00205       typename container_type::iterator find( const value_type & element ) const
00206       {
00207         return container_type::find(element);
00208       }
00209 
00210       return_type insert( const value_type & element )
00211       {
00212         std::pair<typename container_type::iterator, bool> tmp = container_type::insert( element );
00213         return std::make_pair( handled_container_type::handle(*tmp.first), tmp.second);
00214       }
00215     };
00216   }
00217 
00218 
00223   template<typename KeyTypeTagT>
00224   struct hidden_key_map_tag {};
00225 
00226   namespace result_of
00227   {
00229     template<typename element_tag, typename key_type_tag>
00230     struct hidden_key_map_key_type_from_tag;
00231 
00232     template<typename element_type, typename key_type_tag>
00233     struct container<element_type, hidden_key_map_tag<key_type_tag> >
00234     {
00235       typedef hidden_key_map< typename hidden_key_map_key_type_from_tag<element_type, key_type_tag>::type, element_type > type;
00236     };
00238   }
00239 
00240   namespace detail
00241   {
00242     template<typename KeyT, typename ValueT>
00243     std::pair<typename hidden_key_map<KeyT, ValueT>::iterator, bool>
00244         insert( hidden_key_map<KeyT, ValueT> & container, const ValueT & element )
00245     {
00246       return container.insert( element );
00247     }
00248 
00251     template<typename HiddenKeyMapT, typename HandleTagT>
00252     struct IDCompare<
00253           viennagrid::detail::iterator<
00254             viennagrid::hidden_key_map_iterator<HiddenKeyMapT>,
00255             viennagrid::hidden_key_map_const_iterator<HiddenKeyMapT>,
00256             HandleTagT
00257           >
00258         >
00259     {
00260       bool operator() (               viennagrid::detail::iterator<
00261             viennagrid::hidden_key_map_iterator<HiddenKeyMapT>,
00262             viennagrid::hidden_key_map_const_iterator<HiddenKeyMapT>,
00263             HandleTagT
00264           > const & lhs,
00265                                       viennagrid::detail::iterator<
00266             viennagrid::hidden_key_map_iterator<HiddenKeyMapT>,
00267             viennagrid::hidden_key_map_const_iterator<HiddenKeyMapT>,
00268             HandleTagT
00269           > const & rhs )
00270       {
00271         return lhs->second.id() < rhs->second.id();
00272       }
00273     };
00274 
00276   }
00277 }
00278 
00279 #endif