C++ main module for mmsd Package  1.0
STAT_Combinatorial.h
Go to the documentation of this file.
1 #ifndef STAT_Combinatorial_H
2 #define STAT_Combinatorial_H
3 
4 #include "CORE_Object.h"
5 #include "CORE_ListPointers.h"
6 #include <vector>
7 #include <time.h>
21 template<class T>
23 
24  // ATTRIBUTES
25 
26 private:
27 
28 
29  static const tFlag COMBINATION;
30  static const tFlag PERMUTATION;
31  static const tFlag CIRCULAR_PERMUTATION;
32  tFlag mFunction;
33 
34  unsigned int mCount;
35  size_t mSize;
36  T *mVector;
37 
38 
39 
40  T *mFirstIndex;
41  T *mLastIndex;
42  T *mEnd;
43  size_t mDistanceIndex;
44 
45  T *mCombinatorialList;
46  tBoolean mIsDisplay;
47 
48 
49 
50 protected:
51  // METHODS
52 
53  // CONSTRUCTORS
54 
56  STAT_Combinatorial(void);
57 
58 
59 
60  // DESTRUCTORS
61 
62 
65  virtual ~STAT_Combinatorial(void);
66 
67 public:
68  // -----------------------
69  // shared pointer operator
70  // ------------------------
71 
74  void getSharedPointer(boost::shared_ptr<STAT_Combinatorial<T> >& p){
75  SP::CORE_Object r;
77  p=boost::dynamic_pointer_cast<STAT_Combinatorial<T> >(r);
78  };
81  void getSharedPointer( boost::shared_ptr<const STAT_Combinatorial<T> >& p) const{
82  SPC::CORE_Object r;
84  p=boost::dynamic_pointer_cast<const STAT_Combinatorial<T> >(r);
85  };
86 private:
89  inline boost::shared_ptr<STAT_Combinatorial<T> > getThis() {
90  boost::shared_ptr<STAT_Combinatorial<T> > p;
92  return p;
93  };
96  inline boost::shared_ptr<const STAT_Combinatorial<T> > getThis() const {
97  boost::shared_ptr<const STAT_Combinatorial<T> > p;
99  return p;
100  };
101 
102  // ----------------
103  // New constructors
104  // ----------------
105 public:
108  static inline boost::shared_ptr<STAT_Combinatorial<T> > New() {
109  boost::shared_ptr<STAT_Combinatorial<T> > p(new STAT_Combinatorial<T>(),
111  p->setThis(p);
112  return p;
113  };
116  static inline boost::shared_ptr<STAT_Combinatorial<T> > New(const int& n) {
117  boost::shared_ptr<STAT_Combinatorial<T> > p(new STAT_Combinatorial<T>(),
119  p->setSize(n);
120  p->setThis(p);
121  return p;
122  };
123 
124 
125  // -----------------------
126  // operator accessors
127  // -----------------------
128 
133  inline const T& operator[](const int& i) const {
134  ASSERT_IN(i<mSize);
135  return mVector[i];
136  };
137 
142  inline T& operator[](const int& i) {
143  ASSERT_IN(i<mSize);
144  return mVector[i];
145  };
146 
147 
150  inline void resize(const size_t& n) {
151  T* vector=new T[n+1];
152  size_t dim=(n<mSize)?n:mSize;
153  for (size_t i=0;i<dim;i++) vector[i]=mVector[i];
154  for (size_t i=dim;i<=n;i++) vector[i]=i;
155  if (mVector!=null) delete[] mVector;
156  mVector=vector;
157  mSize=n;
158  mEnd=&mVector[mSize];
159 
160  }
163  inline void setSize(const size_t& n) {
164  if (n<mSize) mSize=n;
165  else resize(n);
166  }
167 
170  inline void initVector() {
171  int i=0;
172  for (T *iter=&mVector[0];iter!=mEnd;iter++) *iter=i++;
173  }
176  inline static void initSeed(const long int& v) {
177  srand(v);
178  }
179 
182  inline static void initSeed() {
183  initSeed(time(NULL));
184  }
185 
186 
187 
190  inline void setIsDisplay(const tBoolean& b) {
191  mIsDisplay=b;
192  }
193 
197  template<class UINT>
198  static UINT getCombinationsNumber(const UINT& p,const UINT& n);
199 
204  template<class UINT>
205  inline UINT getCombinationsNumber(const UINT& p) {
206  ASSERT_IN(p<=(UINT)mSize);
207  return getCombinationsNumber(p,(UINT)mSize);
208  };
209 
213  template<class UINT>
214  static UINT getPermutationsNumber(const UINT& p,const UINT& n);
215 
219  template<class UINT>
220  inline UINT getPermutationsNumber(const UINT& p) {
221  ASSERT_IN(p<=(UINT)mSize);
222  return getPermutationsNumber(p,(UINT)mSize);
223  };
227  template<class UINT>
228  static UINT getCircularPermutationsNumber(const UINT& p,const UINT& n);
232  template<class UINT>
233  inline UINT getCircularPermutationsNumber(const UINT& p) {
234  ASSERT_IN(p<=(UINT)mSize);
235  return getCircularPermutationsNumber(p,(UINT)mSize);
236  };
237 
243  inline static void computeRandomPermutation(T* vect,const int& s) {
244  for(int i=0; i<s; i++) {
245  swap(vect[rand()%(s-i)+i],vect[i]);
246  }
247  }
248 
255  inline static void computeRandomPermutation(T* vect,const int& s,const int& q) {
256  int n=s/q;
257  int jp,jj=0;
258  for(int j=0; j<n; j++) {
259  jp=(rand()%(n-j)+j)*q;
260  for (int i=0;i<q;i++) {
261  swap(vect[jp++],vect[jj++]);
262  }
263  }
264  }
265 
269  int computeCombinations(const int& p);
270 
275  inline int computeCombinations(T* combinations,const int& p) {
276  mCombinatorialList=combinations;
277  return computeCombinations(p);
278  }
279 
283  int computePermutations(const int& p);
284 
289  inline int computePermutations(T* cpermutations,const int& p) {
290  mCombinatorialList=cpermutations;
291  return computePermutations(p);
292  }
293 
297  int computeCircularPermutations(const int& p);
298 
303  inline int computeCircularPermutations(T* combinations,const int& p) {
304  mCombinatorialList=combinations;
305  return computeCircularPermutations(p);
306  };
307 
308 
313 
314 
315  if (mCombinatorialList!=null) {
316  T *list=&mCombinatorialList[mCount*(mLastIndex-mFirstIndex)];
317  int i=0;
318  for (T* iter=mFirstIndex;iter !=mLastIndex;iter++) list[i++]=*iter;
319 
320  }
321  mCount++;
322  if (mIsDisplay) {
323  display();
324  }
325  return false;
326  };
327 
332  return oneCombinationDone();
333  }
334 
337  void display();
338 
339 
340 
341 
342 private:
343 
347  tBoolean oneComputationDone();
348 
349  template<class UINT>
350  static UINT greatestCommonDivisor(const UINT& p,
351  const UINT& n);
352 
353 
354 
355 
356  void rotate_discontinuous(T* first1, T* last1,const int& d1,
357  T* first2, T* last2,const int& d2);
358 
359 
360 
361  bool combine_discontinuous(T* first1, T* last1,const int& d1,
362  T* first2, T* last2,const int& d2,
363  const int& d=0);
364  tBoolean permute(T* first1, T* last1,const int& d1);
365 
366  tBoolean permute_(T* first1,T* last1,const int& d1);
367 
368 
369 };
370 
371 #include "STAT_Combinatorial.hpp"
372 
373 
374 
378 
379 #endif
static void initSeed(const long int &v)
init the seed for random computation
Definition: STAT_Combinatorial.h:176
int computeCombinations(T *combinations, const int &p)
compute all combinations of size p combinations is assumed to have the size of getCombinationsNumber(...
Definition: STAT_Combinatorial.h:275
void display()
display on screnn the computation
Definition: STAT_Combinatorial.hpp:65
const T & operator[](const int &i) const
get the i-th element Assert in (i>-1) Assert in (i
Definition: STAT_Combinatorial.h:133
static UINT getCircularPermutationsNumber(const UINT &p, const UINT &n)
compute the number of circular permutations to take p elements among n n!/(p*(n-p)!) ...
Definition: STAT_Combinatorial.hpp:301
STAT_Combinatorial(void)
create an object
Definition: STAT_Combinatorial.hpp:23
void setIsDisplay(const tBoolean &b)
Definition: STAT_Combinatorial.h:190
static boost::shared_ptr< STAT_Combinatorial< T > > New()
return a STAT_Combinatorial shared pointer
Definition: STAT_Combinatorial.h:108
static void computeRandomPermutation(T *vect, const int &s)
permute all the values of vector of size s in an random order
Definition: STAT_Combinatorial.h:243
int computeCombinations(const int &p)
compute all combinations of size p
Definition: STAT_Combinatorial.hpp:204
static void initSeed()
init the seed for random computation
Definition: STAT_Combinatorial.h:182
int computeCircularPermutations(T *combinations, const int &p)
compute all circular permutations of size p combinations is assumed to have the size of getCircularPe...
Definition: STAT_Combinatorial.h:303
#define tBoolean
Definition: types.h:48
tBoolean onePermutationDone()
method called at each permutation done if return true succeeds
Definition: STAT_Combinatorial.h:331
int computePermutations(const int &p)
compute all permutations of size p
Definition: STAT_Combinatorial.hpp:220
#define null
Definition: types.h:13
UINT getCircularPermutationsNumber(const UINT &p)
compute the number of circular permutations to take p elements among n n!/(p*(n-p)!) ...
Definition: STAT_Combinatorial.h:233
static boost::shared_ptr< STAT_Combinatorial< T > > New(const int &n)
return a STAT_Combinatorial shared pointer
Definition: STAT_Combinatorial.h:116
TYPEDEF_SPTR(STAT_IntCombinatorial)
STAT_Combinatorial< int > STAT_IntCombinatorial
Definition: STAT_Combinatorial.h:375
virtual ~STAT_Combinatorial(void)
destroy an object.
Definition: STAT_Combinatorial.hpp:38
void initVector()
init the vector to index
Definition: STAT_Combinatorial.h:170
UINT getCombinationsNumber(const UINT &p)
compute the number of combinations to take p elements among n n!/(n-p)!*p!
Definition: STAT_Combinatorial.h:205
UINT getPermutationsNumber(const UINT &p)
compute the number of permutations to take p elements among n n!/(n-p)!
Definition: STAT_Combinatorial.h:220
static void computeRandomPermutation(T *vect, const int &s, const int &q)
permute all the values by block of size q of the vector of size s
Definition: STAT_Combinatorial.h:255
TYPEDEF_SVPTR(STAT_IntCombinatorial)
void setSize(const size_t &n)
resize
Definition: STAT_Combinatorial.h:163
void getSharedPointer(SP::CORE_Object &p)
get the shared pointer of this class into p
Definition: CORE_Object.h:65
static UINT getPermutationsNumber(const UINT &p, const UINT &n)
compute the number of permutations to take p elements among n n!/(n-p)!
Definition: STAT_Combinatorial.hpp:348
void resize(const size_t &n)
resize the vector & set the End index & the size
Definition: STAT_Combinatorial.h:150
abstract base class for most classes.
Definition: CORE_Object.h:30
This class is the class to generate combinations, permutation, etc...
Definition: STAT_Combinatorial.h:22
int computePermutations(T *cpermutations, const int &p)
compute all permutations of size p combinations is assumed to have the size of getPermutationsNumber(...
Definition: STAT_Combinatorial.h:289
static UINT getCombinationsNumber(const UINT &p, const UINT &n)
compute the number of combinations to take p elements among n n!/(n-p)!*p!
Definition: STAT_Combinatorial.hpp:367
void getSharedPointer(boost::shared_ptr< STAT_Combinatorial< T > > &p)
return the shared pointer corresponding to the class with casting
Definition: STAT_Combinatorial.h:74
tBoolean oneCombinationDone()
method called at each combination done if return true succeeds
Definition: STAT_Combinatorial.h:312
T & operator[](const int &i)
get the i-th element Assert in (i>-1) Assert in (i
Definition: STAT_Combinatorial.h:142
#define ASSERT_IN(a)
Definition: types.h:96
void getSharedPointer(boost::shared_ptr< const STAT_Combinatorial< T > > &p) const
return the shared pointer corresponding to the class whith casting
Definition: STAT_Combinatorial.h:81
int computeCircularPermutations(const int &p)
compute all circular permutations of size p
Definition: STAT_Combinatorial.hpp:233
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14