Class Combinations will compute C(n,k), all the combinations of n things taken k at a time (where k <= n). Let n 'things' be indexed by i (i=0...n-1), e.g. stored in an array of length n. This class computes C(n,k) as sets of k indexes into the 'things' array. These indexes are accessible via member functions Selection() or isSelected(). Next() computes the next combination until there are no more (when it returns -1).
Definition at line 58 of file Combinations.hpp.
#include <Combinations.hpp>
Public Member Functions | |
Combinations (const Combinations &right) noexcept | |
copy constructor More... | |
Combinations (int N, int K) | |
Combinations (void) noexcept | |
Default constructor. More... | |
bool | isSelected (int j) noexcept |
int | Next (void) noexcept |
Combinations & | operator= (const Combinations &right) noexcept |
Assignment operator. More... | |
int | Selection (int j) noexcept |
Private Member Functions | |
int | Increment (int j) noexcept |
Recursive function to increment Index[j]. More... | |
void | init (int N, int K) |
Private Attributes | |
std::vector< int > | Index |
int | k |
int | n |
combinations of n things taken k at a time More... | |
int | nc |
number of combinations computed so far More... | |
|
inlinenoexcept |
Default constructor.
Definition at line 61 of file Combinations.hpp.
|
inline |
Constructor for C(n,k) = combinations of n things taken k at a time (k <= n)
Exception | on invalid input (k>n). |
Definition at line 69 of file Combinations.hpp.
|
inlinenoexcept |
copy constructor
Definition at line 73 of file Combinations.hpp.
|
inlineprivatenoexcept |
Recursive function to increment Index[j].
Definition at line 133 of file Combinations.hpp.
|
inlineprivate |
The initialization routine used by constructors.
Exception | on invalid input (k>n or either n or k < 0). |
Definition at line 117 of file Combinations.hpp.
|
inlinenoexcept |
Return true if the given index j (0 <= j < n) is currently selected (i.e. if j = Selection(i) for some i)
Definition at line 106 of file Combinations.hpp.
|
inlinenoexcept |
Compute the next combination, returning the number of combinations computed so far; if there are no more combinations, return -1.
Definition at line 90 of file Combinations.hpp.
|
inlinenoexcept |
Assignment operator.
Definition at line 80 of file Combinations.hpp.
|
inlinenoexcept |
Return index i (0 <= i < n) of jth selection (0 <= j < k); if j is out of range, return -1.
Definition at line 98 of file Combinations.hpp.
|
private |
Index[j] = index of jth selection (j=0...k-1; I[j]=0...n-1)
Definition at line 150 of file Combinations.hpp.
|
private |
Definition at line 149 of file Combinations.hpp.
|
private |
combinations of n things taken k at a time
Definition at line 149 of file Combinations.hpp.
|
private |
number of combinations computed so far
Definition at line 148 of file Combinations.hpp.