Changeset 248 for 06/libkombilo

Show
Ignore:
Timestamp:
04/07/07 00:25:03 (21 months ago)
Author:
ug
Message:

Snapshot/restore functionality for GameList class.

Location:
06/libkombilo
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • 06/libkombilo/abstractboard.cpp

    r244 r248  
    164164void abstractBoard::clear() { 
    165165  for(int i=0; i<boardsize*boardsize; i++) status[i]=' '; 
    166   undostack = stack<Move>();  
     166  undostack = stack<Move>(); 
    167167} 
    168168 
     
    183183    undostack.push(m); 
    184184    return 1; 
    185   }  
     185  } 
    186186  return 0; 
    187187} 
     
    304304      Move tuple = undostack.top(); 
    305305      undostack.pop(); 
    306        
     306 
    307307      char color = tuple.color; 
    308308      vector<p_cc>* captures = tuple.captures; 
     
    318318      } 
    319319    } 
    320   }   
     320  } 
    321321} 
    322322 
  • 06/libkombilo/abstractboard.h

    r191 r248  
    2828#include <utility> 
    2929#include <stack> 
    30  
    31 const int DEBUG_ABSTRACTBOARD = 0; 
    3230 
    3331class BoardError { 
  • 06/libkombilo/cpptest.cpp

    r244 r248  
    2929    gl.start_processing(); 
    3030    directory_iterator end_itr; 
    31     string path = "/home/ug/go/kombilo/06/tests1/libkombilo"; 
     31    // string path = "/home/ug/go/kombilo/06/tests1/libkombilo"; 
    3232    // string path = "/home/ug/go/gtl/reviews"; 
    33     // string path = "/home/ug/go/gogod06/1998"; 
     33    string path = "/home/ug/go/gogod06/1998"; 
    3434    int counter = 0; 
    3535    for(directory_iterator it(path); it != end_itr; ++it) { 
     
    9494   
    9595  // -------------------- do pattern search -------------------------------------- 
    96   // gl.search(p, &so); 
     96  gl.search(p, &so); 
    9797 
    9898  // ------------------- print some information about current list of games ------------ 
    99   // printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
     99  printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
    100100  // vector<string> res = gl.currentEntriesAsStrings(); 
    101101  // for(vector<string>::iterator it = res.begin(); it != res.end(); it++) 
     
    130130 
    131131  // ------------------- check for duplicates --------------------------------- 
     132  // gl.reset(); 
     133  // int nd = gl.find_duplicates(19); 
     134  // printf("duplicates:\n"); 
     135  // for(int i=0; i<nd; i++) { 
     136  //   // 1st method: retrieve_duplicates_VI 
     137  //   // vector<int> dupl_vector = gl.retrieve_duplicates_VI(i); 
     138  //   // for(vector<int>::iterator it = dupl_vector.begin(); it != dupl_vector.end(); it++) { 
     139  //   //   printf("%s%s\n", gl.currentEntryAsString(*it).c_str(), gl.getSignature(*it).c_str()); 
     140  //   // } 
     141  //    
     142  //   // 2nd method: retrieve_duplicates_PI 
     143  //   int * dupl_vector = gl.retrieve_duplicates_PI(i); 
     144  //   int j = 0; 
     145  //   while(dupl_vector[j] != -1) { 
     146  //     printf("%s%s\n", gl.currentEntryAsString(dupl_vector[j]).c_str(), gl.getSignature(dupl_vector[j]).c_str()); 
     147  //     j++; 
     148  //   } 
     149  //   delete [] dupl_vector; 
     150 
     151  //   printf("--------------------------------------------------- \n"); 
     152  // } 
     153 
     154  // ------------------- snapshot --------------------------------------------- 
     155 
     156  gl.delete_all_snapshots(); 
     157  int handle = gl.snapshot(); 
     158  printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
     159 
    132160  gl.reset(); 
    133   int nd = gl.find_duplicates(19); 
    134   printf("duplicates:\n"); 
    135   for(int i=0; i<nd; i++) { 
    136     // 1st method: retrieve_duplicates_VI 
    137     // vector<int> dupl_vector = gl.retrieve_duplicates_VI(i); 
    138     // for(vector<int>::iterator it = dupl_vector.begin(); it != dupl_vector.end(); it++) { 
    139     //   printf("%s%s\n", gl.currentEntryAsString(*it).c_str(), gl.getSignature(*it).c_str()); 
    140     // } 
    141      
    142     // 2nd method: retrieve_duplicates_PI 
    143     int * dupl_vector = gl.retrieve_duplicates_PI(i); 
    144     int j = 0; 
    145     while(dupl_vector[j] != -1) { 
    146       printf("%s%s\n", gl.currentEntryAsString(dupl_vector[j]).c_str(), gl.getSignature(dupl_vector[j]).c_str()); 
    147       j++; 
    148     } 
    149     delete [] dupl_vector; 
     161  printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
    150162 
    151     printf("--------------------------------------------------- \n"); 
    152   } 
     163  gl.restore(handle, true); 
     164  printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
    153165 
    154166  // ------------------- resetFormat ------------------------------------------ 
  • 06/libkombilo/search.cpp

    r247 r248  
    4343using std::sort; 
    4444#endif 
     45 
     46SnapshotVector::SnapshotVector() : vector<unsigned char>() { 
     47  current = begin(); 
     48} 
     49 
     50SnapshotVector::SnapshotVector(char* c, int size) : vector<unsigned char>() { 
     51  for(int i=0; i<size; i++) push_back(c[i]); 
     52  current = begin(); 
     53} 
     54 
     55void SnapshotVector::pb_int(int d) { 
     56  for(int i = 0; i < 4; i++) { 
     57    push_back((unsigned char)(d % 256)); 
     58    d = d >> 8; 
     59  } 
     60} 
     61 
     62void SnapshotVector::pb_charp(char* c, int size) { 
     63  pb_int(size); 
     64  for(int i=0; i<size; i++) push_back(c[i]); 
     65} 
     66 
     67void SnapshotVector::pb_intp(int* p, int size) { 
     68  pb_int(size); 
     69  for(int i=0; i<size; i++) pb_int(p[i]); 
     70} 
     71 
     72void SnapshotVector::pb_string(string s) { 
     73  pb_int(s.size()+1); 
     74  for(unsigned int i=0; i<s.size(); i++) push_back(s[i]); 
     75  push_back(0); 
     76} 
     77 
     78void SnapshotVector::pb_char(char c) { 
     79  push_back(c); 
     80} 
     81 
     82int SnapshotVector::retrieve_int() { 
     83  int result = 0; 
     84  for(int i=0; i<4; i++) { 
     85    result += *current << (i*8); 
     86    current++; 
     87  } 
     88  return result; 
     89} 
     90 
     91int* SnapshotVector::retrieve_intp() { 
     92  int sz = retrieve_int(); 
     93  int* result = new int[sz]; 
     94  for(int i=0; i<sz; i++) 
     95    result[i] = retrieve_int(); 
     96  return result; 
     97} 
     98 
     99char SnapshotVector::retrieve_char() { 
     100  char c = *current; 
     101  current++; 
     102  return c; 
     103} 
     104 
     105char* SnapshotVector::retrieve_charp() { 
     106  int sz = retrieve_int(); 
     107  char* result = new char[sz]; 
     108  for(int i=0; i<sz; i++) { 
     109    result[i] = *current; 
     110    current++; 
     111  } 
     112  return result; 
     113} 
     114 
     115string SnapshotVector::retrieve_string() { 
     116  char* cp = retrieve_charp(); 
     117  string s(cp); 
     118  delete [] cp; 
     119  return s; 
     120} 
     121 
     122char* SnapshotVector::to_charp() { 
     123  char* result = new char[size()]; 
     124  int counter = 0; 
     125  for(SnapshotVector::iterator it = begin(); it != end(); it++) result[counter++] = *it; 
     126  return result; 
     127} 
     128 
    45129 
    46130PatternError::PatternError() {} 
     
    55139  wW = 0; 
    56140  lW = 0; 
     141} 
     142 
     143void Continuation::from_snv(SnapshotVector& snv) { 
     144  B = snv.retrieve_int(); 
     145  W = snv.retrieve_int(); 
     146  tB = snv.retrieve_int(); 
     147  tW = snv.retrieve_int(); 
     148  wB = snv.retrieve_int(); 
     149  lB = snv.retrieve_int(); 
     150  wW = snv.retrieve_int(); 
     151  lW = snv.retrieve_int(); 
     152} 
     153 
     154void Continuation::to_snv(SnapshotVector& snv) { 
     155  snv.pb_int(B); 
     156  snv.pb_int(W); 
     157  snv.pb_int(tB); 
     158  snv.pb_int(tW); 
     159  snv.pb_int(wB); 
     160  snv.pb_int(lB); 
     161  snv.pb_int(wW); 
     162  snv.pb_int(lW); 
    57163} 
    58164 
     
    143249  return 0; 
    144250} 
    145  
    146251 
    147252 
     
    300405  flip = 0; 
    301406  colorSwitch = 0; 
    302         
     407 
    303408  left = le; 
    304409  right = ri; 
     
    322427 
    323428  contList = CONTLIST; 
     429} 
     430 
     431Pattern::Pattern(SnapshotVector& snv) { 
     432  flip = snv.retrieve_int(); 
     433  colorSwitch = snv.retrieve_int(); 
     434  left = snv.retrieve_int(); 
     435  right = snv.retrieve_int(); 
     436  top = snv.retrieve_int(); 
     437  bottom = snv.retrieve_int(); 
     438  boardsize = snv.retrieve_int(); 
     439  sizeX = snv.retrieve_int(); 
     440  sizeY = snv.retrieve_int(); 
     441  if (snv.retrieve_char()) { // contLabels? 
     442    contLabels = snv.retrieve_charp(); 
     443  } else contLabels = 0; 
     444  initialPos = snv.retrieve_charp(); 
     445  finalPos = snv.retrieve_charp(); 
     446 
     447  int size = snv.retrieve_int(); 
     448  for(int i=0; i<size; i++) 
     449    contList.push_back(MoveNC(snv.retrieve_char(), snv.retrieve_char(), snv.retrieve_char())); // x, y, color 
     450} 
     451 
     452void Pattern::to_snv(SnapshotVector& snv) { 
     453  snv.pb_int(flip); 
     454  snv.pb_int(colorSwitch); 
     455  snv.pb_int(left); 
     456  snv.pb_int(right); 
     457  snv.pb_int(top); 
     458  snv.pb_int(bottom); 
     459  snv.pb_int(boardsize); 
     460  snv.pb_int(sizeX); 
     461  snv.pb_int(sizeY); 
     462  if (contLabels) { 
     463    snv.pb_char(1); 
     464    snv.pb_charp(contLabels, sizeX*sizeY); 
     465  } else snv.pb_char(0); 
     466  snv.pb_charp(initialPos, sizeX*sizeY); 
     467  snv.pb_charp(finalPos, sizeX*sizeY); 
     468  snv.pb_int(contList.size()); 
     469  for(vector<MoveNC>::iterator it = contList.begin(); it != contList.end(); it++) { 
     470    snv.pb_char(it->x); 
     471    snv.pb_char(it->y); 
     472    snv.pb_char(it->color); 
     473  } 
    324474} 
    325475 
     
    368518    if (finalPos) delete [] finalPos; 
    369519    if (contLabels) delete [] contLabels; 
    370      
     520 
    371521    initialPos = new char[sizeX*sizeY]; 
    372522    finalPos = new char[sizeX*sizeY]; 
     
    398548    if (initialPos) delete [] initialPos; 
    399549    if (finalPos) delete [] finalPos; 
    400      
     550 
    401551    initialPos = new char[sizeX*sizeY]; 
    402552    finalPos = new char[sizeX*sizeY]; 
     
    501651  return co; 
    502652} 
    503          
     653 
    504654void PatternList::patternList() { 
    505          
    506655  vector<Pattern> lCS; 
    507656  vector<pair<int,int> > sy; 
     
    13391488  if (rc != SQLITE_OK) throw DBError(); 
    13401489  // printf("init Algo_movelist\n"); 
    1341 }                                          
     1490} 
    13421491 
    13431492void Algo_movelist::newgame_process(int game_id) { 
     
    13481497  for(int i=0; i<50; i++) fpC[i] = 0; 
    13491498} 
    1350          
     1499 
    13511500void Algo_movelist::AB_process(int x, int y) { 
    13521501  movelist.push_back((char)x); 
     
    14011550  } 
    14021551} 
    1403        
     1552 
    14041553void Algo_movelist::pass_process() { 
    14051554  movelist.push_back(19); 
     
    15541703  return result; 
    15551704} 
    1556  
    15571705 
    15581706int Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) {  
     
    22782426  // printf("leave algo_hash_full::initialize_processing\n"); 
    22792427} 
    2280          
     2428 
    22812429void Algo_hash_full::newgame_process(int game_id) { 
    22822430  numStones = 0; 
     
    34133561} 
    34143562 
    3415 Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { 
     3563Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { // LABEL is a char[3] 
    34163564  pos = POS; // note that we do not copy these! 
    34173565  label = LABEL; 
     
    34213569  delete pos; 
    34223570  delete [] label; 
     3571} 
     3572 
     3573Hit::Hit(SnapshotVector& snv) { 
     3574  int length = snv.retrieve_int(); 
     3575  int* data = snv.retrieve_intp(); 
     3576  pos = new ExtendedMoveNumber(length, data); 
     3577  delete [] data; 
     3578  label = snv.retrieve_charp(); 
     3579} 
     3580 
     3581void Hit::to_snv(SnapshotVector& snv) { 
     3582  snv.pb_int(pos->length); 
     3583  snv.pb_intp(pos->data, pos->length); 
     3584  snv.pb_charp(label, 3); 
    34233585} 
    34243586 
     
    34463608  searchInVariations = true; 
    34473609  algos = (1<<30) - 1; // use all available algorithms 
     3610} 
     3611 
     3612SearchOptions::SearchOptions(SnapshotVector& snv) { 
     3613  fixedColor = snv.retrieve_int(); 
     3614  moveLimit = snv.retrieve_int(); 
     3615  nextMove = snv.retrieve_int(); 
     3616  trustHashFull = snv.retrieve_int(); 
     3617  searchInVariations= snv.retrieve_int(); 
     3618  algos = snv.retrieve_int(); 
     3619} 
     3620 
     3621void SearchOptions::to_snv(SnapshotVector& snv) { 
     3622  snv.pb_int(fixedColor); 
     3623  snv.pb_int(moveLimit); 
     3624  snv.pb_int(nextMove); 
     3625  snv.pb_int(trustHashFull); 
     3626  snv.pb_int(searchInVariations); 
     3627  snv.pb_int(algos); 
    34483628} 
    34493629 
     
    35373717    for(vector<Hit* >::iterator it = hits->begin(); it != hits->end(); it++) delete *it; 
    35383718    delete hits; 
     3719    hits = 0; 
    35393720  } 
    35403721  if (candidates) { 
    35413722    for(vector<Candidate* >::iterator it = candidates->begin(); it != candidates->end(); it++) delete *it; 
    35423723    delete candidates; 
    3543   } 
    3544 } 
    3545                           
     3724    candidates = 0; 
     3725  } 
     3726} 
     3727 
     3728void GameListEntry::hits_from_snv(SnapshotVector& snv) { 
     3729  int h_size = snv.retrieve_int(); 
     3730  hits = new vector<Hit* >; 
     3731  for(int j=0; j<h_size; j++) { 
     3732    hits->push_back(new Hit(snv)); 
     3733  } 
     3734} 
     3735 
    35463736int insertEntry(void *gl, int argc, char **argv, char **azColName) { 
    35473737  char winner = '-'; 
     
    35913781  labels = 0; 
    35923782  continuations = 0; 
     3783  mrs_pattern = 0; 
     3784  searchOptions = 0; 
    35933785  dbname = new char[strlen(DBNAME)+2]; 
    35943786  strcpy(dbname, DBNAME); 
     
    36843876    if (rc != SQLITE_OK) throw DBError(); 
    36853877  } 
     3878 
     3879  // set up snapshot db 
     3880  rc = sqlite3_exec(db, "create table if not exists snapshots ( data text );", 0, 0, 0); 
     3881  if (rc != SQLITE_OK) throw DBError(); 
    36863882 
    36873883  // printf("set up Algorithm instances\n"); 
     
    37803976  currentList = 0; 
    37813977  oldList = 0; 
    3782    
     3978 
    37833979  int rc; 
    37843980  rc = sqlite3_exec(db, "begin transaction;", 0, 0, 0); 
     
    37963992  // printf("read.\n"); 
    37973993  // SQLITE_ERROR may occur since table might not yet exist 
    3798    
     3994 
    37993995  readPlayersList(); 
    38003996  rc = sqlite3_exec(db, "commit;", 0, 0, 0); 
     
    38154011GameList::~GameList() { 
    38164012  // printf("enter ~GameList\n"); 
     4013  if (mrs_pattern) delete mrs_pattern; 
     4014  if (searchOptions) delete searchOptions; 
    38174015  if (p_op) delete p_op; 
    38184016  if (labels) delete [] labels; 
    3819   if (continuations) delete [] continuations; 
     4017  if (continuations) delete [] continuations; // FIXME CHECK whether the Continuation destructor is invoked! 
    38204018  if (duplicates) delete duplicates; 
    38214019  delete [] dbname; 
     
    39714169    currentList->push_back(make_pair((*it)->id, counter++)); 
    39724170  } 
     4171  num_hits = 0; 
     4172  num_switched = 0; 
     4173  Bwins = 0; 
     4174  Wwins = 0; 
    39734175} 
    39744176 
     
    42184420 
    42194421char GameList::lookupLabel(char x, char y) { 
    4220   if (!labels) return '?'; 
    4221   return labels[x+y*sizeX]; 
     4422  if (!labels || !mrs_pattern || x < 0 || x >= mrs_pattern->sizeX || y < 0 || y >= mrs_pattern->sizeY) return '?'; 
     4423  return labels[x+y*mrs_pattern->sizeX]; 
    42224424} 
    42234425 
    42244426Continuation GameList::lookupContinuation(char x, char y) { 
    4225   if (!continuations) return Continuation(); 
    4226   return continuations[x+y*sizeX]; 
     4427  if (!continuations || !mrs_pattern || x < 0 || x >= mrs_pattern->sizeX || y < 0 || y >= mrs_pattern->sizeY) return Continuation(); 
     4428  return continuations[x+y*mrs_pattern->sizeX]; 
    42274429} 
    42284430 
     
    42964498 
    42974499void GameList::search(Pattern& pattern, SearchOptions* so) throw(DBError) { 
    4298   SearchOptions* searchOptions; 
     4500  if (mrs_pattern) delete mrs_pattern; 
     4501  mrs_pattern = new Pattern(pattern); 
     4502  if (searchOptions) delete searchOptions; 
    42994503  if (so) searchOptions = new SearchOptions(*so); 
    43004504  else searchOptions = new SearchOptions(); 
    4301   sizeX = pattern.sizeX; // need this in lookupLabel 
    43024505  PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 
    43034506 
     
    43104513  if (it == boardsizes.end()) { 
    43114514    delete searchOptions; 
     4515    searchOptions = 0; 
    43124516    if (oldList) delete oldList; 
    43134517    oldList = 0; 
     
    43604564  continuations = pl.continuations; 
    43614565  pl.continuations = new Continuation[pattern.sizeX*pattern.sizeY]; 
    4362   delete searchOptions; 
     4566 
     4567  // FIXME: delete all candidate lists! 
    43634568} 
    43644569 
     
    44574662  sql1 = "create table if not exists GAME_TAGS ( id integer primary key, game_id integer, tag_id integer );"; 
    44584663  rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 
    4459   if( rc!=SQLITE_OK ) throw DBError(); 
     4664  if (rc != SQLITE_OK) throw DBError(); 
    44604665} 
    44614666 
    44624667void GameList::start_processing(int PROCESSVARIATIONS) throw(DBError) { 
    44634668  // printf("enter start_processing %p\n", p_op); 
     4669 
     4670  delete_all_snapshots(); 
     4671 
    44644672  if (PROCESSVARIATIONS != -1) processVariations = PROCESSVARIATIONS; 
    44654673  else processVariations = p_op->processVariations; 
     
    45654773    if (sz=="") sz = "19"; 
    45664774    int bs = atoi(sz.c_str()); 
    4567     if (bs < 1) {  
    4568       return_val |= UNACCEPTABLE_BOARDSIZE;  
     4775    if (bs < 1) { 
     4776      return_val |= UNACCEPTABLE_BOARDSIZE; 
    45694777      process_results_vector.push_back(return_val); 
    45704778      delete rootNodeProperties; 
     
    49415149        delete [] sig; 
    49425150      } 
    4943        
     5151 
    49445152      if (commit) { 
    49455153        // evaluate tags 
     
    49855193 
    49865194 
     5195int GameList::snapshot() throw(DBError) { 
     5196  // return a handle to a snapshot stored in the main GameList db 
     5197  // the snapshot contains copies of 
     5198  // - orderby, format1, format2 
     5199  // - currentList 
     5200  // - all hits in the GameListEntry's of currentList 
     5201  // - pattern, labels, continuations, num_hits, num_switched, Bwins, Wwins 
     5202 
     5203  SnapshotVector snapshot; 
     5204  snapshot.pb_string(orderby); 
     5205  snapshot.pb_string(format1); 
     5206  snapshot.pb_string(format2); 
     5207 
     5208  snapshot.pb_int(currentList->size()); 
     5209  for(vector<pair<int,int> >::iterator it = currentList->begin(); it != currentList->end(); it++) { 
     5210    snapshot.pb_int(it->first); 
     5211    snapshot.pb_int(it->second); 
     5212    vector<Hit* >* hits = (*all)[it->second]->hits; 
     5213    snapshot.pb_int(hits->size()); 
     5214    for (vector<Hit* >::iterator it_h = hits->begin(); it_h != hits->end(); it_h++) { 
     5215      (*it_h)->to_snv(snapshot); 
     5216    } 
     5217  } 
     5218 
     5219  if (mrs_pattern) { 
     5220    snapshot.pb_char(1); 
     5221    mrs_pattern->to_snv(snapshot); 
     5222  } else snapshot.pb_char(0); 
     5223  if (searchOptions) { 
     5224    snapshot.pb_char(1); 
     5225    searchOptions->to_snv(snapshot); 
     5226  } else snapshot.pb_char(0); 
     5227  if (mrs_pattern && labels && continuations) { 
     5228    snapshot.pb_char(1); 
     5229    snapshot.pb_charp(labels, mrs_pattern->sizeX * mrs_pattern->sizeY); 
     5230    for(int i=0; i<mrs_pattern->sizeX * mrs_pattern->sizeY; i++) continuations[i].to_snv(snapshot); 
     5231  } else snapshot.pb_char(0); 
     5232  snapshot.pb_int(num_hits); 
     5233  snapshot.pb_int(num_switched); 
     5234  snapshot.pb_int(Bwins); 
     5235  snapshot.pb_int(Wwins); 
     5236 
     5237  // insert snapshot into database 
     5238  sqlite3_stmt *ppStmt=0; 
     5239  int rc = sqlite3_prepare(db, "insert into snapshots (data) values (?)", -1, &ppStmt, 0); 
     5240  if (rc != SQLITE_OK || ppStmt==0) throw DBError(); 
     5241  char* snchp = snapshot.to_charp(); 
     5242  rc = sqlite3_bind_blob(ppStmt, 1, snchp, snapshot.size(), SQLITE_TRANSIENT); 
     5243  delete [] snchp; 
     5244  if (rc != SQLITE_OK) throw DBError(); 
     5245  rc = sqlite3_step(ppStmt); 
     5246  if (rc != SQLITE_DONE) throw DBError(); 
     5247  rc = sqlite3_finalize(ppStmt); 
     5248  if (rc != SQLITE_OK) throw DBError(); 
     5249  return sqlite3_last_insert_rowid(db); 
     5250} 
     5251 
     5252void GameList::restore(int handle, bool del) throw(DBError) { 
     5253  // restore the state of the GameList associated with handle 
     5254 
     5255  // retrieve info associated with handle from db 
     5256 
     5257  char* sn = 0; 
     5258  int sn_size = 0; 
     5259  sqlite3_stmt *ppStmt=0; 
     5260  int rc = sqlite3_prepare(db, "select data from snapshots where rowid = ?", -1, &ppStmt, 0); 
     5261  if (rc != SQLITE_OK || ppStmt==0) { 
     5262    printf("%d\n", rc); 
     5263    throw DBError(); 
     5264  } 
     5265  rc = sqlite3_bind_int(ppStmt, 1, handle); 
     5266  if (rc != SQLITE_OK) throw DBError(); 
     5267  rc = sqlite3_step(ppStmt); 
     5268  if (rc == SQLITE_ROW) { 
     5269    sn = (char*)sqlite3_column_blob(ppStmt, 0); 
     5270    sn_size = sqlite3_column_bytes(ppStmt, 0); 
     5271  } else throw DBError(); 
     5272 
     5273  SnapshotVector snapshot(sn, sn_size); 
     5274 
     5275  // parse info 
     5276 
     5277  string ob = snapshot.retrieve_string(); 
     5278  string f1 = snapshot.retrieve_string(); 
     5279  string f2 = snapshot.retrieve_string(); 
     5280  if (ob != orderby || f1 != format1 || f2 != format2) resetFormat(); 
     5281 
     5282  if (oldList) delete oldList; 
     5283  oldList = 0; 
     5284  if (currentList) delete currentList; 
     5285  currentList = new vector<pair<int,int> >; 
     5286  for(vector<GameListEntry* >::iterator it = all->begin(); it != all->end(); it++) { 
     5287    if ((*it)->hits) { 
     5288      for(vector<Hit* >::iterator ith = (*it)->hits->begin(); ith != (*it)->hits->end(); ith++) 
     5289        delete *ith; 
     5290      delete (*it)->hits; 
     5291      (*it)->hits = 0; 
     5292    } 
     5293    if ((*it)->candidates) { 
     5294      for(vector<Candidate* >::iterator itc = (*it)->candidates->begin(); itc != (*it)->candidates->end(); itc++) 
     5295        delete *itc; 
     5296      delete (*it)->candidates; 
     5297      (*it)->candidates = 0; 
     5298    } 
     5299  } 
     5300 
     5301  int cl_size = snapshot.retrieve_int(); 
     5302  for(int i=0; i<cl_size; i++) { 
     5303    currentList->push_back(make_pair(snapshot.retrieve_int(), snapshot.retrieve_int())); 
     5304    (*all)[(*currentList)[currentList->size()-1].second]->hits_from_snv(snapshot); 
     5305  } 
     5306 
     5307  if (mrs_pattern) delete mrs_pattern; 
     5308  if (snapshot.retrieve_char()) mrs_pattern = new Pattern(snapshot); 
     5309  else mrs_pattern = 0; 
     5310 
     5311  if (searchOptions) delete searchOptions; 
     5312  if (snapshot.retrieve_char()) searchOptions = new SearchOptions(snapshot); 
     5313  else searchOptions = 0; 
     5314 
     5315  if (labels) delete [] labels; 
     5316  if (continuations) delete [] continuations; // FIXME check (cf. ~GameList) 
     5317  if (snapshot.retrieve_char()) { 
     5318    labels = snapshot.retrieve_charp(); 
     5319    continuations = new Continuation[mrs_pattern->sizeX * mrs_pattern->sizeY]; 
     5320    for(int i=0; i<mrs_pattern->sizeX * mrs_pattern->sizeY; i++)  
     5321      continuations[i].from_snv(snapshot); 
     5322  } else { 
     5323    labels = 0; 
     5324    continuations = 0; 
     5325  } 
     5326  num_hits = snapshot.retrieve_int(); 
     5327  num_switched = snapshot.retrieve_int(); 
     5328  Bwins = snapshot.retrieve_int(); 
     5329  Wwins = snapshot.retrieve_int(); 
     5330 
     5331  rc = sqlite3_finalize(ppStmt); 
     5332  if (rc != SQLITE_OK) throw DBError(); 
     5333  if (del) { // delete snapshot from db 
     5334    char sql[100]; 
     5335    sprintf(sql, "delete from snapshots where rowid = %d", handle); 
     5336    rc = sqlite3_exec(db, sql, 0, 0, 0); 
     5337    if (rc != SQLITE_OK) throw DBError(); 
     5338  } 
     5339} 
     5340 
     5341