1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #ifndef NODES_H
20 #define NODES_H
21 #include<iostream>
22 #include<string>
23 #include<vector>
24 namespace tree {
25
26
27
28
29
30
31
32 template<class unk>
33 class NODE{
34 public:
35 ¦
36 ¦
37 NODE( std::string xt, const unk &x , NODE<unk> *xcl= 0, NODE<unk> *xcr = 0, NODE<unk> *xp = 0 ):_trait{xt},_states{x}, _cl{xcl}, _cr{xcr}, _p{xp} {
38 std::cout << std::endl <<"This node has "<< trait() << std::endl << " node " << "has the following states => " << states() << std::endl;
39 };
40
41 NODE(const NODE &other);
42 ¦ ~NODE();
43 ¦
44 ¦
45
46 std::string trait()const{
47 return _trait;
48 }
49 unk states()const{
50 std::cout << std::endl << __LINE__ << " sending _states" << std::endl;
51 ¦ ¦ return _states;
52 ¦ };
53 NODE<unk> * cl()const{
54 ¦ ¦ return _cl;
55 ¦ };
56 NODE<unk> * cr()const{
57 ¦ ¦ return _cr;
58 ¦ };
59 NODE<unk> * p()const{
60 ¦ ¦ return _p;
61 ¦ };
62 void states(const unk &x){
63 ¦ ¦ _states = x;
64 ¦ }
65 void trait(std::string xt){
66 ¦_trait = xt;
67 ¦}
68 void cl(NODE<unk> *xcl){
69 ¦ ¦ _cl = xcl;
70 ¦ }
71 void cr(NODE<unk> *xcr){
72 ¦ ¦ _cr = xcr;
73 ¦ }
74 void p(NODE<unk> *xp){
75 ¦ ¦ _p = xp;
76 ¦ }
77 ¦
78 ¦
79 ¦
80 template <class T>
81 friend std::ostream& operator<<(std::ostream& os, const T&);
82
83 template<class T>
84 friend std::ostream& operator<<(std::ostream& os, const NODE<T> &);
85
86 protected:
87 ¦
88 ¦
89 ¦
90 private:
91 ¦
92 ¦std::string _trait;
93 ¦unk _states;
94 ¦NODE<unk> * _cl, * _cr, * _p;
95
96
97 };
98
99
100
101
102 template<class unk>
103 tree::NODE<unk>::~NODE<unk>(){
104 }
105
106
107
108
109
110
111
112 template<class cost>
113 class STATE
114 {
115 public:
116
117 ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
118
119 explicit STATE<cost>(std::string const xa, cost xr)
120 :_nam{xa}, _r{xr}
121 {
122 std::cout << std::endl << "Building a state pair" << std::endl;
123 std::cout << "nam => " << nam() << "\tcost=> " << r() << std::endl;
124 };
125
126
127
128 std::string nam()const {return _nam;};
129 cost r() const {return _r;};
130 void r(cost b ){_r = b;};
131
132
133 void change(cost a, cost b){
134 _nam = a;
135 r(b);
136 }
137
138 friend std::ostream & operator << ( std::ostream & os, const STATE & obj )
139 {
140 os << std::endl << "name => " << obj.nam() << ":cost=> " << obj.r() ;
141 return os;
142 }
143 STATE & operator = (cost b)
144 {
145 r(b);
146 return *this;
147 }
148
149
150 protected:
151
152 private:
153 std::string _nam;
154 cost _r;
155
156 };
157
158
159
160
161
162
163
164 template<class cost_type>
165 class Pstates
166 {
167 public:
168
169
170
171 Pstates (std::vector< STATE<cost_type> > x)
172 : _vstate{x}
173 {
174 for( auto& y : _vstate)
175 std::cout << std::endl <<"adding state " << y << std::endl;
176 };
177
178 ~Pstates (){};
179
180
181 const std::vector< STATE<cost_type> >& vstate() const{
182 return _vstate;
183 }
184 void vstate(std::vector< STATE<cost_type> > in ){
185 _vstate = in;
186 }
187
188
189
190
191
192 const Pstates& operator = ( const Pstates &other );
193 const Pstates& operator = ( const std::vector< STATE<cost_type > > &other ){
194 vstate(other);
195 return *this;
196 } ;
197 STATE<cost_type>& operator [] ( unsigned int const index ){
198 return (_vstate[index]);
199 } ;
200 ¦
201 friend std::ostream & operator << ( std::ostream & os, const Pstates & obj )
202 {
203 for(auto state_pair : obj.vstate() )
204 std::cout << std::endl << "state pair" << state_pair;
205 return os;
206 }
207
208
209 protected:
210
211 private:
212 std::vector< STATE <cost_type> > _vstate;
213
214 };
215
216
217
218
219
220
221
222
223
224 template < class T >
225 class CRAWLER
226 {
227 public:
228
229
230 CRAWLER ();
231
232 CRAWLER (NODE<T> start):pself{&start}, pleft{nullptr}, pright{nullptr}, pparent{nullptr}{
233 if(start.cl() != nullptr)
234 right(start.cl() );
235 if(start.cl() !=nullptr)
236 left(start.cr() );
237 if(start.p() != nullptr)
238 parent( start.p() );
239 };
240
241 CRAWLER(NODE<T> * self, NODE<T> *left = nullptr, NODE<T> *right = nullptr, NODE<T> *parent = nullptr ): pself{self}, pleft{left}, pright{right}, pparent{parent} {
242 self->cr(right);
243 self->cl(left);
244 self->p(parent);
245 };
246 CRAWLER ( const CRAWLER &other );
247 ~CRAWLER ();
248
249
250 NODE<T> * left(){
251 return pleft;
252 }
253 NODE<T> * right(){
254 return pright;
255 }
256 NODE<T> * parent(){
257 return pparent;
258 }
259 NODE<T> * left(NODE<T> *in){
260 return pleft = in;
261 }
262 NODE<T> * right(NODE<T> *in){
263 return pright = in;
264 }
265 NODE<T> * parent(NODE<T> *in){
266 return pparent = in;
267 }
268
269
270
271
272
273 const CRAWLER& operator = ( const CRAWLER &other );
274
275
276 protected:
277 NODE<T> *pleft;
278 NODE<T> *pright;
279 NODE<T> *pparent;
280 NODE<T> *pself;
281
282 private:
283
284
285 };
286
287
288 }
289 #endif