OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
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, typename index_type>
19 {
20 public:
21 
27  template<typename exp1>
28  __device__ __host__ inline grid_key_dx(const grid_key_dx_expression<dim,exp1> & exp)
29  {
30  for (size_t i = 0 ; i < dim ; i++)
31  this->k[i] = exp.value(i);
32  }
33 
35  __device__ __host__ inline grid_key_dx()
36  {}
37 
43  __device__ __host__ inline grid_key_dx(std::initializer_list<long int> p1)
44  {
45  size_t i = 0;
46  for(long int x : p1)
47  {
48  set_d(i,x);
49  i++;
50  if (i >= dim)
51  break;
52  }
53  }
54 
60  __device__ __host__ inline grid_key_dx(const grid_key_dx<dim,index_type> & key)
61  :grid_key_dx(key.k)
62  {
63  }
64 
70  __device__ __host__ inline grid_key_dx(const size_t (&k)[dim])
71  {
72  for (size_t i = 0 ; i < dim ; i++)
73  this->k[i] = k[i];
74  }
75 
81  __device__ __host__ inline grid_key_dx(const long int (&k)[dim])
82  {
83  for (size_t i = 0 ; i < dim ; i++)
84  {this->k[i] = k[i];}
85  }
86 
92  __device__ __host__ inline grid_key_dx(const short (&k)[dim])
93  {
94  for (size_t i = 0 ; i < dim ; i++)
95  {this->k[i] = k[i];}
96  }
97 
103  __device__ __host__ inline grid_key_dx(const unsigned short (&k)[dim])
104  {
105  for (size_t i = 0 ; i < dim ; i++)
106  {this->k[i] = k[i];}
107  }
108 
114  __device__ __host__ inline grid_key_dx(const int (&k)[dim])
115  {
116  for (size_t i = 0 ; i < dim ; i++)
117  {this->k[i] = k[i];}
118  }
119 
125  __device__ __host__ inline grid_key_dx(const unsigned int (&k)[dim])
126  {
127  for (size_t i = 0 ; i < dim ; i++)
128  {this->k[i] = k[i];}
129  }
130 
136  template<typename ...T> inline grid_key_dx(const comb<dim> & cmb)
137  {
138  for (size_t i = 0 ; i < dim ; i++)
139  k[i] = cmb[i];
140  }
141 
148  template<typename ...T> __device__ __host__ inline grid_key_dx(const size_t v,const T...t)
149  {
150 #if defined(SE_CLASS1) && !defined(__NVCC__)
151  if (sizeof...(t) != dim -1)
152  {std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << " creating a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << "\n";}
153 #endif
154  k[dim-1] = v;
155  invert_assign(t...);
156  }
157 
158  __device__ __host__ inline grid_key_dx<dim,index_type> move(int i, int m) const
159  {
160  grid_key_dx<dim,index_type> tmp = *this;
161 
162  tmp.k[i] += m;
163 
164  return tmp;
165  }
166 
170  inline void zero()
171  {
172  for (size_t i = 0 ; i < dim ; i++)
173  k[i] = 0;
174  }
175 
179  inline void one()
180  {
181  for (size_t i = 0 ; i < dim ; i++)
182  k[i] = 1;
183  }
184 
188  inline void invalid()
189  {
190  for (size_t i = 0 ; i < dim ; i++)
191  k[i] = -1;
192  }
193 
199  inline bool isValid()
200  {
201  for (size_t i = 0 ; i < dim ; i++)
202  {
203  if (k[i] != -1)
204  return true;
205  }
206 
207  return false;
208  }
209 
217  __device__ __host__
219  {
220  for (size_t i = 0 ; i < dim ; i++)
221  k[i] += p.k[i];
222 
223  return *this;
224  }
225 
233  __device__ __host__
235  {
236  for (size_t i = 0 ; i < dim ; i++)
237  k[i] -= p.k[i];
238 
239  return *this;
240  }
241 
249  __device__ __host__
252  {
254 
255  return exp_sum;
256  }
257 
265  __device__ __host__
268  {
270 
271  return exp_sum;
272  }
273 
281  __device__ __host__
283  {
285 
286  return exp_sum;
287  }
288 
296  __device__ __host__
299  {
301 
302  return exp_sum;
303  }
304 
312  template <typename T>
314  {
316 
317  return exp_sum;
318  }
319 
327  template<unsigned int dim_t> bool operator==(const grid_key_dx<dim_t,index_type> & key_t) const
328  {
329  if (dim != dim_t)
330  {
331  return false;
332  }
333 
334  // Check the two key index by index
335 
336  for (size_t i = 0 ; i < dim ; i++)
337  {
338  if (k[i] != key_t.k[i])
339  {
340  return false;
341  }
342  }
343 
344  // identical key
345  return true;
346  }
347 
348 
356  template<unsigned int dim_t> bool operator!=(const grid_key_dx<dim_t,index_type> & key_t)
357  {
358  return !this->operator==(key_t);
359  }
360 
368  bool operator<(const grid_key_dx<dim,index_type> & key_t) const
369  {
370  // Check the two key index by index
371 
372  for (long int i = dim-1 ; i >= 0; --i)
373  {
374  if (k[i] < key_t.k[i])
375  {
376  return true;
377  }
378  else if (k[i] > key_t.k[i])
379  {
380  return false;
381  }
382  }
383 
384  // identical key
385  return false;
386  }
387 
388  static bool noPointers()
389  {
390  return true;
391  }
392 
399  template<typename a, typename ...T>
400  inline void set(a v, T...t)
401  {
402 #ifdef SE_CLASS1
403  if (sizeof...(t) != dim -1)
404  std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << "setting a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << std::endl;
405 #endif
406  k[dim-1] = v;
407  invert_assign(t...);
408  }
409 
415  const long int(& get_k() const)[dim]
416  {
417  return k;
418  }
419 
428  {
430 
431  for (size_t i = 0; i < dim ; i++)
432  {
433  p.get(i) = get(i);
434  }
435 
436  return p;
437  }
438 
444  std::string to_string() const
445  {
446  return this->toPointS().toString();
447  }
448 
456  template<typename typeT = size_t>
457  __host__ __device__ inline Point<dim,typeT> toPoint() const
458  {
460 
461  for (size_t i = 0; i < dim ; i++)
462  {
463  p.get(i) = get(i);
464  }
465 
466  return p;
467  }
468 
469 
477  __device__ __host__ inline mem_id value(size_t i) const
478  {
479  return k[i];
480  }
481 
490  __device__ __host__ index_type operator[](index_type i) const
491  {
492  return get(i);
493  }
494 
503  __device__ __host__ index_type get(index_type i) const
504  {
505  return k[i];
506  }
507 
516  __device__ __host__ void set_d(index_type i, index_type id)
517  {
518 #if defined(SE_CLASS1) && !defined(__NVCC__)
519 
520  if (i >= dim)
521  std::cerr << "grid_key_dx error: " << __FILE__ << " " << __LINE__ << " try to access dimension " << i << " on a grid_key_dx of size " << dim << "\n";
522 
523 #endif
524  k[i] = id;
525  }
526 
527 private:
528 
530  index_type k[dim];
531 
540  template<typename a, typename ...T> __device__ __host__ void invert_assign(a v,T...t)
541  {
542  k[sizeof...(T)] = v;
543  invert_assign(t...);
544  }
545 
551  template<typename a, typename ...T> __device__ __host__ void invert_assign(a v)
552  {
553  k[0] = v;
554  }
555 
557  __device__ __host__ void invert_assign()
558  {
559  }
560 
561 };
562 
563 
573 template<unsigned int dim, unsigned int p>
575 {
576 public:
577 
578 
579  template<typename a, typename ...T>grid_key_d(a v,T...t)
580  {
581  k[dim-1] = v;
582  invert_assign(t...);
583  }
584 
585  template<typename a, typename ...T>void invert_assign(a v,T...t)
586  {
587  k[sizeof...(T)] = v;
588  invert_assign(t...);
589  }
590 
591  template<typename a, typename ...T>void invert_assign(a v)
592  {
593  k[0] = v;
594  }
595 
596  mem_id k[dim];
597 };
598 
599 
600 #endif
Main class that encapsulate a sub expression.
bool operator<(const grid_key_dx< dim, index_type > &key_t) const
Check order of two keys.
Definition: grid_key.hpp:368
__device__ __host__ grid_key_dx< dim, index_type > & operator+=(const grid_key_dx< dim, index_type > &p)
sum a grid_key
Definition: grid_key.hpp:218
__device__ __host__ 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:282
__device__ __host__ grid_key_dx(const size_t v, const T...t)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:148
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
__device__ __host__ void invert_assign(a v)
assignment
Definition: grid_key.hpp:551
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
__device__ __host__ grid_key_dx(const unsigned short(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:103
__device__ __host__ void invert_assign()
Constructor.
Definition: grid_key.hpp:557
__device__ __host__ void invert_assign(a v, T...t)
Recursively invert the assignment.
Definition: grid_key.hpp:540
__device__ __host__ index_type operator[](index_type i) const
Get the i index.
Definition: grid_key.hpp:490
__device__ __host__ grid_key_dx_sum< dim, grid_key_dx< dim, index_type >, grid_key_dx< dim, index_type > > operator+(const grid_key_dx< dim, index_type > &p) const
sum a grid_key to the grid_key
Definition: grid_key.hpp:251
__device__ __host__ grid_key_dx(const size_t(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:70
__host__ __device__ Point< dim, typeT > toPoint() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:457
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition: grid_key.hpp:503
bool operator!=(const grid_key_dx< dim_t, index_type > &key_t)
Check if two key are the same.
Definition: grid_key.hpp:356
__device__ __host__ grid_key_dx(const int(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:114
std::string toString() const
Return the string with the point coordinate.
Definition: Point.hpp:398
__device__ __host__ grid_key_dx< dim, index_type > & operator-=(const grid_key_dx< dim, index_type > &p)
sum a grid_key
Definition: grid_key.hpp:234
void one()
Set to one the key.
Definition: grid_key.hpp:179
Point< dim, long int > toPointS() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:427
Expression template for grid_key_dx.
bool isValid()
Check if the key is invalid (all components set to -1)
Definition: grid_key.hpp:199
__device__ __host__ mem_id value(size_t i) const
Get the i index.
Definition: grid_key.hpp:477
__device__ __host__ grid_key_dx(const grid_key_dx< dim, index_type > &key)
Constructor from an other key.
Definition: grid_key.hpp:60
__device__ __host__ grid_key_dx(const unsigned int(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:125
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
Definition: Point.hpp:172
__device__ __host__ grid_key_dx(std::initializer_list< long int > p1)
Constructor from initializer list.
Definition: grid_key.hpp:43
__device__ __host__ grid_key_dx(const long int(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:81
index_type k[dim]
structure that store all the index
Definition: grid_key.hpp:530
void set(a v, T...t)
set the Key from a list of numbers
Definition: grid_key.hpp:400
__device__ __host__ grid_key_dx_sub< dim, grid_key_dx< dim, index_type >, 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:313
__device__ __host__ grid_key_dx(const grid_key_dx_expression< dim, exp1 > &exp)
Constructor from expression.
Definition: grid_key.hpp:28
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
grid_key_dx(const comb< dim > &cmb)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:136
void invalid()
Set to invalid the key.
Definition: grid_key.hpp:188
__device__ __host__ grid_key_dx_sub< dim, grid_key_dx< dim, index_type >, grid_key_dx< dim, index_type > > operator-(const grid_key_dx< dim, index_type > &cmb) const
sum an a combination to the grid_key
Definition: grid_key.hpp:298
__device__ __host__ 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:267
const long int(& get_k() const)[dim]
Return the internal k structure.
Definition: grid_key.hpp:415
grid_key_d is the key to access any element in the grid
Definition: grid_key.hpp:574
void zero()
Set to zero the key.
Definition: grid_key.hpp:170
bool operator==(const grid_key_dx< dim_t, index_type > &key_t) const
Check if two key are the same.
Definition: grid_key.hpp:327
std::string to_string() const
convert the information into a string
Definition: grid_key.hpp:444
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition: grid_key.hpp:516
__device__ __host__ grid_key_dx(const short(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:92
Main class that encapsulate a sum expression.
__device__ __host__ grid_key_dx()
Constructor.
Definition: grid_key.hpp:35