![]() |
University of Murcia, Spain ![]() |
QVCombinationIterator Class ReferenceIterator over all possible combinations over a set.
More...
|
Public Member Functions | |
QVCombinationIterator () | |
Constructs a default combination iterator. | |
QVCombinationIterator (const QVCombinationIterator &combinationIterator) | |
Constructs a new combination iterator that is a copy of other. | |
QVCombinationIterator (const int numElements, const int elementsXSet) | |
Constructs a new combination iterator with a specifics cardinallity and subset's size. | |
virtual bool | increment () |
Obtains the next subset of the iteration, indicating if the iteration has finished. | |
QVCombinationIterator & | operator++ () |
infix ++ operator. | |
void | operator++ (int) |
postfix ++ operator. | |
const double | getSubsetNumber () const |
Obtains the amount of possible subsets of a given size and cardinallity. | |
const bool | finished () const |
Indicate if the iteration has finished. | |
const int | firstIndex () const |
Obtains the first element of the current subset. | |
const int | lastIndex () const |
Obtains the last element of the current subset. | |
const int | getSetCardinallity () const |
Obtains the cardinallity of the combination. | |
const int | getSubsetsSize () const |
Obtains the subset's size. |
Generates a sequence containing all the possible subsets of a given cardinallity, from a original set. This set will be the integer numbers from zero to an specified value, so the elements of the original set should be contained in a QList or a QVector.
Use examples: The following examples show how to use this iterator.
QVCombinationIterator combination(7, 3); while(!combination.finished()) { std::cout << combination; combination.increment(); }
QVCombinationIterator combination(7, 3); do std::cout << combination; while(combination.increment());
for (QVCombinationIterator combination(7, 3); !combination.finished(); combination++) std::cout << combination;
All of the previous examples will print the resulting combination of elements from the original set, producing the following output:
Combination(3 elements over 7): [0] [1] [2] 3 4 5 6 Combination(3 elements over 7): [0] [1] 2 [3] 4 5 6 Combination(3 elements over 7): [0] 1 [2] [3] 4 5 6 Combination(3 elements over 7): 0 [1] [2] [3] 4 5 6 Combination(3 elements over 7): [0] [1] 2 3 [4] 5 6 Combination(3 elements over 7): [0] 1 [2] 3 [4] 5 6 Combination(3 elements over 7): 0 [1] [2] 3 [4] 5 6 Combination(3 elements over 7): [0] 1 2 [3] [4] 5 6 Combination(3 elements over 7): 0 [1] 2 [3] [4] 5 6 Combination(3 elements over 7): 0 1 [2] [3] [4] 5 6 Combination(3 elements over 7): [0] [1] 2 3 4 [5] 6 Combination(3 elements over 7): [0] 1 [2] 3 4 [5] 6 Combination(3 elements over 7): 0 [1] [2] 3 4 [5] 6 Combination(3 elements over 7): [0] 1 2 [3] 4 [5] 6 Combination(3 elements over 7): 0 [1] 2 [3] 4 [5] 6 Combination(3 elements over 7): 0 1 [2] [3] 4 [5] 6 Combination(3 elements over 7): [0] 1 2 3 [4] [5] 6 Combination(3 elements over 7): 0 [1] 2 3 [4] [5] 6 Combination(3 elements over 7): 0 1 [2] 3 [4] [5] 6 Combination(3 elements over 7): 0 1 2 [3] [4] [5] 6 Combination(3 elements over 7): [0] [1] 2 3 4 5 [6] Combination(3 elements over 7): [0] 1 [2] 3 4 5 [6] Combination(3 elements over 7): 0 [1] [2] 3 4 5 [6] Combination(3 elements over 7): [0] 1 2 [3] 4 5 [6] Combination(3 elements over 7): 0 [1] 2 [3] 4 5 [6] Combination(3 elements over 7): 0 1 [2] [3] 4 5 [6] Combination(3 elements over 7): [0] 1 2 3 [4] 5 [6] Combination(3 elements over 7): 0 [1] 2 3 [4] 5 [6] Combination(3 elements over 7): 0 1 [2] 3 [4] 5 [6] Combination(3 elements over 7): 0 1 2 [3] [4] 5 [6] Combination(3 elements over 7): [0] 1 2 3 4 [5] [6] Combination(3 elements over 7): 0 [1] 2 3 4 [5] [6] Combination(3 elements over 7): 0 1 [2] 3 4 [5] [6] Combination(3 elements over 7): 0 1 2 [3] 4 [5] [6] Combination(3 elements over 7): 0 1 2 3 [4] [5] [6]
Meaning the selected elements surrounded by brackets for each combination. The index for each one of the selected elements can be accessed with the operator[]. The number of elements accessible with it can be obtained with method getSubsetsSize(), which is the size of the subsets.
Definition at line 110 of file qvcombinationiterator.h.
QVCombinationIterator::QVCombinationIterator | ( | ) | [inline] |
Constructs a default combination iterator.
Generate a combination iterator with one element, and its internal state is defined to finished.
Definition at line 120 of file qvcombinationiterator.h.
QVCombinationIterator::QVCombinationIterator | ( | const QVCombinationIterator & | combinationIterator | ) | [inline] |
Constructs a new combination iterator that is a copy of other.
combinationIterator | the combination iterator to be copied. |
Definition at line 126 of file qvcombinationiterator.h.
QVCombinationIterator::QVCombinationIterator | ( | const int | numElements, | |
const int | elementsXSet | |||
) | [inline] |
Constructs a new combination iterator with a specifics cardinallity and subset's size.
The new combination iterator contains the first subset of the iteration.
numElements | the cardinallity of the susets, must be > 0. | |
elementsXSet | the subset's size, must be <= numElements. |
Definition at line 135 of file qvcombinationiterator.h.
bool QVCombinationIterator::increment | ( | ) | [virtual] |
Obtains the next subset of the iteration, indicating if the iteration has finished.
Returns a true valor until it obtains the last subset, the following times it returns false valor and passes to an undefined subset.
Definition at line 27 of file qvcombinationiterator.cpp.
Referenced by operator++().
QVCombinationIterator& QVCombinationIterator::operator++ | ( | ) | [inline] |
void QVCombinationIterator::operator++ | ( | int | ) | [inline] |
postfix ++ operator.
Increment the iteration.
Definition at line 161 of file qvcombinationiterator.h.
const double QVCombinationIterator::getSubsetNumber | ( | ) | const [inline] |
Obtains the amount of possible subsets of a given size and cardinallity.
The increment function returns a true valor the first getSubsetNmber - 1 invocations.
Definition at line 167 of file qvcombinationiterator.h.
const bool QVCombinationIterator::finished | ( | ) | const [inline] |
Indicate if the iteration has finished.
Definition at line 171 of file qvcombinationiterator.h.
const int QVCombinationIterator::firstIndex | ( | ) | const [inline] |
Obtains the first element of the current subset.
Definition at line 175 of file qvcombinationiterator.h.
const int QVCombinationIterator::lastIndex | ( | ) | const [inline] |
Obtains the last element of the current subset.
Definition at line 179 of file qvcombinationiterator.h.
const int QVCombinationIterator::getSetCardinallity | ( | ) | const [inline] |
Obtains the cardinallity of the combination.
Definition at line 183 of file qvcombinationiterator.h.
Referenced by operator<<().
const int QVCombinationIterator::getSubsetsSize | ( | ) | const [inline] |
Obtains the subset's size.
Definition at line 187 of file qvcombinationiterator.h.
Referenced by operator<<().