PARP Research Group University of Murcia, Spain


src/qvmath/qvcombinationiterator.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. 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 #include <QVCombinationIterator>
00026 
00027 bool QVCombinationIterator::increment()
00028         {
00029         if (endCondition == true)
00030                 return false;
00031 
00032         const int siz = size();
00033         int pivot = siz-1;
00034 
00035         // Get the first element for which there is a gap with the previous
00036         for (int i=0; (pivot == siz-1) && (i < siz-1); i++)
00037                 // See if there is a gap between actual element and next element
00038                 if (operator[](i+1) > operator[](i) + 1)
00039                         pivot = i;
00040 
00041         //Increment element at pivot index
00042         operator[](pivot)++;
00043 
00044         // Take all elements after pivot element to start
00045         for (int i = 0; i < pivot; i++)
00046                 operator[](i) = i;
00047 
00048         // Check finishing condition
00049         if (operator[](siz-1) == numElements)
00050                 endCondition = true;
00051 
00052         return !endCondition;
00053         }
00054 
00055 std::ostream& operator << ( std::ostream &os, const QVCombinationIterator &combination)
00056         {
00057         os << "Combination(" << combination.getSubsetsSize() << " elements over " << combination.getSetCardinallity() << "):\t";
00058 
00059         for (int j = 0; j < combination[0]; j++)
00060                 os << "  " << j << " ";
00061         os << " [" << combination[0] << "]";
00062 
00063         for (int i = 1; i < combination.getSubsetsSize(); i++)
00064                 {
00065                 for (int j = combination[i-1]+1; j < combination[i]; j++)
00066                         os << "  " << j << " ";
00067                 os << " [" << combination[i] << "]";
00068                 }
00069 
00070         for (int j = combination[combination.getSubsetsSize()-1]; j < combination.getSetCardinallity()-1; j++)
00071                 os << "  " << (j+1) << " ";
00072 
00073         os << std::endl;
00074 
00075         return os;
00076         }



QVision framework. PARP research group, copyright 2007, 2008.