ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/utils/next_combination.hpp
Revision: 1677
Committed: Thu Oct 28 20:41:18 2004 UTC (19 years, 8 months ago) by tim
File size: 6056 byte(s)
Log Message:
ReplaceWildCard is working

File Contents

# User Rev Content
1 tim 1674 /*
2     * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3     *
4     * Contact: oopse@oopse.org
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public License
8     * as published by the Free Software Foundation; either version 2.1
9     * of the License, or (at your option) any later version.
10     * All we ask is that proper credit is given for our work, which includes
11     * - but is not limited to - adding the above copyright notice to the beginning
12     * of your source code files, and to any copyright notice that you may distribute
13     * with programs based on this work.
14     *
15     * This program is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU Lesser General Public License for more details.
19     *
20     * You should have received a copy of the GNU Lesser General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23     *
24     */
25    
26     /**
27     * @file GenerateCombination.hpp
28     * @author tlin
29     * @date 10/27/2004
30     * @version 1.0
31     */
32    
33     #ifndef UTILS_NEXT_COMBINATION_HPP
34     #define UTILS_NEXT_COMBINATION_HPP
35    
36     #include <vector>
37     #include <iterator>
38 tim 1677 #include <iostream>
39 tim 1674 namespace oopse {
40    
41     /**
42 tim 1676 * @fn bool next_combination(IteratorContainer<RandomAccessIterator>& iterContainer, RandomAccessIterator first, RandomAccessIterator last)
43     * @brief STL next_permuationtation like combination sequence generator.
44     * Given the first and last iterator of a sequence, next_combination iteratively generates all possible combination.
45     * @param iterContainer iterator container
46     * @param first the first iterator
47     * @param last the last iterator
48     * @note first and last must be random access iterators and iterContainer must be the container which
49     * element is iterator. And all of the iteratos in iterContainer must be within the range [first, last)
50     *
51     * @code
52     * std::vector<int> iv;
53     * iv.push_back(1);
54     * iv.push_back(8);
55     * std::vector<std::vector<int>::iterator> ic;
56     * while(next_combination(ic, iv.begin(), iv.end())) {
57     * for (i = ic.begin(); i < ic.end(); ++i) {
58     * std::cout << **i << "\t";
59     * }
60     * std::cout << std::endl;
61     * }
62     * //output
63     * //1
64     * //8
65     * //1 8
66 tim 1674 */
67 tim 1675 template<class RandomAccessIterator, template<typename ELEM, typename = std::allocator<ELEM> > class IteratorContainer>
68     bool next_combination(IteratorContainer<RandomAccessIterator>& iterContainer, RandomAccessIterator first, RandomAccessIterator last) {
69 tim 1674 if (first == last) {
70     return false;
71     }
72    
73 tim 1675 RandomAccessIterator endIter = --last;
74     typename IteratorContainer<RandomAccessIterator>::iterator i = iterContainer.end();
75 tim 1674
76     if (iterContainer.empty()) {
77     //if sequence is empty, we insert the first iterator
78     iterContainer.insert(iterContainer.end(), first);
79     return true;
80     } else if (*(--i) != endIter){
81     //if the last iterator in iterContainer does not reaches the end, just increment it
82     ++(*i);
83     return true;
84     } else {// the last iterator in iterContainer does not reaches the end
85    
86     //starts at the end of the sequence and works its way towards the front, looking for two
87     //consecutive members of the sequence where the difference between them is greater
88     //than one. For example , if the sequence contains 1, 5, 8, 9 (total number is 10, begin
89     //index is 0, therefore 9 is the end index, and the current size is 4). At the end of while
90     //loop, j will point to 5, and i will point to 8, next combination should be 1, 6, 7, 8.
91     //If j is less than zero, it means it already reaches the last combination of current size.
92     //For instance, sequence may contain 6, 7, 8, 9 at this time, we need to increase the size
93     // of combination to 5
94 tim 1675 typename IteratorContainer<RandomAccessIterator>::iterator j = i;
95 tim 1674 j--;
96     while( j >= iterContainer.begin() && *i == *j + 1){
97     i--;
98     j--;
99     };
100    
101 tim 1675 RandomAccessIterator raIter;
102 tim 1674 if (j - iterContainer.begin() < 0) { //reaches the last combination of current size
103     //half open range
104     if (last - first + 1 == iterContainer.size()) {
105     //if the current size equals to total number, done
106     return false;
107     } else {
108    
109     //push the first currentSize+1 element into sequence
110    
111 tim 1675 for(i = iterContainer.begin(), raIter= first; i != iterContainer.end(); ++i, ++raIter) {
112     *i = raIter;
113 tim 1674 }
114 tim 1675 iterContainer.insert(iterContainer.end(), raIter);
115 tim 1674
116     return true;
117     }
118    
119     } else {
120     ++(*j);
121 tim 1675 raIter = *j;
122 tim 1674 for(; i != iterContainer.end(); ++i) {
123 tim 1675 ++raIter;
124     *i = raIter;
125 tim 1674 }
126     return true;
127     }
128     }
129     } //end next_combination
130    
131 tim 1676 bool replaceWildCard(std::vector<std::vector<std::string>::iterator>& cont,
132 tim 1677 std::vector<std::string>& sequence, std::vector<std::string>& result,
133     const std::string& wildCard = "X") {
134     if (cont.size() > sequence.size()) {
135     std::cerr << "the size of iterator container is greater than the size of sequence";
136 tim 1676 }
137    
138     bool hasMoreCombination = next_combination(cont, sequence.begin(), sequence.end());
139     if (hasMoreCombination) {
140 tim 1677 result.clear();
141     result.insert(result.begin(), sequence.size(), wildCard);
142     std::vector<std::vector<std::string>::iterator>::iterator i;
143 tim 1676 for ( i = cont.begin(); i != cont.end(); i++){
144     result[*i - sequence.begin()] = **i;
145 tim 1677 }
146 tim 1676 }
147    
148     return hasMoreCombination;
149 tim 1677
150 tim 1676 }
151    
152 tim 1674 } //end namespace oopse
153     #endif //UTILS_NEXT_COMBINATION_HPP
154    

Properties

Name Value
svn:executable *