ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/test/utils/ReplaceWildCard.hpp
Revision: 1661
Committed: Thu Oct 28 02:28:03 2004 UTC (19 years, 8 months ago) by tim
File size: 4374 byte(s)
Log Message:
ReplaceWildCard in progress

File Contents

# Content
1 /*
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 ReplaceWildCard.hpp
28 * @author tlin
29 * @date 10/27/2004
30 * @version 1.0
31 */
32
33 #ifndef UTILS_REPLACEWILDCARD_HPP
34 #define UTILS_REPLACEWILDCARD_HPP
35
36 #include <iostream>
37 #include <vector>
38
39 namespace oopse{
40
41 //use -1 to represent the wild card, it is easy and cheap to operate one integer (or index) instead of string
42 const int WildCard = -1;
43
44 std::vector<std::vector<int> > ReplaceWildCard(int beginIndex, int endIndex, int nWildCard);
45 std::vector<std::vector<int> > adjoint( const std::vector<int>& firstPart, const std::vector<std::vector<int> >& secondPart);
46
47 /**
48 * Driver function for replacing
49 * @code
50 * std::vector<std::vector<int> > result;
51 * result = ReplaceAll(3);
52 * /contents in result
53 * //0, 1, 2
54 * //-1, 1, 2
55 * //0, -1, 2
56 * //0, 1, -1
57 * //-1, -1, 2
58 * //0, -1, -1
59 * //-1, -1, -1
60 * @endcode
61 */
62 std::vector<std::vector<int> > ReplaceAll(int num) {
63 std::vector<std::vector<int> > results;
64 std::vector<std::vector<int> > v;
65
66 for (int i = 0; i <= num; i++) {
67 v = ReplaceWildCard(0, num -1, i);
68 results.insert(results.end(), v.begin(), v.end());
69 }
70 return results;
71 }
72
73 /** Replace a sequence with n wildcards, returns all of the possible replacement*/
74 std::vector<std::vector<int> > ReplaceWildCard(int beginIndex, int endIndex, int nWildCard) {
75 std::vector<std::vector<int> > results;
76
77 int len = endIndex + 1 - beginIndex;
78 int nRecursive = len - nWildCard;
79
80 if (nWildCard == 0) {
81 //if the number of the wild card is zero, just return the whole sequence
82 std::vector<int> singleResult;
83
84 for(int i = beginIndex; i <= endIndex; i++)
85 singleResult.push_back(i);
86
87 results.push_back(singleResult);
88 return results;
89 }
90
91 if (len < nWildCard) {
92 //give warning message
93 std::cerr << "Error" << std::endl;
94 exit(1);
95 } else if (len == nWildCard) {
96 //if the lengths are the same, we only have one choice
97 //replace all of the index with WildCard
98 std::vector<int> singleResult(nWildCard, WildCard);
99 results.push_back(singleResult);
100 return results;
101 } else {
102 //we need to recursive calling ReplaceWildCard
103 std::vector<int> firstPart;
104 std::vector<std::vector<int> > secondPart;
105 std::vector<std::vector<int> > sequences;
106
107 for (int i = 0; i <=nRecursive; i ++) {
108 firstPart.push_back( beginIndex + i);
109 secondPart = ReplaceWildCard(beginIndex + i + 1, endIndex, nWildCard - 1);
110 sequences = adjoint(firstPart, secondPart);
111 results.insert(results.end(), sequences.begin(), sequences.end());
112 }
113
114 return results;
115 }
116 }
117
118 std::vector<std::vector<int> > adjoint( const std::vector<int>& firstPart, const std::vector<std::vector<int> >& secondPart){
119 std::vector<std::vector<int> > results(secondPart.size());
120
121 for (int i = 0; i < secondPart.size(); i++) {
122 results[i].insert(results[i].end(), firstPart.begin(), firstPart.end());
123 results[i].insert(results[i].end(), secondPart[i].begin(), secondPart[i].end());
124 }
125
126 return results;
127 }
128
129
130 }//end namespace std
131
132 #endif //UTILS_REPLACEWILDCARD_HPP