00001 //---------------------------------------------------------------------------- 00002 /** @file SgIncrementalStack.h 00003 Incremental Update Stack for fast undo during search. */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SG_INCREMENTALSTACK_H 00007 #define SG_INCREMENTALSTACK_H 00008 00009 #include "SgBoardColor.h" 00010 #include "SgVector.h" 00011 #include "SgPoint.h" 00012 00013 class SgPointSet; 00014 class SgBWSet; 00015 00016 //---------------------------------------------------------------------------- 00017 00018 /** constants for types of stack entries */ 00019 enum SgIncrementalStackEvent 00020 { 00021 SG_NEW_POINTS = 1000, 00022 SG_ADD_EMPTY = 1001, 00023 SG_NEW_SAFE = 1002, 00024 SG_NEW_UNSAFE = 1003, 00025 SG_UNSAFE_TO_SAFE = 1004, 00026 SG_CAPTURES = 1005, 00027 SG_INCREMENTAL_MOVE = 1006, 00028 SG_NEXTMOVE = 1007, 00029 SG_UNSAFE_TO_HALF_SAFE = 1008, 00030 SG_CAPTURE_HALF_SAFE = 1009 00031 }; 00032 00033 /** Incremental Update Stack for fast undo during search. 00034 Used by GoRegionBoard. */ 00035 class SgIncrementalStack 00036 { 00037 public: 00038 SgIncrementalStack(){} 00039 00040 void Clear(); 00041 00042 bool IsEmpty() const {return m_stack.IsEmpty();} 00043 00044 void PushPts(int type, SgEmptyBlackWhite col, const SgPointSet& pts); 00045 00046 void PushPt(int type, SgEmptyBlackWhite col, SgPoint pt); 00047 00048 void PushPtr(void* ptr) 00049 { 00050 m_stack.PushBack(IntOrPtr(ptr)); 00051 } 00052 00053 void PushPtrEvent(int type, void* ptr); 00054 00055 void PushInt(int i) 00056 { 00057 m_stack.PushBack(IntOrPtr(i)); 00058 } 00059 00060 /** relies on SgPoint == int; add to union if that changes */ 00061 void PushPoint(SgPoint p) 00062 { 00063 m_stack.PushBack(IntOrPtr(p)); 00064 } 00065 00066 void StartMoveInfo(); 00067 00068 SgIncrementalStackEvent PopEvent() 00069 { 00070 return static_cast<SgIncrementalStackEvent>(PopInt()); 00071 } 00072 00073 void* PopPtr() 00074 { 00075 void* p = m_stack.Back().m_ptr; 00076 m_stack.PopBack(); 00077 return p; 00078 } 00079 00080 int PopInt() 00081 { 00082 int i = m_stack.Back().m_int; 00083 m_stack.PopBack(); 00084 return i; 00085 } 00086 00087 /** relies on SgPoint == int; add to union if that changes */ 00088 SgPoint PopPoint() 00089 { 00090 return PopInt(); 00091 } 00092 00093 void SubtractPoints(SgPointSet* set); 00094 00095 void AddPoints(SgPointSet* set); 00096 00097 void SubtractPoints(SgBWSet* set); 00098 00099 void AddPoints(SgBWSet* set); 00100 00101 void SubtractAndAddPoints(SgBWSet* subtractset, SgBWSet* addset); 00102 00103 void SubtractAndAddPoints(SgPointSet* subtractset, SgBWSet* addset); 00104 00105 void SubtractAndAddPoints(SgBWSet* subtractset, SgPointSet* addset); 00106 00107 private: 00108 /** Entry for SgIncrementalStack */ 00109 union IntOrPtr 00110 { 00111 IntOrPtr() 00112 { 00113 } 00114 00115 IntOrPtr(int i) 00116 { 00117 m_int = i; 00118 } 00119 00120 IntOrPtr(void* ptr) 00121 { 00122 m_ptr = ptr; 00123 } 00124 00125 int m_int; 00126 00127 void* m_ptr; 00128 }; 00129 00130 /** Stores incremental state changes for execute/undo moves */ 00131 SgVector<IntOrPtr> m_stack; 00132 }; 00133 00134 //---------------------------------------------------------------------------- 00135 00136 #endif // SG_INCREMENTALSTACK_H