Changeset 240

Show
Ignore:
Timestamp:
03/09/07 16:51:04 (1 year ago)
Author:
ug
Message:

Merged changes to libkombilo branch, -r 238:239

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo-branches/hash_center/Makefile

    r238 r240  
    22        g++ -o cpptest -lsqlite3 -lboost_filesystem cpptest.o search.o sgfparser.o abstractboard.o 
    33 
    4 cpptest.o: cpptest.cpp 
     4cpptest.o: cpptest.cpp search.h abstractboard.h sgfparser.h 
    55        g++ -c cpptest.cpp 
    66 
  • 06/libkombilo-branches/hash_center/abstractboard.cpp

    r225 r240  
    8484  if (DEBUG_ABSTRACTBOARD) printf("AB::abstractBoard(int) 0\n"); 
    8585  boardsize = bs; 
    86   if (boardsize < 3) throw BoardError(); 
     86  if (boardsize < 3) throw BoardError(); // FIXME 
    8787  status = new char[boardsize*boardsize+1]; 
    8888  for (int i = 0; i < boardsize*boardsize; i++) 
  • 06/libkombilo-branches/hash_center/cpptest.cpp

    r238 r240  
    3232    // string path = "/home/ug/go/gtl/reviews"; 
    3333    string path = "/home/ug/go/gogod06/2000"; 
     34    int counter = 0; 
    3435    for(directory_iterator it(path); it != end_itr; ++it) { 
    3536      string n = it->string(); 
     
    4647        } 
    4748        infile.close(); 
    48         gl.process(sgf.c_str(), path.c_str(), n.c_str());    
     49        int flags = CHECK_FOR_DUPLICATES; // |OMIT_DUPLICATES; 
     50        if (gl.process(sgf.c_str(), path.c_str(), n.c_str(), "", flags)) 
     51          if (gl.process_results() & IS_DUPLICATE) printf("is duplicate: %d\n", counter); 
     52        counter++; 
    4953      } 
    5054    } 
    5155    gl.finalize_processing(); 
    52     printf("Processed %d games.\n", gl.size()); 
     56    printf("Now %d games in db.\n", gl.size()); 
    5357  } 
    5458  printf("%d games.\n", gl.size()); 
     
    9094   
    9195  // -------------------- do pattern search -------------------------------------- 
    92   gl.search(p, &so); 
     96  // gl.search(p, &so); 
    9397 
    9498  // ------------------- print some information about current list of games ------------ 
    95   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()); 
    96100  // vector<string> res = gl.currentEntriesAsStrings(); 
    97101  // for(vector<string>::iterator it = res.begin(); it != res.end(); it++) 
     
    100104 
    101105  // ------------------- print some statistics ------------------------------------------ 
    102   printf("Search pattern:\n"); 
    103   printf("%s\n", p.printPattern().c_str()); 
    104   printf("Continuations:\n"); 
    105   for(int y=0; y<p.sizeY; y++) { 
    106     for(int x=0; x<p.sizeX; x++) { 
    107       printf("%c", gl.lookupLabel(x,y)); 
     106  // printf("Search pattern:\n"); 
     107  // printf("%s\n", p.printPattern().c_str()); 
     108  // printf("Continuations:\n"); 
     109  // for(int y=0; y<p.sizeY; y++) { 
     110  //   for(int x=0; x<p.sizeX; x++) { 
     111  //     printf("%c", gl.lookupLabel(x,y)); 
     112  //   } 
     113  //   printf("\n"); 
     114  // } 
     115  // printf("\n"); 
     116  // printf("Statistics:\n");  
     117  // printf("num hits: %d, num switched: %d, B wins: %d, W wins: %d\n", gl.num_hits, gl.num_switched, gl.Bwins, gl.Wwins); 
     118 
     119  // printf("Continuation | Black      ( B wins / W wins ) | White      (B wins / W wins) |\n"); 
     120  // for(int y=0; y<p.sizeY; y++) { 
     121  //   for(int x=0; x<p.sizeX; x++) { 
     122  //     if (gl.lookupLabel(x,y) != '.') { 
     123  //       Continuation cont = gl.lookupContinuation(x,y); 
     124  //       printf("      %c      |   %3d[%3d] (    %3d /    %3d ) |   %3d[%3d] (   %3d /    %3d) | %1.1f /  %1.1f \n", 
     125  //           gl.lookupLabel(x,y), cont.B, cont.tB, cont.wB, cont.lB, cont.W, cont.tW, cont.wW, cont.lW,  
     126  //           cont.wW*100.0/cont.W, cont.wB*100.0/cont.B); 
     127  //     } 
     128  //   } 
     129  // } 
     130 
     131  // ------------------- 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++; 
    108148    } 
    109     printf("\n"); 
    110   } 
    111   printf("\n"); 
    112   printf("Statistics:\n");  
    113   printf("num hits: %d, num switched: %d, B wins: %d, W wins: %d\n", gl.num_hits, gl.num_switched, gl.Bwins, gl.Wwins); 
     149    delete [] dupl_vector; 
    114150 
    115   printf("Continuation | Black      ( B wins / W wins ) | White      (B wins / W wins) |\n"); 
    116   for(int y=0; y<p.sizeY; y++) { 
    117     for(int x=0; x<p.sizeX; x++) { 
    118       if (gl.lookupLabel(x,y) != '.') { 
    119         Continuation cont = gl.lookupContinuation(x,y); 
    120         printf("      %c      |   %3d[%3d] (    %3d /    %3d ) |   %3d[%3d] (   %3d /    %3d) | %1.1f /  %1.1f \n", 
    121             gl.lookupLabel(x,y), cont.B, cont.tB, cont.wB, cont.lB, cont.W, cont.tW, cont.wW, cont.lW,  
    122             cont.wW*100.0/cont.W, cont.wB*100.0/cont.B); 
    123       } 
    124     } 
     151    printf("--------------------------------------------------- \n"); 
    125152  } 
    126153 
  • 06/libkombilo-branches/hash_center/libkombilo.i

    r191 r240  
    55namespace std { 
    66  %template(vectors) vector<string>; 
     7  %template(vectori) vector<int>; 
    78}; 
    89 
  • 06/libkombilo-branches/hash_center/process.py

    r238 r240  
    3131    for filename in filenames: 
    3232        # print filename 
    33         counter += 1 
    3433        try: 
    3534            file = open(filename) 
     
    4140 
    4241        path, fn = os.path.split(filename) 
    43         gl.process(sgf, path, fn) 
     42        if gl.process(sgf, path, fn, '', CHECK_FOR_DUPLICATES_STRICT): 
     43            if gl.process_results() & IS_DUPLICATE:  
     44                print 'duplicate', counter 
     45        else: print 'SGF error' 
     46        counter += 1 
     47         
    4448    gl.finalize_processing() 
    4549    print 'Processed %d games in %.2f seconds' % (counter, time.time()-starttime) 
  • 06/libkombilo-branches/hash_center/search.cpp

    r238 r240  
    871871void Algorithm::branchpoint_process() {} 
    872872void Algorithm::endOfVariation_process() {} 
    873 void Algorithm::endgame_process() {} 
     873void Algorithm::endgame_process(bool commit) {} 
    874874void Algorithm::finalize_process() {} 
    875875int Algorithm::readDB(sqlite3* DB) { return 0; } 
     
    892892  sprintf(sql, "create table if not exists algo_signature_%d ( id integer primary key, signature varchar(12) );", boardsize); 
    893893  int rc = sqlite3_exec(db, sql, 0, 0, 0); 
     894  if (rc != SQLITE_OK) throw DBError(); 
     895  sprintf(sql, "create index if not exists sig_idx on algo_signature_%d(signature);", boardsize); 
     896  rc = sqlite3_exec(db, sql, 0, 0, 0); 
    894897  if (rc != SQLITE_OK) throw DBError(); 
    895898} 
     
    987990} 
    988991 
    989 void Algo_signature::endgame_process() throw(DBError) { 
    990   char* min_signature = symmetrize(signature, boardsize); 
     992void Algo_signature::endgame_process(bool commit) throw(DBError) { 
     993  if (commit) { 
     994    char* min_signature = symmetrize(signature, boardsize); 
     995    char sql[100]; 
     996    sprintf(sql, "insert into algo_signature_%d (id, signature) values (?,?);", boardsize); 
     997    if (dbinsert1blob(db, sql, gid, min_signature, 12)) throw DBError(); 
     998    // for(int i=0; i<12; i++) printf("%c", min_signature[i]); printf("\n"); 
     999    delete [] min_signature; 
     1000  } 
    9911001  delete [] signature; 
     1002} 
     1003 
     1004void Algo_signature::finalize_process() { 
     1005} 
     1006 
     1007char* Algo_signature::get_current_signature() { 
     1008  return symmetrize(signature, boardsize); 
     1009} 
     1010 
     1011vector<int> Algo_signature::search_signature(char* sig) { 
     1012  // to be used during processing! (because we need the db) 
    9921013  char sql[100]; 
    993   sprintf(sql, "insert into algo_signature_%d (id, signature) values (?,?);", boardsize); 
    994   if (dbinsert1blob(db, sql, gid, min_signature, 12)) throw DBError(); 
    995   // for(int i=0; i<12; i++) printf("%c", min_signature[i]); printf("\n"); 
    996   delete [] min_signature; 
    997 
    998  
    999 void Algo_signature::finalize_process() { 
    1000 
    1001  
     1014  sprintf(sql, "select id from algo_signature_%d where signature=? order by id", boardsize); 
     1015  sqlite3_stmt *ppStmt=0; 
     1016  vector<int> result; 
     1017  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
     1018  if (rc != SQLITE_OK || ppStmt==0) throw DBError(); 
     1019  rc = sqlite3_bind_blob(ppStmt, 1, sig, 12, SQLITE_TRANSIENT); 
     1020  if (rc != SQLITE_OK || ppStmt==0) throw DBError(); 
     1021  do { 
     1022    rc = sqlite3_step(ppStmt); 
     1023    if (rc != SQLITE_DONE && rc != SQLITE_ROW) throw DBError(); 
     1024    if (rc == SQLITE_ROW) { 
     1025      result.push_back(sqlite3_column_int(ppStmt, 0)); 
     1026    } 
     1027  } while (rc == SQLITE_ROW); 
     1028  rc = sqlite3_finalize(ppStmt); 
     1029  if (rc != SQLITE_OK) throw DBError(); 
     1030  return result; 
     1031
    10021032 
    10031033Algo_finalpos::Algo_finalpos(int bsize) : Algorithm(bsize) { 
     
    10631093} 
    10641094 
    1065 void Algo_finalpos::endgame_process() throw(DBError) { 
    1066   char sql[100]; 
    1067   sprintf(sql, "insert into algo_finalpos_%d (id, data) values (?,?);", boardsize); 
    1068   if (dbinsert1blob(db, sql, gid, fp, 100)) throw DBError(); 
     1095void Algo_finalpos::endgame_process(bool commit) throw(DBError) { 
     1096  if (commit) { 
     1097    char sql[100]; 
     1098    sprintf(sql, "insert into algo_finalpos_%d (id, data) values (?,?);", boardsize); 
     1099    if (dbinsert1blob(db, sql, gid, fp, 100)) throw DBError(); 
     1100  } 
    10691101  delete [] fp; 
    10701102} 
     
    12411273} 
    12421274 
     1275bool Algo_finalpos::equal(unsigned int i1, unsigned int i2) {  
     1276  // not to be used during processing 
     1277  // i1, i2 correspond to game id's 
     1278  sqlite3_stmt *ppStmt=0; 
     1279  char sql[100]; 
     1280  sprintf(sql, "select data from algo_finalpos_%d where id = %d or id = %d", boardsize, i1, i2); 
     1281  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
     1282  if (rc != SQLITE_OK || ppStmt==0) return false; // FIXME: catch certain errors, (and/or throw DBError?) 
     1283  if (sqlite3_step(ppStmt) == SQLITE_ROW) { 
     1284    char* dd1 = (char*)sqlite3_column_blob(ppStmt, 0); 
     1285    char* d1 = new char[100]; 
     1286    for(int i=0; i<100; i++) d1[i] = dd1[i]; // FIXME: is this necessary? 
     1287    if (sqlite3_step(ppStmt) == SQLITE_ROW) { 
     1288      char* d2 = (char*)sqlite3_column_blob(ppStmt, 0); 
     1289      for(int i=0; i<100; i++)  
     1290        if (d1[i] != d2[i]) { 
     1291          delete [] d1; 
     1292          sqlite3_finalize(ppStmt); 
     1293          return false; 
     1294        } 
     1295      delete [] d1; 
     1296      sqlite3_finalize(ppStmt); 
     1297      return true; 
     1298    } 
     1299    delete [] d1; 
     1300  } 
     1301  sqlite3_finalize(ppStmt); 
     1302  return false; 
     1303} 
     1304 
     1305bool Algo_finalpos::equals_current(unsigned int id1) {  
     1306  // to be used only during processing 
     1307  // id1 here corresponds to a game id 
     1308  bool result = true; 
     1309  sqlite3_stmt *ppStmt=0; 
     1310  char sql[100]; 
     1311  sprintf(sql, "select data from algo_finalpos_%d where id = %d", boardsize, id1); 
     1312  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
     1313  if (rc != SQLITE_OK || ppStmt==0) return false; // FIXME: catch certain errors, (and/or throw DBError?) 
     1314  if (sqlite3_step(ppStmt) == SQLITE_ROW) { 
     1315    char* d = (char*)sqlite3_column_blob(ppStmt, 0); 
     1316    for(int i=0; i<100; i++)  
     1317      if (d[i] != fp[i]) { 
     1318        result = false; 
     1319        break; 
     1320      } 
     1321  } else result = false; 
     1322  sqlite3_finalize(ppStmt); 
     1323  return result; 
     1324} 
     1325 
    12431326 
    12441327Algo_movelist::Algo_movelist(int bsize) : Algorithm(bsize) { 
     
    13481431} 
    13491432 
    1350 void Algo_movelist::endgame_process() throw(DBError) { 
    1351   char* ml = new char[movelist.size()]; 
    1352   int mlIndex = 0; 
    1353   for(vector<char>::iterator it = movelist.begin(); it != movelist.end(); it++) { 
    1354     ml[mlIndex++] = *it; 
    1355   } 
    1356   char sql[100]; 
    1357   sprintf(sql, "insert into algo_movelist_%d (id, movelist, fpC) values (?, ?, ?);", boardsize); 
    1358   if (dbinsert2blobs(db, sql, gid, ml, mlIndex, fpC, 50)) throw DBError(); 
    1359   delete [] ml; 
     1433void Algo_movelist::endgame_process(bool commit) throw(DBError) { 
     1434  if (commit) { 
     1435    char* ml = new char[movelist.size()]; 
     1436    int mlIndex = 0; 
     1437    for(vector<char>::iterator it = movelist.begin(); it != movelist.end(); it++) { 
     1438      ml[mlIndex++] = *it; 
     1439    } 
     1440    char sql[100]; 
     1441    sprintf(sql, "insert into algo_movelist_%d (id, movelist, fpC) values (?, ?, ?);", boardsize); 
     1442    if (dbinsert2blobs(db, sql, gid, ml, mlIndex, fpC, 50)) throw DBError(); 
     1443    delete [] ml; 
     1444  } 
    13601445  delete [] fpC; 
    13611446} 
     
    20692154#endif 
    20702155 
     2156HashFEntry::HashFEntry(hashtype HASHCODE, char* BUF, int LENGTH) { 
     2157  hashCode = HASHCODE; 
     2158  buf = BUF; 
     2159  length = LENGTH; 
     2160} 
     2161 
     2162HashFEntry::HashFEntry(const HashFEntry& hfe) { 
     2163  hashCode = hfe.hashCode; 
     2164  length = hfe.length; 
     2165  buf = new char[length]; 
     2166  for(int i=0; i<length; i++) buf[i] = hfe.buf[i]; 
     2167} 
     2168 
     2169HashFEntry::~HashFEntry() { 
     2170  if (buf) { 
     2171    delete [] buf; 
     2172    buf = 0; 
     2173  } 
     2174} 
     2175 
    20712176HashhitF::HashhitF(int GAMEID, char ORIENTATION, char* blob) { 
    20722177  gameid = GAMEID; 
     
    21452250    buf[6+2*i] = mn.data[i]%256; 
    21462251  } 
     2252  hash_vector.push_back(HashFEntry(hashCode, buf, 5+2*mn.length)); 
     2253  return 0; 
     2254} 
     2255 
     2256int Algo_hash_full::insert_all_hashes() { 
    21472257  char sql[100]; 
    21482258  sprintf(sql, "insert into algo_hash_full_%d (hash, gameid, hit) values (?,?,?);", boardsize); 
     
    21502260  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
    21512261  if (rc != SQLITE_OK || ppStmt==0) return rc; 
    2152   rc = sqlite3_bind_int64(ppStmt, 1, hashCode); 
    2153   if (rc != SQLITE_OK) return rc; 
    2154   rc = sqlite3_bind_int(ppStmt, 2, gid); 
    2155   if (rc != SQLITE_OK) return rc; 
    2156   rc = sqlite3_bind_blob(ppStmt, 3, buf, 5+2*mn.length, SQLITE_TRANSIENT);  
    2157   if (rc != SQLITE_OK) return rc; 
    2158   rc = sqlite3_step(ppStmt); 
    2159   if (rc != SQLITE_DONE) return rc; 
     2262  for(vector<HashFEntry>::iterator it = hash_vector.begin(); it != hash_vector.end(); it++) { 
     2263    rc = sqlite3_bind_int64(ppStmt, 1, it->hashCode); 
     2264    if (rc != SQLITE_OK) return rc; 
     2265    rc = sqlite3_bind_int(ppStmt, 2, gid); 
     2266    if (rc != SQLITE_OK) return rc; 
     2267    rc = sqlite3_bind_blob(ppStmt, 3, it->buf, it->length, SQLITE_TRANSIENT);  
     2268    if (rc != SQLITE_OK) return rc; 
     2269    rc = sqlite3_step(ppStmt); 
     2270    if (rc != SQLITE_DONE) return rc; 
     2271    rc = sqlite3_reset(ppStmt); 
     2272    if (rc != SQLITE_OK) return rc; 
     2273  } 
    21602274  rc = sqlite3_finalize(ppStmt); 
    21612275  if (rc != SQLITE_OK) return rc; 
    2162   delete [] buf
     2276  hash_vector.clear()
    21632277  return 0; // success 
    21642278} 
     
    22742388} 
    22752389 
    2276 void Algo_hash_full::endgame_process() throw(DBError) { 
     2390void Algo_hash_full::endgame_process(bool commit) throw(DBError) { 
    22772391  for(vector<pair<hashtype, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 
    22782392    Move* continuation = 0; 
     
    22812395    delete *it; 
    22822396  } 
     2397  if (commit) { 
     2398    int rc = insert_all_hashes(); 
     2399    if (rc) printf("ouch %d\n",rc); 
     2400  } else hash_vector.clear(); 
    22832401  delete lfc; 
    22842402  delete moveNumber; 
     
    24672585 
    24682586int Algo_hash::insert_hash(hashtype hashCode, int pos) { 
    2469   // printf("insert %lld\n", hashCode); 
     2587  hash_vector.push_back(make_pair(hashCode, pos)); 
     2588  return 0; 
     2589
     2590 
     2591int Algo_hash::insert_all_hashes() { 
     2592  // printf("insert all hashes %d\n", hash_vector.size()); 
    24702593  char sql[200]; 
    24712594  sprintf(sql, "insert into algo_hash_%d_%s (hash, gameid, position) values (?,?,?);", boardsize, dbnameext.c_str()); 
     
    24732596  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
    24742597  if (rc != SQLITE_OK || ppStmt==0) return rc; 
    2475   rc = sqlite3_bind_int64(ppStmt, 1, hashCode); 
    2476   if (rc != SQLITE_OK) return rc; 
    24772598  rc = sqlite3_bind_int(ppStmt, 2, gid); 
    24782599  if (rc != SQLITE_OK) return rc; 
    2479   rc = sqlite3_bind_int(ppStmt, 3, pos); 
    2480   if (rc != SQLITE_OK) return rc; 
    2481   rc = sqlite3_step(ppStmt); 
    2482   if (rc != SQLITE_DONE) return rc; 
     2600  for(vector<pair<hashtype, int> >::iterator it = hash_vector.begin(); it != hash_vector.end(); it++) { 
     2601    // printf("insert %d %d\n", it->first, it->second); 
     2602    rc = sqlite3_bind_int64(ppStmt, 1, it->first); 
     2603    if (rc != SQLITE_OK) return rc; 
     2604    rc = sqlite3_bind_int(ppStmt, 3, it->second); 
     2605    if (rc != SQLITE_OK) return rc; 
     2606    rc = sqlite3_step(ppStmt); 
     2607    if (rc != SQLITE_DONE) return rc; 
     2608    rc = sqlite3_reset(ppStmt); 
     2609    if (rc != SQLITE_OK) return rc; 
     2610  } 
    24832611  rc = sqlite3_finalize(ppStmt); 
    24842612  if (rc != SQLITE_OK) return rc; 
     
    25282656      (*it)->changed = false; 
    25292657      pair<hashtype,int> phi = (*it)->cHC(); 
    2530       if (insert_hash(phi.first, phi.second) != 0) throw DBError(); 
     2658      insert_hash(phi.first, phi.second); 
     2659      // printf("insert hash CORNER %d %d\n", phi.first, phi.second); 
    25312660    } 
    25322661  } 
     
    25692698} 
    25702699 
    2571 void Algo_hash::endgame_process() { 
     2700void Algo_hash::endgame_process(bool commit) { 
    25722701  for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 
    25732702    (*it)->finalize(); 
     2703  if (commit) { 
     2704    int rc = insert_all_hashes(); 
     2705    if (rc) printf("ouch %d\n",rc); 
     2706    hash_vector.clear(); 
     2707  } else hash_vector.clear(); 
    25742708} 
    25752709  
     
    27292863        } 
    27302864      } 
    2731       if (ns > maxNumStones) return CompleteHashKey(); // NOT_HASHABLE 
     2865      if (ns < 3 || ns > maxNumStones) return CompleteHashKey(); // NOT_HASHABLE 
    27322866 
    27332867      // make sure all hash keys are unique 
     
    36313765  if (rootNodeTags.find("RE") == string::npos) rootNodeTags += ",RE"; 
    36323766  if (rootNodeTags.find("DT") == string::npos) rootNodeTags += ",DT"; 
     3767 
     3768  algos |= ALGO_FINALPOS | ALGO_MOVELIST; // these are mandatory at the moment 
    36333769} 
    36343770 
     
    37153851 
    37163852GameList::GameList(char* DBNAME, string ORDERBY, string FORMAT, ProcessOptions* p_options, int cache) throw(DBError) { 
     3853  duplicates = 0; 
    37173854  labels = 0; 
    37183855  continuations = 0; 
     
    39494086  if (labels) delete [] labels; 
    39504087  if (continuations) delete [] continuations; 
     4088  if (duplicates) delete duplicates; 
    39514089  delete [] dbname; 
    39524090  if (all) { 
     
    40584196 
    40594197void GameList::makeIndexHit(int index, vector<Hit* > * hits) { 
    4060   int start = current; 
    4061   int end = oldList->size(); 
    4062   int m = start; 
    4063   while (start < end) { 
    4064     m = (end+start)/2; 
    4065     if (index == (*oldList)[m].first) { 
    4066       currentList->push_back((*oldList)[m]); 
    4067       if (hits) { 
    4068         if ((*all)[(*oldList)[m].second]->hits) delete (*all)[(*oldList)[m].second]->hits; 
    4069         (*all)[(*oldList)[m].second]->hits = hits; 
    4070       } 
    4071       break; 
    4072     } else { 
    4073       if (index < (*oldList)[m].first) end = m; 
    4074       else start = m+1; 
    4075     } 
    4076   } 
    4077   current = m; 
     4198  int m = get_current_index(index, &current); 
     4199  if (m != -1) { 
     4200    currentList->push_back((*oldList)[m]); 
     4201    if (hits) { 
     4202      if ((*all)[(*oldList)[m].second]->hits) delete (*all)[(*oldList)[m].second]->hits; 
     4203      (*all)[(*oldList)[m].second]->hits = hits; 
     4204    } 
     4205  } 
    40784206} 
    40794207 
    40804208void GameList::makeIndexCandidate(int index, vector<Candidate* > * candidates) { 
    4081   int start = current; 
    4082   int end = oldList->size(); 
    4083   int m = start; 
    4084   while (start < end) { 
    4085     m = (end+start)/2; 
    4086     if (index == (*oldList)[m].first) { 
    4087       currentList->push_back((*oldList)[m]); 
    4088       if (candidates) { 
    4089         if ((*all)[(*oldList)[m].second]->candidates) delete (*all)[(*oldList)[m].second]->candidates; 
    4090         (*all)[(*oldList)[m].second]->candidates = candidates; 
    4091       } 
    4092       break; 
    4093     } else { 
    4094       if (index < (*oldList)[m].first) end = m; 
    4095       else start = m+1; 
    4096     } 
    4097   } 
    4098   current = m; 
     4209  int m = get_current_index(index, &current); 
     4210  if (m != -1) { 
     4211    currentList->push_back((*oldList)[m]); 
     4212    if (candidates) { 
     4213      if ((*all)[(*oldList)[m].second]->candidates) delete (*all)[(*oldList)[m].second]->candidates; 
     4214      (*all)[(*oldList)[m].second]->candidates = candidates; 
     4215    } 
     4216  } 
    40994217} 
    41004218 
     
    41734291  if (rc != SQLITE_OK) throw DBError(); 
    41744292  return result; 
     4293} 
     4294 
     4295void GameList::insert_duplicate(int i1, int i2, vector<vector<int> >* dupl) { 
     4296  int ii1 = get_current_index_CL(i1); 
     4297  int ii2 = get_current_index_CL(i2); 
     4298  // printf("insert_duplicate %d, %d\n", ii1, ii2); 
     4299  if (ii1 == -1 || ii2 == -1) return; 
     4300  bool inserted = false; 
     4301  for(vector<vector<int> >::iterator it = dupl->begin(); it != dupl->end(); it++) { 
     4302    vector<int>::iterator i = it->begin(); 
     4303    while(i != it->end() && *i != ii1 && *i != ii2) i++; 
     4304    if (i == it->end()) continue; 
     4305    int insert = ii1; 
     4306    if (*i == ii1) insert = ii2; 
     4307    while (i != it->end() && *i != insert) i++; 
     4308    if (i == it->end()) it->push_back(insert); 
     4309    sort(it->begin(), it->end()); 
     4310    inserted = true; 
     4311    break; 
     4312  } 
     4313  if (!inserted) { 
     4314    vector<int> new_list; 
     4315    if (ii1 < ii2) { 
     4316      new_list.push_back(ii1); 
     4317      new_list.push_back(ii2); 
     4318    } else { 
     4319      new_list.push_back(ii2); 
     4320      new_list.push_back(ii1); 
     4321    } 
     4322    dupl->push_back(new_list); 
     4323  } 
     4324} 
     4325 
     4326int GameList::find_duplicates(int bs, bool strict) throw(DBError) { 
     4327  int bs_index = 0; 
     4328  if (duplicates) delete duplicates; 
     4329  duplicates = new vector<vector<int> >; 
     4330  if (strict) { 
     4331    vector<int>::iterator it = boardsizes.begin(); 
     4332    while (it != boardsizes.end() && *it != bs) { 
     4333      bs_index++; 
     4334      it++; 
     4335    } 
     4336    if (it == boardsizes.end()) { 
     4337      return 0; 
     4338    } 
     4339  } 
     4340  sort(currentList->begin(), currentList->end()); 
     4341  sqlite3_stmt *ppStmt=0; 
     4342  char sql[200]; 
     4343  sprintf(sql, "select as1.id,as2.id from algo_signature_%d as1 join algo_signature_%d as2 on as1.signature = as2.signature where as1.id < as2.id;", bs, bs); 
     4344  int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
     4345  if (rc != SQLITE_OK || ppStmt==0) throw DBError(); 
     4346  do { 
     4347    rc = sqlite3_step(ppStmt); 
     4348    if (rc != SQLITE_DONE && rc != SQLITE_ROW) throw DBError(); 
     4349    if (rc == SQLITE_ROW) { 
     4350      if (!strict || ((Algo_finalpos*)algo_ps[20*bs_index+algo_finalpos])->equal(sqlite3_column_int(ppStmt, 0), sqlite3_column_int(ppStmt, 1))) 
     4351        insert_duplicate(sqlite3_column_int(ppStmt, 0), sqlite3_column_int(ppStmt, 1), duplicates); 
     4352    } 
     4353  } while (rc == SQLITE_ROW); 
     4354  rc = sqlite3_finalize(ppStmt); 
     4355  if (rc != SQLITE_OK) throw DBError(); 
     4356  sort(currentList->begin(), currentList->end(), sndcomp); 
     4357  return duplicates->size(); 
     4358} 
     4359 
     4360vector<int> GameList::retrieve_duplicates_VI(unsigned int i) { 
     4361  if (i>=duplicates->size()) return vector<int>(); 
     4362  return (*duplicates)[i]; 
     4363} 
     4364 
     4365int* GameList::retrieve_duplicates_PI(unsigned int i) { 
     4366  if (i>=duplicates->size()) return 0; 
     4367  int* result = new int[(*duplicates)[i].size()+1]; 
     4368  int j = 0; 
     4369  for(vector<int>::iterator it = (*duplicates)[i].begin(); it != (*duplicates)[i].end(); it++) 
     4370    result[j++] = *it; 
     4371  result[(*duplicates)[i].size()] = -1; 
     4372  return result; 
     4373} 
     4374 
     4375 
     4376int GameList::get_current_index(int id, int* start) { 
     4377  // use this in between start_sorted() and end_sorted() only! 
     4378  int end = oldList->size(); 
     4379  int m = *start; 
     4380  while (*start < end) { 
     4381    m = (end+*start)/2; 
     4382    if (id == (*oldList)[m].first) { 
     4383      *start = m; 
     4384      return m; 
     4385    } else { 
     4386      if (id < (*oldList)[m].first) end = m; 
     4387      else *start = m+1; 
     4388    } 
     4389  } 
     4390  return -1;  
     4391} 
     4392 
     4393int GameList::get_current_index_CL(int id, int start) { 
     4394  // use this in between start_sorted() and end_sorted() only! 
     4395  int end = currentList->size(); 
     4396  int m = start; 
     4397  while (start < end) { 
     4398    m = (end+start)/2; 
     4399    if (id == (*currentList)[m].first) return m; 
     4400    else { 
     4401      if (id < (*currentList)[m].first) end = m; 
     4402      else start = m+1; 
     4403    } 
     4404  } 
     4405  return -1;  
    41754406} 
    41764407 
     
    42744505 
    42754506string GameList::currentEntryAsString(int i) { 
    4276   if (i < 0 || i >= (int)currentList->size()) return ""; 
    4277   else return (*all)[(*currentList)[i].second]->gameInfoStr + resultsStr((*all)[(*currentList)[i].second]); 
     4507  if (i < 0 || i >= (int)currentList->size()) { 
     4508    return ""; 
     4509  } else return (*all)[(*currentList)[i].second]->gameInfoStr + resultsStr((*all)[(*currentList)[i].second]); 
    42784510} 
    42794511 
     
    45564788} 
    45574789 
    4558 int GameList::process(const char* sgf, const char* path, const char* fn, const char* DBTREE) throw(SGFError,DBError) { 
     4790int GameList::process(const char* sgf, const char* path, const char* fn, 
     4791                      const char* DBTREE, int flags) throw(SGFError,DBError) { 
     4792  process_results_vector.clear(); 
    45594793  const char* dbtree = ""; 
    45604794  if (DBTREE) dbtree = DBTREE; 
     
    45644798    c = new Cursor(sgf, 1); // parse sgf sloppily 
    45654799  } catch (SGFError) { 
    4566     return 1
     4800    return 0
    45674801  } 
    45684802 
     
    45734807    printf("current: %d\n", current); 
    45744808    current++; 
     4809    int return_val = 0; 
    45754810    if (algo_db2 && !(current%5000)) { 
    45764811      int rc = sqlite3_exec(algo_db2, "commit", 0, 0, 0); 
     
    45964831    if (sz=="") sz = "19"; 
    45974832    int bs = atoi(sz.c_str()); 
     4833    if (bs < 3) {  
     4834      return_val |= UNACCEPTABLE_BOARDSIZE;  
     4835      process_results_vector.push_back(return_val); 
     4836      delete [] rootNodeProperties; 
     4837      root = root->down; 
     4838      pos++; 
     4839      continue; 
     4840    } 
    45984841    int algo_offset = -1; 
    45994842    int bs_ctr = 0; 
     
    47244967    int game_id = sqlite3_last_insert_rowid(db); 
    47254968 
    4726     // evaluate tags 
    4727     if ((*rootNodeProperties)[posHA] != "") { // handicap game 
    4728       char sql[100]; 
    4729       sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, HANDI_TAG); 
    4730       rc = sqlite3_exec(db, sql, 0, 0, 0); 
    4731       if (rc != SQLITE_OK)  throw DBError(); 
    4732     } 
    4733     if ((*rootNodeProperties)[posWR].find('p') != string::npos || 
    4734         (*rootNodeProperties)[posBR].find('p') != string::npos) {  
    4735       // at least one of the players is professional 
    4736       char sql[100]; 
    4737       sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, PROFESSIONAL_TAG); 
    4738       rc = sqlite3_exec(db, sql, 0, 0, 0); 
    4739       if (rc != SQLITE_OK)  throw DBError(); 
    4740     } 
    4741  
    4742     delete rootNodeProperties; 
    47434969 
    47444970    // printf("play through the game\n"); 
     4971    bool commit = true; 
    47454972 
    47464973    Node* currentN = root; 
     
    48775104        }  
    48785105      } catch (SGFError) { 
     5106        return_val |= SGF_ERROR; 
    48795107        caughtSGFError = true; 
     5108        if (OMIT_GAMES_WITH_SGF_ERRORS) { 
     5109          commit = false; 
     5110          // (FIXME should exit from the loop here) 
     5111        } 
    48805112      } 
    48815113 
     
    48915123      } 
    48925124 
    4893       if (caughtSGFError) currentN = 0; 
     5125      if (caughtSGFError) currentN = 0; // stop here with this branch 
    48945126      else currentN = currentN->next; 
    48955127 
     
    49115143    } // while 
    49125144    { 
     5145      // check for duplicates (if desired) 
     5146      bool is_duplicate = false; 
     5147      if (flags & (CHECK_FOR_DUPLICATES|CHECK_FOR_DUPLICATES_STRICT)) { 
     5148        char* sig = ((Algo_signature*)algo_ps[20*algo_offset])->get_current_signature(); 
     5149        vector<int> all_duplicates = ((Algo_signature*)algo_ps[20*algo_offset])->search_signature(sig); 
     5150        if (all_duplicates.size()) { 
     5151          // printf("dupl %d\n", all_duplicates.size()); 
     5152          is_duplicate = true; 
     5153          if ((flags & CHECK_FOR_DUPLICATES_STRICT) && (p_op->algos & ALGO_FINALPOS)) { 
     5154            vector<int>::iterator d_it = all_duplicates.begin(); 
     5155            while (d_it != all_duplicates.end() && !((Algo_finalpos*)algo_ps[20*algo_offset + algo_finalpos])->equals_current(*d_it)) 
     5156              d_it++; 
     5157            if (d_it == all_duplicates.end()) is_duplicate = false; 
     5158          } 
     5159          if (is_duplicate) { 
     5160            return_val |= IS_DUPLICATE; 
     5161            if (flags & OMIT_DUPLICATES) commit = false; 
     5162          } 
     5163        } 
     5164        delete [] sig; 
     5165      } 
     5166       
     5167      if (commit) { 
     5168        // evaluate tags 
     5169        if ((*rootNodeProperties)[posHA] != "") { // handicap game 
     5170          char sql[100]; 
     5171          sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, HANDI_TAG); 
     5172          rc = sqlite3_exec(db, sql, 0, 0, 0); 
     5173          if (rc != SQLITE_OK)  throw DBError(); 
     5174        } 
     5175        if ((*rootNodeProperties)[posWR].find('p') != string::npos || 
     5176            (*rootNodeProperties)[posBR].find('p') != string::npos) {  
     5177          // at least one of the players is professional 
     5178          char sql[100]; 
     5179          sprintf(sql, "insert into GAME_TAGS (game_id, tag_id) values (%d, %d);", game_id, PROFESSIONAL_TAG); 
     5180          rc = sqlite3_exec(db, sql, 0, 0, 0); 
     5181          if (rc != SQLITE_OK)  throw DBError(); 
     5182        } 
     5183      } else { 
     5184        return_val |= NOT_INSERTED_INTO_DB; 
     5185        char sql[200]; 
     5186        sprintf(sql, "delete from GAMES where id=%d", game_id); 
     5187        rc = sqlite3_exec(db, sql, 0, 0, 0); 
     5188        if (rc) printf("ouch %d\n", rc); 
     5189      } 
    49135190      for(int a=20*algo_offset; a < 20*(algo_offset+1); a++) { 
    49145191        // printf("endgame %d\n", a); 
    4915         if (algo_ps[a]) algo_ps[a]->endgame_process(); 
     5192        if (algo_ps[a]) algo_ps[a]->endgame_process(commit); 
    49165193      } 
    49175194    } 
     5195    delete rootNodeProperties; 
     5196    process_results_vector.push_back(return_val); 
    49185197    root = root->down; 
    49195198    pos++; 
    49205199  } 
    49215200  delete c; 
    4922   return 0; // success 
    4923 
     5201  return process_results_vector.size(); 
     5202
     5203 
     5204int GameList::process_results(unsigned int i) { 
     5205  if (i<0 || i>=process_results_vector.size()) return INDEX_OUT_OF_RANGE; 
     5206  return process_results_vector[i]; 
     5207
     5208 
    49245209 
    49255210VarInfo::VarInfo(Node* N, abstractBoard* B, int I) { 
  • 06/libkombilo-branches/hash_center/search.h

    r238 r240  
    211211    virtual void branchpoint_process(); 
    212212    virtual void endOfVariation_process(); 
    213     virtual void endgame_process(); 
     213    virtual void endgame_process(bool commit=true); 
    214214    virtual void finalize_process(); 
    215215    virtual int readDB(sqlite3* DB); 
     
    235235    void branchpoint_process(); 
    236236    void endOfVariation_process(); 
    237     void endgame_process() throw(DBError); 
     237    void endgame_process(bool commit=true) throw(DBError); 
    238238    void finalize_process(); 
    239239 
    240240    int counter; 
    241241    char* signature; 
     242    char* get_current_signature(); 
     243    std::vector<int> search_signature(char* sig); 
    242244  private: 
    243245    bool main_variation; 
     
    258260    void branchpoint_process(); 
    259261    void endOfVariation_process(); 
    260     void endgame_process() throw(DBError); 
     262    void endgame_process(bool commit=true) throw(DBError); 
    261263    void finalize_process(); 
    262264 
     
    266268    int readDB(sqlite3* DB); 
    267269    int search(PatternList& patternList, GameList& gl, SearchOptions& options); 
     270 
     271    bool equal(unsigned int id1, unsigned int id2); // id1, id2 refer to id's in the database! 
     272    bool equals_current(unsigned int id1); 
    268273}; 
    269274 
     
    327332    void branchpoint_process(); 
    328333    void endOfVariation_process(); 
    329     void endgame_process() throw(DBError); 
     334    void endgame_process(bool commit=true) throw(DBError); 
    330335    void finalize_process(); 
    331336    int readDB(sqlite3* DB); 
     
    339344}; 
    340345 
     346class HashFEntry { 
     347  public: 
     348    hashtype hashCode; 
     349    char* buf; 
     350    int length; 
     351 
     352    HashFEntry(hashtype HASHCODE, char* BUF, int LENGTH); 
     353    HashFEntry(const HashFEntry& hfe); 
     354    ~HashFEntry(); 
     355}; 
    341356 
    342357class HashhitF { // hashing hit for full board search 
     
    384399    void branchpoint_process(); 
    385400    void endOfVariation_process() throw(DBError); 
    386     void endgame_process() throw(DBError); 
     401    void endgame_process(bool commit=true) throw(DBError); 
    387402    void finalize_process(); 
    388403    int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); 
    389404 
    390     int insert_hash(hashtype hashCode, ExtendedMoveNumber& mn, Move* continuation); 
    391405    hashtype compute_hashkey(Pattern& pattern); 
    392406 
     407    int maxNumStones; 
     408    int numStones; 
     409  private: 
    393410    hashtype currentHashCode; 
    394411    ExtendedMoveNumber* moveNumber; 
    395412    std::vector<std::pair<hashtype, ExtendedMoveNumber>* > *lfc; // hash code + move number, still looking for continuation 
    396413    std::stack<HashVarInfo>* branchpoints; 
    397     int maxNumStones; 
    398     int numStones; 
     414    int insert_hash(hashtype hashCode, ExtendedMoveNumber& mn, Move* continuation); 
     415    int insert_all_hashes(); 
     416    std::vector<HashFEntry> hash_vector; 
    399417}; 
    400418 
     
    498516    virtual void branchpoint_process(); 
    499517    virtual void endOfVariation_process(); 
    500     virtual void endgame_process(); 
     518    virtual void endgame_process(bool commit=true); 
    501519    virtual void finalize_process(); 
    502520    virtual HashQuery searchQuery(PatternList& patternList); 
    503521    virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); 
    504522 
    505     virtual int insert_hash(hashtype hashCod, int pos); 
    506523    virtual CompleteHashKey compute_hashkey(PatternList& pl, int CS); 
    507524    std::vector<HashInstance* >* hi; 
     525    static const hashtype hashCodes[]; 
    508526    std::string dbnameext; 
    509527    int maxNumStones; 
    510  
    511     static const hashtype hashCodes[]; 
     528    std::vector<std::pair<hashtype, int> > hash_vector; 
     529    virtual int insert_hash(hashtype hashCod, int pos); 
     530    int insert_all_hashes(); 
    512531}; 
    513532 
     
    645664    ~VarInfo(); 
    646665}; 
     666 
     667// process flags (used to determine the behavior for individual games - in contrast to 
     668// options which apply to the whole GameList and are given in ProcessOptions) 
     669const int CHECK_FOR_DUPLICATES = 1; // check for duplicates using the signature 
     670const int CHECK_FOR_DUPLICATES_STRICT = 2; // check for duplicates using the final position 
     671                                           // (if ALGO_FINAPOS is available) 
     672const int OMIT_DUPLICATES = 4; 
     673const int OMIT_GAMES_WITH_SGF_ERRORS = 8;  
     674const int INDEX_OUT_OF_RANGE = 16; 
     675 
     676// process return values 
     677// 0:   SGF error occurred when parsing the "tree structure" (i.e. before parsing the individual nodes) 
     678//      database was not changed 
     679// n>0: n games were processed, use process_results to access the individual results  
     680 
     681// flags used in process_results 
     682const int UNACCEPTABLE_BOARDSIZE = 1; // (database not changed)  
     683const int SGF_ERROR = 2; 
     684    // SGF error occurred when playing through the game  
     685    // (and the rest of the concerning variation was not used). 
     686    // Depending on OMIT_GAMES_WITH_SGF_ERRORS, everything before this node (and other variations,  
     687    // if any) was inserted, or the database was not changed. 
     688const int IS_DUPLICATE = 4; 
     689const int NOT_INSERTED_INTO_DB = 8; 
     690 
    647691 
    648692class GameList { 
     
    682726    // ------- processing SGF games (to populate the db) -------------------------- 
    683727    void start_processing(int PROCESSVARIATIONS=-1) throw(DBError); 
    684     int process(const char* sgf, const char* path, const char* fn, const char* DBTREE = 0) throw(SGFError,DBError); 
     728    int process(const char* sgf, const char* path, const char* fn, 
     729                const char* DBTREE = 0, int flags=0) throw(SGFError,DBError); 
     730    int process_results(unsigned int i=0); // result for i-th processed game in most recently processed SGF collection 
    685731    void finalize_processing() throw(DBError); 
    686732     
     733    // int remove_game(int index); // TODO 
     734    // int remove_all_current_games(); 
     735 
    687736    // ------- pattern search ----------------------------------------------------- 
    688737    // options is copied in the search method (if != 0), so the caller has to free the pointer 
     
    705754    void deleteTag(int tag, int i = -1) throw(DBError); 
    706755    std::vector<int> getTags(int i, int tag=0) throw(DBError); // note the order of arguments! 
     756     
     757    // ------- duplicates --------------------------------------------------------- 
     758    int find_duplicates(int bs, bool strict=false) throw(DBError); // return number of duplicate array 
     759    std::vector<int> retrieve_duplicates_VI(unsigned int i); 
     760    int* retrieve_duplicates_PI(unsigned int i); // same as above, but returns Pointer to Int  
     761                                                 // (an array terminated by -1) 
     762                                                 // The caller must free the pointer himself 
     763                                                 // (before calling find_duplicates again). 
    707764 
    708765    // ------- misc --------------------------------------------------------------- 
     
    735792    void makeIndexHit(int index, std::vector<Hit* > *hits); 
    736793    void setCurrentFromIndex(int index); 
     794    int get_current_index(int id, int* start); // returns the index in oldList of the game with game id "id"  
     795                                               // (if available, otherwise returns -1), 
     796                                               // use this between start_sorted and end_sorted 
     797    int get_current_index_CL(int id, int start=0); // returns the index in currentList of the game with game id "id"  
     798