-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinationGenerator.cpp
117 lines (96 loc) · 2.93 KB
/
CombinationGenerator.cpp
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
113
114
115
116
117
/***
* Name: Ansel Blume
* UserID: ablume
* Date: Nov 29 2016
* Filename: CombinationGenerator.cpp
* Description: Generates all of the combinations of an array of integers
* one by one.
* Sources of help: None.
***/
#include "CombinationGenerator.h"
#include <cassert>
#define CGen CombinationGenerator
CGen::CGen( vector<int> set, int chooseNum ) : set( set ), chooseNum( chooseNum )
{
// Initialize indices array with starting indices
for ( int i = 0; i < chooseNum; i++ )
indices.push_back( i );
// Calculate number of combinations
toGenerate = 1;
// Multiply by n! / (n - r)!
for ( int i = set.size(); i > set.size() - chooseNum; i-- )
toGenerate *= i;
// Divide by r!
for ( int i = 2; i <= chooseNum; i++ )
toGenerate /= i;
}
/**
* Gets the next combination of the set choose "chooseNum".
*
* Returns an empty vector if there are no more combinations to generate.
*/
vector<int> CGen::getNextCombination()
{
// If there are no more combinations left to generate
if ( !hasNextCombination() )
return vector<int>();
// Update the indices to generate the next combination after the first one
if ( isFirst )
isFirst = false;
else
incrementIndices();
vector<int> combination;
// Get the elements in the set at the new indices
for ( int i = 0; i < indices.size(); i++ )
combination.push_back( set[indices[i]] );
toGenerate--; // We've generated the next combination
return combination;
}
/**
* Returns whether there are more combinations to generate.
*/
bool CGen::hasNextCombination()
{
return toGenerate != 0;
}
/**
* Modifies the indices to generate the next combination of the set.
*/
void CGen::incrementIndices()
{
// Start at last index
int inspectionIndex = indices.size() - 1;
if ( indices[inspectionIndex] < set.size() - 1 )
{
indices[inspectionIndex]++;
return;
}
while ( true )
{
// Move to the previous array index
inspectionIndex--;
// Method shouldn't be getting called if it is possible to generate
// a negative index
assert( inspectionIndex >= 0 );
// If there is still room to increment inspectionIndex without colliding
// with the next index in the array
if ( indices[inspectionIndex] < indices[inspectionIndex + 1] - 1 )
{
indices[inspectionIndex]++;
// Make all following numbers 1 + the previous
flattenIndices( inspectionIndex );
return;
}
}
}
/**
* Makes all numbers after "index" in the indices array be one plus the
* previous term.
*/
void CGen::flattenIndices( int index )
{
// Make all numbers after index be one plus the previous
int startNum = indices[index];
for ( int i = index + 1; i < indices.size(); i++ )
indices[i] = ++startNum;
}