1 |
tim |
166 |
/* |
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 |
tim |
168 |
|
36 |
|
|
#include <iostream> |
37 |
tim |
166 |
#include <vector> |
38 |
|
|
|
39 |
|
|
namespace oopse{ |
40 |
|
|
|
41 |
tim |
169 |
//use -1 to represent the wild card, it is easy and cheap to handle integer |
42 |
tim |
166 |
const int WildCard = -1; |
43 |
|
|
|
44 |
tim |
168 |
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 |
tim |
166 |
/** |
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 |
tim |
169 |
* //-1, 1, -1 |
59 |
tim |
166 |
* //0, -1, -1 |
60 |
|
|
* //-1, -1, -1 |
61 |
|
|
* @endcode |
62 |
|
|
*/ |
63 |
|
|
std::vector<std::vector<int> > ReplaceAll(int num) { |
64 |
|
|
std::vector<std::vector<int> > results; |
65 |
|
|
std::vector<std::vector<int> > v; |
66 |
|
|
|
67 |
|
|
for (int i = 0; i <= num; i++) { |
68 |
|
|
v = ReplaceWildCard(0, num -1, i); |
69 |
tim |
168 |
results.insert(results.end(), v.begin(), v.end()); |
70 |
tim |
166 |
} |
71 |
|
|
return results; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/** Replace a sequence with n wildcards, returns all of the possible replacement*/ |
75 |
tim |
168 |
std::vector<std::vector<int> > ReplaceWildCard(int beginIndex, int endIndex, int nWildCard) { |
76 |
tim |
166 |
std::vector<std::vector<int> > results; |
77 |
|
|
|
78 |
|
|
int len = endIndex + 1 - beginIndex; |
79 |
|
|
int nRecursive = len - nWildCard; |
80 |
|
|
|
81 |
|
|
if (nWildCard == 0) { |
82 |
|
|
//if the number of the wild card is zero, just return the whole sequence |
83 |
|
|
std::vector<int> singleResult; |
84 |
|
|
|
85 |
tim |
168 |
for(int i = beginIndex; i <= endIndex; i++) |
86 |
tim |
166 |
singleResult.push_back(i); |
87 |
|
|
|
88 |
|
|
results.push_back(singleResult); |
89 |
|
|
return results; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
if (len < nWildCard) { |
93 |
|
|
//give warning message |
94 |
|
|
std::cerr << "Error" << std::endl; |
95 |
|
|
exit(1); |
96 |
|
|
} else if (len == nWildCard) { |
97 |
|
|
//if the lengths are the same, we only have one choice |
98 |
|
|
//replace all of the index with WildCard |
99 |
|
|
std::vector<int> singleResult(nWildCard, WildCard); |
100 |
|
|
results.push_back(singleResult); |
101 |
|
|
return results; |
102 |
|
|
} else { |
103 |
|
|
//we need to recursive calling ReplaceWildCard |
104 |
|
|
std::vector<int> firstPart; |
105 |
|
|
std::vector<std::vector<int> > secondPart; |
106 |
tim |
168 |
std::vector<std::vector<int> > sequences; |
107 |
tim |
166 |
|
108 |
|
|
for (int i = 0; i <=nRecursive; i ++) { |
109 |
tim |
169 |
firstPart.clear(); |
110 |
|
|
for(int j = 0; j < i; ++j) { |
111 |
|
|
firstPart.push_back(beginIndex + j); |
112 |
|
|
} |
113 |
|
|
firstPart.push_back(WildCard); |
114 |
tim |
166 |
secondPart = ReplaceWildCard(beginIndex + i + 1, endIndex, nWildCard - 1); |
115 |
tim |
168 |
sequences = adjoint(firstPart, secondPart); |
116 |
|
|
results.insert(results.end(), sequences.begin(), sequences.end()); |
117 |
tim |
166 |
} |
118 |
|
|
|
119 |
|
|
return results; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
tim |
168 |
std::vector<std::vector<int> > adjoint( const std::vector<int>& firstPart, const std::vector<std::vector<int> >& secondPart){ |
124 |
tim |
166 |
std::vector<std::vector<int> > results(secondPart.size()); |
125 |
|
|
|
126 |
|
|
for (int i = 0; i < secondPart.size(); i++) { |
127 |
tim |
168 |
results[i].insert(results[i].end(), firstPart.begin(), firstPart.end()); |
128 |
|
|
results[i].insert(results[i].end(), secondPart[i].begin(), secondPart[i].end()); |
129 |
tim |
166 |
} |
130 |
|
|
|
131 |
|
|
return results; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
}//end namespace std |
136 |
|
|
|
137 |
tim |
168 |
#endif //UTILS_REPLACEWILDCARD_HPP |