1 #ifndef STAT_Combinatorial_HPP
2 #define STAT_Combinatorial_HPP
28 mFunction=COMBINATION;
32 mCombinatorialList=
null;
39 if (mVector!=
null)
delete[] mVector;
48 return oneCombinationDone();
51 return permute(mFirstIndex,mLastIndex,mDistanceIndex);
53 case CIRCULAR_PERMUTATION:
54 if (mDistanceIndex<=1)
return onePermutationDone();
55 return permute(mFirstIndex+1,mLastIndex,mDistanceIndex-1);
74 for (iter++; iter !=mLastIndex; iter++) {
75 cout <<
", " << *iter;
82 for (iter++; iter !=mEnd; iter++)
84 cout <<
", " << *iter;
99 return onePermutationDone();
101 if (onePermutationDone())
return true;
103 swap(*first1, *(first1+1));
104 return onePermutationDone();
107 if (onePermutationDone())
return true;
112 if (onePermutationDone())
return true;
116 if (onePermutationDone())
return true;
119 if (onePermutationDone())
return true;
123 if (onePermutationDone())
return true;
126 return onePermutationDone();
131 for (p = fp1; p != last1; p++) {
133 if (permute_(fp1, last1, d1-1))
return true;
134 std::reverse(fp1, last1);
137 return permute_(fp1, last1, d1-1);
150 return onePermutationDone();
153 if (onePermutationDone())
return true;
156 if (onePermutationDone())
return true;
162 if (onePermutationDone())
return true;
166 if (onePermutationDone())
return true;
170 if (onePermutationDone())
return true;
173 if (onePermutationDone())
return true;
177 if (onePermutationDone())
return true;
180 if (onePermutationDone())
return true;
188 for (p = fp1; p != last1; p++)
190 if (permute_(fp1, last1, d1-1))
return true;
191 std::reverse(fp1, last1);
194 if (permute_(fp1, last1, d1-1))
return true;
195 std::reverse(first1, last1);
207 mFirstIndex=&mVector[0];
208 mLastIndex=&mVector[p];
211 mFunction=COMBINATION;
212 combine_discontinuous(mFirstIndex, mLastIndex, mDistanceIndex,
213 mLastIndex, mEnd, mSize-p);
223 mFirstIndex=&mVector[0];
224 mLastIndex=&mVector[p];
226 mFunction=PERMUTATION;
228 combine_discontinuous(mFirstIndex, mLastIndex, mDistanceIndex,
229 mLastIndex, mEnd, mSize-p);
236 mFirstIndex=&mVector[0];
237 mLastIndex=&mVector[p];
239 mFunction=CIRCULAR_PERMUTATION;
241 combine_discontinuous(mFirstIndex, mLastIndex, mDistanceIndex,
242 mLastIndex, mEnd, mSize-p);
249 T* first2, T* last2,
const int& d2)
253 std::rotate(first2, std::swap_ranges(first1, last1, first2), last2);
257 while (first2 != last2)
258 swap(*--i1, *--last2);
259 std::rotate(first1, i1, last1);
266 T* first2, T* last2,
const int& d2,
271 if (d1 == 0 || d2 == 0)
272 return oneComputationDone();
276 for ( i2= first2; i2 != last2; i2++)
278 if (oneComputationDone())
return true;
286 for (
int d22 = d2; i2 != last2; ++i2, d22--)
288 if (combine_discontinuous(f1p, last1, d1-1, i2, last2, d22,d+1))
return true;
292 if (oneComputationDone())
return true;
294 rotate_discontinuous(first1, last1, d1, first2+1, last2, d2-1);
296 rotate_discontinuous(first1, last1, d1, first2, last2, d2);
300 template <
class UINT>
312 r = getCombinationsNumber(d1, d1+d2);
314 catch (
const std::overflow_error&)
316 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCircularPermutationsNumber()");
318 for (--d1; d1 > 1; --d1)
320 if (r > std::numeric_limits<UINT>::max()/d1)
321 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCircularPermutationsNumber()");
327 if (d1 > std::numeric_limits<UINT>::max() - d2)
328 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCircularPermutationsNumber()");
333 if (r > std::numeric_limits<UINT>::max()/s)
334 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCircularPermutationsNumber()");
337 for (--s; s > d2; --s)
339 if (r > std::numeric_limits<UINT>::max()/s)
340 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCircularPermutationsNumber()");
347 template <
class UINT>
352 if (d1 > std::numeric_limits<UINT>::max() - d2)
353 throw std::overflow_error(
"overflow in STAT_Combinatorial::getPermutationsNumber");
358 if (r > std::numeric_limits<UINT>::max() / s)
359 throw std::overflow_error(
"overflow in STAT_Combinatorial::getPermutationsNumber");
376 if (d1 > std::numeric_limits<UINT>::max() - d2)
377 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCombinationsNumber");
382 for (k = 2; k <= d1; ++k, --s) {
384 g = greatestCommonDivisor(r, k);
387 if (r > std::numeric_limits<UINT>::max() / t)
388 throw std::overflow_error(
"overflow in STAT_Combinatorial::getCombinationsNumber");
void display()
display on screnn the computation
Definition: STAT_Combinatorial.hpp:65
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
int computeCombinations(const int &p)
compute all combinations of size p
Definition: STAT_Combinatorial.hpp:204
#define tBoolean
Definition: types.h:48
int computePermutations(const int &p)
compute all permutations of size p
Definition: STAT_Combinatorial.hpp:220
#define null
Definition: types.h:13
virtual ~STAT_Combinatorial(void)
destroy an object.
Definition: STAT_Combinatorial.hpp:38
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
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
virtual void setType(tString type)
set the type of the object
Definition: CORE_Object.h:116
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
#define ASSERT_IN(a)
Definition: types.h:96
int computeCircularPermutations(const int &p)
compute all circular permutations of size p
Definition: STAT_Combinatorial.hpp:233
#define tFlag
Definition: types.h:14