Changeset 249 for 06/libkombilo-branches/hash_center
- Timestamp:
- 04/07/07 14:37:43 (1 year ago)
- Files:
-
- 06/libkombilo-branches/hash_center/abstractboard.cpp (modified) (4 diffs)
- 06/libkombilo-branches/hash_center/abstractboard.h (modified) (1 diff)
- 06/libkombilo-branches/hash_center/cpptest.cpp (modified) (1 diff)
- 06/libkombilo-branches/hash_center/search.cpp (modified) (40 diffs)
- 06/libkombilo-branches/hash_center/search.h (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo-branches/hash_center/abstractboard.cpp
r246 r249 164 164 void abstractBoard::clear() { 165 165 for(int i=0; i<boardsize*boardsize; i++) status[i]=' '; 166 undostack = stack<Move>(); 166 undostack = stack<Move>(); 167 167 } 168 168 … … 183 183 undostack.push(m); 184 184 return 1; 185 } 185 } 186 186 return 0; 187 187 } … … 304 304 Move tuple = undostack.top(); 305 305 undostack.pop(); 306 306 307 307 char color = tuple.color; 308 308 vector<p_cc>* captures = tuple.captures; … … 318 318 } 319 319 } 320 } 320 } 321 321 } 322 322 06/libkombilo-branches/hash_center/abstractboard.h
r191 r249 28 28 #include <utility> 29 29 #include <stack> 30 31 const int DEBUG_ABSTRACTBOARD = 0;32 30 33 31 class BoardError { 06/libkombilo-branches/hash_center/cpptest.cpp
r243 r249 151 151 152 152 // ------------------- check for duplicates --------------------------------- 153 // gl.reset(); 154 // int nd = gl.find_duplicates(19); 155 // printf("duplicates:\n"); 156 // for(int i=0; i<nd; i++) { 157 // // 1st method: retrieve_duplicates_VI 158 // // vector<int> dupl_vector = gl.retrieve_duplicates_VI(i); 159 // // for(vector<int>::iterator it = dupl_vector.begin(); it != dupl_vector.end(); it++) { 160 // // printf("%s%s\n", gl.currentEntryAsString(*it).c_str(), gl.getSignature(*it).c_str()); 161 // // } 162 // 163 // // 2nd method: retrieve_duplicates_PI 164 // int * dupl_vector = gl.retrieve_duplicates_PI(i); 165 // int j = 0; 166 // while(dupl_vector[j] != -1) { 167 // printf("%s%s\n", gl.currentEntryAsString(dupl_vector[j]).c_str(), gl.getSignature(dupl_vector[j]).c_str()); 168 // j++; 169 // } 170 // delete [] dupl_vector; 171 172 // printf("--------------------------------------------------- \n"); 173 // } 174 175 // ------------------- snapshot --------------------------------------------- 176 177 gl.delete_all_snapshots(); 178 int handle = gl.snapshot(); 179 printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 180 153 181 gl.reset(); 154 int nd = gl.find_duplicates(19); 155 printf("duplicates:\n"); 156 for(int i=0; i<nd; i++) { 157 // 1st method: retrieve_duplicates_VI 158 // vector<int> dupl_vector = gl.retrieve_duplicates_VI(i); 159 // for(vector<int>::iterator it = dupl_vector.begin(); it != dupl_vector.end(); it++) { 160 // printf("%s%s\n", gl.currentEntryAsString(*it).c_str(), gl.getSignature(*it).c_str()); 161 // } 162 163 // 2nd method: retrieve_duplicates_PI 164 int * dupl_vector = gl.retrieve_duplicates_PI(i); 165 int j = 0; 166 while(dupl_vector[j] != -1) { 167 printf("%s%s\n", gl.currentEntryAsString(dupl_vector[j]).c_str(), gl.getSignature(dupl_vector[j]).c_str()); 168 j++; 169 } 170 delete [] dupl_vector; 182 printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 171 183 172 printf("--------------------------------------------------- \n");173 }184 gl.restore(handle, true); 185 printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 174 186 175 187 // ------------------- resetFormat ------------------------------------------ 06/libkombilo-branches/hash_center/search.cpp
r246 r249 45 45 #endif 46 46 47 SnapshotVector::SnapshotVector() : vector<unsigned char>() { 48 current = begin(); 49 } 50 51 SnapshotVector::SnapshotVector(char* c, int size) : vector<unsigned char>() { 52 for(int i=0; i<size; i++) push_back(c[i]); 53 current = begin(); 54 } 55 56 void SnapshotVector::pb_int(int d) { 57 for(int i = 0; i < 4; i++) { 58 push_back((unsigned char)(d % 256)); 59 d = d >> 8; 60 } 61 } 62 63 void SnapshotVector::pb_charp(char* c, int size) { 64 pb_int(size); 65 for(int i=0; i<size; i++) push_back(c[i]); 66 } 67 68 void SnapshotVector::pb_intp(int* p, int size) { 69 pb_int(size); 70 for(int i=0; i<size; i++) pb_int(p[i]); 71 } 72 73 void SnapshotVector::pb_string(string s) { 74 pb_int(s.size()+1); 75 for(unsigned int i=0; i<s.size(); i++) push_back(s[i]); 76 push_back(0); 77 } 78 79 void SnapshotVector::pb_char(char c) { 80 push_back(c); 81 } 82 83 int SnapshotVector::retrieve_int() { 84 int result = 0; 85 for(int i=0; i<4; i++) { 86 result += *current << (i*8); 87 current++; 88 } 89 return result; 90 } 91 92 int* SnapshotVector::retrieve_intp() { 93 int sz = retrieve_int(); 94 int* result = new int[sz]; 95 for(int i=0; i<sz; i++) 96 result[i] = retrieve_int(); 97 return result; 98 } 99 100 char SnapshotVector::retrieve_char() { 101 char c = *current; 102 current++; 103 return c; 104 } 105 106 char* SnapshotVector::retrieve_charp() { 107 int sz = retrieve_int(); 108 char* result = new char[sz]; 109 for(int i=0; i<sz; i++) { 110 result[i] = *current; 111 current++; 112 } 113 return result; 114 } 115 116 string SnapshotVector::retrieve_string() { 117 char* cp = retrieve_charp(); 118 string s(cp); 119 delete [] cp; 120 return s; 121 } 122 123 char* SnapshotVector::to_charp() { 124 char* result = new char[size()]; 125 int counter = 0; 126 for(SnapshotVector::iterator it = begin(); it != end(); it++) result[counter++] = *it; 127 return result; 128 } 129 130 47 131 PatternError::PatternError() {} 48 132 … … 56 140 wW = 0; 57 141 lW = 0; 142 } 143 144 void Continuation::from_snv(SnapshotVector& snv) { 145 B = snv.retrieve_int(); 146 W = snv.retrieve_int(); 147 tB = snv.retrieve_int(); 148 tW = snv.retrieve_int(); 149 wB = snv.retrieve_int(); 150 lB = snv.retrieve_int(); 151 wW = snv.retrieve_int(); 152 lW = snv.retrieve_int(); 153 } 154 155 void Continuation::to_snv(SnapshotVector& snv) { 156 snv.pb_int(B); 157 snv.pb_int(W); 158 snv.pb_int(tB); 159 snv.pb_int(tW); 160 snv.pb_int(wB); 161 snv.pb_int(lB); 162 snv.pb_int(wW); 163 snv.pb_int(lW); 58 164 } 59 165 … … 306 412 flip = 0; 307 413 colorSwitch = 0; 308 414 309 415 left = le; 310 416 right = ri; … … 328 434 329 435 contList = CONTLIST; 436 } 437 438 Pattern::Pattern(SnapshotVector& snv) { 439 flip = snv.retrieve_int(); 440 colorSwitch = snv.retrieve_int(); 441 left = snv.retrieve_int(); 442 right = snv.retrieve_int(); 443 top = snv.retrieve_int(); 444 bottom = snv.retrieve_int(); 445 boardsize = snv.retrieve_int(); 446 sizeX = snv.retrieve_int(); 447 sizeY = snv.retrieve_int(); 448 if (snv.retrieve_char()) { // contLabels? 449 contLabels = snv.retrieve_charp(); 450 } else contLabels = 0; 451 initialPos = snv.retrieve_charp(); 452 finalPos = snv.retrieve_charp(); 453 454 int size = snv.retrieve_int(); 455 for(int i=0; i<size; i++) 456 contList.push_back(MoveNC(snv.retrieve_char(), snv.retrieve_char(), snv.retrieve_char())); // x, y, color 457 } 458 459 void Pattern::to_snv(SnapshotVector& snv) { 460 snv.pb_int(flip); 461 snv.pb_int(colorSwitch); 462 snv.pb_int(left); 463 snv.pb_int(right); 464 snv.pb_int(top); 465 snv.pb_int(bottom); 466 snv.pb_int(boardsize); 467 snv.pb_int(sizeX); 468 snv.pb_int(sizeY); 469 if (contLabels) { 470 snv.pb_char(1); 471 snv.pb_charp(contLabels, sizeX*sizeY); 472 } else snv.pb_char(0); 473 snv.pb_charp(initialPos, sizeX*sizeY); 474 snv.pb_charp(finalPos, sizeX*sizeY); 475 snv.pb_int(contList.size()); 476 for(vector<MoveNC>::iterator it = contList.begin(); it != contList.end(); it++) { 477 snv.pb_char(it->x); 478 snv.pb_char(it->y); 479 snv.pb_char(it->color); 480 } 330 481 } 331 482 … … 374 525 if (finalPos) delete [] finalPos; 375 526 if (contLabels) delete [] contLabels; 376 527 377 528 initialPos = new char[sizeX*sizeY]; 378 529 finalPos = new char[sizeX*sizeY]; … … 403 554 if (initialPos) delete [] initialPos; 404 555 if (finalPos) delete [] finalPos; 405 556 406 557 initialPos = new char[sizeX*sizeY]; 407 558 finalPos = new char[sizeX*sizeY]; … … 585 736 586 737 void PatternList::patternList() { 587 588 738 vector<Pattern> lCS; 589 739 vector<pair<int,int> > sy; … … 1054 1204 int rc = sqlite3_exec(db, sql, 0, 0, 0); 1055 1205 if (rc != SQLITE_OK) { 1056 // printf("error %d\n", rc);1057 1206 throw DBError(); 1058 1207 } … … 1358 1507 if (rc != SQLITE_OK) throw DBError(); 1359 1508 // printf("init Algo_movelist\n"); 1360 } 1509 } 1361 1510 1362 1511 void Algo_movelist::newgame_process(int game_id) { … … 1367 1516 for(int i=0; i<50; i++) fpC[i] = 0; 1368 1517 } 1369 1518 1370 1519 void Algo_movelist::AB_process(int x, int y) { 1371 1520 movelist.push_back((char)x); … … 1418 1567 } 1419 1568 } 1420 1569 1421 1570 void Algo_movelist::pass_process() { 1422 1571 movelist.push_back(19); … … 1571 1720 return result; 1572 1721 } 1573 1574 1722 1575 1723 int Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) { … … 2191 2339 2192 2340 HashhitF::HashhitF() { 2193 // printf("oops\n");2194 2341 cont = 0; 2195 2342 emn = 0; … … 2321 2468 // printf("leave algo_hash_full::initialize_processing\n"); 2322 2469 } 2323 2470 2324 2471 void Algo_hash_full::newgame_process(int game_id) { 2325 2472 numStones = 0; … … 3970 4117 } 3971 4118 3972 Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { 4119 Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { // LABEL is a char[3] 3973 4120 pos = POS; // note that we do not copy these! 3974 4121 label = LABEL; … … 3978 4125 delete pos; 3979 4126 delete [] label; 4127 } 4128 4129 Hit::Hit(SnapshotVector& snv) { 4130 int length = snv.retrieve_int(); 4131 int* data = snv.retrieve_intp(); 4132 pos = new ExtendedMoveNumber(length, data); 4133 delete [] data; 4134 label = snv.retrieve_charp(); 4135 } 4136 4137 void Hit::to_snv(SnapshotVector& snv) { 4138 snv.pb_int(pos->length); 4139 snv.pb_intp(pos->data, pos->length); 4140 snv.pb_charp(label, 3); 3980 4141 } 3981 4142 … … 4003 4164 searchInVariations = true; 4004 4165 algos = (1<<30) - 1; // use all available algorithms 4166 } 4167 4168 SearchOptions::SearchOptions(SnapshotVector& snv) { 4169 fixedColor = snv.retrieve_int(); 4170 moveLimit = snv.retrieve_int(); 4171 nextMove = snv.retrieve_int(); 4172 trustHashFull = snv.retrieve_int(); 4173 searchInVariations= snv.retrieve_int(); 4174 algos = snv.retrieve_int(); 4175 } 4176 4177 void SearchOptions::to_snv(SnapshotVector& snv) { 4178 snv.pb_int(fixedColor); 4179 snv.pb_int(moveLimit); 4180 snv.pb_int(nextMove); 4181 snv.pb_int(trustHashFull); 4182 snv.pb_int(searchInVariations); 4183 snv.pb_int(algos); 4005 4184 } 4006 4185 … … 4094 4273 for(vector<Hit* >::iterator it = hits->begin(); it != hits->end(); it++) delete *it; 4095 4274 delete hits; 4275 hits = 0; 4096 4276 } 4097 4277 if (candidates) { 4098 4278 for(vector<Candidate* >::iterator it = candidates->begin(); it != candidates->end(); it++) delete *it; 4099 4279 delete candidates; 4100 } 4101 } 4102 4280 candidates = 0; 4281 } 4282 } 4283 4284 void GameListEntry::hits_from_snv(SnapshotVector& snv) { 4285 int h_size = snv.retrieve_int(); 4286 hits = new vector<Hit* >; 4287 for(int j=0; j<h_size; j++) { 4288 hits->push_back(new Hit(snv)); 4289 } 4290 } 4291 4103 4292 int insertEntry(void *gl, int argc, char **argv, char **azColName) { 4104 4293 char winner = '-'; … … 4148 4337 labels = 0; 4149 4338 continuations = 0; 4339 mrs_pattern = 0; 4340 searchOptions = 0; 4150 4341 dbname = new char[strlen(DBNAME)+2]; 4151 4342 strcpy(dbname, DBNAME); … … 4241 4432 if (rc != SQLITE_OK) throw DBError(); 4242 4433 } 4434 4435 // set up snapshot db 4436 rc = sqlite3_exec(db, "create table if not exists snapshots ( data text );", 0, 0, 0); 4437 if (rc != SQLITE_OK) throw DBError(); 4243 4438 4244 4439 // printf("set up Algorithm instances\n"); … … 4341 4536 currentList = 0; 4342 4537 oldList = 0; 4343 4538 4344 4539 int rc; 4345 4540 rc = sqlite3_exec(db, "begin transaction;", 0, 0, 0); … … 4353 4548 rc = sqlite3_exec(db, sql.c_str(), insertEntry, this, 0); 4354 4549 if (rc != SQLITE_OK && rc != SQLITE_ERROR) { 4355 // printf("sql error %d\n", rc);4356 4550 throw DBError(); 4357 4551 } 4358 4552 // printf("read.\n"); 4359 4553 // SQLITE_ERROR may occur since table might not yet exist 4360 4554 4361 4555 readPlayersList(); 4362 4556 rc = sqlite3_exec(db, "commit;", 0, 0, 0); … … 4377 4571 GameList::~GameList() { 4378 4572 // printf("enter ~GameList\n"); 4573 if (mrs_pattern) delete mrs_pattern; 4574 if (searchOptions) delete searchOptions; 4379 4575 if (p_op) delete p_op; 4380 4576 if (labels) delete [] labels; 4381 if (continuations) delete [] continuations; 4577 if (continuations) delete [] continuations; // FIXME CHECK whether the Continuation destructor is invoked! 4382 4578 if (duplicates) delete duplicates; 4383 4579 delete [] dbname; … … 4537 4733 currentList->push_back(make_pair((*it)->id, counter++)); 4538 4734 } 4735 num_hits = 0; 4736 num_switched = 0; 4737 Bwins = 0; 4738 Wwins = 0; 4539 4739 } 4540 4740 4541 4741 void GameList::tagsearch(int tag) throw(DBError) { 4542 4742 char sql[200]; 4543 4743 4544 4744 if (!tag) return; 4545 4745 if (tag > 0) { … … 4623 4823 4624 4824 int GameList::find_duplicates(int bs, bool strict) throw(DBError) { 4825 if (!currentList->size()) return 0; // this also deals with the case of an empty db 4625 4826 int bs_index = 0; 4626 4827 if (duplicates) delete duplicates; … … 4783 4984 4784 4985 char GameList::lookupLabel(char x, char y) { 4785 if (!labels ) return '?';4786 return labels[x+y* sizeX];4986 if (!labels || !mrs_pattern || x < 0 || x >= mrs_pattern->sizeX || y < 0 || y >= mrs_pattern->sizeY) return '?'; 4987 return labels[x+y*mrs_pattern->sizeX]; 4787 4988 } 4788 4989 4789 4990 Continuation GameList::lookupContinuation(char x, char y) { 4790 if (!continuations ) return Continuation();4791 return continuations[x+y* sizeX];4991 if (!continuations || !mrs_pattern || x < 0 || x >= mrs_pattern->sizeX || y < 0 || y >= mrs_pattern->sizeY) return Continuation(); 4992 return continuations[x+y*mrs_pattern->sizeX]; 4792 4993 } 4793 4994 … … 4861 5062 4862 5063 void GameList::search(Pattern& pattern, SearchOptions* so) throw(DBError) { 4863 SearchOptions* searchOptions; 5064 if (mrs_pattern) delete mrs_pattern; 5065 mrs_pattern = new Pattern(pattern); 5066 if (searchOptions) delete searchOptions; 4864 5067 if (so) searchOptions = new SearchOptions(*so); 4865 5068 else searchOptions = new SearchOptions(); 4866 sizeX = pattern.sizeX; // need this in lookupLabel4867 5069 PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 4868 5070 // printf("created pl, size: %d\n", pl.size()); … … 4877 5079 if (it == boardsizes.end()) { 4878 5080 delete searchOptions; 5081 searchOptions = 0; 4879 5082 if (oldList) delete oldList; 4880 5083 oldList = 0; 4881 5084 if (currentList) delete currentList; 4882 currentList = 0;5085 currentList = new vector<pair<int,int> >; 4883 5086 return; 4884 5087 } … … 4940 5143 continuations = pl.continuations; 4941 5144 pl.continuations = new Continuation[pattern.sizeX*pattern.sizeY]; 4942 delete searchOptions; 5145 5146 // FIXME: delete all candidate lists! 4943 5147 } 4944 5148 … … 5037 5241 sql1 = "create table if not exists GAME_TAGS ( id integer primary key, game_id integer, tag_id integer );"; 5038 5242 rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 5039 if ( rc!=SQLITE_OK) throw DBError();5243 if (rc != SQLITE_OK) throw DBError(); 5040 5244 } 5041 5245 5042 5246 void GameList::start_processing(int PROCESSVARIATIONS) throw(DBError) { 5043 5247 // printf("enter start_processing %p\n", p_op); 5248 5249 delete_all_snapshots(); 5250 5044 5251 if (PROCESSVARIATIONS != -1) processVariations = PROCESSVARIATIONS; 5045 5252 else processVariations = p_op->processVariations; … … 5144 5351 if (sz=="") sz = "19"; 5145 5352 int bs = atoi(sz.c_str()); 5146 if (bs < 1) { 5147 return_val |= UNACCEPTABLE_BOARDSIZE; 5353 if (bs < 1) { 5354 return_val |= UNACCEPTABLE_BOARDSIZE; 5148 5355 process_results_vector.push_back(return_val); 5149 5356 delete rootNodeProperties; … … 5242 5449 int rc = sqlite3_prepare(db, sql_ins_rnp.c_str(), -1, &ppStmt, 0); 5243 5450 if (rc != SQLITE_OK || ppStmt==0) { 5244 // printf("db error %d\n", rc);5245 5451 throw DBError(); // FIXME: catch busy error, (and/or throw DBError?) 5246 5452 } … … 5295 5501 // printf("nn\n"); 5296 5502 bool caughtSGFError = false; 5503 char* propValue = 0; 5504 5297 5505 try { 5298 5506 5299 5507 // parse current node, watch out for B, W, AB, AW, AE properties 5300 5301 5508 const char* s = currentN->SGFstring.c_str(); 5302 5509 int lSGFstring = strlen(s); … … 5333 5540 ID[IDindex] = 0; // found next property ID 5334 5541 bool IDrelevant= (!strcmp(ID,"B") || !strcmp(ID,"W") || !strcmp(ID,"AB") || !strcmp(ID,"AW") || !strcmp(ID,"AE")); 5335 char* propValue = new char[lSGFstring+1];5542 propValue = new char[100000]; 5336 5543 int propValueIndex = 0; 5337 5544 int oldPropValueIndex = 0; … … 5346 5553 if (97 <= s[i] && s[i] <= 96+bs) { // valid board coordinate? 5347 5554 propValue[propValueIndex++] = s[i]; 5555 if (propValueIndex > 99990) throw SGFError(); 5348 5556 } else if (s[i] == 't') { ; // allow passes, but do not record them (we handle them a little sloppily here) 5557 } else if (s[i] == ':') { 5558 if (propValueIndex - oldPropValueIndex != 2) 5559 throw SGFError(); 5560 char rect1 = 'a'; 5561 char rect2 = 'a'; 5562 i++; 5563 while (i<lSGFstring && (s[i] == '\t' || s[i] == ' ' || s[i] == '\r' || s[i] == '\n')) i++; 5564 if (i >= lSGFstring) throw SGFError(); 5565 if (97 <= s[i] && s[i] <= 96+bs) // valid board coordinate? 5566 rect1 = s[i]; 5567 else throw SGFError(); 5568 i++; 5569 while (i<lSGFstring && (s[i] == '\t' || s[i] == ' ' || s[i] == '\r' || s[i] == '\n')) i++; 5570 if (i >= lSGFstring) throw SGFError(); 5571 if (97 <= s[i] && s[i] <= 96+bs) // valid board coordinate? 5572 rect2 = s[i]; 5573 else throw SGFError(); 5574 i++; 5575 while (i<lSGFstring && (s[i] == '\t' || s[i] == ' ' || s[i] == '\r' || s[i] == '\n')) i++; 5576 if (i >= lSGFstring) throw SGFError(); 5577 if (s[i] == ']') { 5578 char st1 = propValue[propValueIndex-2]; 5579 char st2 = propValue[propValueIndex-1]; 5580 propValueIndex -= 2; // do not want to have the first entry twice! 5581 for(char x1 = st1; x1 <= rect1; x1++) { 5582 for(char x2 = st2; x2 <= rect2; x2++) { 5583 propValue[propValueIndex++] = x1; 5584 propValue[propValueIndex++] = x2; 5585 if (propValueIndex > 99990) throw SGFError(); 5586 } 5587 } 5588 oldPropValueIndex = propValueIndex; 5589 break; 5590 } else throw SGFError(); 5349 5591 } else { 5350 5592 throw SGFError(); … … 5353 5595 } 5354 5596 if (i >= lSGFstring) throw SGFError(); 5355 if (propValueIndex - oldPropValueIndex != 0 && propValueIndex - oldPropValueIndex != 2) 5597 5598 if (propValueIndex - oldPropValueIndex != 0 && propValueIndex - oldPropValueIndex != 2) { 5356 5599 throw SGFError(); 5600 } 5357 5601 oldPropValueIndex = propValueIndex; 5358 5602 … … 5371 5615 } 5372 5616 delete [] propValue; 5617 propValue = 0; 5373 5618 continue; 5374 5619 } … … 5415 5660 } 5416 5661 delete [] propValue; 5662 propValue = 0; 5417 5663 } 5418 5664 } catch (SGFError) { 5665 if (propValue) { 5666 delete [] propValue; 5667 propValue = 0; 5668 } 5419 5669 return_val |= SGF_ERROR; 5420 5670 caughtSGFError = true; … … 5477 5727 delete [] sig; 5478 5728 } 5479 5729 5480 5730 if (commit) { 5481 5731 // evaluate tags … … 5521 5771 5522 5772 5773 int GameList::snapshot() throw(DBError) { 5774 // return a handle to a snapshot stored in the main GameList db 5775 // the snapshot contains copies of 5776 // - orderby, format1, format2 5777 // - currentList 5778 // - all hits in the GameListEntry's of currentList 5779 // - pattern, labels, continuations, num_hits, num_switched, Bwins, Wwins 5780 5781 SnapshotVector snapshot; 5782 snapshot.pb_string(orderby); 5783 snapshot.pb_string(format1); 5784 snapshot.pb_string(format2); 5785 5786 snapshot.pb_int(currentList->size()); 5787 for(vector<pair<int,int> >::iterator it = currentList->begin(); it != currentList->end(); it++) { 5788 snapshot.pb_int(it->first); 5789 snapshot.pb_int(it->second); 5790 vector<Hit* >* hits = (*all)[it->second]->hits; 5791 snapshot.pb_int(hits->size()); 5792 for (vector<Hit* >::iterator it_h = hits->begin(); it_h != hits->end(); it_h++) { 5793 (*it_h)->to_snv(snapshot); 5794 } 5795 } 5796 5797 if (mrs_pattern) { 5798 snapshot.pb_char(1); 5799 mrs_pattern->to_snv(snapshot); 5800 } else snapshot.pb_char(0); 5801 if (searchOptions) { 5802 snapshot.pb_char(1); 5803 searchOptions->to_snv(snapshot); 5804 } else snapshot.pb_char(0); 5805 if (mrs_pattern && labels && continuations) { 5806 snapshot.pb_char(1); 5807 snapshot.pb_charp(labels, mrs_pattern->sizeX * mrs_pattern->sizeY); 5808 for(int i=0; i<mrs_pattern->sizeX * mrs_pattern->sizeY; i++) continuations[i].to_snv(snapshot); 5809 } else snapshot.pb_char(0); 5810 snapshot.pb_int(num_hits); 5811 snapshot.pb_int(num_switched); 5812 snapshot.pb_int(Bwins); 5813 snapshot.pb_int(Wwins); 5814 5815 // insert snapshot into database 5816 sqlite3_stmt *ppStmt=0; 5817 int rc = sqlite3_prepare(db, "insert into snapshots (data) values (?)", -1, &ppStmt, 0); 5818 if (rc != SQLITE_OK || ppStmt==0) throw DBError(); 5819 char* snchp = snapshot.to_charp(); 5820 rc = sqlite3_bind_blob(ppStmt, 1, snchp, snapshot.size(), SQLITE_TRANSIENT); 5821 delete [] snchp; 5822 if (rc != SQLITE_OK) throw DBError(); 5823 rc = sqlite3_step(ppStmt); 5824 if (rc != SQLITE_DONE) throw DBError(); 5825 rc = sqlite3_finalize(ppStmt); 5826 if (rc != SQLITE_OK) throw DBError(); 5827 return sqlite3_last_insert_rowid(db); 5828 } 5829 5830 void GameList::restore(int handle, bool del) throw(DBError) { 5831 // restore the state of the GameList associated with handle 5832 5833 // retrieve info associated with handle from db 5834 5835 char* sn = 0; 5836 int sn_size = 0; 5837 sqlite3_stmt *ppStmt=0; 5838 int rc = sqlite3_prepare(db, "select data from snapshots where rowid = ?", -1, &ppStmt, 0); 5839 if (rc != SQLITE_OK || ppStmt==0) { 5840 printf("%d\n", rc); 5841 throw DBError(); 5842 } 5843 rc = sqlite3_bind_int(ppStmt, 1, handle); 5844 if (rc != SQLITE_OK) throw DBError(); 5845 rc = sqlite3_step(ppStmt); 5846 if (rc == SQLITE_ROW) { 5847 sn = (char*)sqlite3_column_blob(ppStmt, 0); 5848 sn_size = sqlite3_column_bytes(ppStmt, 0); 5849 } else throw DBError(); 5850 5851 SnapshotVector snapshot(sn, sn_size); 5852 5853 // parse info 5854 5855 string ob = snapshot.retrieve_string(); 5856 string f1 = snapshot.retrieve_string(); 5857 string f2 = snapshot.retrieve_string(); 5858 if (ob != orderby || f1 != format1 || f2 != format2) resetFormat(); 5859 5860 if (oldList) delete oldList; 5861 oldList = 0; 5862 if (currentList) delete currentList; 5863 currentList = new vector<pair<int,int> >; 5864 for(vector<GameListEntry* >::iterator it = all->begin(); it != all->end(); it++) { 5865 if ((*it)->hits) { 5866 for(vector<Hit* >::iterator ith = (*it)->hits->begin(); ith != (*it)->hits->end(); ith++) 5867 delete *ith; 5868 delete (*it)->hits; 5869 (*it)->hits = 0; 5870 } 5871 if ((*it)->candidates) { 5872 for(vector<Candidate* >::iterator itc = (*it)->candidates->begin(); itc != (*it)->candidates->end(); itc++) 5873 delete *itc; 5874 delete (*it)->candidates; 5875 (*it)->candidates = 0; 5876 } 5877 } 5878 5879 int cl_size = snapshot.retrieve_int(); 5880 for(int i=0; i<cl_size; i++) { 5881 currentList->push_back(make_pair(snapshot.retrieve_int(), snapshot.retrieve_int())); 5882 (*all)[(*currentList)[currentList->size()-1].second]->hits_from_snv(snapshot); 5883 } 5884 5885 if (mrs_pattern) delete mrs_pattern; 5886 if (snapshot.retrieve_char()) mrs_pattern = new Pattern(snapshot); 5887 else mrs_pattern = 0; 5888 5889 if (searchOptions) delete searchOptions; 5890 if (snapshot.retrieve_char()) searchOptions = new SearchOptions(snapshot); 5891 else searchOptions = 0; 5892 5893 if (labels) delete [] labels; 5894 if (continuations) delete [] continuations; // FIXME check (cf. ~GameList) 5895 if (snapshot.retrieve_char()) { 5896 labels = snapshot.retrieve_charp(); 5897 continuations = new Continuation[mrs_pattern->sizeX * mrs_pattern->sizeY]; 5898 for(int i=0; i<mrs_pattern->sizeX * mrs_pattern->sizeY; i++) 5899 continuations[i].from_snv(snapshot); 5900 } else { 5901 labels = 0; 5902 continuations = 0; 5903 } 5904 num_hits = snapshot.retrieve_int(); 5905 num_switched = snapshot.retrieve_int(); 5906 Bwins = snapshot.retrieve_int(); 5907 Wwins = snapshot.retrieve_int(); 5908 5909 rc = sqlite3_finalize(ppStmt); 5910 if (rc != SQLITE_OK) throw DBError(); 5911 if (del) { // delete snapshot from db 5912 char sql[100]; 5913 sprintf(sql, "delete from snapshots where rowid = %d", handle); 5914 rc = sqlite3_exec(db, sql, 0, 0, 0); 5915 if (rc != SQLITE_OK) throw DBError(); 5916 } 5917 } 5918 5919 void GameList::delete_snapshot(int handle) throw(DBError) { 5920 char sql[100]; 5921 sprintf(sql, "delete from snapshots where rowid = %d", handle); 5922 int rc = sqlite3_exec(db, sql, 0, 0, 0); 5923 if (rc != SQLITE_OK) throw DBError(); 5924 } 5925 5926 void GameList::delete_all_snapshots() throw(DBError) { 5927 int rc = sqlite3_exec(db, "delete from snapshots", 0, 0, 0); 5928 if (rc != SQLITE_OK) throw DBError(); 5929 } 5930 5523 5931 VarInfo::VarInfo(Node* N, abstractBoard* B, int I) { 5524 5932 n = N; 06/libkombilo-branches/hash_center/search.h
r243 r249 43 43 44 44 const char NO_CONT = 255; 45 46 class SnapshotVector : public std::vector<unsigned char> { 47 public: 48 SnapshotVector(); 49 SnapshotVector(char* c, int size); 50 51 void pb_int(int d); 52 void pb_charp(char* c, int size); 53 void pb_char(char c); 54 void pb_string(std::string s); 55 void pb_intp(int* p, int size); 56 57 int retrieve_int(); 58 int* retrieve_intp(); 59 char retrieve_char(); 60 char* retrieve_charp(); 61 std::string retrieve_string(); 62 63 char* to_charp(); 64 65 private: 66 SnapshotVector::iterator current; 67 }; 68 45 69 46 70 class PatternError { … … 117 141 Pattern(int type, int BOARDSIZE, int sX, int sY, char* iPos, char* CONTLABELS = 0); 118 142 Pattern(const Pattern& p); 143 Pattern(SnapshotVector& snv); 119 144 ~Pattern(); 120 145 Pattern& operator=(const Pattern& p); … … 129 154 int operator==(const Pattern& p); 130 155 std::string printPattern(); 156 void to_snv(SnapshotVector& snv); 131 157 132 158 static int flipsX(int i, int x, int y, int XX, int YY); … … 148 174 int lW; // black loses (where cont. is W) 149 175 Continuation(); 176 void from_snv(SnapshotVector& snv); 177 void to_snv(SnapshotVector& snv); 150 178 }; 151 179 … … 190 218 char* label; // this does not really contain the label, but rather the position of the continuation move 191 219 Hit(ExtendedMoveNumber* POS, char* LABEL); 220 Hit(SnapshotVector& snv); // takes a SnapshotVector and reads information produced by Hit::to_snv() 192 221 ~Hit(); 193 222 static bool cmp_pts(Hit* a, Hit* b); 223 void to_snv(SnapshotVector& snv); 194 224 }; 195 225 … … 678 708 SearchOptions(); 679 709 SearchOptions(int FIXEDCOLOR, int NEXTMOVE, int MOVELIMIT=10000); 710 SearchOptions(SnapshotVector& snv); 711 void to_snv(SnapshotVector& snv); 680 712 }; 681 713 … … 690 722 GameListEntry(int ID, char WINNER, std::string GAMEINFOSTR); 691 723 ~GameListEntry(); 724 725 void hits_from_snv(SnapshotVector& snv); 692 726 }; 693 727 … … 754 788 int Bwins; 755 789 int Wwins; 756 790 Pattern* mrs_pattern; // most recent search pattern 791 SearchOptions* searchOptions; 757 792 // ---------------------------------------------------------------------------- 758 793 // the following methods provide the user interface … … 792 827 void deleteTag(int tag, int i = -1) throw(DBError); 793 828 std::vector<int> getTags(int i, int tag=0) throw(DBError); // note the order of arguments! 794 829 795 830 // ------- duplicates --------------------------------------------------------- 796 831 int find_duplicates(int bs, bool strict=false) throw(DBError); // return number of duplicate array … … 800 835 // The caller must free the pointer himself 801 836 // (before calling find_duplicates again). 837 838 // ------- snapshot, restore -------------------------------------------------- 839 840 int snapshot() throw(DBError); 841 void restore(int handle, bool del) throw(DBError); 842 void delete_snapshot(int handle) throw(DBError); 843 void delete_all_snapshots() throw(DBError); 802 844 803 845 // ------- misc --------------------------------------------------------------- … … 847 889 int posHA; 848 890 int SGFtagsSize; 849 int sizeX; // keeps track of width of search pattern during search850 891 sqlite3* algo_db1; 851 892 sqlite3* algo_db2;
