Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef SG_NODE_H
00026 #define SG_NODE_H
00027
00028 #include <string>
00029 #include "SgProp.h"
00030 #include "SgPointSet.h"
00031 #include "SgVector.h"
00032
00033
00034
00035
00036
00037
00038
00039
00040 class SgNode
00041 {
00042 public:
00043 enum Direction {
00044 PREVIOUS,
00045 NEXT,
00046 NEXT_RIGHTMOST,
00047 PREV_DEPTHFIRST,
00048 NEXT_DEPTHFIRST,
00049 PREV_TERMINAL,
00050 NEXT_TERMINAL,
00051 PREV_BRANCH,
00052 NEXT_BRANCH,
00053 LEFT_BROTHER,
00054 RIGHT_BROTHER,
00055 MAIN_BRANCH,
00056 START_OF_GAME,
00057 END_OF_GAME
00058 };
00059
00060 SgNode();
00061
00062 ~SgNode();
00063
00064
00065 SgNode* CopyTree() const;
00066
00067
00068
00069
00070 SgVector<SgPoint> VectorProp(SgPropID prop) const;
00071
00072 bool HasFather() const
00073 {
00074 return (m_father != 0);
00075 }
00076
00077 bool IsRoot() const
00078 {
00079 return (m_father == 0);
00080 }
00081
00082 bool HasLeftBrother() const;
00083
00084 bool HasRightBrother() const
00085 {
00086 return (m_brother != 0);
00087 }
00088
00089 bool HasSon() const
00090 {
00091 return (m_son != 0);
00092 }
00093
00094
00095 bool HasBrother() const
00096 {
00097 return HasLeftBrother() || HasRightBrother();
00098 }
00099
00100
00101
00102 bool IsOnMain() const;
00103
00104
00105 bool IsTerminal() const
00106 {
00107 return (m_son == 0);
00108 }
00109
00110
00111 bool IsBranchPoint() const
00112 {
00113 return (m_son && m_son->m_brother);
00114 }
00115
00116 int NumSons() const;
00117
00118 int NumLeftBrothers() const;
00119
00120 SgNode* Root() const;
00121
00122 SgNode* Father() const
00123 {
00124 return m_father;
00125 }
00126
00127 SgNode* LeftBrother() const;
00128
00129 SgNode* RightBrother() const
00130 {
00131 return m_brother;
00132 }
00133
00134 SgNode* LeftMostSon() const
00135 {
00136 return m_son;
00137 }
00138
00139 SgNode* RightMostSon() const;
00140
00141
00142 SgNode* NextDepthFirst() const;
00143
00144
00145 SgNode* PrevDepthFirst() const;
00146
00147
00148
00149 SgNode* NodeInDirection(Direction dir) const;
00150
00151
00152
00153
00154 bool ContainsText(const std::string& findText);
00155
00156
00157 void PathToRoot(SgVectorOf<SgNode>* path) const;
00158
00159
00160
00161
00162
00163 void ShortestPathTo(SgNode* node, int* numBack,
00164 SgVectorOf<SgNode>* path) const;
00165
00166
00167
00168 void PromoteNode();
00169
00170
00171
00172
00173 void PromotePath();
00174
00175
00176 void DeleteSubtree();
00177
00178
00179 void DeleteBranches();
00180
00181
00182
00183
00184
00185
00186
00187
00188 void DeleteTree();
00189
00190 SgNode* NewFather();
00191
00192 SgNode* NewRightBrother();
00193
00194 SgNode* NewLeftMostSon();
00195
00196
00197 SgNode* NewRightMostSon();
00198
00199
00200 void AppendTo(SgNode* n);
00201
00202
00203
00204 static SgNode* LinkTrees(const SgVectorOf<SgNode>& roots);
00205
00206 SgPropList& Props()
00207 {
00208 return m_props;
00209 }
00210
00211 const SgPropList& Props() const
00212 {
00213 return m_props;
00214 }
00215
00216 void Add(const SgProp* prop)
00217 {
00218
00219
00220 SG_ASSERT(! prop->Flag(SG_PROPCLASS_ROOT) || ! HasFather()
00221 || ! Father()->HasFather());
00222 m_props.Add(prop);
00223 }
00224
00225 SgProp* Get(SgPropID id) const
00226 {
00227 return m_props.Get(id);
00228 }
00229
00230
00231
00232 bool HasProp(SgPropID id) const;
00233
00234
00235 bool HasNodeMove() const;
00236
00237
00238
00239 SgBlackWhite NodePlayer() const;
00240
00241 SgPoint NodeMove() const;
00242
00243
00244
00245
00246
00247 SgNode* TopProp(SgPropID id) const;
00248
00249
00250
00251
00252 int GetIntProp(SgPropID id) const;
00253
00254
00255 bool GetIntProp(SgPropID id, int* value) const;
00256
00257
00258
00259
00260 double GetRealProp(SgPropID id) const;
00261
00262
00263
00264
00265 void SetIntProp(SgPropID id, int value);
00266
00267
00268
00269
00270
00271
00272
00273 void SetRealProp(SgPropID id, double value, int precision = 0);
00274
00275
00276
00277
00278 void SetStringProp(SgPropID id, const std::string& value);
00279
00280
00281
00282
00283 bool GetStringProp(SgPropID id, std::string* value) const;
00284
00285
00286
00287
00288 void SetListProp(SgPropID id, const SgVector<SgPoint>& value);
00289
00290 void SetListProp(SgPropID id, const SgPointSet& value);
00291
00292
00293
00294 void AddComment(const std::string& comment);
00295
00296
00297
00298
00299 SgProp* CopyPropFrom(const SgNode& sourceNode, SgPropID id);
00300
00301
00302 void CopyAllPropsFrom(const SgNode& sourceNode);
00303
00304
00305
00306
00307
00308 SgProp* AddMoveProp(SgMove move, SgBlackWhite player);
00309
00310
00311
00312 SgBlackWhite Player() const;
00313
00314
00315
00316
00317
00318 int CountNodes(bool fSetPropOnThisNode);
00319
00320 static void CopySubtree(const SgNode* node, SgNode* copy);
00321
00322 #ifndef NDEBUG
00323
00324 static void GetStatistics(int* numAlloc, int* numUsed);
00325 #endif
00326
00327 static void MemCheck();
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 static std::string TreeIndex(const SgNode* node);
00347
00348 private:
00349 SgNode* m_son;
00350
00351 SgNode* m_father;
00352
00353 SgNode* m_brother;
00354
00355 SgPropList m_props;
00356
00357 bool m_marked;
00358
00359 void LinkWithBrother(SgNode* node);
00360
00361 void Mark()
00362 {
00363 m_marked = true;
00364 }
00365
00366 void Unmark()
00367 {
00368 m_marked = false;
00369 }
00370
00371 bool IsMarked() const
00372 {
00373 return m_marked;
00374 }
00375
00376
00377 SgNode(const SgNode&);
00378
00379
00380 SgNode& operator=(const SgNode&);
00381
00382 #ifndef NDEBUG
00383 static int s_alloc;
00384
00385 static int s_free;
00386 #endif
00387 };
00388
00389
00390
00391
00392 class SgSonNodeIterator
00393 {
00394 public:
00395 SgSonNodeIterator(SgNode* node)
00396 : m_nextNode(node->LeftMostSon())
00397 { }
00398
00399 void operator++()
00400 {
00401 m_nextNode = m_nextNode->RightBrother();
00402 }
00403
00404 SgNode* operator*() const
00405 {
00406 SG_ASSERT(m_nextNode);
00407 return m_nextNode;
00408 }
00409
00410 operator bool() const
00411 {
00412 return m_nextNode != 0;
00413 }
00414
00415 private:
00416 SgNode* m_nextNode;
00417
00418
00419 SgSonNodeIterator(const SgSonNodeIterator&);
00420
00421
00422 SgSonNodeIterator& operator=(const SgSonNodeIterator&);
00423 };
00424
00425
00426
00427
00428 class SgSonNodeConstIterator
00429 {
00430 public:
00431 SgSonNodeConstIterator(const SgNode* node)
00432 : m_nextNode(node->LeftMostSon())
00433 { }
00434
00435 void operator++()
00436 {
00437 m_nextNode = m_nextNode->RightBrother();
00438 }
00439
00440 const SgNode* operator*() const
00441 {
00442 SG_ASSERT(m_nextNode);
00443 return m_nextNode;
00444 }
00445
00446 operator bool() const
00447 {
00448 return m_nextNode != 0;
00449 }
00450
00451 private:
00452 SgNode* m_nextNode;
00453
00454
00455 SgSonNodeConstIterator(const SgSonNodeConstIterator&);
00456
00457
00458 SgSonNodeConstIterator& operator=(const SgSonNodeConstIterator&);
00459 };
00460
00461
00462
00463
00464 class SgNodeIterator
00465 {
00466 public:
00467
00468
00469
00470
00471 SgNodeIterator(SgNode* rootOfSubtree, bool postOrder = false);
00472
00473
00474
00475 void Abort()
00476 {
00477 m_nextNode = 0;
00478 }
00479
00480 void operator++()
00481 {
00482 SG_ASSERT(m_nextNode);
00483 Next();
00484 }
00485
00486 SgNode* operator*() const
00487 {
00488 SG_ASSERT(m_nextNode);
00489 return m_nextNode;
00490 };
00491
00492 operator bool() const
00493 {
00494 return m_nextNode != 0;
00495 }
00496
00497 private:
00498
00499 bool Next();
00500
00501 bool m_postOrder;
00502
00503 SgNode* const m_rootOfSubtree;
00504
00505 SgNode* m_nextNode;
00506
00507
00508 SgNodeIterator(const SgNodeIterator&);
00509
00510
00511 SgNodeIterator& operator=(const SgNodeIterator&);
00512 };
00513
00514
00515
00516
00517 class SgNodeConstIterator
00518 {
00519 public:
00520
00521
00522
00523
00524 SgNodeConstIterator(const SgNode* rootOfSubtree, bool postOrder = false);
00525
00526
00527 bool Next();
00528
00529
00530
00531 void Abort()
00532 {
00533 m_nextNode = 0;
00534 }
00535
00536 void operator++()
00537 {
00538 SG_ASSERT(m_nextNode);
00539 Next();
00540 }
00541
00542 const SgNode* operator*() const
00543 {
00544 SG_ASSERT(m_nextNode);
00545 return m_nextNode;
00546 };
00547
00548 operator bool() const
00549 {
00550 return m_nextNode != 0;
00551 }
00552
00553 private:
00554 bool m_postOrder;
00555
00556 const SgNode* const m_rootOfSubtree;
00557
00558 const SgNode* m_nextNode;
00559
00560
00561 SgNodeConstIterator(const SgNodeConstIterator&);
00562
00563
00564 SgNodeConstIterator& operator=(const SgNodeConstIterator&);
00565 };
00566
00567
00568
00569 #endif // SG_NODE_H