OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
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 /* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
12  * in this case a warning is produced
13  *
14  * \tparam dim dimensionality
15  *
16  */
17 template <unsigned int dim>
19 {
20 public:
21  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";}
22  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" ;}
23  inline static void pw3() {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " stop with smaller index than start \n";}
24  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";}
25  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";}
26 };
27 
28 /* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
29  * in this case a warning is produced
30  *
31  * \tparam dim dimensionality
32  * \tparam gb type of grid
33  *
34  */
35 template <unsigned int dim>
37 {
38 public:
39  inline static void pw1(size_t i, const grid_key_dx<dim> & gk_start) {}
40  inline static void pw2(size_t i, const grid_key_dx<dim> & gk_start) {}
41  inline static void pw3() {}
42  inline static void pw4(size_t i, const grid_key_dx<dim> & gk_stop, const grid_sm<dim,void> & grid_base) {}
43  inline static void pw5() {}
44 };
45 
46 
60 template<unsigned int dim, typename warn>
62 {
63 #ifdef DEBUG
64  bool initialized = false;
65 #endif
66 
69 
72 
75 
79  void Initialize()
80  {
81  // Check that start and stop are inside the domain otherwise crop them
82 
83  for (size_t i = 0 ; i < dim ; i++)
84  {
85  // if start smaller than 0
86  if (gk_start.get(i) < 0)
87  {
88 #ifdef DEBUG
89  warn::pw1(i,gk_start);
90 #endif
91  if (gk_start.get(i) < gk_stop.get(i))
92  {
93 #ifdef DEBUG
94  warn::pw2(i,gk_start);
95 #endif
96  gk_start.set_d(i,0);
97  }
98  else
99  {
100 #ifdef DEBUG
101  warn::pw3();
102 #endif
103  // No points are available
104  gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
105  break;
106  }
107  }
108 
109  // if stop bigger than the domain
110  if (gk_stop.get(i) >= (long int)grid_base.size(i))
111  {
112 #ifdef DEBUG
113  warn::pw4(i,gk_stop,grid_base);
114 #endif
115 
116  if (gk_start.get(i) < (long int)grid_base.size(i))
117  gk_stop.set_d(i,grid_base.size(i)-1);
118  else
119  {
120  // No point are available
121  gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
122  break;
123  }
124  }
125  }
126 
128  for (unsigned int i = 0 ; i < dim ; i++)
129  {
130  this->gk.set_d(i,gk_start.get(i));
131  }
132 
133 #ifdef DEBUG
134  initialized = true;
135 #endif
136  }
137 
138 public:
139 
147 {}
148 
159  : grid_key_dx_iterator_sub(g_s_it.grid_base,g_s_it.gk_start,g_s_it.gk_stop)
160  {}
161 
162 
176  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)
177  : grid_key_dx_iterator<dim>(g),grid_base(g),gk_start(start), gk_stop(stop)
178  {
179 #ifdef DEBUG
180 
183  for (unsigned int i = 0 ; i < dim ; i++)
184  {
185  if (gk_start.get(i) > gk_stop.get(i))
186  {
187  warn::pw5();
188  }
189  }
190 #endif
191 
192  Initialize();
193  }
194 
195 
208  template<typename T> grid_key_dx_iterator_sub(const grid_sm<dim,T> & g, const size_t m)
209  : grid_key_dx_iterator<dim>(g),grid_base(g)
210  {
211  // Initialize the start and stop point
212  for (unsigned int i = 0 ; i < dim ; i++)
213  {
214  gk_start.set(i,m);
215  gk_stop.set(i,g.size(i)-m);
216  }
217 
218  //
219  Initialize();
220  }
221 
233  template<typename T> grid_key_dx_iterator_sub(const grid_sm<dim,T> & g, const size_t (& start)[dim], const size_t (& stop)[dim])
234  : grid_key_dx_iterator<dim>(g),grid_base(g),gk_start(start), gk_stop(stop)
235  {
236 #ifdef DEBUG
237 
240  for (unsigned int i = 0 ; i < dim ; i++)
241  {
242  if (start[i] > stop[i])
243  {
244  warn::pw5();
245  }
246  }
247 #endif
248 
249  Initialize();
250  }
251 
261  {
262 #ifdef DEBUG
263  if (initialized == false)
264  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
265 #endif
266 
268 
269  size_t id = this->gk.get(0);
270  this->gk.set_d(0,id+1);
271 
273 
274  size_t i = 0;
275  for ( ; i < dim-1 ; i++)
276  {
277  size_t id = this->gk.get(i);
278  if ((long int)id > gk_stop.get(i))
279  {
280  // ! overflow, increment the next index
281 
282  this->gk.set_d(i,gk_start.get(i));
283  id = this->gk.get(i+1);
284  this->gk.set_d(i+1,id+1);
285  }
286  else
287  {
288  break;
289  }
290  }
291 
292  return *this;
293  }
294 
303  inline bool isNext()
304  {
305 #ifdef DEBUG
306  if (initialized == false)
307  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
308 #endif
309 
310  if (this->gk.get(dim-1) <= gk_stop.get(dim-1))
311  {
313 
314  return true;
315  }
316 
318  return false;
319  }
320 
324  inline grid_key_dx<dim> get()
325  {
326 #ifdef DEBUG
327  if (initialized == false)
328  {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
329 #endif
330 
332  }
333 
344  {
345  // Reinitialize the iterator
346 
348  grid_base = g_s_it.grid_base;
349  gk_start = g_s_it.gk_start;
350  gk_stop = g_s_it.gk_stop;
351 
352 
353 #ifdef DEBUG
354 
357  for (unsigned int i = 0 ; i < dim ; i++)
358  {
359  if (gk_start.get(i) > gk_stop.get(i))
360  {
361  warn::pw5();
362  }
363  }
364 
365  initialized = true;
366 #endif
367 
368  Initialize();
369  }
370 
376  inline size_t getVolume()
377  {
378  return Box<dim,long int>::getVolumeKey(gk_start.k, gk_stop.k);
379  }
380 
384  inline void reset()
385  {
387 
388  for (size_t i = 0 ; i < dim ; i++)
389  {this->gk.set_d(i,gk_start.get(i));}
390  }
391 
398  {
399  return grid_base;
400  }
401 };
402 
403 
405 
419 template<typename warn>
421 {
422 
423 public:
424 
426  {}
427 
429  {return *this;}
430 
431  inline bool isNext()
432  {return false;}
433 
434  inline size_t getVolume()
435  {return 0;}
436 
437  inline void reset()
438  {}
439 };
440 
441 #endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_ */
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>
void reinitialize(const grid_key_dx_iterator_sub< dim, warn > &g_s_it)
Reinitialize the iterator.
mem_id k[dim]
structure that store all the index
Definition: grid_key.hpp:327
void reinitialize(const grid_key_dx_iterator< dim > &key)
Reinitialize the grid_key_dx_iterator.
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
size_t size() const
Return the size of the grid.
Definition: grid_sm.hpp:552
grid_key_dx_iterator< dim > & operator++()
Get the next element.
bool isNext()
Check if there is the next element.
const grid_sm< dim, void > & getGridInfo()
Return the grid information related to this grid.
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>
mem_id get(size_t i) const
Get the i index.
Definition: grid_key.hpp:302
grid_key_dx< dim > gk_stop
stop point
grid_key_dx_iterator_sub(const grid_sm< dim, T > &g, const size_t m)
Constructor require a grid grid<dim,T>
grid_sm< dim, void > grid_base
grid base where we are iterating
void reset()
Reset the iterator (it restart from the beginning)
grid_key_dx< dim > gk_start
start point
const grid_key_dx< dim > & get()
Get the actual key.
grid_key_dx_iterator_sub(const grid_key_dx_iterator_sub< dim > &g_s_it)
Constructor from another grid_key_dx_iterator_sub.
void set(a v, T...t)
set the grid key from a list of numbers
Definition: grid_key.hpp:227
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:315
T getVolumeKey() const
Get the volume spanned by the Box P1 and P2 interpreted as grid key.
Definition: Box.hpp:1011
size_t getVolume()
Get the volume spanned by this sub-grid iterator.
grid_key_dx_iterator_sub()
Default constructor.