• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

/export/development/ViennaGrid/release/ViennaGrid-1.0.0/viennagrid/detail/segment_iterators.hpp

Go to the documentation of this file.
00001 #ifndef VIENNAGRID_SEGMENT_ITERATORS_HPP
00002 #define VIENNAGRID_SEGMENT_ITERATORS_HPP
00003 
00004 /* =======================================================================
00005    Copyright (c) 2011, Institute for Microelectronics,
00006                        Institute for Analysis and Scientific Computing,
00007                        TU Wien.
00008 
00009                             -----------------
00010                      ViennaGrid - The Vienna Grid Library
00011                             -----------------
00012 
00013    Authors:      Karl Rupp                           rupp@iue.tuwien.ac.at
00014                  Josef Weinbub                    weinbub@iue.tuwien.ac.at
00015                
00016    (A list of additional contributors can be found in the PDF manual)
00017 
00018    License:      MIT (X11), see file LICENSE in the base directory
00019 ======================================================================= */
00020 
00021 
00022 #include <vector>
00023 #include <list>
00024 #include <map>
00025 #include <stack>
00026 
00027 #include "viennagrid/forwards.h"
00028 #include "viennagrid/detail/domain_iterators.hpp"
00029 #include "viennagrid/detail/element_iterators.hpp"
00030 #include "viennagrid/segment.hpp"
00031 
00036 namespace viennagrid
00037 {
00039   template <typename IteratorType, typename ElementType>
00040   class on_segment_iterator : public std::iterator < std::forward_iterator_tag, ElementType >
00041   {
00042       typedef on_segment_iterator<IteratorType, ElementType>        self_type;
00043     
00044     public:
00045       on_segment_iterator(IteratorType const & it) : iter_(it) {}
00046 
00047       ElementType & operator*() const { return **iter_; }
00048       ElementType * operator->() const { return *iter_; }
00049 
00050       self_type & operator++() { ++iter_; return *this; }
00051       self_type operator++(int) { self_type tmp = *this; ++*this; return tmp; }
00052 
00053       bool operator==(const self_type & i) const { return iter_ == i.iter_; }
00054       bool operator!=(const self_type & i) const { return iter_ != i.iter_; }
00055 
00056     private:
00057       IteratorType iter_;
00058   };
00059   
00061   template <typename IteratorType, typename ElementType>
00062   class const_on_segment_iterator : public std::iterator < std::forward_iterator_tag, ElementType >
00063   {
00064       typedef const_on_segment_iterator<IteratorType, ElementType>        self_type;
00065     public:
00066       const_on_segment_iterator(IteratorType const & it) : iter_(it) {}
00067 
00068       const ElementType & operator*() const { return **iter_; }
00069       const ElementType * operator->() const { return *iter_; }
00070 
00071       self_type & operator++() { ++iter_; return *this; }
00072       self_type operator++(int) { self_type tmp = *this; ++*this; return tmp; }
00073 
00074       bool operator==(const self_type& i) const { return iter_ == i.iter_; }
00075       bool operator!=(const self_type& i) const { return iter_ != i.iter_; }
00076 
00077     private:
00078       IteratorType iter_;
00079   };
00080   
00081   
00082   
00083   //generic segment iterator
00084   template <typename Config,
00085             long dim,
00086             long cell_level = Config::cell_tag::dim>
00087   struct segment_iterators;
00088   
00089   
00090   //special case: cells:
00092   template <typename Config, long cell_level>
00093   struct segment_iterators< Config, cell_level, cell_level>
00094   {
00095     typedef segment_t<Config>                     segment_type;
00096     typedef element_t<Config,
00097                     typename Config::cell_tag>    cell_type;
00098                     
00099     typedef typename result_of::element_container<segment_type,
00100                                                   Config::cell_tag::dim>::type   container_type;
00101     
00102     typedef typename container_type::iterator          iterator;
00103     typedef typename container_type::const_iterator    const_iterator;
00104   };
00105   
00106   
00107   //non-const iteration:
00108   // interface function for container creation,
00109   // non-const:
00110 
00111   //container for iteration over a STL vector
00113   template <typename config_type, long dim>
00114   class ncell_range < segment_t<config_type>, dim, false >
00115   {
00116       typedef segment_t<config_type>                        segment_type;
00117       typedef element_t< config_type,
00118                        typename topology::bndcells<typename config_type::cell_tag, dim>::tag
00119                      >                                                         element_type;
00120                      
00121       typedef element_t< config_type,
00122                        typename config_type::cell_tag
00123                      >                                                         cell_type;
00124                      
00125       //typedef std::vector< element_type >     container_type;
00126       typedef typename result_of::element_container<segment_type, dim>::type   container_type;
00127     
00128     public: 
00129       //typedef typename container_type::iterator   iterator;
00130       typedef on_segment_iterator< typename container_type::iterator, element_type >   iterator;
00131       
00132       ncell_range() : cont_(NULL) {};
00133       
00134       ncell_range(ncell_proxy<segment_type> const & p) : cont_(p.get().container(dimension_tag<dim>())) {}
00135       
00136       ncell_range(segment_type & d) : cont_(d.container(dimension_tag<dim>())) {}
00137       
00138       
00139       ncell_range & operator=(ncell_proxy<segment_type> p)
00140       { 
00141         cont_ = p.get().container(dimension_tag<dim>());
00142         return *this;
00143       }
00144       
00145       iterator begin() const { return iterator(cont_->begin()); }
00146       iterator end()   const { return iterator(cont_->end()); }
00147 
00148       std::size_t size() const { return cont_->size(); }
00149 
00150       element_type & operator[](std::size_t index)
00151       {
00152         typedef typename assert_bracket_operator_access<container_type>::type  asserted_type;
00153         assert(index < size());
00154         return *((*cont_)[index]); 
00155       }
00156 
00157       element_type const & operator[](std::size_t index) const 
00158       {
00159         typedef typename assert_bracket_operator_access<container_type>::type  asserted_type;
00160         assert(index < size());
00161         return *((*cont_)[index]); 
00162       }
00163 
00164       template <typename DomainType, long dim2, bool b2>
00165       friend class const_ncell_range;
00166 
00167     private:
00168       container_type * cont_;
00169   };
00170   
00172   template <long dim, typename DomainConfig>
00173   ncell_range<segment_t<DomainConfig>, dim>
00174   ncells(segment_t<DomainConfig> & d)
00175   {
00176     return ncell_range< segment_t<DomainConfig>, dim>(d);
00177   }
00178   
00184   template <typename DomainConfig>
00185   ncell_proxy< segment_t<DomainConfig> >
00186   ncells(segment_t<DomainConfig> & d)
00187   {
00188     return ncell_proxy< segment_t<DomainConfig> >(d);
00189   }
00190 
00191 
00192   //
00193   // const version
00194   //
00195   
00196   
00198   template <typename config_type, long dim>
00199   class const_ncell_range < segment_t<config_type>, dim, false >
00200   {
00201       typedef segment_t<config_type>                        segment_type;
00202       typedef element_t< config_type,
00203                        typename topology::bndcells<typename config_type::cell_tag, dim>::tag
00204                      >                                                         element_type;
00205 
00206       typedef element_t< config_type,
00207                        typename config_type::cell_tag
00208                      >                                                         cell_type;
00209                      
00210       //typedef std::vector< element_type >     container_type;
00211       typedef typename result_of::element_container<segment_type, dim>::type      container_type;
00212     
00213     public: 
00214       //typedef typename container_type::const_iterator   iterator;
00215       typedef on_segment_iterator< typename container_type::const_iterator, const element_type >   iterator;
00216       
00217       const_ncell_range() : cont_(NULL) {};
00218       
00219       const_ncell_range(const_ncell_proxy< segment_type > const & p) : cont_(p.get().container(dimension_tag<dim>())) {}
00220 
00221       const_ncell_range(ncell_proxy< segment_type > const & p) : cont_(p.get().container(dimension_tag<dim>())) {}
00222 
00223       const_ncell_range(segment_type const & d) : cont_(d.container(dimension_tag<dim>())) {}
00224 
00225       const_ncell_range(ncell_range<segment_type, dim > const & other) : cont_(other.cont_) {}
00226 
00227 
00228       const_ncell_range & operator=(const_ncell_proxy< segment_type > const & p)
00229       { 
00230         cont_ = p.get().container(dimension_tag<dim>());
00231         return *this;
00232       }
00233       
00234       iterator begin() const { return iterator(cont_->begin()); }
00235       iterator end()   const { return iterator(cont_->end()); }
00236       
00237       std::size_t size() const { return cont_->size(); }
00238       
00239       element_type & operator[](std::size_t index)
00240       {
00241         typedef typename assert_bracket_operator_access<container_type>::type  asserted_type;
00242         assert(index < size());
00243         return *((*cont_)[index]); 
00244       }
00245 
00246       element_type const & operator[](std::size_t index) const 
00247       {
00248         typedef typename assert_bracket_operator_access<container_type>::type  asserted_type;
00249         assert(index < size());
00250         return *((*cont_)[index]); 
00251       }
00252       
00253     private:
00254       const container_type * cont_;
00255   };
00256   
00258   template <long dim, typename DomainConfig>
00259   const_ncell_range< segment_t<DomainConfig>, dim>
00260   ncells(segment_t<DomainConfig> const & d)
00261   {
00262     return const_ncell_range< segment_t<DomainConfig>, dim>(d);
00263   }
00264   
00270   template <typename DomainConfig>
00271   const_ncell_proxy< segment_t<DomainConfig> >
00272   ncells(segment_t<DomainConfig> const & d)
00273   {
00274     return const_ncell_proxy< segment_t<DomainConfig> >(d);
00275   }
00276   
00277   
00278 } //namespace
00279 #endif
00280 

Generated on Wed Sep 14 2011 19:21:30 for ViennaGrid by  doxygen 1.7.1