this repo has no description
1/**** , [ bobocheTree.cc ],
2 Copyright (c) 2008 Universite d'Orleans - Jeremie Vautard
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 *************************************************************************/
22
23#include "Strategy.hh"
24
25StrategyImp::StrategyImp() {
26 cout<<"Default constructor of StrategyImp should not be called !"<<endl;
27 pointers=1;
28 zetag=StrategyNode::Dummy();
29 todos=0;
30 father=NULL;
31}
32
33void StrategyImp::todosUpdate(int i) {
34 todos += i;
35 if (father != NULL) father->todosUpdate(i);
36}
37
38StrategyImp::StrategyImp(StrategyNode tag) {
39 // cout<<"Strategy imp constructor"<<endl;
40 pointers=1;
41 zetag=tag;
42 todos=0;
43 father=NULL;
44 // cout<<"strategyimp constructor fini"<<endl;
45}
46
47
48StrategyImp::~StrategyImp() {
49 for (vector<Strategy>::iterator i = nodes.begin();i != nodes.end();i++) {
50 if (((*i).imp->father) == this) {
51 (*i).imp->father = NULL;
52 }
53 }
54}
55
56
57Strategy::Strategy() {
58 // cout<<"strategy default"<<endl;
59 StrategyNode tag = StrategyNode::Dummy();
60 imp = new StrategyImp(tag);
61}
62
63Strategy::Strategy(StrategyNode tag) {
64 // cout<<"Strategy with tag"<<endl;
65 imp = new StrategyImp(tag);
66 //cout<<"passed imp creation. End of strategy creator"<<endl;
67}
68
69Strategy::Strategy(StrategyImp* imp) {
70 this->imp = imp;
71 this->imp->pointers++;
72}
73
74Strategy::Strategy(bool qt,int VMin, int VMax, int scope, vector<int> values) {
75 // cout<<"strategy with values"<<endl;
76 StrategyNode tag(2,qt,VMin,VMax,scope);
77 tag.valeurs=values;
78 imp = new StrategyImp(tag);
79
80}
81
82
83Strategy::Strategy(const Strategy& tree) {
84 // cout<<"Strategy copy"<<endl;
85 imp = tree.imp;
86 (imp->pointers)++;
87}
88
89
90Strategy& Strategy::operator = (const Strategy& rvalue) {
91 // cout<<"Strategy = "<<endl;
92 if (imp != NULL) {
93 (imp->pointers)--;
94 if ( (imp->pointers) == 0) {
95 // cout<<"no more references for the imp. Delete"<<endl;
96 delete imp;
97 }
98 }
99 imp = rvalue.imp;
100 (imp->pointers)++;
101 return *this;
102}
103
104
105Strategy::~Strategy() {
106 // cout<<"strategy destructor"<<endl;
107 (imp->pointers)--;
108 if ( (imp->pointers) == 0) {
109 // cout<<"no more references for the imp. Delete"<<endl;
110 delete imp;
111 }
112}
113
114
115StrategyNode Strategy::getTag() {
116 return imp->zetag;
117}
118
119Strategy Strategy::getFather() {
120 if (hasFather()) return Strategy(imp->father);
121 return Dummy();
122}
123
124bool Strategy::hasFather() {
125 if (imp->father != NULL) {
126 for (int i=0;i< imp->father->nodes.size();i++) {
127 if ((imp->father->nodes[i].imp) == imp)
128 return true;
129 }
130 }
131 return false;
132}
133
134Strategy Strategy::getChild(int i) {
135 if (i<0 || i>=degree() ) {cout<<"Child "<<i<<" does not exist"<<endl;abort();}
136 return imp->nodes[i];
137}
138
139Strategy Strategy::getSubStrategy(vector<int> position) {
140 if (position.empty()) return *this;
141 int deg = degree();
142 if (deg == 0) {
143 cout<<"Did not find substrategy"<<endl;
144 return Strategy::Dummy();
145 }
146 for (int i=0;i<deg;i++) {
147 Strategy child=getChild(i);
148 bool ok=true;
149 if (child.values().size() == 0) {
150 ok = false;
151 }
152 for (int j=0;(j<child.values().size()) && ok;j++) {
153 if (child.value(j) != position[j]) ok=false;
154 }
155 if (ok) {
156 position.erase(position.begin(),position.begin() + (child.values().size()));
157 return child.getSubStrategy(position);
158 }
159 }
160 cout<<"Did not find substrategy"<<endl;
161 return Strategy::Dummy();
162}
163
164void Strategy::attach(Strategy child) {
165 if (child.isDummy()) {
166 int todosToAdd = 0;
167
168 for (int i=0;i<child.degree();i++) {
169 this->attach(child.getChild(i));
170 }
171 }
172 else {
173 imp->nodes.push_back(child);
174 todosUpdate(child.imp->todos);
175 (child.imp)->father = this->imp;
176 }
177}
178
179void Strategy::detach(Strategy son) {
180
181 vector<Strategy>::iterator it = imp->nodes.begin();
182 while (it != (imp->nodes.end()) && ( (*it).id() != son.id())) {
183 it++;}
184 if ( it != imp->nodes.end()) {
185 todosUpdate(0-((*it).imp->todos));
186 (*it).imp->father=NULL;
187 imp->nodes.erase(it);
188 }
189}
190
191
192void Strategy::detach(unsigned int i) {
193 if (imp->nodes.size() < i) return;
194
195 vector<Strategy>::iterator it = imp->nodes.begin()+i;
196 todosUpdate(0-((*it).imp->todos));
197 (*it).imp->father=NULL;
198 imp->nodes.erase(it);
199}
200
201Strategy Strategy::STrue() {
202 Strategy ret(StrategyNode::STrue());
203 return ret;
204}
205
206Strategy Strategy::SFalse() {
207 Strategy ret(StrategyNode::SFalse());
208 return ret;
209}
210
211Strategy Strategy::Dummy() {
212 Strategy ret(StrategyNode::Dummy());
213 return ret;
214}
215
216Strategy Strategy::Stodo() {
217 Strategy ret(StrategyNode::Todo());
218 ret.imp->todos=1;
219 return ret;
220}
221
222vector<int> Strategy::getPosition() {
223 vector<int> ret;
224 Strategy asc = *this;
225 while (!asc.isDummy()) {
226// cout<<"GetPosition adding "<<asc.values().size()<<" elements to a vector of size "<<ret.size()<<endl;
227 vector<int> ret2;
228 ret2.reserve(ret.size() + asc.values().size() +1);
229 for (int i=0;i<asc.values().size();i++) {
230 ret2.push_back(asc.values()[i]);
231 }
232 for (int i=0;i<ret.size();i++) {
233 ret2.push_back(ret[i]);
234 }
235 ret=ret2;
236 asc = asc.getFather();
237 }
238 return ret;
239}
240
241int Strategy::checkIntegrity() {
242 int ret=0;
243 for (unsigned int i=0;i < (this->degree());i++) {
244 if ( (((imp->nodes[i]).imp)->father) != imp) {
245 ret++;
246 cout<< (((imp->nodes[i]).imp)->father) << " should be " << imp <<endl;
247 }
248 ret += (getChild(i).checkIntegrity());
249 }
250 return ret;
251}
252
253