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.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 
22  // Constructor from expression
23  template<typename exp1> inline grid_key_dx(const grid_key_dx_expression<dim,exp1> & exp)
24  {
25  for (size_t i = 0 ; i < dim ; i++)
26  this->k[i] = exp.value(i);
27  }
28 
30  inline grid_key_dx()
31  {}
32 
35  :grid_key_dx(key.k)
36  {
37  }
38 
40  inline grid_key_dx(const size_t (&k)[dim])
41  {
42  for (size_t i = 0 ; i < dim ; i++)
43  this->k[i] = k[i];
44  }
45 
47  inline grid_key_dx(const long int (&k)[dim])
48  {
49  for (size_t i = 0 ; i < dim ; i++)
50  this->k[i] = k[i];
51  }
52 
54  template<typename ...T> inline grid_key_dx(const comb<dim> & cmb)
55  {
56  for (size_t i = 0 ; i < dim ; i++)
57  k[i] = cmb[i];
58  }
59 
61  template<typename ...T> inline grid_key_dx(const size_t v,const T...t)
62  {
63 #ifdef DEBUG
64  if (sizeof...(t) != dim -1)
65  std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << "creating a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << "\n";
66 #endif
67  k[dim-1] = v;
68  invert_assign(t...);
69  }
70 
71  /* \brief Set to zero the key
72  *
73  */
74  inline void zero()
75  {
76  for (size_t i = 0 ; i < dim ; i++)
77  k[i] = 0;
78  }
79 
80  /* \brief Set to one the key
81  *
82  */
83  inline void one()
84  {
85  for (size_t i = 0 ; i < dim ; i++)
86  k[i] = 1;
87  }
88 
89  /* \brief Set to invalid the key
90  *
91  */
92  inline void invalid()
93  {
94  for (size_t i = 0 ; i < dim ; i++)
95  k[i] = -1;
96  }
97 
98  /* \brief sum a grid_key
99  *
100  * \param comb combination (or relative movement)
101  *
102  * \return a grid_key_dx_expression that encapsulate the expression
103  *
104  */
105  inline grid_key_dx<dim> & operator+=(const grid_key_dx<dim> & p)
106  {
107  for (size_t i = 0 ; i < dim ; i++)
108  k[i] += p.k[i];
109 
110  return *this;
111  }
112 
113  /* \brief sum a grid_key
114  *
115  * \param comb combination (or relative movement)
116  *
117  * \return a grid_key_dx_expression that encapsulate the expression
118  *
119  */
120  inline grid_key_dx<dim> & operator-=(const grid_key_dx<dim> & p)
121  {
122  for (size_t i = 0 ; i < dim ; i++)
123  k[i] -= p.k[i];
124 
125  return *this;
126  }
127 
128  /* \brief sum a grid_key to the grid_key
129  *
130  * \param p grid_key to sum
131  *
132  * \return a grid_key_dx_expression that encapsulate the expression
133  *
134  */
136  {
138 
139  return exp_sum;
140  }
141 
142  /* \brief sum an a combination to the grid_key
143  *
144  * \param comb combination (or relative movement)
145  *
146  * \return a grid_key_dx_expression that encapsulate the expression
147  *
148  */
150  {
152 
153  return exp_sum;
154  }
155 
156  /* \brief sum an a combination to the grid_key
157  *
158  * \param comb combination (or relative movement)
159  *
160  * \return a grid_key_dx_expression that encapsulate the expression
161  *
162  */
163  inline grid_key_dx_sum<dim,grid_key_dx<dim>,comb<dim>> operator+(const comb<dim> & cmb) const
164  {
166 
167  return exp_sum;
168  }
169 
170  /* \brief sum an a combination to the grid_key
171  *
172  * \param comb combination (or relative movement)
173  *
174  * \return a grid_key_dx_expression that encapsulate the expression
175  *
176  */
177  inline grid_key_dx_sub<dim,grid_key_dx<dim>,grid_key_dx<dim>> operator-(const grid_key_dx<dim> & cmb) const
178  {
180 
181  return exp_sum;
182  }
183 
184  /* \brief sum this key to another grid expression
185  *
186  * \param cmb expression
187  *
188  * \return a grid_key_dx_expression that encapsulate the expression
189  *
190  */
191  template <typename T> inline 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
192  {
194 
195  return exp_sum;
196  }
197 
198  /* \brief Check if two key are the same
199  *
200  * \param key_t key to check
201  *
202  * \return true if the two key are equal
203  *
204  */
205  template<unsigned int dim_t> bool operator==(const grid_key_dx<dim_t> & key_t)
206  {
207  if (dim != dim_t)
208  {
209  return false;
210  }
211 
212  // Check the two key index by index
213 
214  for (size_t i = 0 ; i < dim ; i++)
215  {
216  if (k[i] != key_t.k[i])
217  {
218  return false;
219  }
220  }
221 
222  // identical key
223  return true;
224  }
225 
227  template<typename a, typename ...T>void set(a v, T...t)
228  {
229 #ifdef DEBUG
230  if (sizeof...(t) != dim -1)
231  std::cerr << "Error grid_key: " << __FILE__ << " " << __LINE__ << "setting a key of dimension " << dim << " require " << dim << " numbers " << sizeof...(t) + 1 << " provided" << "\n";
232 #endif
233  k[dim-1] = v;
234  invert_assign(t...);
235  }
236 
243  {
245 
246  for (size_t i = 0; i < dim ; i++)
247  {
248  p.get(i) = get(i);
249  }
250 
251  return p;
252  }
253 
259  std::string to_string()
260  {
261  return this->toPointS().toString();
262  }
263 
270  {
272 
273  for (size_t i = 0; i < dim ; i++)
274  {
275  p.get(i) = get(i);
276  }
277 
278  return p;
279  }
280 
281 
289  mem_id value(size_t i) const
290  {
291  return k[i];
292  }
293 
302  mem_id get(size_t i) const
303  {
304  return k[i];
305  }
306 
315  void set_d(size_t i, mem_id id)
316  {
317 #ifdef DEBUG
318 
319  if (i >= dim)
320  std::cerr << "grid_key_dx error: " << __FILE__ << " " << __LINE__ << " try to access dimension " << i << " on a grid_key_dx of size " << dim << "\n";
321 
322 #endif
323  k[i] = id;
324  }
325 
327  mem_id k[dim];
328 
329 private:
330 
336  template<typename a, typename ...T>void invert_assign(a v,T...t)
337  {
338  k[sizeof...(T)] = v;
339  invert_assign(t...);
340  }
341 
342  template<typename a, typename ...T>void invert_assign(a v)
343  {
344  k[0] = v;
345  }
346 
347  void invert_assign()
348  {
349  }
350 
351 };
352 
353 
363 template<unsigned int dim, unsigned int p>
365 {
366 public:
367 
368 
369  template<typename a, typename ...T>grid_key_d(a v,T...t)
370  {
371  k[dim-1] = v;
372  invert_assign(t...);
373  }
374 
375  template<typename a, typename ...T>void invert_assign(a v,T...t)
376  {
377  k[sizeof...(T)] = v;
378  invert_assign(t...);
379  }
380 
381  template<typename a, typename ...T>void invert_assign(a v)
382  {
383  k[0] = v;
384  }
385 
386  mem_id k[dim];
387 };
388 
389 
390 #endif
Main class that encapsulate a sub expression.
mem_id value(size_t i) const
Get the i index.
Definition: grid_key.hpp:289
mem_id k[dim]
structure that store all the index
Definition: grid_key.hpp:327
grid_key_dx(const size_t(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:40
grid_key_dx(const grid_key_dx< dim > &key)
Constructor from an other key.
Definition: grid_key.hpp:34
grid_key_dx(const size_t v, const T...t)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:61
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
grid_key_dx(const long int(&k)[dim])
Constructor from buffer reference.
Definition: grid_key.hpp:47
std::string to_string()
convert the information into a string
Definition: grid_key.hpp:259
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
Point< dim, size_t > toPoint() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:269
grid_key_dx()
Constructor.
Definition: grid_key.hpp:30
Point< dim, long int > toPointS() const
Convert to a point the grid_key_dx.
Definition: grid_key.hpp:242
Expression template for grid_key_dx.
T get(int i) const
Get coordinate.
Definition: Point.hpp:42
grid_key_dx(const comb< dim > &cmb)
Construct a grid key from a list of numbers.
Definition: grid_key.hpp:54
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:202
void invert_assign(a v, T...t)
Recursively invert the assignment.
Definition: grid_key.hpp:336
void set(a v, T...t)
set the grid key from a list of numbers
Definition: grid_key.hpp:227
grid_key_d is the key to access any element in the grid
Definition: grid_key.hpp:364
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:315
Main class that encapsulate a sum expression.