Changeset 240
- Timestamp:
- 03/09/07 16:51:04 (1 year ago)
- Files:
-
- 06/libkombilo-branches/hash_center/Makefile (modified) (1 diff)
- 06/libkombilo-branches/hash_center/abstractboard.cpp (modified) (1 diff)
- 06/libkombilo-branches/hash_center/cpptest.cpp (modified) (4 diffs)
- 06/libkombilo-branches/hash_center/libkombilo.i (modified) (1 diff)
- 06/libkombilo-branches/hash_center/process.py (modified) (2 diffs)
- 06/libkombilo-branches/hash_center/search.cpp (modified) (30 diffs)
- 06/libkombilo-branches/hash_center/search.h (modified) (13 diffs)
- 06/libkombilo-branches/hash_center/testsearch.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo-branches/hash_center/Makefile
r238 r240 2 2 g++ -o cpptest -lsqlite3 -lboost_filesystem cpptest.o search.o sgfparser.o abstractboard.o 3 3 4 cpptest.o: cpptest.cpp 4 cpptest.o: cpptest.cpp search.h abstractboard.h sgfparser.h 5 5 g++ -c cpptest.cpp 6 6 06/libkombilo-branches/hash_center/abstractboard.cpp
r225 r240 84 84 if (DEBUG_ABSTRACTBOARD) printf("AB::abstractBoard(int) 0\n"); 85 85 boardsize = bs; 86 if (boardsize < 3) throw BoardError(); 86 if (boardsize < 3) throw BoardError(); // FIXME 87 87 status = new char[boardsize*boardsize+1]; 88 88 for (int i = 0; i < boardsize*boardsize; i++) 06/libkombilo-branches/hash_center/cpptest.cpp
r238 r240 32 32 // string path = "/home/ug/go/gtl/reviews"; 33 33 string path = "/home/ug/go/gogod06/2000"; 34 int counter = 0; 34 35 for(directory_iterator it(path); it != end_itr; ++it) { 35 36 string n = it->string(); … … 46 47 } 47 48 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++; 49 53 } 50 54 } 51 55 gl.finalize_processing(); 52 printf(" Processed %d games.\n", gl.size());56 printf("Now %d games in db.\n", gl.size()); 53 57 } 54 58 printf("%d games.\n", gl.size()); … … 90 94 91 95 // -------------------- do pattern search -------------------------------------- 92 gl.search(p, &so);96 // gl.search(p, &so); 93 97 94 98 // ------------------- 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()); 96 100 // vector<string> res = gl.currentEntriesAsStrings(); 97 101 // for(vector<string>::iterator it = res.begin(); it != res.end(); it++) … … 100 104 101 105 // ------------------- 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++; 108 148 } 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; 114 150 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"); 125 152 } 126 153 06/libkombilo-branches/hash_center/libkombilo.i
r191 r240 5 5 namespace std { 6 6 %template(vectors) vector<string>; 7 %template(vectori) vector<int>; 7 8 }; 8 9 06/libkombilo-branches/hash_center/process.py
r238 r240 31 31 for filename in filenames: 32 32 # print filename 33 counter += 134 33 try: 35 34 file = open(filename) … … 41 40 42 41 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 44 48 gl.finalize_processing() 45 49 print 'Processed %d games in %.2f seconds' % (counter, time.time()-starttime) 06/libkombilo-branches/hash_center/search.cpp
r238 r240 871 871 void Algorithm::branchpoint_process() {} 872 872 void Algorithm::endOfVariation_process() {} 873 void Algorithm::endgame_process( ) {}873 void Algorithm::endgame_process(bool commit) {} 874 874 void Algorithm::finalize_process() {} 875 875 int Algorithm::readDB(sqlite3* DB) { return 0; } … … 892 892 sprintf(sql, "create table if not exists algo_signature_%d ( id integer primary key, signature varchar(12) );", boardsize); 893 893 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); 894 897 if (rc != SQLITE_OK) throw DBError(); 895 898 } … … 987 990 } 988 991 989 void Algo_signature::endgame_process() throw(DBError) { 990 char* min_signature = symmetrize(signature, boardsize); 992 void 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 } 991 1001 delete [] signature; 1002 } 1003 1004 void Algo_signature::finalize_process() { 1005 } 1006 1007 char* Algo_signature::get_current_signature() { 1008 return symmetrize(signature, boardsize); 1009 } 1010 1011 vector<int> Algo_signature::search_signature(char* sig) { 1012 // to be used during processing! (because we need the db) 992 1013 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 } 1002 1032 1003 1033 Algo_finalpos::Algo_finalpos(int bsize) : Algorithm(bsize) { … … 1063 1093 } 1064 1094 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(); 1095 void 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 } 1069 1101 delete [] fp; 1070 1102 } … … 1241 1273 } 1242 1274 1275 bool 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 1305 bool 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 1243 1326 1244 1327 Algo_movelist::Algo_movelist(int bsize) : Algorithm(bsize) { … … 1348 1431 } 1349 1432 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; 1433 void 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 } 1360 1445 delete [] fpC; 1361 1446 } … … 2069 2154 #endif 2070 2155 2156 HashFEntry::HashFEntry(hashtype HASHCODE, char* BUF, int LENGTH) { 2157 hashCode = HASHCODE; 2158 buf = BUF; 2159 length = LENGTH; 2160 } 2161 2162 HashFEntry::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 2169 HashFEntry::~HashFEntry() { 2170 if (buf) { 2171 delete [] buf; 2172 buf = 0; 2173 } 2174 } 2175 2071 2176 HashhitF::HashhitF(int GAMEID, char ORIENTATION, char* blob) { 2072 2177 gameid = GAMEID; … … 2145 2250 buf[6+2*i] = mn.data[i]%256; 2146 2251 } 2252 hash_vector.push_back(HashFEntry(hashCode, buf, 5+2*mn.length)); 2253 return 0; 2254 } 2255 2256 int Algo_hash_full::insert_all_hashes() { 2147 2257 char sql[100]; 2148 2258 sprintf(sql, "insert into algo_hash_full_%d (hash, gameid, hit) values (?,?,?);", boardsize); … … 2150 2260 int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 2151 2261 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 } 2160 2274 rc = sqlite3_finalize(ppStmt); 2161 2275 if (rc != SQLITE_OK) return rc; 2162 delete [] buf;2276 hash_vector.clear(); 2163 2277 return 0; // success 2164 2278 } … … 2274 2388 } 2275 2389 2276 void Algo_hash_full::endgame_process( ) throw(DBError) {2390 void Algo_hash_full::endgame_process(bool commit) throw(DBError) { 2277 2391 for(vector<pair<hashtype, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 2278 2392 Move* continuation = 0; … … 2281 2395 delete *it; 2282 2396 } 2397 if (commit) { 2398 int rc = insert_all_hashes(); 2399 if (rc) printf("ouch %d\n",rc); 2400 } else hash_vector.clear(); 2283 2401 delete lfc; 2284 2402 delete moveNumber; … … 2467 2585 2468 2586 int 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 2591 int Algo_hash::insert_all_hashes() { 2592 // printf("insert all hashes %d\n", hash_vector.size()); 2470 2593 char sql[200]; 2471 2594 sprintf(sql, "insert into algo_hash_%d_%s (hash, gameid, position) values (?,?,?);", boardsize, dbnameext.c_str()); … … 2473 2596 int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 2474 2597 if (rc != SQLITE_OK || ppStmt==0) return rc; 2475 rc = sqlite3_bind_int64(ppStmt, 1, hashCode);2476 if (rc != SQLITE_OK) return rc;2477 2598 rc = sqlite3_bind_int(ppStmt, 2, gid); 2478 2599 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 } 2483 2611 rc = sqlite3_finalize(ppStmt); 2484 2612 if (rc != SQLITE_OK) return rc; … … 2528 2656 (*it)->changed = false; 2529 2657 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); 2531 2660 } 2532 2661 } … … 2569 2698 } 2570 2699 2571 void Algo_hash::endgame_process( ) {2700 void Algo_hash::endgame_process(bool commit) { 2572 2701 for(vector<HashInstance* >::iterator it = hi->begin(); it != hi->end(); it++) 2573 2702 (*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(); 2574 2708 } 2575 2709 … … 2729 2863 } 2730 2864 } 2731 if (ns > maxNumStones) return CompleteHashKey(); // NOT_HASHABLE2865 if (ns < 3 || ns > maxNumStones) return CompleteHashKey(); // NOT_HASHABLE 2732 2866 2733 2867 // make sure all hash keys are unique … … 3631 3765 if (rootNodeTags.find("RE") == string::npos) rootNodeTags += ",RE"; 3632 3766 if (rootNodeTags.find("DT") == string::npos) rootNodeTags += ",DT"; 3767 3768 algos |= ALGO_FINALPOS | ALGO_MOVELIST; // these are mandatory at the moment 3633 3769 } 3634 3770 … … 3715 3851 3716 3852 GameList::GameList(char* DBNAME, string ORDERBY, string FORMAT, ProcessOptions* p_options, int cache) throw(DBError) { 3853 duplicates = 0; 3717 3854 labels = 0; 3718 3855 continuations = 0; … … 3949 4086 if (labels) delete [] labels; 3950 4087 if (continuations) delete [] continuations; 4088 if (duplicates) delete duplicates; 3951 4089 delete [] dbname; 3952 4090 if (all) { … … 4058 4196 4059 4197 void 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, ¤t); 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 } 4078 4206 } 4079 4207 4080 4208 void 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, ¤t); 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 } 4099 4217 } 4100 4218 … … 4173 4291 if (rc != SQLITE_OK) throw DBError(); 4174 4292 return result; 4293 } 4294 4295 void 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 4326 int 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 4360 vector<int> GameList::retrieve_duplicates_VI(unsigned int i) { 4361 if (i>=duplicates->size()) return vector<int>(); 4362 return (*duplicates)[i]; 4363 } 4364 4365 int* 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 4376 int 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 4393 int 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; 4175 4406 } 4176 4407 … … 4274 4505 4275 4506 string 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]); 4278 4510 } 4279 4511 … … 4556 4788 } 4557 4789 4558 int GameList::process(const char* sgf, const char* path, const char* fn, const char* DBTREE) throw(SGFError,DBError) { 4790 int 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(); 4559 4793 const char* dbtree = ""; 4560 4794 if (DBTREE) dbtree = DBTREE; … … 4564 4798 c = new Cursor(sgf, 1); // parse sgf sloppily 4565 4799 } catch (SGFError) { 4566 return 1;4800 return 0; 4567 4801 } 4568 4802 … … 4573 4807 printf("current: %d\n", current); 4574 4808 current++; 4809 int return_val = 0; 4575 4810 if (algo_db2 && !(current%5000)) { 4576 4811 int rc = sqlite3_exec(algo_db2, "commit", 0, 0, 0); … … 4596 4831 if (sz=="") sz = "19"; 4597 4832 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 } 4598 4841 int algo_offset = -1; 4599 4842 int bs_ctr = 0; … … 4724 4967 int game_id = sqlite3_last_insert_rowid(db); 4725 4968 4726 // evaluate tags4727 if ((*rootNodeProperties)[posHA] != "") { // handicap game4728 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 professional4736 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;4743 4969 4744 4970 // printf("play through the game\n"); 4971 bool commit = true; 4745 4972 4746 4973 Node* currentN = root; … … 4877 5104 } 4878 5105 } catch (SGFError) { 5106 return_val |= SGF_ERROR; 4879 5107 caughtSGFError = true; 5108 if (OMIT_GAMES_WITH_SGF_ERRORS) { 5109 commit = false; 5110 // (FIXME should exit from the loop here) 5111 } 4880 5112 } 4881 5113 … … 4891 5123 } 4892 5124 4893 if (caughtSGFError) currentN = 0; 5125 if (caughtSGFError) currentN = 0; // stop here with this branch 4894 5126 else currentN = currentN->next; 4895 5127 … … 4911 5143 } // while 4912 5144 { 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 } 4913 5190 for(int a=20*algo_offset; a < 20*(algo_offset+1); a++) { 4914 5191 // 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); 4916 5193 } 4917 5194 } 5195 delete rootNodeProperties; 5196 process_results_vector.push_back(return_val); 4918 5197 root = root->down; 4919 5198 pos++; 4920 5199 } 4921 5200 delete c; 4922 return 0; // success 4923 } 5201 return process_results_vector.size(); 5202 } 5203 5204 int 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 4924 5209 4925 5210 VarInfo::VarInfo(Node* N, abstractBoard* B, int I) { 06/libkombilo-branches/hash_center/search.h
r238 r240 211 211 virtual void branchpoint_process(); 212 212 virtual void endOfVariation_process(); 213 virtual void endgame_process( );213 virtual void endgame_process(bool commit=true); 214 214 virtual void finalize_process(); 215 215 virtual int readDB(sqlite3* DB); … … 235 235 void branchpoint_process(); 236 236 void endOfVariation_process(); 237 void endgame_process( ) throw(DBError);237 void endgame_process(bool commit=true) throw(DBError); 238 238 void finalize_process(); 239 239 240 240 int counter; 241 241 char* signature; 242 char* get_current_signature(); 243 std::vector<int> search_signature(char* sig); 242 244 private: 243 245 bool main_variation; … … 258 260 void branchpoint_process(); 259 261 void endOfVariation_process(); 260 void endgame_process( ) throw(DBError);262 void endgame_process(bool commit=true) throw(DBError); 261 263 void finalize_process(); 262 264 … … 266 268 int readDB(sqlite3* DB); 267 269 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); 268 273 }; 269 274 … … 327 332 void branchpoint_process(); 328 333 void endOfVariation_process(); 329 void endgame_process( ) throw(DBError);334 void endgame_process(bool commit=true) throw(DBError); 330 335 void finalize_process(); 331 336 int readDB(sqlite3* DB); … … 339 344 }; 340 345 346 class 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 }; 341 356 342 357 class HashhitF { // hashing hit for full board search … … 384 399 void branchpoint_process(); 385 400 void endOfVariation_process() throw(DBError); 386 void endgame_process( ) throw(DBError);401 void endgame_process(bool commit=true) throw(DBError); 387 402 void finalize_process(); 388 403 int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); 389 404 390 int insert_hash(hashtype hashCode, ExtendedMoveNumber& mn, Move* continuation);391 405 hashtype compute_hashkey(Pattern& pattern); 392 406 407 int maxNumStones; 408 int numStones; 409 private: 393 410 hashtype currentHashCode; 394 411 ExtendedMoveNumber* moveNumber; 395 412 std::vector<std::pair<hashtype, ExtendedMoveNumber>* > *lfc; // hash code + move number, still looking for continuation 396 413 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; 399 417 }; 400 418 … … 498 516 virtual void branchpoint_process(); 499 517 virtual void endOfVariation_process(); 500 virtual void endgame_process( );518 virtual void endgame_process(bool commit=true); 501 519 virtual void finalize_process(); 502 520 virtual HashQuery searchQuery(PatternList& patternList); 503 521 virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); 504 522 505 virtual int insert_hash(hashtype hashCod, int pos);506 523 virtual CompleteHashKey compute_hashkey(PatternList& pl, int CS); 507 524 std::vector<HashInstance* >* hi; 525 static const hashtype hashCodes[]; 508 526 std::string dbnameext; 509 527 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(); 512 531 }; 513 532 … … 645 664 ~VarInfo(); 646 665 }; 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) 669 const int CHECK_FOR_DUPLICATES = 1; // check for duplicates using the signature 670 const int CHECK_FOR_DUPLICATES_STRICT = 2; // check for duplicates using the final position 671 // (if ALGO_FINAPOS is available) 672 const int OMIT_DUPLICATES = 4; 673 const int OMIT_GAMES_WITH_SGF_ERRORS = 8; 674 const 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 682 const int UNACCEPTABLE_BOARDSIZE = 1; // (database not changed) 683 const 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. 688 const int IS_DUPLICATE = 4; 689 const int NOT_INSERTED_INTO_DB = 8; 690 647 691 648 692 class GameList { … … 682 726 // ------- processing SGF games (to populate the db) -------------------------- 683 727 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 685 731 void finalize_processing() throw(DBError); 686 732 733 // int remove_game(int index); // TODO 734 // int remove_all_current_games(); 735 687 736 // ------- pattern search ----------------------------------------------------- 688 737 // options is copied in the search method (if != 0), so the caller has to free the pointer … … 705 754 void deleteTag(int tag, int i = -1) throw(DBError); 706 755 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). 707 764 708 765 // ------- misc --------------------------------------------------------------- … … 735 792 void makeIndexHit(int index, std::vector<Hit* > *hits); 736 793 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
