ViennaGrid - The Vienna Grid Library  2.1.0
viennagrid/element/element_key.hpp
Go to the documentation of this file.
00001 #ifndef VIENNAGRID_ELEMENT_KEY_HPP
00002 #define VIENNAGRID_ELEMENT_KEY_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 
00017 #include <map>
00018 #include <iostream>
00019 #include <algorithm>
00020 
00021 #include "viennagrid/forwards.hpp"
00022 #include "viennagrid/element/element.hpp"
00023 #include "viennagrid/topology/vertex.hpp"
00024 
00025 #include "viennagrid/storage/container.hpp"
00026 #include "viennagrid/storage/hidden_key_map.hpp"
00027 
00032 namespace viennagrid
00033 {
00034 
00036   template <typename element_type>
00037   class element_key
00038   {
00039     typedef typename element_type::tag            ElementTag;
00040     typedef typename viennagrid::result_of::element< element_type, vertex_tag >::type vertex_type;
00041     typedef typename vertex_type::id_type id_type;
00042 
00043   public:
00044 
00045     explicit element_key( std::vector< id_type > const & ids) : vertex_ids(ids) {}
00046 
00047     explicit element_key( const element_type & el2) : vertex_ids( viennagrid::elements<vertex_tag>(el2).size() )
00048     {
00049       typedef typename viennagrid::result_of::const_element_range< element_type, vertex_tag >::type vertex_range;
00050       typedef typename viennagrid::result_of::const_iterator< vertex_range >::type const_iterator;
00051 
00052       std::size_t i = 0;
00053       vertex_range vertices_el2 = elements<vertex_tag>(el2);
00054       for (const_iterator vit = vertices_el2.begin();
00055            vit != vertices_el2.end();
00056            ++vit, ++i)
00057         vertex_ids[i] = static_cast<id_type>( (*vit).id() );
00058       //sort it:
00059       std::sort(vertex_ids.begin(), vertex_ids.end());
00060     }
00061 
00062     element_key( const element_key & ek2) : vertex_ids(ek2.vertex_ids.size())
00063     {
00064       std::copy( ek2.vertex_ids.begin(), ek2.vertex_ids.end(), vertex_ids.begin() );
00065     }
00066 
00067     bool operator < (element_key const & epc2) const
00068     {
00069       if ( vertex_ids.size() != epc2.vertex_ids.size() )
00070           return vertex_ids.size() < epc2.vertex_ids.size();
00071 
00072       for (std::size_t i=0; i < vertex_ids.size(); ++i)
00073       {
00074         if ( vertex_ids[i] > epc2.vertex_ids[i] )
00075           return false;
00076         else if ( vertex_ids[i] < epc2.vertex_ids[i] )
00077           return true;
00078       }
00079       return false;
00080     }
00081 
00082     void print() const
00083     {
00084       for (typename std::vector<id_type>::const_iterator vit = vertex_ids.begin();
00085             vit != vertex_ids.end();
00086             ++vit)
00087         std::cout << *vit << " ";
00088       std::cout << std::endl;
00089     }
00090 
00091   private:
00092       // TODO: rather than hard-wiring std::vector, make this configurable
00093     std::vector< id_type > vertex_ids;
00094   };
00095 }
00096 
00097 
00098 
00099 namespace viennagrid
00100 {
00102   struct element_key_tag {};
00103 
00104   namespace result_of
00105   {
00107     template<typename element_type>
00108     struct hidden_key_map_key_type_from_tag<element_type, element_key_tag>
00109     {
00110       typedef element_key<element_type> type;
00111     };
00113   }
00114 }
00115 
00116 #endif