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
comb.hpp
1 #ifndef COMB_HPP
2 #define COMB_HPP
3 
4 #define COMB_ERROR 1001lu
5 
6 #include "util/se_util.hpp"
7 
33 template<unsigned int dim>
34 struct comb
35 {
37  char c[dim];
38 
45  inline bool isValid()
46  {
47  for (size_t i = 0 ; i < dim ; i++)
48  {
49  if (c[i] != 1 && c[i] != -1 && c[i] != 0)
50  {
51  return false;
52  }
53  }
54 
55  return true;
56  }
57 
66  inline bool isSub(comb<dim> cmb)
67  {
68  for (size_t i = 0 ; i < dim ; i++)
69  {
70  if (c[i] != 0 && c[i] != cmb.c[i])
71  {
72  return false;
73  }
74  }
75 
76  return true;
77  }
78 
83  inline void zero()
84  {
85  for (size_t i = 0 ; i < dim ; i++)
86  {
87  c[i] = 0;
88  }
89  }
90 
95  inline void mone()
96  {
97  for (size_t i = 0 ; i < dim ; i++)
98  {
99  c[i] = -1;
100  }
101  }
102 
108  inline comb<dim> operator&(char c_)
109  {
110  comb<dim> ret;
111 
112  for (size_t i = 0 ; i < dim ; i++)
113  {
114  ret.c[i] = c[i] & c_;
115  }
116 
117  return ret;
118  }
119 
125  inline comb<dim> operator-(const comb<dim> & t)
126  {
127  comb<dim> ret;
128 
129  for (size_t i = 0 ; i < dim ; i++)
130  {
131  ret.c[i] = c[i] - t.c[i];
132  }
133 
134  return ret;
135  }
136 
148  inline comb<dim> flip()
149  {
150  comb<dim> ret;
151 
152  for (size_t i = 0 ; i < dim ; i++)
153  {
154  ret.c[i] = -abs(c[i]);
155  }
156 
157  return ret;
158  }
159 
166  {
167  comb<dim> ret;
168 
169  for (size_t i = 0 ; i < dim ; i++)
170  {
171  ret.c[i] = -c[i];
172  }
173 
174  return ret;
175  }
176 
182  inline comb<dim> operator+(const comb<dim> & t)
183  {
184  comb<dim> ret;
185 
186  for (size_t i = 0 ; i < dim ; i++)
187  {
188  ret.c[i] = c[i] + t.c[i];
189  }
190 
191  return ret;
192  }
193 
202  inline bool operator!=(const comb<dim> & t) const
203  {
204  // Check if the two combination match
205 
206  for (size_t i = 0 ; i < dim ; i++)
207  {
208  if (c[i] != t.c[i])
209  return true;
210  }
211 
212  // They match
213 
214  return false;
215  }
216 
225  inline bool operator==(const comb<dim> & t) const
226  {
227  return !this->operator!=(t);
228  }
229 
237  inline char operator[](int i) const
238  {
239  return c[i];
240  }
241 
248  inline char * getComb()
249  {
250  return c;
251  }
252 
259  inline const char * getComb() const
260  {
261  return c;
262  }
263 
273  inline char value(int i) const
274  {
275  return c[i];
276  }
277 
278 
279  /* \brief It return the number of zero in the combination
280  *
281  * \return number of zero
282  *
283  */
284  inline int n_zero() const
285  {
286  int zero = 0;
287 
288  for (size_t i = 0 ; i < dim ; i++)
289  {
290  if (c[i] == 0) zero++;
291  }
292 
293  return zero;
294  }
295 
298  {}
299 
305  comb(std::initializer_list<char> c)
306  {
307  size_t i = 0;
308  for(char x : c)
309  {this->c[c.size() - i - 1] = x;i++;}
310  }
311 
325  bool isNegative()
326  {
327  for (size_t i = 0; i < dim ; i++)
328  {
329  if (c[i] > 0)
330  return false;
331  }
332 
333  return true;
334  }
335 
341  std::string to_string() const
342  {
343  size_t i;
344 
345  std::stringstream str;
346  str << "(";
347 
348  // convert to string
349  for (i = 0 ; i < dim - 1 ; i++)
350  str << std::to_string(c[i]) << ",";
351 
352  str << std::to_string(c[i]) << ")";
353 
354  return str.str();
355  }
356 
364  size_t inline lin() const
365  {
366  size_t ret = 0;
367  size_t accu = 1;
368 
369  for (size_t i = 0 ; i < dim ; i++)
370  {
371  ret += (c[i] + 1) * accu;
372  accu *= 3;
373  }
374 
375  return ret;
376  }
377 };
378 
384 template<>
385 struct comb<0>
386 {
388  char c[0];
389 
396  inline bool isValid()
397  {
398  return true;
399  }
400 
409  inline bool isSub(comb<0> cmb)
410  {
411  return true;
412  }
413 
418  inline void zero()
419  {
420  }
421 
422 
431  inline bool operator!=(const comb<0> & t) const
432  {
433  // They match
434 
435  return true;
436  }
437 
446  inline bool operator==(const comb<0> & t) const
447  {
448  return true;
449  }
450 
458  inline char operator[](int i)
459  {
460  return 0;
461  }
462 
469  inline char * getComb()
470  {
471  return c;
472  }
473 
483  inline char value(int i) const
484  {
485  return c[i];
486  }
487 
488 
489  /* \brief It return the number of zero in the combination
490  *
491  * \return number of zero
492  *
493  */
494  inline int n_zero()
495  {
496  return 0;
497  }
498 
499  /* \brief produce an unique number from the combination
500  *
501  *
502  *
503  */
504  inline size_t lin()
505  {
506  return 0;
507  }
508 };
509 
510 // create a specialization of std::vector<comb<0>>
511 
512 namespace std
513 {
525  template<>
526  class vector<comb<0>>
527  {
530 
531  public:
532 
538  size_t size()
539  {
540  return 0;
541  }
542 
550  comb<0> & operator[](size_t i)
551  {
552  return ptr[i];
553  }
554 
560  void push_back(comb<0> & obj)
561  {
562  }
563  };
564 }
565 
566 #endif
char operator[](int i)
Get the i combination coefficient.
Definition: comb.hpp:458
bool operator==(const comb< dim > &t) const
Compare two combination.
Definition: comb.hpp:225
void zero()
Set all the elements to zero.
Definition: comb.hpp:418
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
comb< dim > operator-(const comb< dim > &t)
Subtract the combinations and return the result.
Definition: comb.hpp:125
comb< dim > operator-()
Subtract the combinations and return the result.
Definition: comb.hpp:165
bool operator!=(const comb< 0 > &t) const
Compare two combination.
Definition: comb.hpp:431
bool isValid()
check if it is a valid combination
Definition: comb.hpp:396
std::string to_string() const
Convert a combination into string.
Definition: comb.hpp:341
comb< dim > operator+(const comb< dim > &t)
sum the combinations and return the result
Definition: comb.hpp:182
bool isSub(comb< dim > cmb)
Check if the combination is a sub-element.
Definition: comb.hpp:66
size_t lin() const
Linearization.
Definition: comb.hpp:364
char value(int i) const
get the index i of the combination
Definition: comb.hpp:483
const char * getComb() const
get the combination array pointer
Definition: comb.hpp:259
void push_back(comb< 0 > &obj)
Do nothing.
Definition: comb.hpp:560
comb()
Default constructor.
Definition: comb.hpp:297
char * getComb()
get the combination array pointer
Definition: comb.hpp:248
comb< dim > flip()
flip the coefficent of the combination to be < 0
Definition: comb.hpp:148
comb(std::initializer_list< char > c)
Constructor from a list of numbers.
Definition: comb.hpp:305
void mone()
Set all the elements to -1.
Definition: comb.hpp:95
comb< 0 > & operator[](size_t i)
Do nothing.
Definition: comb.hpp:550
void zero()
Set all the elements to zero.
Definition: comb.hpp:83
comb< dim > operator&(char c_)
Bitwise operator &.
Definition: comb.hpp:108
bool operator!=(const comb< dim > &t) const
Compare two combination.
Definition: comb.hpp:202
bool isSub(comb< 0 > cmb)
Check if the combination is a sub-element.
Definition: comb.hpp:409
bool operator==(const comb< 0 > &t) const
Compare two combination.
Definition: comb.hpp:446
bool isValid()
check if it is a valid combination
Definition: comb.hpp:45
bool isNegative()
Check if any alement in the combination is <= 0.
Definition: comb.hpp:325
comb< 0 > * ptr
Pointer to nothing.
Definition: comb.hpp:529
char value(int i) const
get the index i of the combination
Definition: comb.hpp:273
char * getComb()
get the combination array pointer
Definition: comb.hpp:469
char operator[](int i) const
Get the i combination coefficient.
Definition: comb.hpp:237
char c[dim]
Array that store the combination.
Definition: comb.hpp:37
size_t size()
Return 0.
Definition: comb.hpp:538