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_sub.hpp
1 /*
2  * grid_key_dx_iterator_sub.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_SUB_HPP_
9 #define OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_
10 
11 #include "grid_key_dx_iterator.hpp"
12 #include "Grid/grid_key.hpp"
13 
14 /* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
15  * in this case a warning is produced
16  *
17  * \tparam dim dimensionality
18  *
19  */
20 template <unsigned int dim>
22 {
23 public:
24 
30  inline static void pw1(size_t i, const grid_key_dx<dim> & gk_start) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " start with index smaller than zero x[" << i << "]=" << gk_start.get(i) << "\n";}
31 
38  inline static void pw2(size_t i, const grid_key_dx<dim> & gk_start) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " Cropping start point x[" << i << "]=" << gk_start.get(i) << " to x[" << i << "]=0 \n" ;}
39 
40 
45  inline static void pw3() {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " stop with smaller index than start \n";}
46 
47 
55  inline static void pw4(size_t i, const grid_key_dx<dim> & gk_stop, const grid_sm<dim,void> & grid_base) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " stop index bigger than cell domain x[" << i << "]=" << gk_stop.get(i) << " > " << grid_base.size(i) << "\n";}
56 
61  inline static void pw5() {std::cerr << "Warning grid_key_dx_iterator_sub: " << __FILE__ << ":" << __LINE__ << " the starting point is not smaller or equal than than the stop point in all the coordinates" << "\n";}
62 };
63 
64 /* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
65  * in this case a warning is produced
66  *
67  * \tparam dim dimensionality
68  * \tparam gb type of grid
69  *
70  */
71 template <unsigned int dim>
73 {
74 public:
75 
82  inline static void pw1(size_t i, const grid_key_dx<dim> & gk_start) {}
83 
90  inline static void pw2(size_t i, const grid_key_dx<dim> & gk_start) {}
91 
96  inline static void pw3() {}
97 
105  inline static void pw4(size_t i, const grid_key_dx<dim> & gk_stop, const grid_sm<dim,void> & grid_base) {}
106 
111  inline static void pw5() {}
112 };
113 
114 template<unsigned int dim, typename stl_type>
116 {
117  static inline void inc(grid_key_dx<dim> & gk,
118  grid_key_dx<dim> & gk_start,
119  grid_key_dx<dim> & gk_stop,
120  stl_type & stl_code,
121  grid_sm<dim,void> & grid_base)
122  {
123  long int i = 0;
124  for ( ; i < dim-1 ; i++)
125  {
126  /* coverity[dead_error_begin] */
127  size_t id = gk.get(i);
128  if ((long int)id > gk_stop.get(i))
129  {
130  // ! overflow, increment the next index
131 
132  size_t idr = gk.get(i) - gk_start.get(i);
133  gk.set_d(i,gk_start.get(i));
134  id = gk.get(i+1);
135  gk.set_d(i+1,id+1);
136 
137  stl_code.adjust_offset(i,idr,grid_base);
138  }
139  else
140  {
141  break;
142  }
143  }
144  }
145 };
146 
147 
161 template<unsigned int dim, typename stencil, typename warn>
162 class grid_key_dx_iterator_sub : public grid_key_dx_iterator<dim,stencil>
163 {
164 #ifdef SE_CLASS1
165  bool initialized = false;
166 #endif
167 
170 
173 
176 
180  void Initialize()
181  {
182  // Check that start and stop are inside the domain otherwise crop them
183 
184  for (size_t i = 0 ; i < dim ; i++)
185  {
186  // if start smaller than 0
187  if (gk_start.get(i) < 0)
188  {
189 #ifdef SE_CLASS1
190  warn::pw1(i,gk_start);
191 #endif
192  if (gk_start.get(i) < gk_stop.get(i))
193  {
194 #ifdef SE_CLASS1
195  warn::pw2(i,gk_start);
196 #endif
197  gk_start.set_d(i,0);
198  }
199  else
200  {
201 #ifdef SE_CLASS1
202  warn::pw3();
203 #endif
204  // No points are available
205  gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
206  break;
207  }
208  }
209 
210  // if stop bigger than the domain
211  if (gk_stop.get(i) >= (long int)grid_base.size(i))
212  {
213 #ifdef SE_CLASS1
214  warn::pw4(i,gk_stop,grid_base);
215 #endif
216 
217  if (gk_start.get(i) < (long int)grid_base.size(i))
218  gk_stop.set_d(i,grid_base.size(i)-1);
219  else
220  {
221  // No point are available
222  gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
223  break;
224  }
225  }
226  }
227 
229  for (unsigned int i = 0 ; i < dim ; i++)
230  {
231  this->gk.set_d(i,gk_start.get(i));
232  }
233 
234 #ifdef SE_CLASS1
235  initialized = true;
236 #endif
237  }
238 
244  {
246 
247 // post_increment_sub_impl<dim,stencil>::inc(this->gk,gk_start,gk_stop,this->stl_code,grid_base);
248 
249  long int i = 0;
250  for ( ; i < dim-1 ; i++)
251  {
252  /* coverity[dead_error_begin] */
253  size_t id = this->gk.get(i);
254  if ((long int)id > gk_stop.get(i))
255  {
256  // ! overflow, increment the next index
257 
258  size_t idr = this->gk.get(i) - gk_start.get(i);
259  this->gk.set_d(i,gk_start.get(i));
260  id = this->gk.get(i+1);
261  this->gk.set_d(i+1,id+1);
262 
263  this->stl_code.adjust_offset(i,idr,grid_base);
264  }
265  else
266  {
267  break;
268  }
269  }
270  }
271 
272 public:
273 
281  {}
282 
290  :grid_key_dx_iterator<dim,stencil>(g_s_it),grid_base(g_s_it.grid_base),gk_start(g_s_it.gk_start), gk_stop(g_s_it.gk_stop)
291  {
292 #ifdef SE_CLASS1
293 
296  for (unsigned int i = 0 ; i < dim ; i++)
297  {
298  if (gk_start.get(i) > gk_stop.get(i))
299  {
300  warn::pw5();
301  }
302  }
303 #endif
304 
305  Initialize();
306  }
307 
308 
322  template<typename T> grid_key_dx_iterator_sub(const grid_sm<dim,T> & g, const grid_key_dx<dim> & start, const grid_key_dx<dim> & stop)
323  : grid_key_dx_iterator<dim,stencil>(g),grid_base(g),gk_start(start), gk_stop(stop)
324  {
325 #ifdef SE_CLASS1
326 
329  for (unsigned int i = 0 ; i < dim ; i++)
330  {
331  if (gk_start.get(i) > gk_stop.get(i))
332  {
333  warn::pw5();
334  }
335  }
336 #endif
337 
338  Initialize();
339  }
340 
341 
356  template<typename T>
358  const grid_key_dx<dim> & start,
359  const grid_key_dx<dim> & stop,
360  const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
361  :grid_key_dx_iterator<dim,stencil>(g,stencil_pnt),grid_base(g),gk_start(start), gk_stop(stop)
362  {
363 #ifdef SE_CLASS1
364 
367  for (unsigned int i = 0 ; i < dim ; i++)
368  {
369  if (gk_start.get(i) > gk_stop.get(i))
370  {
371  warn::pw5();
372  }
373  }
374 #endif
375 
376  Initialize();
378  }
379 
389  grid_key_dx_iterator_sub(const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
390  :grid_key_dx_iterator<dim,stencil>(stencil_pnt)
391  {
392  }
393 
406  template<typename T> grid_key_dx_iterator_sub(const grid_sm<dim,T> & g, const size_t m)
407  :grid_key_dx_iterator<dim>(g),grid_base(g)
408  {
409  // Initialize the start and stop point
410  for (unsigned int i = 0 ; i < dim ; i++)
411  {
412  gk_start.set_d(i,m);
413  gk_stop.set_d(i,g.size(i)-m-1);
414  }
415 
416  //
417  Initialize();
418  }
419 
431  template<typename T> grid_key_dx_iterator_sub(const grid_sm<dim,T> & g, const size_t (& start)[dim], const size_t (& stop)[dim])
432  :grid_key_dx_iterator<dim>(g),grid_base(g),gk_start(start), gk_stop(stop)
433  {
434 #ifdef SE_CLASS1
435 
438  for (unsigned int i = 0 ; i < dim ; i++)
439  {
440  if (start[i] > stop[i])
441  {
442  warn::pw5();
443  }
444  }
445 #endif
446 
447  Initialize();
448  }
449 
458  {
459 #ifdef SE_CLASS1
460  if (initialized == false)
461  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
462 #endif
463 
465 
466  size_t id = this->gk.get(0);
467  this->gk.set_d(0,id+1);
468 
469  this->stl_code.increment();
470 
471  post_increment();
472 
473  return *this;
474  }
475 
482  {
483 #ifdef SE_CLASS1
484  if (initialized == false)
485  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
486 #endif
487 
488  for (size_t i = 0 ; i < nsteps ; i++)
489  {
490  this->operator ++();
491  }
492 
493  return *this;
494  }
495 
503  inline bool isNext()
504  {
505 #ifdef SE_CLASS1
506  if (initialized == false)
507  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
508 #endif
509 
510  if (this->gk.get(dim-1) <= gk_stop.get(dim-1))
511  {
513 
514  return true;
515  }
516 
518  return false;
519  }
520 
526  inline grid_key_dx<dim> get() const
527  {
528 #ifdef SE_CLASS1
529  if (initialized == false)
530  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
531 #endif
532 
534  }
535 
545  inline void reinitialize(const grid_key_dx_iterator_sub<dim> & g_s_it)
546  {
547  // Reinitialize the iterator
548 
550  grid_base = g_s_it.getGridInfo();
551  gk_start = g_s_it.getStart();
552  gk_stop = g_s_it.getStop();
553 
554 
555 #ifdef SE_CLASS1
556 
559  for (unsigned int i = 0 ; i < dim ; i++)
560  {
561  if (gk_start.get(i) > gk_stop.get(i))
562  {
563  warn::pw5();
564  }
565  }
566 
567  initialized = true;
568 #endif
569 
570  Initialize();
572  }
573 
579  inline size_t getVolume()
580  {
581  return Box<dim,long int>::getVolumeKey(gk_start.get_k(), gk_stop.get_k());
582  }
583 
587  inline void reset()
588  {
590 
591  for (size_t i = 0 ; i < dim ; i++)
592  {this->gk.set_d(i,gk_start.get(i));}
593  }
594 
600  inline const grid_sm<dim,void> & getGridInfo() const
601  {
602  return grid_base;
603  }
604 
610  const grid_key_dx<dim> & getStart() const
611  {
612  return gk_start;
613  }
614 
620  const grid_key_dx<dim> & getStop() const
621  {
622  return gk_stop;
623  }
624 
630  template<unsigned int tot_add>
631  inline void private_sum()
632  {
633  this->stl_code.template private_sum<tot_add>();
634  }
635 
641  inline void private_adjust(size_t tot_add)
642  {
643  this->stl_code.template private_adjust(tot_add);
644  }
645 };
646 
647 
649 
663 template<typename warn>
665 {
666 
667 public:
668 
670  {}
671 
680  {return *this;}
681 
689  inline bool isNext()
690  {return false;}
691 
699  inline size_t getVolume()
700  {return 0;}
701 
708  inline void reset()
709  {}
710 };
711 
712 #endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_ */
void private_adjust(size_t tot_add)
Sum a template constant.
Declaration print_warning_on_adjustment.
Definition: grid_sm.hpp:74
grid_key_dx_iterator_sub(const grid_sm< dim, T > &g, const grid_key_dx< dim > &start, const grid_key_dx< dim > &stop, const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor require a grid grid<dim,T>
grid_key_dx_iterator_sub(const grid_sm< dim, T > &g, const grid_key_dx< dim > &start, const grid_key_dx< dim > &stop)
Constructor require a grid grid<dim,T>
grid_key_dx_iterator_sub< dim, stencil, warn > & operator++()
Get the next element.
static void pw4(size_t i, const grid_key_dx< dim > &gk_stop, const grid_sm< dim, void > &grid_base)
print warning type4
grid_key_dx_iterator_sub< dim, stencil, warn > & operator+=(int nsteps)
increment the operator by more than one
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.
size_t getVolume()
Get the volume spanned by this sub-grid iterator.
size_t size() const
Return the size of the grid.
Definition: grid_sm.hpp:572
grid_key_dx_iterator_sub(const grid_sm< dim, T > &g, const size_t m)
Constructor require a grid grid<dim,T>
const grid_key_dx< dim > & getStop() const
Stop point.
grid_key_dx_iterator_sub()
Default constructor.
grid_key_dx_iterator_sub(const grid_sm< dim, T > &g, const size_t(&start)[dim], const size_t(&stop)[dim])
Constructor require a grid grid<dim,T>
grid_key_dx_iterator< 0 > & operator++()
Next point.
const grid_sm< dim, void > & getGridInfo() const
Return the grid information related to this grid.
void calc_stencil_offset(const grid_key_dx< dim > &start_p)
Calculate the stencil offset.
static void pw4(size_t i, const grid_key_dx< dim > &gk_stop, const grid_sm< dim, void > &grid_base)
print warning type4
static void pw5()
print warning type5
mem_id get(size_t i) const
Get the i index.
Definition: grid_key.hpp:394
static void pw2(size_t i, const grid_key_dx< dim > &gk_start)
print warning type2
void private_sum()
Sum a template constant.
grid_sm< dim, void > grid_base
grid base where we are iterating
grid_key_dx_iterator_sub(const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor.
static void pw3()
print warning type3
const grid_key_dx< dim > & get() const
Get the actual key.
static void pw5()
print warning type5
static void pw3()
print warning type3
static void pw2(size_t i, const grid_key_dx< dim > &gk_start)
print warning type2
static void pw1(size_t i, const grid_key_dx< dim > &gk_start)
print warning type1
grid_key_dx< dim > gk
Actual key.
grid_key_dx_iterator_sub(const grid_key_dx_iterator_sub< dim, stencil > &g_s_it)
Constructor from another grid_key_dx_iterator_sub.
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:77
grid_key_dx< dim > gk_start
start point
grid_key_dx< dim > gk_stop
stop point
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
void reset()
Reset the iterator (it restart from the beginning)
bool isNext()
Check if there is the next element.
void post_increment()
After incremented we have to check we did not overflo any dimension and in case adjust the dimensions...
const grid_key_dx< dim > & getStart() const
Starting point.
void reinitialize(const grid_key_dx_iterator_sub< dim > &g_s_it)
Reinitialize the iterator.
static void pw1(size_t i, const grid_key_dx< dim > &gk_start)
print warning type1