OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
comb.hpp
1#ifndef COMB_HPP
2#define COMB_HPP
3
4#define COMB_ERROR 1001lu
5
6#include "util/se_util.hpp"
7
33template<unsigned int dim>
34struct comb
35{
37 signed 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&(signed 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
124 inline void sign_flip()
125 {
126 for (size_t i = 0 ; i < dim ; i++)
127 {
128 c[i] = -c[i];
129 }
130 }
131
137 inline comb<dim> operator-(const comb<dim> & t)
138 {
139 comb<dim> ret;
140
141 for (size_t i = 0 ; i < dim ; i++)
142 {
143 ret.c[i] = c[i] - t.c[i];
144 }
145
146 return ret;
147 }
148
161 {
162 comb<dim> ret;
163
164 for (size_t i = 0 ; i < dim ; i++)
165 {
166 ret.c[i] = -abs(c[i]);
167 }
168
169 return ret;
170 }
171
178 {
179 comb<dim> ret;
180
181 for (size_t i = 0 ; i < dim ; i++)
182 {
183 ret.c[i] = -c[i];
184 }
185
186 return ret;
187 }
188
194 inline comb<dim> operator+(const comb<dim> & t)
195 {
196 comb<dim> ret;
197
198 for (size_t i = 0 ; i < dim ; i++)
199 {
200 ret.c[i] = c[i] + t.c[i];
201 }
202
203 return ret;
204 }
205
214 inline bool operator!=(const comb<dim> & t) const
215 {
216 // Check if the two combination match
217
218 for (size_t i = 0 ; i < dim ; i++)
219 {
220 if (c[i] != t.c[i])
221 return true;
222 }
223
224 // They match
225
226 return false;
227 }
228
237 inline bool operator==(const comb<dim> & t) const
238 {
239 return !this->operator!=(t);
240 }
241
249 inline signed char operator[](int i) const
250 {
251 return c[i];
252 }
253
260 inline signed char * getComb()
261 {
262 return c;
263 }
264
271 inline const signed char * getComb() const
272 {
273 return c;
274 }
275
285 inline signed char value(int i) const
286 {
287 return c[i];
288 }
289
290
291 /* \brief It return the number of zero in the combination
292 *
293 * \return number of zero
294 *
295 */
296 inline int n_zero() const
297 {
298 int zero = 0;
299
300 for (size_t i = 0 ; i < dim ; i++)
301 {
302 if (c[i] == 0) zero++;
303 }
304
305 return zero;
306 }
307
310 {}
311
317 comb(std::initializer_list<signed char> c)
318 {
319 size_t i = 0;
320 for(signed char x : c)
321 {this->c[c.size() - i - 1] = x;i++;}
322 }
323
338 {
339 for (size_t i = 0; i < dim ; i++)
340 {
341 if (c[i] > 0)
342 return false;
343 }
344
345 return true;
346 }
347
353 std::string to_string() const
354 {
355 size_t i;
356
357 std::stringstream str;
358 str << "(";
359
360 // convert to string
361 for (i = 0 ; i < dim - 1 ; i++)
362 str << std::to_string(c[i]) << ",";
363
364 str << std::to_string(c[i]) << ")";
365
366 return str.str();
367 }
368
376 size_t inline lin() const
377 {
378 size_t ret = 0;
379 size_t accu = 1;
380
381 for (size_t i = 0 ; i < dim ; i++)
382 {
383 ret += (c[i] + 1) * accu;
384 accu *= 3;
385 }
386
387 return ret;
388 }
389};
390
396template<>
397struct comb<0>
398{
400 signed char c[0];
401
408 inline bool isValid()
409 {
410 return true;
411 }
412
421 inline bool isSub(comb<0> cmb)
422 {
423 return true;
424 }
425
430 inline void zero()
431 {
432 }
433
434
443 inline bool operator!=(const comb<0> & t) const
444 {
445 // They match
446
447 return true;
448 }
449
458 inline bool operator==(const comb<0> & t) const
459 {
460 return true;
461 }
462
470 inline signed char operator[](int i)
471 {
472 return 0;
473 }
474
481 inline signed char * getComb()
482 {
483 return c;
484 }
485
495 inline signed char value(int i) const
496 {
497 return c[i];
498 }
499
500
501 /* \brief It return the number of zero in the combination
502 *
503 * \return number of zero
504 *
505 */
506 inline int n_zero()
507 {
508 return 0;
509 }
510
511 /* \brief produce an unique number from the combination
512 *
513 *
514 *
515 */
516 inline size_t lin()
517 {
518 return 0;
519 }
520};
521
522// create a specialization of std::vector<comb<0>>
523
524namespace std
525{
537 template<>
538 class vector<comb<0>>
539 {
542
543 public:
544
550 size_t size()
551 {
552 return 0;
553 }
554
562 comb<0> & operator[](size_t i)
563 {
564 return ptr[i];
565 }
566
572 void push_back(comb<0> & obj)
573 {
574 }
575 };
576}
577
578#endif
comb< 0 > * ptr
Pointer to nothing.
Definition comb.hpp:541
comb< 0 > & operator[](size_t i)
Do nothing.
Definition comb.hpp:562
size_t size()
Return 0.
Definition comb.hpp:550
void push_back(comb< 0 > &obj)
Do nothing.
Definition comb.hpp:572
bool isSub(comb< 0 > cmb)
Check if the combination is a sub-element.
Definition comb.hpp:421
signed char * getComb()
get the combination array pointer
Definition comb.hpp:481
signed char value(int i) const
get the index i of the combination
Definition comb.hpp:495
bool operator!=(const comb< 0 > &t) const
Compare two combination.
Definition comb.hpp:443
bool isValid()
check if it is a valid combination
Definition comb.hpp:408
bool operator==(const comb< 0 > &t) const
Compare two combination.
Definition comb.hpp:458
signed char operator[](int i)
Get the i combination coefficient.
Definition comb.hpp:470
void zero()
Set all the elements to zero.
Definition comb.hpp:430
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition comb.hpp:35
bool operator==(const comb< dim > &t) const
Compare two combination.
Definition comb.hpp:237
comb< dim > flip()
flip the coefficent of the combination to be < 0
Definition comb.hpp:160
bool isSub(comb< dim > cmb)
Check if the combination is a sub-element.
Definition comb.hpp:66
signed char * getComb()
get the combination array pointer
Definition comb.hpp:260
void mone()
Set all the elements to -1.
Definition comb.hpp:95
signed char value(int i) const
get the index i of the combination
Definition comb.hpp:285
comb< dim > operator-(const comb< dim > &t)
Subtract the combinations and return the result.
Definition comb.hpp:137
const signed char * getComb() const
get the combination array pointer
Definition comb.hpp:271
void zero()
Set all the elements to zero.
Definition comb.hpp:83
comb< dim > operator-()
Subtract the combinations and return the result.
Definition comb.hpp:177
void sign_flip()
Flip the sign of the combination.
Definition comb.hpp:124
comb(std::initializer_list< signed char > c)
Constructor from a list of numbers.
Definition comb.hpp:317
bool isNegative()
Check if any alement in the combination is <= 0.
Definition comb.hpp:337
signed char c[dim]
Array that store the combination.
Definition comb.hpp:37
comb< dim > operator&(signed char c_)
Bitwise operator &.
Definition comb.hpp:108
comb()
Default constructor.
Definition comb.hpp:309
bool operator!=(const comb< dim > &t) const
Compare two combination.
Definition comb.hpp:214
signed char operator[](int i) const
Get the i combination coefficient.
Definition comb.hpp:249
size_t lin() const
Linearization.
Definition comb.hpp:376
std::string to_string() const
Convert a combination into string.
Definition comb.hpp:353
comb< dim > operator+(const comb< dim > &t)
sum the combinations and return the result
Definition comb.hpp:194
bool isValid()
check if it is a valid combination
Definition comb.hpp:45