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.hpp
1 #ifndef GRID_KEY_DX
2 #define GRID_KEY_DX
3 
4 #include "Grid/comb.hpp"
5 #include "Grid/grid_key_expression.hpp"
6 #include "Space/Shape/Point.hpp"
7 
17 template<unsigned int dim>
19 {
20 public:
21 
27  template<typename exp1> inline grid_key_dx(const grid_key_dx_expression<dim,exp1> & exp)
28  {
29  for (size_t i = 0 ; i < dim ; i++)
30  this->k[i] = exp.value(i);
31  }
32 
34  inline grid_key_dx()
35  {}
36 
42  inline grid_key_dx(std::initializer_list<long int> p1)
43  {
44  size_t i = 0;
45  for(long int x : p1)
46  {
47  set_d(i,x);
48  i++;
49  if (i >= dim)
50  break;
51  }
52  }
53 
60  :grid_key_dx(key.k)
61  {
62  }
63 
69  inline grid_key_dx(const size_t (&k)[dim])
70  {
71  for (size_t i = 0 ; i < dim ; i++)
72  this->k[i] = k[i];
73  }
74 
80  inline grid_key_dx(const long int (&k)[dim])
81  {
82  for (size_t i = 0 ; i < dim ; i++)
83  {this->k[i] = k[i];}
84  }
85 
91  template<typename ...T> inline grid_key_dx(const comb<dim> & cmb)
92  {
93  for (size_t i = 0 ; i < dim ; i++)
94  k[i] = cmb[i];
95  }
96 
103  template<typename ...T> inline grid_key_dx(const size_t v,const T...t)
104  {
105 #ifdef SE_CLASS1
106  if (sizeof...(t) != dim -1)
107  {std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << " creating a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << "\n";}
108 #endif
109  k[dim-1] = v;
110  invert_assign(t...);
111  }
112 
116  inline void zero()
117  {
118  for (size_t i = 0 ; i < dim ; i++)
119  k[i] = 0;
120  }
121 
125  inline void one()
126  {
127  for (size_t i = 0 ; i < dim ; i++)
128  k[i] = 1;
129  }
130 
134  inline void invalid()
135  {
136  for (size_t i = 0 ; i < dim ; i++)
137  k[i] = -1;
138  }
139 
145  inline bool isValid()
146  {
147  for (size_t i = 0 ; i < dim ; i++)
148  {
149  if (k[i] != -1)
150  return true;
151  }
152 
153  return false;
154  }
155 
164  {
165  for (size_t i = 0 ; i < dim ; i++)
166  k[i] += p.k[i];
167 
168  return *this;
169  }
170 
179  {
180  for (size_t i = 0 ; i < dim ; i++)
181  k[i] -= p.k[i];
182 
183  return *this;
184  }
185 
194  {
196 
197  return exp_sum;
198  }
199 
208  {
210 
211  return exp_sum;
212  }
213 
222  {
224 
225  return exp_sum;
226  }
227 
236  {
238 
239  return exp_sum;
240  }
241 
250  {
252 
253  return exp_sum;
254  }
255 
263  template<unsigned int dim_t> bool operator==(const grid_key_dx<dim_t> & key_t) const
264  {
265  if (dim != dim_t)
266  {
267  return false;
268  }
269 
270  // Check the two key index by index
271 
272  for (size_t i = 0 ; i < dim ; i++)
273  {
274  if (k[i] != key_t.k[i])
275  {
276  return false;
277  }
278  }
279 
280  // identical key
281  return true;
282  }
283 
284 
292  template<unsigned int dim_t> bool operator!=(const grid_key_dx<dim_t> & key_t)
293  {
294  return !this->operator==(key_t);
295  }
296 
297 
304  template<typename a, typename ...T>
305  inline void set(a v, T...t)
306  {
307 #ifdef SE_CLASS1
308  if (sizeof...(t) != dim -1)
309  std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << "setting a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << std::endl;
310 #endif
311  k[dim-1] = v;
312  invert_assign(t...);
313  }
314 
320  const long int(& get_k() const)[dim]
321  {
322  return k;
323  }
324 
333  {
335 
336  for (size_t i = 0; i < dim ; i++)
337  {
338  p.get(i) = get(i);
339  }
340 
341  return p;
342  }
343 
349  std::string to_string()
350  {
351  return this->toPointS().toString();
352  }
353 
362  {
364 
365  for (size_t i = 0; i < dim ; i++)
366  {
367  p.get(i) = get(i);
368  }
369 
370  return p;
371  }
372 
373 
381  inline mem_id value(size_t i) const
382  {
383  return k[i];
384  }
385 
394  inline mem_id get(size_t i) const
395  {
396  return k[i];
397  }
398 
407  inline void set_d(size_t i, mem_id id)
408  {
409 #ifdef SE_CLASS1
410 
411  if (i >= dim)
412  std::cerr << "grid_key_dx error: " << __FILE__ << " " << __LINE__ << " try to access dimension " << i << " on a grid_key_dx of size " << dim << "\n";
413 
414 #endif
415  k[i] = id;
416  }
417 
418 private:
419 
421  mem_id k[dim];
422 
431  template<typename a, typename ...T>void invert_assign(a v,T...t)
432  {
433  k[sizeof...(T)] = v;
434  invert_assign(t...);
435  }
436 
442  template<typename a, typename ...T>void invert_assign(a v)
443  {
444  k[0] = v;
445  }
446 
449  {
450  }
451 
452 };
453 
454 
464 template<unsigned int dim, unsigned int p>
466 {
467 public:
468 
469 
470  template<typename a, typename ...T>grid_key_d(a v,T...t)
471  {
472  k[dim-1] = v;
473  invert_assign(t...);
474  }
475 
476  template<typename a, typename ...T>void invert_assign(a v,T...t)
477  {
478  k[sizeof...(T)] = v;
479  invert_assign(t...);
480  }
481 
482  template<typename a, typename ...T>void invert_assign(a v)
483  {
484  k[0] = v;
485  }
486 
487  mem_id k[dim];
488 };
489 
490 
491 #endif
Main class that encapsulate a sub expression.
bool operator==(const grid_key_dx< dim_t > &key_t) const
Check if two key are the same.
Definition: grid_key.hpp:263
mem_id value(size_t i) const
Get the i index.
Definition: grid_key.hpp:381
grid_key_dx(const size_t(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:69
grid_key_dx(const grid_key_dx< dim > &key)
Constructor from an other key.
Definition: grid_key.hpp:59
grid_key_dx(const size_t v, const T...t)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:103
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:371
grid_key_dx_sub< dim, grid_key_dx< dim >, grid_key_dx< dim > > operator-(const grid_key_dx< dim > &cmb) const
sum an a combination to the grid_key
Definition: grid_key.hpp:235
void invalid()
Set to invalid the key.
Definition: grid_key.hpp:134
grid_key_dx(const long int(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:80
grid_key_dx_sum< dim, grid_key_dx< dim >, Point< dim, long int > > operator+(const Point< dim, long int > &p) const
sum a point to the grid_key
Definition: grid_key.hpp:207
grid_key_dx(std::initializer_list< long int > p1)
Constructor from initializer list.
Definition: grid_key.hpp:42
std::string to_string()
convert the information into a string
Definition: grid_key.hpp:349
Point< dim, size_t > toPoint() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:361
grid_key_dx()
Constructor.
Definition: grid_key.hpp:34
void invert_assign(a v)
assignment
Definition: grid_key.hpp:442
const long int(& get_k() const)[dim]
Return the internal k structure.
Definition: grid_key.hpp:320
grid_key_dx(const grid_key_dx_expression< dim, exp1 > &exp)
Constructor from expression.
Definition: grid_key.hpp:27
grid_key_dx_sum< dim, grid_key_dx< dim >, comb< dim > > operator+(const comb< dim > &cmb) const
sum an a combination to the grid_key
Definition: grid_key.hpp:221
Point< dim, long int > toPointS() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:332
Expression template for grid_key_dx.
grid_key_dx< dim > & operator-=(const grid_key_dx< dim > &p)
sum a grid_key
Definition: grid_key.hpp:178
void zero()
Set to zero the key.
Definition: grid_key.hpp:116
grid_key_dx(const comb< dim > &cmb)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:91
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
grid_key_dx< dim > & operator+=(const grid_key_dx< dim > &p)
sum a grid_key
Definition: grid_key.hpp:163
grid_key_dx_sum< dim, grid_key_dx< dim >, grid_key_dx< dim > > operator+(const grid_key_dx< dim > &p) const
sum a grid_key to the grid_key
Definition: grid_key.hpp:193
bool operator!=(const grid_key_dx< dim_t > &key_t)
Check if two key are the same.
Definition: grid_key.hpp:292
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
void invert_assign(a v, T...t)
Recursively invert the assignment.
Definition: grid_key.hpp:431
void set(a v, T...t)
set the Key from a list of numbers
Definition: grid_key.hpp:305
bool isValid()
Check if the key is invalid (all components set to -1)
Definition: grid_key.hpp:145
grid_key_d is the key to access any element in the grid
Definition: grid_key.hpp:465
void invert_assign()
Constructor.
Definition: grid_key.hpp:448
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:407
grid_key_dx_sub< dim, grid_key_dx< dim >, grid_key_dx_expression< dim, T > > operator-(const grid_key_dx_expression< dim, T > &cmb) const
sum this key to another grid expression
Definition: grid_key.hpp:249
Main class that encapsulate a sum expression.
void one()
Set to one the key.
Definition: grid_key.hpp:125
mem_id k[dim]
structure that store all the index
Definition: grid_key.hpp:421