src/qvmath/qvcombinationiterator.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #ifndef QVCOMBINATIONITERATOR_H
00026 #define QVCOMBINATIONITERATOR_H
00027 
00028 #include <qvmath.h>
00029 #include <QVector>
00030 
00110 class QVCombinationIterator: public QVector<int>
00111         {
00112         private:
00113                 int numElements;
00114                 bool endCondition;
00115 
00116         public:
00120                 QVCombinationIterator(): QVector<int>(1), numElements(1), endCondition(true)
00121                         { }
00122 
00126                 QVCombinationIterator(const QVCombinationIterator &combinationIterator): QVector<int>(combinationIterator),
00127                         numElements(combinationIterator.numElements), endCondition(combinationIterator.endCondition)
00128                         { }
00129 
00135                 QVCombinationIterator(const int numElements, const int elementsXSet): QVector<int>(elementsXSet),
00136                         numElements(numElements), endCondition(false)
00137                         {
00138                         Q_ASSERT(numElements > 0);
00139                         Q_ASSERT(numElements >= elementsXSet);
00140                         for (int i=0; i < elementsXSet; i++)
00141                                 operator[](i) = i;
00142                         }
00143  
00149                 virtual bool increment();
00150 
00154                 // infix ++ operator
00155                 QVCombinationIterator& operator++ ()    { increment(); return *this; }
00156 
00160                 // postfix ++ operator
00161                 void operator++ (int)                   { ++(*this); }
00162 
00167                 const double getSubsetNumber()  const   { return qvCombination(numElements, size()); }
00168 
00171                 const bool finished()           const   { return endCondition; }
00172 
00175                 const int firstIndex()          const   { return first(); }
00176 
00179                 const int lastIndex()           const   { return last(); }
00180 
00183                 const int getSetCardinallity()  const   { return numElements; }
00184 
00187                 const int getSubsetsSize()      const   { return size(); }
00188         };
00189 
00197 std::ostream& operator << ( std::ostream &os, const QVCombinationIterator &combination);
00198 
00199 #endif