| 1 | // File: search.h |
|---|
| 2 | // part of libkombilo, http://www.u-go.net/kombilo/ |
|---|
| 3 | |
|---|
| 4 | // Copyright (c) 2006 Ulrich Goertz <u@g0ertz.de> |
|---|
| 5 | |
|---|
| 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy of |
|---|
| 7 | // this software and associated documentation files (the "Software"), to deal in |
|---|
| 8 | // the Software without restriction, including without limitation the rights to |
|---|
| 9 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|---|
| 10 | // of the Software, and to permit persons to whom the Software is furnished to do |
|---|
| 11 | // so, subject to the following conditions: |
|---|
| 12 | // |
|---|
| 13 | // The above copyright notice and this permission notice shall be included in all |
|---|
| 14 | // copies or substantial portions of the Software. |
|---|
| 15 | // |
|---|
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 22 | // SOFTWARE. |
|---|
| 23 | |
|---|
| 24 | #ifndef _SEARCH_H_ |
|---|
| 25 | #define _SEARCH_H_ |
|---|
| 26 | |
|---|
| 27 | #include <vector> |
|---|
| 28 | #include <utility> |
|---|
| 29 | #include <stack> |
|---|
| 30 | #include <sqlite3.h> |
|---|
| 31 | #include "abstractboard.h" |
|---|
| 32 | #include "sgfparser.h" |
|---|
| 33 | typedef char* char_p; |
|---|
| 34 | |
|---|
| 35 | #if (defined(__BORLANDC__) || defined(_MSC_VER)) |
|---|
| 36 | typedef __int64 hashtype; |
|---|
| 37 | const hashtype NOT_HASHABLE = 9223372036854775807i64; |
|---|
| 38 | #else |
|---|
| 39 | typedef long long hashtype; |
|---|
| 40 | const hashtype NOT_HASHABLE = 9223372036854775807LL; |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | const char NO_CONT = 255; |
|---|
| 44 | |
|---|
| 45 | class SnapshotVector : public std::vector<unsigned char> { |
|---|
| 46 | public: |
|---|
| 47 | SnapshotVector(); |
|---|
| 48 | SnapshotVector(char* c, int size); |
|---|
| 49 | |
|---|
| 50 | void pb_int(int d); |
|---|
| 51 | void pb_charp(char* c, int size); |
|---|
| 52 | void pb_char(char c); |
|---|
| 53 | void pb_string(std::string s); |
|---|
| 54 | void pb_intp(int* p, int size); |
|---|
| 55 | |
|---|
| 56 | int retrieve_int(); |
|---|
| 57 | int* retrieve_intp(); |
|---|
| 58 | char retrieve_char(); |
|---|
| 59 | char* retrieve_charp(); |
|---|
| 60 | std::string retrieve_string(); |
|---|
| 61 | |
|---|
| 62 | char* to_charp(); |
|---|
| 63 | |
|---|
| 64 | private: |
|---|
| 65 | SnapshotVector::iterator current; |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | class PatternError { |
|---|
| 70 | public: |
|---|
| 71 | PatternError(); |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | class DBError { |
|---|
| 75 | public: |
|---|
| 76 | DBError(); |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | class Symmetries { |
|---|
| 80 | public: |
|---|
| 81 | char* dataX; |
|---|
| 82 | char* dataY; |
|---|
| 83 | char* dataCS; |
|---|
| 84 | char sizeX; |
|---|
| 85 | char sizeY; |
|---|
| 86 | Symmetries(char sX=0, char sY=0); |
|---|
| 87 | ~Symmetries(); |
|---|
| 88 | Symmetries(const Symmetries& s); |
|---|
| 89 | Symmetries& operator=(const Symmetries& s); |
|---|
| 90 | void set(char i, char j, char k, char l, char cs) throw(PatternError); |
|---|
| 91 | char getX(char i, char j) throw(PatternError); |
|---|
| 92 | char getY(char i, char j) throw(PatternError); |
|---|
| 93 | char getCS(char i, char j) throw(PatternError); |
|---|
| 94 | char has_key(char i, char j) throw(PatternError); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | const int CORNER_NW_PATTERN = 0; |
|---|
| 98 | const int CORNER_NE_PATTERN = 1; |
|---|
| 99 | const int CORNER_SW_PATTERN = 2; |
|---|
| 100 | const int CORNER_SE_PATTERN = 3; |
|---|
| 101 | const int SIDE_N_PATTERN = 4; |
|---|
| 102 | const int SIDE_W_PATTERN = 5; |
|---|
| 103 | const int SIDE_E_PATTERN = 6; |
|---|
| 104 | const int SIDE_S_PATTERN = 7; |
|---|
| 105 | const int CENTER_PATTERN = 8; |
|---|
| 106 | const int FULLBOARD_PATTERN = 9; |
|---|
| 107 | |
|---|
| 108 | class Pattern { |
|---|
| 109 | public: |
|---|
| 110 | int left; // left, right, top, bottom "==" anchors |
|---|
| 111 | int right; |
|---|
| 112 | int bottom; |
|---|
| 113 | int top; |
|---|
| 114 | int boardsize; |
|---|
| 115 | |
|---|
| 116 | int sizeX; |
|---|
| 117 | int sizeY; |
|---|
| 118 | |
|---|
| 119 | int flip; // used for elements of a patternList |
|---|
| 120 | int colorSwitch; // dito |
|---|
| 121 | char* initialPos; |
|---|
| 122 | char* finalPos; |
|---|
| 123 | char* contLabels; |
|---|
| 124 | std::vector<MoveNC> contList; |
|---|
| 125 | |
|---|
| 126 | // Pattern constructors |
|---|
| 127 | // |
|---|
| 128 | // the char* contLabels, if != 0, should have the same size as the pattern, and should |
|---|
| 129 | // contain pre-fixed label (which should be re-used when presenting the search results) |
|---|
| 130 | // Positions without a given label should contain '.' |
|---|
| 131 | // |
|---|
| 132 | // Note: the char*'s iPos and CONTLABELS will NOT be free'ed by the Pattern class. |
|---|
| 133 | |
|---|
| 134 | Pattern(); |
|---|
| 135 | Pattern(int le, int ri, int to, int bo, int BOARDSIZE, int sX, int sY, char* iPos, const std::vector<MoveNC>& CONTLIST, char* CONTLABELS = 0) throw(PatternError); |
|---|
| 136 | Pattern(int type, int BOARDSIZE, int sX, int sY, char* iPos, std::vector<MoveNC> CONTLIST, char* CONTLABELS = 0); |
|---|
| 137 | Pattern(int type, int BOARDSIZE, int sX, int sY, char* iPos, char* CONTLABELS = 0); |
|---|
| 138 | Pattern(const Pattern& p); |
|---|
| 139 | Pattern(SnapshotVector& snv); |
|---|
| 140 | ~Pattern(); |
|---|
| 141 | Pattern& operator=(const Pattern& p); |
|---|
| 142 | Pattern& copy(const Pattern& p); |
|---|
| 143 | |
|---|
| 144 | char getInitial(int i, int j); |
|---|
| 145 | char getFinal(int i, int j); |
|---|
| 146 | |
|---|
| 147 | char BW2XO(char c); |
|---|
| 148 | int operator==(const Pattern& p); |
|---|
| 149 | std::string printPattern(); |
|---|
| 150 | void to_snv(SnapshotVector& snv); |
|---|
| 151 | |
|---|
| 152 | static int flipsX(int i, int x, int y, int XX, int YY); |
|---|
| 153 | static int flipsY(int i, int x, int y, int XX, int YY); |
|---|
| 154 | static int PatternInvFlip(int i); |
|---|
| 155 | static int compose_flips(int i, int j); // returns index of flip "first j, then i" |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | class Continuation { |
|---|
| 159 | public: |
|---|
| 160 | int B ; // number of all black continuations |
|---|
| 161 | int W ; |
|---|
| 162 | int tB; // black tenuki |
|---|
| 163 | int tW; |
|---|
| 164 | int wB; // black wins (where cont. is B) |
|---|
| 165 | int lB; // black loses (where cont. is B) |
|---|
| 166 | int wW; // black wins (where cont. is W) |
|---|
| 167 | int lW; // black loses (where cont. is W) |
|---|
| 168 | Continuation(); |
|---|
| 169 | void from_snv(SnapshotVector& snv); |
|---|
| 170 | void to_snv(SnapshotVector& snv); |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | class PatternList { |
|---|
| 174 | public: |
|---|
| 175 | Pattern pattern; |
|---|
| 176 | int fixedColor; // allow switching colors |
|---|
| 177 | int nextMove; // 1: next must be black, 2: next must be white |
|---|
| 178 | std::vector<Pattern> data; |
|---|
| 179 | std::vector<Symmetries> symmetries; |
|---|
| 180 | Continuation* continuations; |
|---|
| 181 | int* flipTable; |
|---|
| 182 | int special; |
|---|
| 183 | |
|---|
| 184 | PatternList(Pattern& p, int fColor, int nMove) throw (PatternError); |
|---|
| 185 | ~PatternList(); |
|---|
| 186 | |
|---|
| 187 | char invertColor(char co); |
|---|
| 188 | void patternList(); |
|---|
| 189 | Pattern get(int i); |
|---|
| 190 | int size(); |
|---|
| 191 | char* updateContinuations(int orientation, int x, int y, char co, bool tenuki, char winner); |
|---|
| 192 | char* sortContinuations(); // and give them names to be used as labels |
|---|
| 193 | }; |
|---|
| 194 | |
|---|
| 195 | class Candidate { |
|---|
| 196 | public: |
|---|
| 197 | char x; |
|---|
| 198 | char y; |
|---|
| 199 | char orientation; // == index in corresp patternList |
|---|
| 200 | |
|---|
| 201 | Candidate(char X, char Y, char ORIENTATION); |
|---|
| 202 | }; |
|---|
| 203 | |
|---|
| 204 | class Hit { |
|---|
| 205 | public: |
|---|
| 206 | ExtendedMoveNumber* pos; |
|---|
| 207 | char* label; // this does not really contain the label, but rather the position of the continuation move |
|---|
| 208 | Hit(ExtendedMoveNumber* POS, char* LABEL); |
|---|
| 209 | Hit(SnapshotVector& snv); // takes a SnapshotVector and reads information produced by Hit::to_snv() |
|---|
| 210 | ~Hit(); |
|---|
| 211 | static bool cmp_pts(Hit* a, Hit* b); |
|---|
| 212 | void to_snv(SnapshotVector& snv); |
|---|
| 213 | }; |
|---|
| 214 | |
|---|
| 215 | class GameList; |
|---|
| 216 | class SearchOptions; |
|---|
| 217 | |
|---|
| 218 | class Algorithm { |
|---|
| 219 | public: |
|---|
| 220 | Algorithm(int bsize); |
|---|
| 221 | virtual ~Algorithm(); |
|---|
| 222 | |
|---|
| 223 | virtual void initialize_process(sqlite3* DB); |
|---|
| 224 | virtual void newgame_process(int game_id); |
|---|
| 225 | virtual void AB_process(int x, int y); |
|---|
| 226 | virtual void AW_process(int x, int y); |
|---|
| 227 | virtual void AE_process(int x, int y, char removed); |
|---|
| 228 | virtual void endOfNode_process(); |
|---|
| 229 | virtual void move_process(Move m); |
|---|
| 230 | virtual void pass_process(); |
|---|
| 231 | virtual void branchpoint_process(); |
|---|
| 232 | virtual void endOfVariation_process(); |
|---|
| 233 | virtual void endgame_process(bool commit=true); |
|---|
| 234 | virtual void finalize_process(); |
|---|
| 235 | virtual int readDB(sqlite3* DB); |
|---|
| 236 | virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options); |
|---|
| 237 | |
|---|
| 238 | int gid; |
|---|
| 239 | int boardsize; |
|---|
| 240 | sqlite3* db; |
|---|
| 241 | }; |
|---|
| 242 | |
|---|
| 243 | class Algo_signature : public Algorithm { |
|---|
| 244 | public: |
|---|
| 245 | Algo_signature(int bsize); |
|---|
| 246 | ~Algo_signature(); |
|---|
| 247 | void initialize_process(sqlite3* DB) throw(DBError); |
|---|
| 248 | void newgame_process(int game_id); |
|---|
| 249 | void AB_process(int x, int y); |
|---|
| 250 | void AW_process(int x, int y); |
|---|
| 251 | void AE_process(int x, int y, char removed); |
|---|
| 252 | void endOfNode_process(); |
|---|
| 253 | void move_process(Move m); |
|---|
| 254 | void pass_process(); |
|---|
| 255 | void branchpoint_process(); |
|---|
| 256 | void endOfVariation_process(); |
|---|
| 257 | void endgame_process(bool commit=true) throw(DBError); |
|---|
| 258 | void finalize_process(); |
|---|
| 259 | |
|---|
| 260 | int counter; |
|---|
| 261 | char* signature; |
|---|
| 262 | char* get_current_signature(); |
|---|
| 263 | std::vector<int> search_signature(char* sig); |
|---|
| 264 | private: |
|---|
| 265 | bool main_variation; |
|---|
| 266 | }; |
|---|
| 267 | |
|---|
| 268 | class Algo_finalpos : public Algorithm { |
|---|
| 269 | public: |
|---|
| 270 | Algo_finalpos(int bsize); |
|---|
| 271 | ~Algo_finalpos(); |
|---|
| 272 | void initialize_process(sqlite3* DB) throw(DBError); |
|---|
| 273 | void newgame_process(int game_id); |
|---|
| 274 | void AB_process(int x, int y); |
|---|
| 275 | void AW_process(int x, int y); |
|---|
| 276 | void AE_process(int x, int y, char removed); |
|---|
| 277 | void endOfNode_process(); |
|---|
| 278 | void move_process(Move m); |
|---|
| 279 | void pass_process(); |
|---|
| 280 | void branchpoint_process(); |
|---|
| 281 | void endOfVariation_process(); |
|---|
| 282 | void endgame_process(bool commit=true) throw(DBError); |
|---|
| 283 | void finalize_process(); |
|---|
| 284 | |
|---|
| 285 | char* fp; |
|---|
| 286 | int fpIndex; |
|---|
| 287 | std::map<int, char* > *data; |
|---|
| 288 | int readDB(sqlite3* DB); |
|---|
| 289 | int search(PatternList& patternList, GameList& gl, SearchOptions& options); |
|---|
| 290 | |
|---|
| 291 | bool equal(unsigned int id1, unsigned int id2); // id1, id2 refer to id's in the database! |
|---|
| 292 | bool equals_current(unsigned int id1); |
|---|
| 293 | }; |
|---|
| 294 | |
|---|
| 295 | // in x-coord: |
|---|
| 296 | const int ENDOFNODE = 128; |
|---|
| 297 | const int BRANCHPOINT = 64; |
|---|
| 298 | const int ENDOFVARIATION = 32; |
|---|
| 299 | |
|---|
| 300 | // in y-coord |
|---|
| 301 | const int REMOVE = 128; |
|---|
| 302 | const int BLACK = 64; |
|---|
| 303 | const int WHITE = 32; |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | class MovelistCand { |
|---|
| 307 | public: |
|---|
| 308 | int orientation; |
|---|
| 309 | Pattern* p; |
|---|
| 310 | char* dicts; |
|---|
| 311 | ExtendedMoveNumber dictsF; |
|---|
| 312 | bool dictsFound; |
|---|
| 313 | ExtendedMoveNumber dictsFI; |
|---|
| 314 | bool dictsFoundInitial; |
|---|
| 315 | bool dictsDR; |
|---|
| 316 | int dictsNO; |
|---|
| 317 | std::vector<MoveNC> contList; |
|---|
| 318 | int contListIndex; |
|---|
| 319 | p_cc Xinterv; |
|---|
| 320 | p_cc Yinterv; |
|---|
| 321 | char mx; |
|---|
| 322 | char my; |
|---|
| 323 | |
|---|
| 324 | MovelistCand(Pattern* P, int ORIENTATION, char* DICTS, int NO, char X, char Y); |
|---|
| 325 | ~MovelistCand(); |
|---|
| 326 | char dictsget(char x, char y); |
|---|
| 327 | void dictsset(char x, char y, char d); |
|---|
| 328 | bool in_relevant_region(char x, char y); |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | class VecMC : public std::vector<MovelistCand* > { |
|---|
| 332 | public: |
|---|
| 333 | VecMC(); |
|---|
| 334 | ~VecMC(); |
|---|
| 335 | VecMC* deepcopy(ExtendedMoveNumber& COUNTER, int CANDSSIZE); |
|---|
| 336 | ExtendedMoveNumber counter; |
|---|
| 337 | int candssize; |
|---|
| 338 | }; |
|---|
| 339 | |
|---|
| 340 | class Algo_movelist : public Algorithm { |
|---|
| 341 | public: |
|---|
| 342 | Algo_movelist(int bsize); |
|---|
| 343 | ~Algo_movelist(); |
|---|
| 344 | void initialize_process(sqlite3* DB) throw(DBError); |
|---|
| 345 | void newgame_process(int game_id); |
|---|
| 346 | void AB_process(int x, int y); |
|---|
| 347 | void AW_process(int x, int y); |
|---|
| 348 | void AE_process(int x, int y, char removed); |
|---|
| 349 | void endOfNode_process(); |
|---|
| 350 | void move_process(Move m); |
|---|
| 351 | void pass_process(); |
|---|
| 352 | void branchpoint_process(); |
|---|
| 353 | void endOfVariation_process(); |
|---|
| 354 | void endgame_process(bool commit=true) throw(DBError); |
|---|
| 355 | void finalize_process(); |
|---|
| 356 | int readDB(sqlite3* DB); |
|---|
| 357 | int search(PatternList& patternList, GameList& gl, SearchOptions& options); |
|---|
| 358 | |
|---|
| 359 | std::vector<char> movelist; |
|---|
| 360 | char* fpC; |
|---|
| 361 | std::map<int, char* > *data1; |
|---|
| 362 | std::map<int, char* > *data2; |
|---|
| 363 | std::map<int, int> *data1l; |
|---|
| 364 | }; |
|---|
| 365 | |
|---|
| 366 | class HashFEntry { |
|---|
| 367 | public: |
|---|
| 368 | hashtype hashCode; |
|---|
| 369 | char* buf; |
|---|
| 370 | int length; |
|---|
| 371 | |
|---|
| 372 | HashFEntry(hashtype HASHCODE, char* BUF, int LENGTH); |
|---|
| 373 | HashFEntry(const HashFEntry& hfe); |
|---|
| 374 | ~HashFEntry(); |
|---|
| 375 | }; |
|---|
| 376 | |
|---|
| 377 | class HashhitF { // hashing hit for full board search |
|---|
| 378 | public: |
|---|
| 379 | int gameid; |
|---|
| 380 | char orientation; |
|---|
| 381 | MoveNC* cont; |
|---|
| 382 | ExtendedMoveNumber* emn; |
|---|
| 383 | |
|---|
| 384 | HashhitF(); |
|---|
| 385 | HashhitF(int GAMEID, char ORIENTATION, char* blob); |
|---|
| 386 | ~HashhitF(); |
|---|
| 387 | }; |
|---|
| 388 | |
|---|
| 389 | class HashhitCS { // hasihing hit for corner/side pattern search |
|---|
| 390 | public: |
|---|
| 391 | int gameid; |
|---|
| 392 | int position; |
|---|
| 393 | bool cs; |
|---|
| 394 | HashhitCS(int GAMEID, int POSITION, bool CS); |
|---|
| 395 | }; |
|---|
| 396 | |
|---|
| 397 | class HashVarInfo { |
|---|
| 398 | public: |
|---|
| 399 | hashtype chc; |
|---|
| 400 | std::vector<std::pair<hashtype, ExtendedMoveNumber>* > * lfc; |
|---|
| 401 | ExtendedMoveNumber* moveNumber; |
|---|
| 402 | int numStones; |
|---|
| 403 | |
|---|
| 404 | HashVarInfo(hashtype CHC, std::vector<std::pair<hashtype, ExtendedMoveNumber>* > * LFC, ExtendedMoveNumber* MOVENUMBER, int NUMSTONES); |
|---|
| 405 | }; |
|---|
| 406 | |
|---|
| 407 | class Algo_hash_full : public Algorithm { |
|---|
| 408 | public: |
|---|
| 409 | Algo_hash_full(int bsize, int MAXNUMSTONES = 50); |
|---|
| 410 | ~Algo_hash_full(); |
|---|
| 411 | void initialize_process(sqlite3* DB) throw(DBError); |
|---|
| 412 | void newgame_process(int game_id); |
|---|
| 413 | void AB_process(int x, int y); |
|---|
| 414 | void AW_process(int x, int y); |
|---|
| 415 | void AE_process(int x, int y, char removed); |
|---|
| 416 | void endOfNode_process(); |
|---|
| 417 | void move_process(Move m) throw(DBError); |
|---|
| 418 | void pass_process(); |
|---|
| 419 | void branchpoint_process(); |
|---|
| 420 | void endOfVariation_process() throw(DBError); |
|---|
| 421 | void endgame_process(bool commit=true) throw(DBError); |
|---|
| 422 | void finalize_process(); |
|---|
| 423 | int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); |
|---|
| 424 | |
|---|
| 425 | hashtype compute_hashkey(Pattern& pattern); |
|---|
| 426 | |
|---|
| 427 | int maxNumStones; |
|---|
| 428 | int numStones; |
|---|
| 429 | private: |
|---|
| 430 | hashtype currentHashCode; |
|---|
| 431 | ExtendedMoveNumber* moveNumber; |
|---|
| 432 | std::vector<std::pair<hashtype, ExtendedMoveNumber>* > *lfc; // hash code + move number, still looking for continuation |
|---|
| 433 | std::stack<HashVarInfo>* branchpoints; |
|---|
| 434 | int insert_hash(hashtype hashCode, ExtendedMoveNumber& mn, Move* continuation); |
|---|
| 435 | int insert_all_hashes(); |
|---|
| 436 | std::vector<HashFEntry> hash_vector; |
|---|
| 437 | }; |
|---|
| 438 | |
|---|
| 439 | class HashInstance { |
|---|
| 440 | // When processing sgf games, Algo_hash maintains a list of HashInstance's - |
|---|
| 441 | // those are regions on the board for which hash codes are put into the |
|---|
| 442 | // database |
|---|
| 443 | |
|---|
| 444 | public: |
|---|
| 445 | HashInstance(char X, char Y, char SIZEX, char SIZEY, int BOARDSIZE); |
|---|
| 446 | ~HashInstance(); |
|---|
| 447 | bool inRelevantRegion(char X, char Y); |
|---|
| 448 | |
|---|
| 449 | char xx; // position on the board |
|---|
| 450 | char yy; |
|---|
| 451 | int pos; |
|---|
| 452 | int boardsize; |
|---|
| 453 | char sizeX; // size of the pattern |
|---|
| 454 | char sizeY; |
|---|
| 455 | bool changed; |
|---|
| 456 | |
|---|
| 457 | void initialize(); |
|---|
| 458 | void finalize(); |
|---|
| 459 | void addB(char x, char y); |
|---|
| 460 | void removeB(char x, char y); |
|---|
| 461 | void addW(char x, char y); |
|---|
| 462 | void removeW(char x, char y); |
|---|
| 463 | void bppush(); |
|---|
| 464 | void bppop(); |
|---|
| 465 | std::pair<hashtype,int> cHC(); // returns min(currentHashCode) and corresp. index |
|---|
| 466 | hashtype* currentHashCode; // array of 8 hashtype values (to automatically symmetrize hash codes) |
|---|
| 467 | std::stack<std::pair<hashtype*,int> >* branchpoints; |
|---|
| 468 | int numStones; |
|---|
| 469 | }; |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | class Algo_hash : public Algorithm { |
|---|
| 473 | // This class should not be used by the "end-user" (see Algo_hash_corner and |
|---|
| 474 | // Algo_hash_sides instead) |
|---|
| 475 | |
|---|
| 476 | public: |
|---|
| 477 | Algo_hash(int bsize, const std::string& DBNAMEEXT, int MAXNUMSTONES); |
|---|
| 478 | virtual ~Algo_hash(); |
|---|
| 479 | virtual void initialize_process(sqlite3* DB) throw(DBError); |
|---|
| 480 | virtual void newgame_process(int game_id); |
|---|
| 481 | virtual void AB_process(int x, int y); |
|---|
| 482 | virtual void AW_process(int x, int y); |
|---|
| 483 | virtual void AE_process(int x, int y, char removed); |
|---|
| 484 | virtual void endOfNode_process(); |
|---|
| 485 | virtual void move_process(Move m) throw(DBError); |
|---|
| 486 | virtual void pass_process(); |
|---|
| 487 | virtual void branchpoint_process(); |
|---|
| 488 | virtual void endOfVariation_process(); |
|---|
| 489 | virtual void endgame_process(bool commit=true); |
|---|
| 490 | virtual void finalize_process(); |
|---|
| 491 | virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); |
|---|
| 492 | |
|---|
| 493 | virtual std::pair<hashtype,int> compute_hashkey(PatternList& pl, int CS); |
|---|
| 494 | static const hashtype hashCodes[]; |
|---|
| 495 | std::string dbnameext; |
|---|
| 496 | std::vector<HashInstance>* hi; |
|---|
| 497 | int maxNumStones; |
|---|
| 498 | std::vector<std::pair<hashtype, int> > hash_vector; |
|---|
| 499 | virtual int insert_hash(hashtype hashCod, int pos); |
|---|
| 500 | int insert_all_hashes(); |
|---|
| 501 | }; |
|---|
| 502 | |
|---|
| 503 | class Algo_hash_corner : public Algo_hash { |
|---|
| 504 | public: |
|---|
| 505 | Algo_hash_corner(int bsize, int SIZE=7, int MAXNUMSTONES = 20); |
|---|
| 506 | std::pair<hashtype,int> compute_hashkey(PatternList& pl, int CS); |
|---|
| 507 | int size; |
|---|
| 508 | }; |
|---|
| 509 | |
|---|
| 510 | // class Algo_hash_side : public Algo_hash { |
|---|
| 511 | // public: |
|---|
| 512 | // Algo_hash_side(int bsize, int SIZEX=6, int SIZEY=4); |
|---|
| 513 | // int sizeX; |
|---|
| 514 | // int sizeY; |
|---|
| 515 | // }; |
|---|
| 516 | |
|---|
| 517 | // class UIntervals { |
|---|
| 518 | // public: |
|---|
| 519 | // UIntervals(); |
|---|
| 520 | // |
|---|
| 521 | // int first(); |
|---|
| 522 | // void append(UIntervals interv); |
|---|
| 523 | // void inters(UIntervals uinterv); |
|---|
| 524 | // int isEmpty(); |
|---|
| 525 | // |
|---|
| 526 | // std::vector<pair<int,int>> data; |
|---|
| 527 | // |
|---|
| 528 | // }; |
|---|
| 529 | // |
|---|
| 530 | // |
|---|
| 531 | // class Algo_intervals : public Algorithm { |
|---|
| 532 | // public: |
|---|
| 533 | // Algo_intervals(int bsize); |
|---|
| 534 | // ~Algo_intervals(); |
|---|
| 535 | // |
|---|
| 536 | // std::vector<long> movesArr; |
|---|
| 537 | // std::vector<long> moveIntsArr; |
|---|
| 538 | // |
|---|
| 539 | // std::vector<vector<int>*> moves; |
|---|
| 540 | // |
|---|
| 541 | // int counter; |
|---|
| 542 | // int ignore; |
|---|
| 543 | // }; |
|---|
| 544 | // |
|---|
| 545 | // const int MAXNOMOVES = 16777215; |
|---|
| 546 | // const int FLAG_POINTER = 16777216; |
|---|
| 547 | // const int FLAG_BLACK = 33554432; |
|---|
| 548 | // const int FLAG_WHITE = 67108864; |
|---|
| 549 | |
|---|
| 550 | const int ALGO_FINALPOS = 1; |
|---|
| 551 | const int ALGO_MOVELIST = 2; |
|---|
| 552 | const int ALGO_HASH_FULL = 4; |
|---|
| 553 | const int ALGO_HASH_CORNER = 8; |
|---|
| 554 | const int ALGO_INTERVALS = 16; |
|---|
| 555 | const int ALGO_HASH_CENTER = 32; |
|---|
| 556 | const int ALGO_HASH_SIDE = 64; |
|---|
| 557 | |
|---|
| 558 | const int algo_finalpos = 1; |
|---|
| 559 | const int algo_movelist = 2; |
|---|
| 560 | const int algo_hash_full = 3; |
|---|
| 561 | const int algo_hash_corner = 4; |
|---|
| 562 | const int algo_intervals = 5; |
|---|
| 563 | const int algo_hash_center = 6; |
|---|
| 564 | const int algo_hash_side = 7; |
|---|
| 565 | |
|---|
| 566 | typedef Algorithm* algo_p; |
|---|
| 567 | |
|---|
| 568 | class ProcessOptions { |
|---|
| 569 | public: |
|---|
| 570 | bool processVariations; |
|---|
| 571 | bool sgfInDB; |
|---|
| 572 | std::string rootNodeTags; // a comma-separated list of those SGF tags which should be written to the database |
|---|
| 573 | int algos; // algorithms to be used |
|---|
| 574 | int algo_hash_full_maxNumStones; |
|---|
| 575 | int algo_hash_corner_maxNumStones; |
|---|
| 576 | |
|---|
| 577 | std::string asString(); |
|---|
| 578 | void validate(); |
|---|
| 579 | std::vector<std::string>* SGFTagsAsStrings(); |
|---|
| 580 | |
|---|
| 581 | ProcessOptions(); // sets default values which have to be overwritten |
|---|
| 582 | ProcessOptions(std::string s); |
|---|
| 583 | }; |
|---|
| 584 | |
|---|
| 585 | class SearchOptions { |
|---|
| 586 | public: |
|---|
| 587 | int fixedColor; |
|---|
| 588 | int nextMove; // 0 undetermined, 1 = next move must be black, 2 = next move must be white |
|---|
| 589 | int moveLimit; |
|---|
| 590 | bool trustHashFull; |
|---|
| 591 | bool searchInVariations; |
|---|
| 592 | int algos; |
|---|
| 593 | |
|---|
| 594 | SearchOptions(); |
|---|
| 595 | SearchOptions(int FIXEDCOLOR, int NEXTMOVE, int MOVELIMIT=10000); |
|---|
| 596 | SearchOptions(SnapshotVector& snv); |
|---|
| 597 | void to_snv(SnapshotVector& snv); |
|---|
| 598 | }; |
|---|
| 599 | |
|---|
| 600 | class GameListEntry { |
|---|
| 601 | public: |
|---|
| 602 | int id; // id within the concerning database |
|---|
| 603 | std::string gameInfoStr; |
|---|
| 604 | char winner; |
|---|
| 605 | std::vector<Hit* > * hits; // used for hits |
|---|
| 606 | std::vector<Candidate* > * candidates; // used for candidates |
|---|
| 607 | |
|---|
| 608 | GameListEntry(int ID, char WINNER, std::string GAMEINFOSTR); |
|---|
| 609 | ~GameListEntry(); |
|---|
| 610 | |
|---|
| 611 | void hits_from_snv(SnapshotVector& snv); |
|---|
| 612 | }; |
|---|
| 613 | |
|---|
| 614 | class VarInfo { |
|---|
| 615 | public: |
|---|
| 616 | Node* n; |
|---|
| 617 | abstractBoard* b; |
|---|
| 618 | int i; |
|---|
| 619 | |
|---|
| 620 | VarInfo(Node* N, abstractBoard* B, int I); |
|---|
| 621 | VarInfo(const VarInfo& v); |
|---|
| 622 | ~VarInfo(); |
|---|
| 623 | }; |
|---|
| 624 | |
|---|
| 625 | // process flags (used to determine the behavior for individual games - in contrast to |
|---|
| 626 | // options which apply to the whole GameList and are given in ProcessOptions) |
|---|
| 627 | const int CHECK_FOR_DUPLICATES = 1; // check for duplicates using the signature |
|---|
| 628 | const int CHECK_FOR_DUPLICATES_STRICT = 2; // check for duplicates using the final position |
|---|
| 629 | // (if ALGO_FINAPOS is available) |
|---|
| 630 | const int OMIT_DUPLICATES = 4; |
|---|
| 631 | const int OMIT_GAMES_WITH_SGF_ERRORS = 8; |
|---|
| 632 | |
|---|
| 633 | // process return values |
|---|
| 634 | // 0: SGF error occurred when parsing the "tree structure" (i.e. before parsing the individual nodes) |
|---|
| 635 | // database was not changed |
|---|
| 636 | // n>0: n games were processed, use process_results to access the individual results |
|---|
| 637 | |
|---|
| 638 | // flags used in process_results |
|---|
| 639 | const int UNACCEPTABLE_BOARDSIZE = 1; // (database not changed) |
|---|
| 640 | const int SGF_ERROR = 2; |
|---|
| 641 | // SGF error occurred when playing through the game |
|---|
| 642 | // (and the rest of the concerning variation was not used). |
|---|
| 643 | // Depending on OMIT_GAMES_WITH_SGF_ERRORS, everything before this node (and other variations, |
|---|
| 644 | // if any) was inserted, or the database was not changed. |
|---|
| 645 | const int IS_DUPLICATE = 4; |
|---|
| 646 | const int NOT_INSERTED_INTO_DB = 8; |
|---|
| 647 | const int INDEX_OUT_OF_RANGE = 16; |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | class GameList { |
|---|
| 651 | public: |
|---|
| 652 | char* dbname; |
|---|
| 653 | std::string orderby; |
|---|
| 654 | // constructor receives a FORMAT string; see trac wiki for the syntax to be used |
|---|
| 655 | std::string format1; // extracted from FORMAT; the column list of the sql query to retrieve the games |
|---|
| 656 | std::string format2; // extracted from FORMAT, used as template when inserting the query results into the game list |
|---|
| 657 | int numColumns; |
|---|
| 658 | int processVariations; |
|---|
| 659 | |
|---|
| 660 | std::vector<int> boardsizes; |
|---|
| 661 | std::vector<algo_p> algo_ps; |
|---|
| 662 | std::vector<sqlite3*> algo_dbs; |
|---|
| 663 | std::vector<GameListEntry* > * all; |
|---|
| 664 | std::vector<std::pair<int,int> > * currentList; // pair of game id and position within all |
|---|
| 665 | // (usually sorted w.r.t. second component) |
|---|
| 666 | std::vector<std::pair<int,int> > * oldList; |
|---|
| 667 | int current; |
|---|
| 668 | sqlite3* db; |
|---|
| 669 | int readDBs; |
|---|
| 670 | char* labels; |
|---|
| 671 | Continuation* continuations; |
|---|
| 672 | int num_hits; |
|---|
| 673 | int num_switched; |
|---|
| 674 | int Bwins; |
|---|
| 675 | int Wwins; |
|---|
| 676 | Pattern* mrs_pattern; // most recent search pattern |
|---|
| 677 | SearchOptions* searchOptions; |
|---|
| 678 | // ---------------------------------------------------------------------------- |
|---|
| 679 | // the following methods provide the user interface |
|---|
| 680 | |
|---|
| 681 | // ------- constructor -------------------------------------------------------- |
|---|
| 682 | // p_options will be copied by GameList, so the caller has to free the pointer |
|---|
| 683 | GameList(char* DBNAME, std::string ORDERBY="", std::string FORMAT="", ProcessOptions* p_options=0, int cache=100) throw(DBError); |
|---|
| 684 | |
|---|
| 685 | // ------- processing SGF games (to populate the db) -------------------------- |
|---|
| 686 | void start_processing(int PROCESSVARIATIONS=-1) throw(DBError); |
|---|
| 687 | int process(const char* sgf, const char* path, const char* fn, |
|---|
| 688 | const char* DBTREE = 0, int flags=0) throw(SGFError,DBError); |
|---|
| 689 | int process_results(unsigned int i=0); // result for i-th processed game in most recently processed SGF collection |
|---|
| 690 | void finalize_processing() throw(DBError); |
|---|
| 691 | |
|---|
| 692 | // int remove_game(int index); // TODO |
|---|
| 693 | // int remove_all_current_games(); |
|---|
| 694 | |
|---|
| 695 | // ------- pattern search ----------------------------------------------------- |
|---|
| 696 | // options is copied in the search method (if != 0), so the caller has to free the pointer |
|---|
| 697 | void search(Pattern& pattern, SearchOptions* options = 0) throw(DBError); |
|---|
| 698 | char lookupLabel(char x, char y); |
|---|
| 699 | Continuation lookupContinuation(char x, char y); |
|---|
| 700 | |
|---|
| 701 | // ------- signature search --------------------------------------------------- |
|---|
| 702 | // if boardsize != 0 in sigsearch, then the signature is "symmetrized" with respect |
|---|
| 703 | // to boardsize |
|---|
| 704 | void sigsearch(char* sig, int boardsize) throw(DBError); |
|---|
| 705 | std::string getSignature(int i) throw(DBError); |
|---|
| 706 | |
|---|
| 707 | // ------- game info search --------------------------------------------------- |
|---|
| 708 | void gisearch(char* sql, int complete=0) throw(DBError); |
|---|
| 709 | |
|---|
| 710 | // ------- tagging ------------------------------------------------------------ |
|---|
| 711 | void tagsearch(int tag) throw(DBError); |
|---|
| 712 | void setTag(int tag, int start=0, int end=0) throw(DBError); |
|---|
| 713 | void deleteTag(int tag, int i = -1) throw(DBError); |
|---|
| 714 | std::vector<int> getTags(int i, int tag=0) throw(DBError); // note the order of arguments! |
|---|
| 715 | |
|---|
| 716 | // ------- duplicates --------------------------------------------------------- |
|---|
| 717 | int find_duplicates(int bs, bool strict=false) throw(DBError); // return number of duplicate array |
|---|
| 718 | std::vector<int> retrieve_duplicates_VI(unsigned int i); |
|---|
| 719 | int* retrieve_duplicates_PI(unsigned int i); // same as above, but returns Pointer to Int |
|---|
| 720 | // (an array terminated by -1) |
|---|
| 721 | // The caller must free the pointer himself |
|---|
| 722 | // (before calling find_duplicates again). |
|---|
| 723 | |
|---|
| 724 | // ------- snapshot, restore -------------------------------------------------- |
|---|
| 725 | |
|---|
| 726 | int snapshot() throw(DBError); |
|---|
| 727 | void restore(int handle, bool del) throw(DBError); |
|---|
| 728 | void delete_snapshot(int handle) throw(DBError); |
|---|
| 729 | void delete_all_snapshots() throw(DBError); |
|---|
| 730 | |
|---|
| 731 | // ------- misc --------------------------------------------------------------- |
|---|
| 732 | void reset(); // reset currentList to all |
|---|
| 733 | void resetFormat(std::string ORDERBY="", std::string FORMAT=""); |
|---|
| 734 | int size(); |
|---|
| 735 | int numHits(); |
|---|
| 736 | std::string resultsStr(GameListEntry* gle); |
|---|
| 737 | std::string currentEntryAsString(int i); |
|---|
| 738 | std::vector<std::string> currentEntriesAsStrings(int start=0, int end=0); |
|---|
| 739 | std::string getSGF(int i) throw(DBError); |
|---|
| 740 | std::string getCurrentProperty(int i, std::string tag) throw (DBError); |
|---|
| 741 | |
|---|
| 742 | // ------- list of all players ------------------------------------------------- |
|---|
| 743 | int plSize(); |
|---|
| 744 | std::string plEntry(int i); |
|---|
| 745 | |
|---|
| 746 | // ----------------------------------------------------------------------------- |
|---|
| 747 | // internal methods (called from the algorithm classes) |
|---|
| 748 | ~GameList(); |
|---|
| 749 | int start(); |
|---|
| 750 | int next(); |
|---|
| 751 | int start_sorted(); |
|---|
| 752 | int end_sorted(); |
|---|
| 753 | char getCurrentWinner(); |
|---|
| 754 | std::vector<Candidate* > *getCurrentCandidateList(); |
|---|
| 755 | void makeCurrentCandidate(std::vector<Candidate* > *candidates); |
|---|
| 756 | void makeCurrentHit(std::vector<Hit* > *hits); |
|---|
| 757 | void makeIndexCandidate(int index, std::vector<Candidate* > *candidates); |
|---|
| 758 | void makeIndexHit(int index, std::vector<Hit* > *hits); |
|---|
| 759 | void setCurrentFromIndex(int index); |
|---|
| 760 | int get_current_index(int id, int* start); // returns the index in oldList of the game with game id "id" |
|---|
| 761 | // (if available, otherwise returns -1), |
|---|
| 762 | // use this between start_sorted and end_sorted |
|---|
| 763 | int get_current_index_CL(int id, int start=0); // returns the index in currentList of the game with game id "id" |
|---|
| 764 | // (if available, otherwise returns -1), requires currentList to |
|---|
| 765 | // be sorted wrt first component (see duplicates()) |
|---|
| 766 | |
|---|
| 767 | private: |
|---|
| 768 | void createGamesDB() throw(DBError); |
|---|
| 769 | void readDB() throw(DBError); |
|---|
| 770 | void addAlgos(int bs); |
|---|
| 771 | int posDT; // used when parsing the DT, SZ, BR, WR, HA fields during processing |
|---|
| 772 | int posSZ; |
|---|
| 773 | int posBR; |
|---|
| 774 | int posWR; |
|---|
| 775 | int posHA; |
|---|
| 776 | int SGFtagsSize; |
|---|
| 777 | sqlite3* algo_db1; |
|---|
| 778 | sqlite3* algo_db2; |
|---|
| 779 | ProcessOptions* p_op; |
|---|
| 780 | std::vector<std::string>* SGFtags; |
|---|
| 781 | std::string sql_ins_rnp; // sql string to insert root node properties |
|---|
| 782 | std::vector<std::string> pl; // list of all players |
|---|
| 783 | void readPlayersList() throw(DBError); |
|---|
| 784 | std::vector<std::vector<int> >* duplicates; |
|---|
| 785 | void insert_duplicate(int i1, int i2, std::vector<std::vector<int> >* dupl); |
|---|
| 786 | std::vector<int> process_results_vector; |
|---|
| 787 | }; |
|---|
| 788 | |
|---|
| 789 | const int HANDI_TAG = 1; |
|---|
| 790 | const int PROFESSIONAL_TAG = 2; |
|---|
| 791 | |
|---|
| 792 | #endif |
|---|
| 793 | |
|---|