libkombilo
0.7
|
00001 00026 #ifndef _PATTERN_H_ 00027 #define _PATTERN_H_ 00028 00029 #include <vector> 00030 #include <stack> 00031 #include <fstream> 00032 #include <stdint.h> 00033 #include <sqlite3.h> 00034 #include "boost/unordered_map.hpp" 00035 00036 #include "abstractboard.h" 00037 #include "sgfparser.h" 00038 00039 00040 typedef char* char_p; 00041 typedef int64_t hashtype; 00042 #if (defined(__BORLANDC__) || defined(_MSC_VER)) 00043 const hashtype NOT_HASHABLE = 9223372036854775807i64; 00044 #else 00045 const hashtype NOT_HASHABLE = 9223372036854775807LL; // == (1 << 63) -1; in fact, (1<<64)-1 shoul be OK, too. 00046 #endif 00047 00048 const char NO_CONT = 255; 00049 00050 const int CORNER_NW_PATTERN = 0; 00051 const int CORNER_NE_PATTERN = 1; 00052 const int CORNER_SW_PATTERN = 2; 00053 const int CORNER_SE_PATTERN = 3; 00054 const int SIDE_N_PATTERN = 4; 00055 const int SIDE_W_PATTERN = 5; 00056 const int SIDE_E_PATTERN = 6; 00057 const int SIDE_S_PATTERN = 7; 00058 const int CENTER_PATTERN = 8; 00059 const int FULLBOARD_PATTERN = 9; 00060 00061 const int ALGO_FINALPOS = 1; 00062 const int ALGO_MOVELIST = 2; 00063 const int ALGO_HASH_FULL = 4; 00064 const int ALGO_HASH_CORNER = 8; 00065 // const int ALGO_INTERVALS = 16; 00066 const int ALGO_HASH_CENTER = 32; 00067 const int ALGO_HASH_SIDE = 64; 00068 00069 const int algo_finalpos = 1; 00070 const int algo_movelist = 2; 00071 const int algo_hash_full = 3; 00072 const int algo_hash_corner = 4; 00073 const int algo_intervals = 5; 00074 const int algo_hash_center = 6; 00075 const int algo_hash_side = 7; 00076 00077 char* flipped_sig(int f, char* sig, int boardsize); 00078 char* symmetrize(char* sig, int boardsize); 00079 00080 class SnapshotVector : public std::vector<unsigned char> { 00081 public: 00082 SnapshotVector(); 00083 SnapshotVector(char* c, int size); 00084 00085 void pb_int(int d); 00086 void pb_hashtype(hashtype d); 00087 void pb_int64(int64_t d); 00088 void pb_charp(const char* c, int size); 00089 void pb_char(char c); 00090 void pb_string(std::string s); 00091 void pb_intp(int* p, int size); 00092 00093 int retrieve_int(); 00094 hashtype retrieve_hashtype(); 00095 int64_t retrieve_int64(); 00096 int* retrieve_intp(); 00097 char retrieve_char(); 00098 char* retrieve_charp(); 00099 std::string retrieve_string(); 00100 00101 char* to_charp(); 00102 00103 private: 00104 SnapshotVector::iterator current; 00105 }; 00106 00107 00108 class PatternError { 00109 public: 00110 PatternError(); 00111 }; 00112 00113 class DBError { 00114 public: 00115 DBError(); 00116 }; 00117 00118 class Symmetries { 00119 public: 00120 char* dataX; 00121 char* dataY; 00122 char* dataCS; 00123 char sizeX; 00124 char sizeY; 00125 Symmetries(char sX=0, char sY=0); 00126 ~Symmetries(); 00127 Symmetries(const Symmetries& s); 00128 Symmetries& operator=(const Symmetries& s); 00129 void set(char i, char j, char k, char l, char cs) throw(PatternError); 00130 char getX(char i, char j) throw(PatternError); 00131 char getY(char i, char j) throw(PatternError); 00132 char getCS(char i, char j) throw(PatternError); 00133 char has_key(char i, char j) throw(PatternError); 00134 }; 00135 00183 class Pattern { 00184 public: 00185 int left; // left, right, top, bottom "==" anchors 00186 int right; 00187 int bottom; 00188 int top; 00189 int boardsize; 00190 00191 int sizeX; 00192 int sizeY; 00193 00194 int flip; // used for elements of a patternList 00195 int colorSwitch; // dito 00196 char* initialPos; 00197 char* finalPos; 00198 char* contLabels; 00199 std::vector<MoveNC> contList; 00200 00201 // Pattern constructors 00202 // 00203 // the char* contLabels, if != 0, should have the same size as the pattern, and should 00204 // contain pre-fixed label (which should be re-used when presenting the search results) 00205 // Positions without a given label should contain '.' 00206 // 00207 // Note: the char*'s iPos and CONTLABELS will NOT be free'ed by the Pattern class. 00208 00209 Pattern(); 00210 Pattern(int le, int ri, int to, int bo, int BOARDSIZE, int sX, int sY, const char* iPos, const std::vector<MoveNC>& CONTLIST, const char* CONTLABELS = 0) throw(PatternError); 00211 Pattern(int le, int ri, int to, int bo, int BOARDSIZE, int sX, int sY, const char* iPos) throw(PatternError); 00212 Pattern(int type, int BOARDSIZE, int sX, int sY, const char* iPos, const std::vector<MoveNC>& CONTLIST, const char* CONTLABELS = 0); 00213 Pattern(int type, int BOARDSIZE, int sX, int sY, const char* iPos, const char* CONTLABELS = 0); 00214 Pattern(const Pattern& p); 00215 Pattern(SnapshotVector& snv); 00216 ~Pattern(); 00217 Pattern& operator=(const Pattern& p); 00218 Pattern& copy(const Pattern& p); 00219 00220 char getInitial(int i, int j); 00221 char getFinal(int i, int j); 00222 00223 char BW2XO(char c); 00224 int operator==(const Pattern& p); 00225 std::string printPattern(); 00226 void to_snv(SnapshotVector& snv); 00227 00228 static int flipsX(int i, int x, int y, int XX, int YY); 00229 static int flipsY(int i, int x, int y, int XX, int YY); 00230 static int PatternInvFlip(int i); 00231 static int compose_flips(int i, int j); // returns index of flip "first j, then i" 00232 }; 00233 00234 class Continuation { 00235 public: 00236 int B ; 00237 int W ; 00238 int tB; 00239 int tW; 00240 int wB; 00241 int lB; 00242 int wW; 00243 int lW; 00244 Continuation(); 00245 00246 friend class GameList; 00247 00248 private: 00249 void from_snv(SnapshotVector& snv); 00250 void to_snv(SnapshotVector& snv); 00251 }; 00252 00253 class PatternList { 00254 public: 00255 Pattern pattern; 00256 int fixedColor; 00257 int nextMove; 00258 std::vector<Pattern> data; 00259 std::vector<Symmetries> symmetries; 00260 Continuation* continuations; 00261 int* flipTable; 00262 int special; 00263 00264 PatternList(Pattern& p, int fColor, int nMove) throw (PatternError); 00265 ~PatternList(); 00266 00267 void patternList(); 00268 Pattern get(int i); 00269 int size(); 00270 00271 friend class GameList; 00272 friend class Algo_movelist; 00273 friend class Algo_hash_full; 00274 00275 private: 00276 char* updateContinuations(int orientation, int x, int y, char co, bool tenuki, char winner); 00277 char* sortContinuations(); // and give them names to be used as labels 00278 char invertColor(char co); 00279 }; 00280 00281 class Candidate { 00282 public: 00283 char x; 00284 char y; 00285 char orientation; // == index in corresp patternList 00286 00287 Candidate(char X, char Y, char ORIENTATION); 00288 }; 00289 00290 class Hit { 00291 public: 00292 ExtendedMoveNumber* pos; 00293 char* label; // this does not really contain the label, but rather the position of the continuation move 00294 Hit(ExtendedMoveNumber* POS, char* LABEL); 00295 Hit(SnapshotVector& snv); // takes a SnapshotVector and reads information produced by Hit::to_snv() 00296 ~Hit(); 00297 static bool cmp_pts(Hit* a, Hit* b); 00298 void to_snv(SnapshotVector& snv); 00299 }; 00300 00301 00302 00303 00304 #endif 00305