--- trunk/OOPSE-2.0/src/utils/next_combination.hpp 2004/10/28 19:33:46 1675 +++ branches/new_design/OOPSE-2.0/src/utils/next_combination.hpp 2004/10/28 22:34:02 1683 @@ -24,7 +24,7 @@ */ /** - * @file GenerateCombination.hpp + * @file next_combination.hpp * @author tlin * @date 10/27/2004 * @version 1.0 @@ -35,14 +35,36 @@ #include #include - +#include namespace oopse { /** - * STL next_permuationtation like combination sequence generator - * - *

Preconditions:

- * + * @brief STL next_permuationtation like combination sequence generator. + * Given the first and last iterator of a sequence, next_combination iteratively generates all + * possible combinations. + * @return if more combination is availiable, otherwise return false + * @param iterContainer iterator container + * @param first the first iterator + * @param last the last iterator + * @note first and last must be random access iterators and iterContainer must be the container of + * random access iterators . And all of the iteratos in iterContainer must be within the range [first, last) + * + * @code + * std::vector iv; + * iv.push_back(1); + * iv.push_back(8); + * std::vector::iterator> ic; + * while(next_combination(ic, iv.begin(), iv.end())) { + * for (i = ic.begin(); i < ic.end(); ++i) { + * std::cout << **i << "\t"; + * } + * std::cout << std::endl; + * } + * //output + * //1 + * //8 + * //1 8 + * @endcode */ template > class IteratorContainer> bool next_combination(IteratorContainer& iterContainer, RandomAccessIterator first, RandomAccessIterator last) { @@ -58,15 +80,15 @@ bool next_combination(IteratorContainer sv; + * std::vector::iterator> sic; + * std::vector resultString; + * sv.push_back("H"); + * sv.push_back("C"); + * sv.push_back("N"); + + * while (replaceWildCard(sic, sv, resultString)) { + * for(std::vector::iterator i = resultString.begin(); i != resultString.end(); ++i) { + * std::cout << *i << "\t"; + * } + * std::cout << std::endl; + * } + * //output + * //H X X + * //X C X + * //X X N + * //H C X + * //H X N + * //X C N + * //H C N + * @endcode + */ +bool replaceWildCard(std::vector::iterator>& cont, + std::vector& sequence, std::vector& result, + const std::string& wildCard = "X") { + if (cont.size() > sequence.size()) { + std::cerr << "the size of iterator container is greater than the size of sequence"; + } + + bool hasMoreCombination = next_combination(cont, sequence.begin(), sequence.end()); + if (hasMoreCombination) { + result.clear(); + result.insert(result.begin(), sequence.size(), wildCard); + std::vector::iterator>::iterator i; + for ( i = cont.begin(); i != cont.end(); i++){ + result[*i - sequence.begin()] = **i; + } + } + + return hasMoreCombination; + +}//end replaceWildCard + } //end namespace oopse #endif //UTILS_NEXT_COMBINATION_HPP