-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinatiorialGenerator.h
113 lines (91 loc) · 2.83 KB
/
CombinatiorialGenerator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* File: CombinatiorialGenerator.h
* Author: ph4r05
*
* Created on May 16, 2014, 2:05 PM
*/
#ifndef COMBINATIORIALGENERATOR_H
#define COMBINATIORIALGENERATOR_H
#include "base.h"
#include <stdio.h>
#include <limits.h>
class CombinatiorialGenerator {
private:
/**
* Number of possible elements to choose.
*/
ULONG up;
/**
* Number of elements we want to choose.
*/
ULONG down;
/**
* Total number of all combinations.
*/
ULONG totalNum;
/**
* Number of bytes to represent the combination.
*/
ULONG byteWidth;
/**
* Number of ULONGs to represent the combination.
*/
ULONG byteUlongWidth;
/**
* if false then the generator is before beginning, next has to be called to
* get to the first combination.
*/
bool started;
/**
* Ordering number of the current combination.
*/
ULONG counter;
/**
* Current state = indexes of the individual choices (e.g., {1,2,3} for initial combination X over 3).
* Length of this array = down.
*/
ULONG * curState;
/**
* Current combination generated - bit array of given size with given combination.
* Length of this array = CEIL(up/8).
*/
uchar * curCombination;
bool curCombinationValid;
/**
* Current combination generated - bit array of given size with given combination.
* ULONG representation for computation later.
* Length of this array = CEIL(up/8/SIZEOF_ULONG).
*/
ULONG * curUlongCombination;
bool curUlongCombinationValid;
void firstCombination();
public:
CombinatiorialGenerator(ULONG up, ULONG down);
virtual ~CombinatiorialGenerator();
// Reset the internal state.
void reset();
// Getters.
inline ULONG getUp() { return this->up; }
inline ULONG getDown() { return this->down; }
inline ULONG getTotalNum() { return this->totalNum; }
inline ULONG getByteWidth() { return this->byteWidth; }
inline ULONG getCounter() { return this->counter; }
inline const ULONG * getCurState() { return this->curState; }
inline ULONG * getCurStateEx() { return this->curState; }
const uchar * getCurCombination();
const ULONG * getCurUlongCombination();
// Move to the next.
bool next();
// Return coefficient of the quadratic term combination
static ULONG getQuadIdx(ULONG N, ULONG x1, ULONG x2);
static ULONG getCubeIdx(ULONG N, ULONG x1, ULONG x2, ULONG x3);
/**
* Compute binomial coefficient.
*
* @param n
* @param k
* @return
*/
static ULONG binomial(ULONG n, ULONG k);
};
#endif /* COMBINATIORIALGENERATOR_H */