ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/utils/next_combination.hpp
Revision: 1720
Committed: Sat Nov 6 05:21:55 2004 UTC (19 years, 8 months ago) by tim
File size: 7317 byte(s)
Log Message:
adding ForceField and TypeContainer classes

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 tim 1680 * @file next_combination.hpp
28 tim 1674 * @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 * @brief STL next_permuationtation like combination sequence generator.
43 tim 1678 * Given the first and last iterator of a sequence, next_combination iteratively generates all
44 tim 1679 * possible combinations.
45 tim 1678 * @return if more combination is availiable, otherwise return false
46 tim 1676 * @param iterContainer iterator container
47     * @param first the first iterator
48     * @param last the last iterator
49 tim 1678 * @note first and last must be random access iterators and iterContainer must be the container of
50     * random access iterators . And all of the iteratos in iterContainer must be within the range [first, last)
51 tim 1676 *
52     * @code
53     * std::vector<int> iv;
54     * iv.push_back(1);
55     * iv.push_back(8);
56     * std::vector<std::vector<int>::iterator> ic;
57     * while(next_combination(ic, iv.begin(), iv.end())) {
58     * for (i = ic.begin(); i < ic.end(); ++i) {
59     * std::cout << **i << "\t";
60     * }
61     * std::cout << std::endl;
62     * }
63     * //output
64     * //1
65     * //8
66     * //1 8
67 tim 1679 * @endcode
68 tim 1674 */
69 tim 1675 template<class RandomAccessIterator, template<typename ELEM, typename = std::allocator<ELEM> > class IteratorContainer>
70     bool next_combination(IteratorContainer<RandomAccessIterator>& iterContainer, RandomAccessIterator first, RandomAccessIterator last) {
71 tim 1674 if (first == last) {
72     return false;
73     }
74    
75 tim 1675 RandomAccessIterator endIter = --last;
76     typename IteratorContainer<RandomAccessIterator>::iterator i = iterContainer.end();
77 tim 1674
78     if (iterContainer.empty()) {
79     //if sequence is empty, we insert the first iterator
80     iterContainer.insert(iterContainer.end(), first);
81     return true;
82     } else if (*(--i) != endIter){
83 tim 1680 //if the last iterator in iterContainer does not reaches the end, just increase its iterator by 1
84 tim 1674 ++(*i);
85     return true;
86     } else {// the last iterator in iterContainer does not reaches the end
87    
88     //starts at the end of the sequence and works its way towards the front, looking for two
89     //consecutive members of the sequence where the difference between them is greater
90 tim 1680 //than one. For example , if the sequence contains 1, 5, 8, 9 (total number is 10, first is 0
91     //and the last is 10 (due to STL's half open range)). At the end of while
92 tim 1674 //loop, j will point to 5, and i will point to 8, next combination should be 1, 6, 7, 8.
93     //If j is less than zero, it means it already reaches the last combination of current size.
94     //For instance, sequence may contain 6, 7, 8, 9 at this time, we need to increase the size
95     // of combination to 5
96 tim 1675 typename IteratorContainer<RandomAccessIterator>::iterator j = i;
97 tim 1674 j--;
98     while( j >= iterContainer.begin() && *i == *j + 1){
99     i--;
100     j--;
101     };
102    
103 tim 1675 RandomAccessIterator raIter;
104 tim 1674 if (j - iterContainer.begin() < 0) { //reaches the last combination of current size
105     //half open range
106     if (last - first + 1 == iterContainer.size()) {
107     //if the current size equals to total number, done
108     return false;
109     } else {
110    
111     //push the first currentSize+1 element into sequence
112    
113 tim 1675 for(i = iterContainer.begin(), raIter= first; i != iterContainer.end(); ++i, ++raIter) {
114     *i = raIter;
115 tim 1674 }
116 tim 1675 iterContainer.insert(iterContainer.end(), raIter);
117 tim 1674
118     return true;
119     }
120    
121     } else {
122     ++(*j);
123 tim 1675 raIter = *j;
124 tim 1674 for(; i != iterContainer.end(); ++i) {
125 tim 1675 ++raIter;
126     *i = raIter;
127 tim 1674 }
128     return true;
129     }
130     }
131     } //end next_combination
132    
133 tim 1678 /**
134     * @brief iteratively replace the sequence with wild cards
135     * @return true if more combination sequence is avaliable, otherwise return true
136 tim 1681 * @param cont iterator container, if expect the whole series of combinations, pass an empty iterator
137     * container. The user should not modify this iterator container
138 tim 1678 * @param sequence the whole sequence used to generate combination
139     * @param result a possible combination sequence which is set on return
140 tim 1681 * @param wildCard the wild card string. Its value is "X" by default
141 tim 1678 * @note since next_combination never returns an empty sequence, replaceWildCard will not generate
142     * one special combination, which is n identical wild cards (n is equal to the size of the passing sequence)
143     *
144     * @code
145     * std::vector<std::string> sv;
146     * std::vector<std::vector<std::string>::iterator> sic;
147     * std::vector<std::string> resultString;
148     * sv.push_back("H");
149     * sv.push_back("C");
150     * sv.push_back("N");
151    
152 tim 1720 * while (replaceWithWildCard(sic, sv, resultString)) {
153 tim 1678 * for(std::vector<std::string>::iterator i = resultString.begin(); i != resultString.end(); ++i) {
154     * std::cout << *i << "\t";
155     * }
156     * std::cout << std::endl;
157     * }
158     * //output
159     * //H X X
160     * //X C X
161     * //X X N
162     * //H C X
163     * //H X N
164     * //X C N
165     * //H C N
166 tim 1682 * @endcode
167 tim 1678 */
168 tim 1720 bool replaceWithWildCard(std::vector<std::vector<std::string>::iterator>& cont,
169 tim 1677 std::vector<std::string>& sequence, std::vector<std::string>& result,
170     const std::string& wildCard = "X") {
171     if (cont.size() > sequence.size()) {
172     std::cerr << "the size of iterator container is greater than the size of sequence";
173 tim 1676 }
174    
175     bool hasMoreCombination = next_combination(cont, sequence.begin(), sequence.end());
176     if (hasMoreCombination) {
177 tim 1677 result.clear();
178     result.insert(result.begin(), sequence.size(), wildCard);
179     std::vector<std::vector<std::string>::iterator>::iterator i;
180 tim 1676 for ( i = cont.begin(); i != cont.end(); i++){
181     result[*i - sequence.begin()] = **i;
182 tim 1677 }
183 tim 1676 }
184    
185     return hasMoreCombination;
186 tim 1677
187 tim 1680 }//end replaceWildCard
188 tim 1676
189 tim 1674 } //end namespace oopse
190     #endif //UTILS_NEXT_COMBINATION_HPP
191    

Properties

Name Value
svn:executable *