00001 #ifndef GRAL_GB_BASE_CELL_ON_CELL_ITERATOR_H
00002 #define GRAL_GB_BASE_CELL_ON_CELL_ITERATOR_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "Gral/Base/common-grid-basics.h"
00019 #include "Gral/Base/facet-on-cell-function.h"
00020 #include "Utility/ref-ptr.h"
00021
00022 #include <map>
00023
00024 namespace GrAL {
00025
00066 template<class G,
00067 class NBTABLE = facet_on_cell_function<G,
00068 typename grid_types<G>::cell_handle>,
00069 class GT = grid_types<G> >
00070 class cell_on_cell_iterator {
00071 typedef cell_on_cell_iterator<G,NBTABLE,GT> self;
00072 public:
00073 typedef G grid_type;
00074 typedef typename GT::Cell Cell;
00075 typedef typename GT::Facet Facet;
00076 typedef typename GT::cell_handle cell_handle;
00077 typedef typename GT::FacetOnCellIterator FacetOnCellIterator;
00078 typedef Cell value_type;
00079 typedef Cell anchor_type;
00080 typedef typename get_incidence_iterator_category<typename category<value_type> ::type,
00081 typename category<anchor_type>::type>::type
00082 category;
00083 private:
00084 ref_ptr<NBTABLE const> nbs;
00085 Cell c;
00086 FacetOnCellIterator fc;
00087
00088
00089
00090
00091
00092
00093 typedef std::map<grid_type const*, ref_ptr<NBTABLE const> > context_table;
00094 static context_table * ctxt;
00095 public:
00096 cell_on_cell_iterator() : nbs(0) {}
00097 cell_on_cell_iterator(Cell const& c_)
00098 : c(c_), fc(c)
00099 {
00100 init_table();
00101 advance_till_valid();
00102 }
00103 cell_on_cell_iterator(Cell const& c_, NBTABLE const& nbs_)
00104 : nbs(&nbs_), c(c_), fc(c)
00105 { advance_till_valid();}
00106
00123 explicit cell_on_cell_iterator(FacetOnCellIterator const& fc_) : c(fc_.TheAnchor()), fc(fc_)
00124 {
00125 init_table();
00126
00127 }
00128 void init_table() {
00129 REQUIRE(ctxt->find(& (c.TheGrid())) != ctxt->end(),
00130 "No neighbor table registered for grid " << & (c.TheGrid()),1);
00131 nbs = (*ctxt)[& (c.TheGrid())];
00132 }
00133
00135 self& operator++() { ++fc; advance_till_valid(); return (*this);}
00136 Cell operator* () const { cv(); return Cell(TheGrid(), handle());}
00137 cell_handle handle() const { return const_cast<NBTABLE &>(*nbs)[fc];}
00138 bool IsDone() const { return fc.IsDone();}
00139
00140 grid_type const& TheGrid() const { return c.TheGrid();}
00141 Cell const& TheCell() const { return c;}
00142 Cell const& TheAnchor() const { return c;}
00143 Facet TheFacet() const { return *fc;}
00144 FacetOnCellIterator TheFacetOnCellIterator() const { return fc;}
00145
00146 static void init();
00147
00148 static void init (grid_type const& g_);
00149 static void remove (grid_type const& g_);
00150
00151 static void map_nb_table(grid_type const& g_, NBTABLE const& nbs_);
00152
00153 bool valid_neighbor() const { return valid(handle());}
00154 bool inner_facet() const { return valid(handle());}
00155 bool boundary_facet() const { return !valid(handle());}
00156
00157 void next_facet() { ++fc;}
00158 void next_neighbor() { next_facet(); advance_till_valid();}
00159
00160 static self begin(Cell c) { return self(c);}
00161 static self end (Cell c) { return self(GrAL::end<FacetOnCellIterator>(c));}
00162
00163 bool operator==(self rhs) const { return c == rhs.c && fc == rhs.fc;}
00164 bool operator!=(self rhs) const { return !(*this == rhs);}
00165
00166 private:
00167 void cv() const { REQUIRE(valid_neighbor(), "",1);}
00168 bool valid(cell_handle ch) const { return TheGrid().valid_cell(ch);}
00169
00170 void advance_till_valid()
00171 {
00172 while(! fc.IsDone() && ! valid(handle()))
00173 ++fc;
00174 }
00175 };
00176
00177
00178 template<class G, class NBTABLE, class GT>
00179 inline
00180 cell_on_cell_iterator<G,NBTABLE, GT> gral_begin(typename GT::Cell const& c, cell_on_cell_iterator<G,NBTABLE, GT>)
00181 { return cell_on_cell_iterator<G,NBTABLE, GT>::begin(c);}
00182
00183 template<class G, class NBTABLE, class GT>
00184 inline
00185 cell_on_cell_iterator<G,NBTABLE, GT> gral_end (typename GT::Cell const& c, cell_on_cell_iterator<G,NBTABLE, GT>)
00186 { return cell_on_cell_iterator<G,NBTABLE, GT>::end(c);}
00187
00188
00189
00190 }
00191
00192 #ifdef NMWR_INCLUDE_TEMPLATE_DEFS
00193 #include "Gral/Iterators/cell-on-cell-iterator.C"
00194 #endif
00195
00196 #endif
00197