OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
grid_key_dx_iterator.hpp
1 /*
2  * grid_key_dx_iterator.hpp
3  *
4  * Created on: Dec 15, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_
9 #define OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_
10 
11 #include "Grid/grid_sm.hpp"
12 #include "stencil_type.hpp"
13 
27 template<unsigned int dim, typename stencil=no_stencil>
29 {
30 #ifdef SE_CLASS1
31  // Actual status of the iterator, when the iterator is not initialized cannot be used
32  // and reinitialize must be called
33  bool initialized = false;
34 #endif
35 
38 
47  inline size_t get_gk(size_t i) const
48  {
49  return gk.get(i);
50  }
51 
52 protected:
53 
56 
59  stencil stl_code;
60 
61 public:
62 
70  {
71 #ifdef SE_CLASS1
72  initialized = false;
73 #endif
74  }
75 
81  : grid_base(g_it.grid_base)
82  {
84 
85  for (size_t i = 0 ; i < dim ; i++)
86  {
87  gk.set_d(i,g_it.get_gk(i));
88  }
89 
90  stl_code = g_it.stl_code;
91 
92 #ifdef SE_CLASS1
93  initialized = true;
94 #endif
95  }
96 
103  template<typename T>
105  const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
106  :grid_base(g)
107  {
108  reset();
109 
110  grid_key_dx<dim> zero;
111  for (size_t i = 0 ; i < dim ; i++) {zero.set_d(i,0);}
112 
113  // calculate the offsets for the stencil code
114  stl_code.calc_offsets(g,zero,stencil_pnt);
115 
116 #ifdef SE_CLASS1
117  initialized = true;
118 #endif
119  }
120 
128  grid_key_dx_iterator(const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
129  {
130  // calculate the offsets for the stencil code
131  stl_code.set_stencil(stencil_pnt);
132  }
133 
138  template<typename T> grid_key_dx_iterator(const grid_sm<dim,T> & g)
139  : grid_base(g)
140  {
141  reset();
142 
143 #ifdef SE_CLASS1
144  initialized = true;
145 #endif
146  }
147 
156  {
157  grid_base = key_it.grid_base;
158 
160 
161  for (size_t i = 0 ; i < dim ; i++)
162  {gk.set_d(i,key_it.get_gk(i));}
163 
164  return *this;
165  }
166 
174  {
176 
177  size_t id = gk.get(0);
178  gk.set_d(0,id+1);
179 
180  stl_code.increment();
181 
183 
184  size_t i = 0;
185  for ( ; i < dim-1 ; i++)
186  {
187  /* coverity[dead_error_begin] */
188  size_t id = gk.get(i);
189  if (id >= grid_base.size(i))
190  {
191  // ! overflow, increment the next index
192 
193  size_t idr = gk.get(i);
194  gk.set_d(i,0);
195  id = gk.get(i+1);
196  gk.set_d(i+1,id+1);
197 
198  stl_code.adjust_offset(i,idr,grid_base);
199  }
200  else
201  {
202  break;
203  }
204  }
205 
206  return *this;
207  }
208 
217  inline void set(int d, size_t sz)
218  {
219  // set the counter dim to sz
220 
221  gk.set_d(d,sz);
222  }
223 
231  template<unsigned int id> inline size_t getStencil() const
232  {
233  return stl_code.template getStencil<id>();
234  }
235 
244  inline bool isNext()
245  {
246  if (gk.get(dim-1) < (long int)grid_base.size(dim-1))
247  {
249 
250  return true;
251  }
252 
254  return false;
255  }
256 
264  inline const grid_key_dx<dim> & get() const
265  {
266  return gk;
267  }
268 
275  {
276  grid_base = key.getGridInfo();
277  reset();
278  }
279 
285  inline const grid_sm<dim,void> & getGridInfo() const
286  {
287  return grid_base;
288  }
289 
293  inline void reset()
294  {
295  // Initialize to 0 the index
296 
297  for (size_t i = 0 ; i < dim ; i++)
298  {gk.set_d(i,0);}
299 
300  // here we check if grid have a size equal to zero or negative
301  // in this case the grid has no points
302 
303  for (size_t i = 0 ; i < dim ; i++)
304  {
305  // If the size of the grid is zero in any dimension set the iterator
306  // to the end point
307  if (grid_base.size(i) == 0)
308  gk.set_d(dim-1,grid_base.size(dim-1));
309  }
310  }
311 
317  void calc_stencil_offset(const grid_key_dx<dim> & start_p)
318  {
319  // calculate the offsets for the stencil code
320  stl_code.calc_offsets(grid_base,start_p);
321  }
322 
329  {
330  grid_key_dx<dim> zero;
331  zero.zero();
332  return zero;
333  }
334 
335 
341  {
342  grid_key_dx<dim> stop;
343 
344  for (size_t i = 0 ; i < dim ; i++)
345  {
346  stop.set_d(i,grid_base.size(i) - 1);
347  }
348 
349  return stop;
350  }
351 
357  inline size_t getVolume()
358  {
360  }
361 
362 
363 };
364 
365 
366 #endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_HPP_ */
grid_key_dx_iterator()
Default constructor.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
void reinitialize(const grid_key_dx_iterator< dim > &key)
Reinitialize the grid_key_dx_iterator.
grid_key_dx_iterator(const grid_key_dx_iterator< dim > &g_it)
Constructor from a grid_key_dx_iterator<dim>
size_t size() const
Return the size of the grid.
Definition: grid_sm.hpp:572
grid_key_dx_iterator< dim > operator=(const grid_key_dx_iterator< dim > &key_it)
Constructor from another grid_key_dx_iterator.
size_t get_gk(size_t i) const
return the index i of the gk key
void calc_stencil_offset(const grid_key_dx< dim > &start_p)
Calculate the stencil offset.
size_t getVolume()
Get the volume spanned by this sub-grid iterator.
grid_key_dx< dim > getStop()
Return the stop point of the iteration.
void reset()
Reset the iterator (it restart from the beginning)
grid_key_dx_iterator(const grid_sm< dim, T > &g)
Constructor require a grid_sm<dim,T>
grid_sm< dim, void > grid_base
information of the grid where this iterator iterate
grid_key_dx< dim > getStart()
Return the starting point of the iteration.
void zero()
Set to zero the key.
Definition: grid_key.hpp:116
grid_key_dx_iterator(const grid_sm< dim, T > &g, const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor require a grid_sm<dim,T>
bool isNext()
Check if there is the next element.
size_t getStencil() const
Get the actual position.
void set(int d, size_t sz)
Set the dimension.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
grid_key_dx_iterator(const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor.
grid_key_dx< dim > gk
Actual key.
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:407
T getVolumeKey() const
Get the volume spanned by the Box P1 and P2 interpreted as grid key.
Definition: Box.hpp:1154
const grid_sm< dim, void > & getGridInfo() const
Get the information about the grid.
grid_key_dx_iterator< dim, stencil > & operator++()
Get the next element.