Show
Ignore:
Timestamp:
04/07/07 14:37:43 (1 year ago)
Author:
ug
Message:

Merge -r 246:248 from libkombilo main branch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo-branches/hash_center/abstractboard.cpp

    r246 r249  
    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-branches/hash_center/abstractboard.h

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

    r243 r249  
    151151 
    152152  // ------------------- 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 
    153181  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()); 
    171183 
    172     printf("--------------------------------------------------- \n"); 
    173   } 
     184  gl.restore(handle, true); 
     185  printf("num games: %d, num hits: %d\n", gl.size(), gl.numHits()); 
    174186 
    175187  // ------------------- resetFormat ------------------------------------------ 
  • 06/libkombilo-branches/hash_center/search.cpp

    r246 r249  
    4545#endif 
    4646 
     47SnapshotVector::SnapshotVector() : vector<unsigned char>() { 
     48  current = begin(); 
     49} 
     50 
     51SnapshotVector::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 
     56void 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 
     63void 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 
     68void 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 
     73void 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 
     79void SnapshotVector::pb_char(char c) { 
     80  push_back(c); 
     81} 
     82 
     83int 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 
     92int* 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 
     100char SnapshotVector::retrieve_char() { 
     101  char c = *current; 
     102  current++; 
     103  return c; 
     104} 
     105 
     106char* 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 
     116string SnapshotVector::retrieve_string() { 
     117  char* cp = retrieve_charp(); 
     118  string s(cp); 
     119  delete [] cp; 
     120  return s; 
     121} 
     122 
     123char* 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 
    47131PatternError::PatternError() {} 
    48132 
     
    56140  wW = 0; 
    57141  lW = 0; 
     142} 
     143 
     144void 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 
     155void 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); 
    58164} 
    59165 
     
    306412  flip = 0; 
    307413  colorSwitch = 0; 
    308         
     414 
    309415  left = le; 
    310416  right = ri; 
     
    328434 
    329435  contList = CONTLIST; 
     436} 
     437 
     438Pattern::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 
     459void 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  } 
    330481} 
    331482 
     
    374525    if (finalPos) delete [] finalPos; 
    375526    if (contLabels) delete [] contLabels; 
    376      
     527 
    377528    initialPos = new char[sizeX*sizeY]; 
    378529    finalPos = new char[sizeX*sizeY]; 
     
    403554    if (initialPos) delete [] initialPos; 
    404555    if (finalPos) delete [] finalPos; 
    405      
     556 
    406557    initialPos = new char[sizeX*sizeY]; 
    407558    finalPos = new char[sizeX*sizeY]; 
     
    585736 
    586737void PatternList::patternList() { 
    587  
    588738  vector<Pattern> lCS; 
    589739  vector<pair<int,int> > sy; 
     
    10541204  int rc = sqlite3_exec(db, sql, 0, 0, 0); 
    10551205  if (rc != SQLITE_OK) { 
    1056 //     printf("error %d\n", rc); 
    10571206    throw DBError(); 
    10581207  } 
     
    13581507  if (rc != SQLITE_OK) throw DBError(); 
    13591508  // printf("init Algo_movelist\n"); 
    1360 }                                          
     1509} 
    13611510 
    13621511void Algo_movelist::newgame_process(int game_id) { 
     
    13671516  for(int i=0; i<50; i++) fpC[i] = 0; 
    13681517} 
    1369          
     1518 
    13701519void Algo_movelist::AB_process(int x, int y) { 
    13711520  movelist.push_back((char)x); 
     
    14181567  } 
    14191568} 
    1420        
     1569 
    14211570void Algo_movelist::pass_process() { 
    14221571  movelist.push_back(19); 
     
    15711720  return result; 
    15721721} 
    1573  
    15741722 
    15751723int Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) {  
     
    21912339 
    21922340HashhitF::HashhitF() { 
    2193 //   printf("oops\n"); 
    21942341  cont = 0; 
    21952342  emn = 0; 
     
    23212468  // printf("leave algo_hash_full::initialize_processing\n"); 
    23222469} 
    2323          
     2470 
    23242471void Algo_hash_full::newgame_process(int game_id) { 
    23252472  numStones = 0; 
     
    39704117} 
    39714118 
    3972 Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { 
     4119Hit::Hit(ExtendedMoveNumber* POS, char* LABEL) { // LABEL is a char[3] 
    39734120  pos = POS; // note that we do not copy these! 
    39744121  label = LABEL; 
     
    39784125  delete pos; 
    39794126  delete [] label; 
     4127} 
     4128 
     4129Hit::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 
     4137void 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); 
    39804141} 
    39814142 
     
    40034164  searchInVariations = true; 
    40044165  algos = (1<<30) - 1; // use all available algorithms 
     4166} 
     4167 
     4168SearchOptions::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 
     4177void 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); 
    40054184} 
    40064185 
     
    40944273    for(vector<Hit* >::iterator it = hits->begin(); it != hits->end(); it++) delete *it; 
    40954274    delete hits; 
     4275    hits = 0; 
    40964276  } 
    40974277  if (candidates) { 
    40984278    for(vector<Candidate* >::iterator it = candidates->begin(); it != candidates->end(); it++) delete *it; 
    40994279    delete candidates; 
    4100   } 
    4101 
    4102                           
     4280    candidates = 0; 
     4281  } 
     4282
     4283 
     4284void 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 
    41034292int insertEntry(void *gl, int argc, char **argv, char **azColName) { 
    41044293  char winner = '-'; 
     
    41484337  labels = 0; 
    41494338  continuations = 0; 
     4339  mrs_pattern = 0; 
     4340  searchOptions = 0; 
    41504341  dbname = new char[strlen(DBNAME)+2]; 
    41514342  strcpy(dbname, DBNAME); 
     
    42414432    if (rc != SQLITE_OK) throw DBError(); 
    42424433  } 
     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(); 
    42434438 
    42444439  // printf("set up Algorithm instances\n"); 
     
    43414536  currentList = 0; 
    43424537  oldList = 0; 
    4343    
     4538 
    43444539  int rc; 
    43454540  rc = sqlite3_exec(db, "begin transaction;", 0, 0, 0); 
     
    43534548  rc = sqlite3_exec(db, sql.c_str(), insertEntry, this, 0); 
    43544549  if (rc != SQLITE_OK && rc != SQLITE_ERROR) { 
    4355 //     printf("sql error %d\n", rc); 
    43564550    throw DBError();  
    43574551  } 
    43584552  // printf("read.\n"); 
    43594553  // SQLITE_ERROR may occur since table might not yet exist 
    4360    
     4554 
    43614555  readPlayersList(); 
    43624556  rc = sqlite3_exec(db, "commit;", 0, 0, 0); 
     
    43774571GameList::~GameList() { 
    43784572  // printf("enter ~GameList\n"); 
     4573  if (mrs_pattern) delete mrs_pattern; 
     4574  if (searchOptions) delete searchOptions; 
    43794575  if (p_op) delete p_op; 
    43804576  if (labels) delete [] labels; 
    4381   if (continuations) delete [] continuations; 
     4577  if (continuations) delete [] continuations; // FIXME CHECK whether the Continuation destructor is invoked! 
    43824578  if (duplicates) delete duplicates; 
    43834579  delete [] dbname; 
     
    45374733    currentList->push_back(make_pair((*it)->id, counter++)); 
    45384734  } 
     4735  num_hits = 0; 
     4736  num_switched = 0; 
     4737  Bwins = 0; 
     4738  Wwins = 0; 
    45394739} 
    45404740 
    45414741void GameList::tagsearch(int tag) throw(DBError) { 
    45424742  char sql[200]; 
    4543    
     4743 
    45444744  if (!tag) return; 
    45454745  if (tag > 0) { 
     
    46234823 
    46244824int 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 
    46254826  int bs_index = 0; 
    46264827  if (duplicates) delete duplicates; 
     
    47834984 
    47844985char 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]; 
    47874988} 
    47884989 
    47894990Continuation 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]; 
    47924993} 
    47934994 
     
    48615062 
    48625063void 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; 
    48645067  if (so) searchOptions = new SearchOptions(*so); 
    48655068  else searchOptions = new SearchOptions(); 
    4866   sizeX = pattern.sizeX; // need this in lookupLabel 
    48675069  PatternList pl(pattern, searchOptions->fixedColor, searchOptions->nextMove); 
    48685070//   printf("created pl, size: %d\n", pl.size()); 
     
    48775079  if (it == boardsizes.end()) { 
    48785080    delete searchOptions; 
     5081    searchOptions = 0; 
    48795082    if (oldList) delete oldList; 
    48805083    oldList = 0; 
    48815084    if (currentList) delete currentList; 
    4882     currentList = 0
     5085    currentList = new vector<pair<int,int> >
    48835086    return; 
    48845087  } 
     
    49405143  continuations = pl.continuations; 
    49415144  pl.continuations = new Continuation[pattern.sizeX*pattern.sizeY]; 
    4942   delete searchOptions; 
     5145 
     5146  // FIXME: delete all candidate lists! 
    49435147} 
    49445148 
     
    50375241  sql1 = "create table if not exists GAME_TAGS ( id integer primary key, game_id integer, tag_id integer );"; 
    50385242  rc = sqlite3_exec(db, sql1.c_str(), 0, 0, 0); 
    5039   if( rc!=SQLITE_OK ) throw DBError(); 
     5243  if (rc != SQLITE_OK) throw DBError(); 
    50405244} 
    50415245 
    50425246void GameList::start_processing(int PROCESSVARIATIONS) throw(DBError) { 
    50435247  // printf("enter start_processing %p\n", p_op); 
     5248 
     5249  delete_all_snapshots(); 
     5250 
    50445251  if (PROCESSVARIATIONS != -1) processVariations = PROCESSVARIATIONS; 
    50455252  else processVariations = p_op->processVariations; 
     
    51445351    if (sz=="") sz = "19"; 
    51455352    int bs = atoi(sz.c_str()); 
    5146     if (bs < 1) {  
    5147       return_val |= UNACCEPTABLE_BOARDSIZE;  
     5353    if (bs < 1) { 
     5354      return_val |= UNACCEPTABLE_BOARDSIZE; 
    51485355      process_results_vector.push_back(return_val); 
    51495356      delete rootNodeProperties; 
     
    52425449    int rc = sqlite3_prepare(db, sql_ins_rnp.c_str(), -1, &ppStmt, 0); 
    52435450    if (rc != SQLITE_OK || ppStmt==0) { 
    5244 //       printf("db error %d\n", rc); 
    52455451      throw DBError(); // FIXME: catch busy error, (and/or throw DBError?) 
    52465452    } 
     
    52955501      // printf("nn\n"); 
    52965502      bool caughtSGFError = false; 
     5503      char* propValue = 0; 
     5504 
    52975505      try { 
    52985506 
    52995507        // parse current node, watch out for B, W, AB, AW, AE properties 
    5300  
    53015508        const char* s = currentN->SGFstring.c_str(); 
    53025509        int lSGFstring = strlen(s); 
     
    53335540          ID[IDindex] = 0; // found next property ID 
    53345541          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]; 
    53365543          int propValueIndex = 0; 
    53375544          int oldPropValueIndex = 0; 
     
    53465553              if (97 <= s[i] && s[i] <= 96+bs) { // valid board coordinate? 
    53475554                propValue[propValueIndex++] = s[i]; 
     5555                if (propValueIndex > 99990) throw SGFError(); 
    53485556              } 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(); 
    53495591              } else { 
    53505592                throw SGFError(); 
     
    53535595            } 
    53545596            if (i >= lSGFstring) throw SGFError(); 
    5355             if (propValueIndex - oldPropValueIndex != 0 && propValueIndex - oldPropValueIndex != 2) 
     5597 
     5598            if (propValueIndex - oldPropValueIndex != 0 && propValueIndex - oldPropValueIndex != 2) { 
    53565599              throw SGFError(); 
     5600            } 
    53575601            oldPropValueIndex = propValueIndex; 
    53585602 
     
    53715615            } 
    53725616            delete [] propValue; 
     5617            propValue = 0; 
    53735618            continue; 
    53745619          } 
     
    54155660              } 
    54165661              delete [] propValue; 
     5662              propValue = 0; 
    54175663        }  
    54185664      } catch (SGFError) { 
     5665        if (propValue) { 
     5666          delete [] propValue; 
     5667          propValue = 0; 
     5668        } 
    54195669        return_val |= SGF_ERROR; 
    54205670        caughtSGFError = true; 
     
    54775727        delete [] sig; 
    54785728      } 
    5479        
     5729 
    54805730      if (commit) { 
    54815731        // evaluate tags 
     
    55215771 
    55225772 
     5773int 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 
     5830void 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 
     5919void 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 
     5926void 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 
    55235931VarInfo::VarInfo(Node* N, abstractBoard* B, int I) { 
    55245932  n = N; 
  • 06/libkombilo-branches/hash_center/search.h

    r243 r249  
    4343 
    4444const char NO_CONT = 255; 
     45 
     46class 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 
    4569 
    4670class PatternError { 
     
    117141    Pattern(int type, int BOARDSIZE, int sX, int sY, char* iPos, char* CONTLABELS = 0); 
    118142    Pattern(const Pattern& p); 
     143    Pattern(SnapshotVector& snv); 
    119144    ~Pattern(); 
    120145    Pattern& operator=(const Pattern& p); 
     
    129154    int operator==(const Pattern& p); 
    130155    std::string printPattern(); 
     156    void to_snv(SnapshotVector& snv); 
    131157 
    132158    static int flipsX(int i, int x, int y, int XX, int YY); 
     
    148174    int lW; // black loses (where cont. is W) 
    149175    Continuation(); 
     176    void from_snv(SnapshotVector& snv); 
     177    void to_snv(SnapshotVector& snv); 
    150178}; 
    151179 
     
    190218    char* label; // this does not really contain the label, but rather the position of the continuation move 
    191219    Hit(ExtendedMoveNumber* POS, char* LABEL); 
     220    Hit(SnapshotVector& snv); // takes a SnapshotVector and reads information produced by Hit::to_snv() 
    192221    ~Hit(); 
    193222    static bool cmp_pts(Hit* a, Hit* b); 
     223    void to_snv(SnapshotVector& snv); 
    194224}; 
    195225 
     
    678708    SearchOptions(); 
    679709    SearchOptions(int FIXEDCOLOR, int NEXTMOVE, int MOVELIMIT=10000); 
     710    SearchOptions(SnapshotVector& snv); 
     711    void to_snv(SnapshotVector& snv); 
    680712}; 
    681713 
     
    690722    GameListEntry(int ID, char WINNER, std::string GAMEINFOSTR); 
    691723    ~GameListEntry(); 
     724 
     725    void hits_from_snv(SnapshotVector& snv); 
    692726}; 
    693727 
     
    754788    int Bwins; 
    755789    int Wwins; 
    756  
     790    Pattern* mrs_pattern; // most recent search pattern 
     791    SearchOptions* searchOptions; 
    757792    // ---------------------------------------------------------------------------- 
    758793    // the following methods provide the user interface 
     
    792827    void deleteTag(int tag, int i = -1) throw(DBError); 
    793828    std::vector<int> getTags(int i, int tag=0) throw(DBError); // note the order of arguments! 
    794      
     829 
    795830    // ------- duplicates --------------------------------------------------------- 
    796831    int find_duplicates(int bs, bool strict=false) throw(DBError); // return number of duplicate array 
     
    800835                                                 // The caller must free the pointer himself 
    801836                                                 // (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); 
    802844 
    803845    // ------- misc --------------------------------------------------------------- 
     
    847889    int posHA; 
    848890    int SGFtagsSize; 
    849     int sizeX; // keeps track of width of search pattern during search 
    850891    sqlite3* algo_db1; 
    851892    sqlite3* algo_db2;