32 |
|
|
33 |
|
#ifndef UTILS_REPLACEWILDCARD_HPP |
34 |
|
#define UTILS_REPLACEWILDCARD_HPP |
35 |
< |
|
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 |
41 |
> |
//use -1 to represent the wild card, it is easy and cheap to handle integer |
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 |
55 |
|
* //0, -1, 2 |
56 |
|
* //0, 1, -1 |
57 |
|
* //-1, -1, 2 |
58 |
+ |
* //-1, 1, -1 |
59 |
|
* //0, -1, -1 |
60 |
|
* //-1, -1, -1 |
61 |
|
* @endcode |
66 |
|
|
67 |
|
for (int i = 0; i <= num; i++) { |
68 |
|
v = ReplaceWildCard(0, num -1, i); |
69 |
< |
results.insert(v.begin(), v.end(), results.end()); |
69 |
> |
results.insert(results.end(), v.begin(), v.end()); |
70 |
|
} |
71 |
|
return results; |
72 |
|
} |
73 |
|
|
74 |
|
/** Replace a sequence with n wildcards, returns all of the possible replacement*/ |
75 |
< |
std::vector<vector<int> > ReplaceWildCard(int beginIndex, int endIndex, int nWildCard) { |
75 |
> |
std::vector<std::vector<int> > ReplaceWildCard(int beginIndex, int endIndex, int nWildCard) { |
76 |
|
std::vector<std::vector<int> > results; |
77 |
|
|
78 |
|
int len = endIndex + 1 - beginIndex; |
82 |
|
//if the number of the wild card is zero, just return the whole sequence |
83 |
|
std::vector<int> singleResult; |
84 |
|
|
85 |
< |
for(int i = beginIdex; i <= endIndex; i++) |
85 |
> |
for(int i = beginIndex; i <= endIndex; i++) |
86 |
|
singleResult.push_back(i); |
87 |
|
|
88 |
|
results.push_back(singleResult); |
103 |
|
//we need to recursive calling ReplaceWildCard |
104 |
|
std::vector<int> firstPart; |
105 |
|
std::vector<std::vector<int> > secondPart; |
106 |
+ |
std::vector<std::vector<int> > sequences; |
107 |
|
|
108 |
|
for (int i = 0; i <=nRecursive; i ++) { |
109 |
< |
firstPart.push_back( beginIndex + i); |
109 |
> |
firstPart.clear(); |
110 |
> |
for(int j = 0; j < i; ++j) { |
111 |
> |
firstPart.push_back(beginIndex + j); |
112 |
> |
} |
113 |
> |
firstPart.push_back(WildCard); |
114 |
|
secondPart = ReplaceWildCard(beginIndex + i + 1, endIndex, nWildCard - 1); |
115 |
< |
results.push_back(adjoint(firstPart, secondPart)); |
115 |
> |
sequences = adjoint(firstPart, secondPart); |
116 |
> |
results.insert(results.end(), sequences.begin(), sequences.end()); |
117 |
|
} |
118 |
|
|
119 |
|
return results; |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
< |
std::vector<std::vector<int> > adjoint( int firstPart, const std::vector<std::vector<int> >& secondPart){ |
123 |
> |
std::vector<std::vector<int> > adjoint( const std::vector<int>& firstPart, const std::vector<std::vector<int> >& secondPart){ |
124 |
|
std::vector<std::vector<int> > results(secondPart.size()); |
125 |
|
|
126 |
|
for (int i = 0; i < secondPart.size(); i++) { |
127 |
< |
results[i].insert(firstPart.being(), firstPart.end(), results[i].end()); |
128 |
< |
results[i].insert(secondPart[i].begin(), secondPart[i].end(), results[i].begin()); |
127 |
> |
results[i].insert(results[i].end(), firstPart.begin(), firstPart.end()); |
128 |
> |
results[i].insert(results[i].end(), secondPart[i].begin(), secondPart[i].end()); |
129 |
|
} |
130 |
|
|
131 |
|
return results; |
134 |
|
|
135 |
|
}//end namespace std |
136 |
|
|
137 |
< |
#endif //UTILS_REPLACEWILDCARD_HPP |
137 |
> |
#endif //UTILS_REPLACEWILDCARD_HPP |