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