Changeset 182

Show
Ignore:
Timestamp:
09/21/06 23:57:16 (2 years ago)
Author:
ug
Message:

Finished full board hashing, plus several minor improvements.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • 06/libkombilo/cpptest.cc

    r180 r182  
    22 
    33int main() { 
    4         GameList gl("t1.db", "id", "pw,pb,dt%%1 - %2, %3", ALGO_FINALPOS | ALGO_MOVELIST, 19); 
    5         gl.gisearch("pw = 'Hane Naoki'"); 
    6         Pattern p(CENTER_PATTERN, 19, 3, 5, ".X..OX.OX.OXOXO") ; 
     4        GameList gl("t1.db", "id", "[[PW]] - [[PB]] ([[winner]]), [[filename]]", ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL, 19); 
     5        // gl.gisearch("pw = 'Hane Naoki'"); 
     6        // Pattern p(CENTER_PATTERN, 19, 3, 5, ".X..OX.OX.OXOXO") ; 
     7  Pattern p(FULLBOARD_PATTERN, 19, 19, 19, ".....................O.O........OX......XO......X.OXX.XX...X,.OOXXX..OOOX.O....X.OXOOXOO..OX.......XOXXOXXOOXXOO....OX.XXXOOOXO.O.O...OXX..XOX..XO.X.XO...O.......X.....XO..O.,X....,.....XOO................X......X............X....O...........................................O.O...........O.....,.....X...........X.O.X............................................"); 
    78  PatternList pl(p, 0, 0, 19); 
    89        SearchOptions so; 
  • 06/libkombilo/process.py

    r181 r182  
    1919 
    2020    starttime = time.time() 
    21     algos = ALGO_FINALPOS | ALGO_MOVELIST # | ALGO_HASH_FULL 
     21    algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL 
    2222    try: 
    2323        gl = GameList('t1.db', 'id', '', algos, 19) 
  • 06/libkombilo/search.cc

    r180 r182  
    769769void Algorithm::finalize_process() {} 
    770770int Algorithm::readDB(sqlite3* DB) { return 0; } 
    771 void Algorithm::search(PatternList& patternList, GameList& gl, SearchOptions& options) {
     771int Algorithm::search(PatternList& patternList, GameList& gl, SearchOptions& options) { return -1;
    772772 
    773773Algo_signature::Algo_signature(int bsize) : Algorithm(bsize) { 
     
    945945int Algo_finalpos::readDB(sqlite3* DB) {  
    946946        db = DB; 
    947         // FIXME: do we want to delete data before adding values? 
    948947        if (data) { 
    949948                for(vector<char* >::iterator it = data->begin(); it != data->end(); it++) delete [] *it; 
     
    971970} 
    972971 
    973 void Algo_finalpos::search(PatternList& patternList, GameList& gl, SearchOptions& options) { // progress bar?! 
     972int Algo_finalpos::search(PatternList& patternList, GameList& gl, SearchOptions& options) { // progress bar?! 
    974973 
    975974        // Put the pattern into bitmap format, which is the format the final 
     
    10981097        delete [] allbitlengths; 
    10991098        delete [] allbits; 
     1099        return 0; 
    11001100} 
    11011101 
     
    11941194       
    11951195void Algo_movelist::pass_process() { 
    1196         // FIXME !!! need to add pass move here to end up with correct 
    1197         // counter! 
     1196        movelist.push_back(19); 
     1197        movelist.push_back(19); 
    11981198} 
    11991199 
     
    12651265} 
    12661266 
    1267 void Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) {  
     1267int Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) {  
    12681268        // FIXME progbar, variations!!! 
    12691269 
     
    12941294                // branchpoints = []; 
    12951295 
    1296                 // printf("oh oh\n"); 
    12971296                char* movel = (*data1)[index-1]; 
    12981297                int movelistIndex = 0; 
    12991298                int endMovelist = (*data1l)[index-1]; 
    1300                 // printf("oh oh\n"); 
    13011299                // printf("len movelist: %d\n", (*data1l)[index-1]); 
    13021300                // int nodeCtr = 0; 
     
    16141612                delete [] contListIndex; 
    16151613        } 
     1614        return 0; 
    16161615} 
    16171616 
     
    17481747 
    17491748int Algo_hash::insert_hash(long long hashCode, ExtendedMoveNumber& mn, Move* continuation) { 
    1750         char* sql = "insert into algo_hash (hash, game, position) values (?,?,?);"; 
     1749        // FIXME it would almost certainly improve the performance to use several 
     1750        // integer fields in the db instead of one gameinfo string 
     1751        // probably is not worth the effort, though 
     1752        char* buf = new char[30 + mn.length*2]; 
     1753        if (continuation) { 
     1754          buf[0] = continuation->x; 
     1755                buf[1] = continuation->y; 
     1756          buf[2] = continuation->color; 
     1757        } else { 
     1758                buf[0] = NO_CONT; 
     1759                buf[1] = 0; 
     1760                buf[2] = 0; 
     1761        } 
     1762        buf[3] = mn.length/256; 
     1763        buf[4] = mn.length%256; 
     1764        for (int i=0; i<mn.length; i++) { 
     1765                buf[5+2*i] = mn.data[i]/265; 
     1766                buf[6+2*i] = mn.data[i]%256; 
     1767        } 
     1768        char* sql = "insert into algo_hash (hash, gameid, hit) values (?,?,?);"; 
    17511769        sqlite3_stmt *ppStmt=0; 
    17521770        int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); 
     
    17541772        rc = sqlite3_bind_int64(ppStmt, 1, currentHashCode); 
    17551773        if (rc != SQLITE_OK) return rc; 
    1756         rc = sqlite3_bind_text(ppStmt, 2, game, -1, SQLITE_TRANSIENT);  
    1757         // FIXME: build value from game, continuation, moveNumber, careful: 
    1758         // continuation == 0 is possible 
     1774        rc = sqlite3_bind_int(ppStmt, 2, gid); 
    17591775        if (rc != SQLITE_OK) return rc; 
    1760         rc = sqlite3_bind_text(ppStmt, 3, position, boardsize*boardsize, SQLITE_TRANSIENT); 
     1776        rc = sqlite3_bind_blob(ppStmt, 3, buf, 5+2*mn.length, SQLITE_TRANSIENT);  
    17611777        if (rc != SQLITE_OK) return rc; 
    17621778        rc = sqlite3_step(ppStmt); 
     
    17641780        rc = sqlite3_finalize(ppStmt); 
    17651781        if (rc != SQLITE_OK) return rc; 
     1782        delete [] buf; 
    17661783        return 0; // success 
    17671784} 
     
    17691786void Algo_hash::initialize_process(sqlite3* DB) throw(DBError) { 
    17701787        db = DB; 
    1771         char* zErrMsg = 0
    1772         int rc = sqlite3_exec(db, "create table if not exists algo_hash ( id integer primary key, hash integer, game text, position text );",  
    1773                                      0, 0, &zErrMsg); 
     1788        int rc = sqlite3_exec(db, "create table if not exists algo_hash ( id integer primary key, hash integer, gameid integer, hit text );", 0, 0, 0)
     1789        if (rc != SQLITE_OK) throw DBError(); 
     1790        rc = sqlite3_exec(db, "create index if not exists hash_idx on algo_hash(hash);", 0, 0, 0); 
    17741791        if (rc != SQLITE_OK) throw DBError(); 
    17751792} 
     
    17771794void Algo_hash::newgame_process(int game_id) { 
    17781795        gid = game_id; 
    1779   game = new char[1]; game[0] = 0; // game id FIXME 
    1780         position = new char[boardsize*boardsize]; 
    1781         for(int i=0; i<boardsize*boardsize; i++) position[i] = '.'; 
    17821796        moveNumber = new ExtendedMoveNumber(0); 
    17831797        currentHashCode = 0; // start with empty board 
     
    17891803        for(vector<pair<long long, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 
    17901804                Move* continuation = new Move(x,y,'B'); 
    1791                 int rc = insert_hash((*it)->first, (*it)->second, continuation); 
    1792                 if (rc != 0) printf("ouch %d\n", rc); 
     1805                if (insert_hash((*it)->first, (*it)->second, continuation) != 0) throw DBError(); 
    17931806                delete continuation; 
    17941807                delete *it; 
     
    17961809        delete lfc; 
    17971810        lfc = new vector<pair<long long, ExtendedMoveNumber>* >; 
    1798         position[x+boardsize*y] = 'B'; 
    17991811        currentHashCode += hashCodes[x + boardsize*y]; 
    18001812} 
     
    18031815        for(vector<pair<long long, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 
    18041816                Move* continuation = new Move(x,y,'W'); 
    1805                 int rc = insert_hash((*it)->first, (*it)->second, continuation); 
    1806                 if (rc != 0) printf("ouch %d\n", rc); 
     1817                if (insert_hash((*it)->first, (*it)->second, continuation) != 0) throw DBError(); 
    18071818                delete continuation; 
    18081819                delete *it; 
     
    18101821        delete lfc; 
    18111822        lfc = new vector<pair<long long, ExtendedMoveNumber>* >; 
    1812         position[x+boardsize*y] = 'W'; 
    18131823        currentHashCode -= hashCodes[x + boardsize*y]; 
    18141824}                  
     
    18171827  if (removed == 'B') currentHashCode -= hashCodes[x + boardsize*y]; 
    18181828  else currentHashCode += hashCodes[x + boardsize*y]; 
    1819         position[x+boardsize*y] = '.'; 
    18201829} 
    18211830 
     
    18261835 
    18271836void Algo_hash::pass_process() { 
    1828         // FIXME: decide whether we count pass as continuation 
     1837        // (we do not count pass as continuation) 
    18291838} 
    18301839 
     
    18391848        delete lfc; 
    18401849        lfc = new vector<pair<long long, ExtendedMoveNumber>* >; 
    1841         int epsilon = (m.color == 'B' ? 1 : -1); 
     1850        int epsilon = (m.color == 'B' || m.color == 'X' ? 1 : -1); 
    18421851        currentHashCode += epsilon * hashCodes[m.x + boardsize*m.y]; 
    1843         if (m.color == 'X' || m.color == 'O') printf("strange\n"); 
    1844         position[m.x+boardsize*m.y] = m.color; 
    18451852 
    18461853  if (m.captures) { 
     
    18501857                        int yy = it->second; 
    18511858 
    1852                         currentHashCode -= epsilon * hashCodes[xx + boardsize*yy]; 
    1853                         position[xx+boardsize*yy] = '.'; 
     1859                        currentHashCode += epsilon * hashCodes[xx + boardsize*yy]; 
    18541860                } 
    18551861  } 
     
    18721878        } 
    18731879        delete lfc; 
    1874         delete [] game; 
    1875         delete [] position; 
    18761880        delete moveNumber; 
    18771881} 
     
    18821886 
    18831887long long Algo_hash::compute_hashkey(Pattern& pattern) { 
    1884         if (pattern.sizeX != boardsize-1 || pattern.sizeY != boardsize-1)  
     1888        if (pattern.sizeX != boardsize || pattern.sizeY != boardsize) 
    18851889                return NOT_HASHABLE; 
    18861890  long long hashkey = 0; 
     
    18971901} 
    18981902 
    1899 int Algo_hash::readDB(sqlite3* DB) { 
     1903Hashhit::Hashhit(int GAMEID, char ORIENTATION, char* blob) { 
     1904        gameid = GAMEID; 
     1905        orientation = ORIENTATION; 
     1906        cont = new MoveNC(blob[0], blob[1], blob[2]); 
     1907        emn = new ExtendedMoveNumber; 
     1908        emn->length = blob[3] * 256 + blob[4]; 
     1909        emn->data = new int[emn->length]; 
     1910        for(int i=0; i<emn->length; i++) { 
     1911                emn->data[i] = blob[5+2*i]*256 + blob[6+2*i]; 
     1912        } 
     1913
     1914 
     1915Hashhit::Hashhit() { 
     1916        printf("oops\n"); 
     1917        cont = 0; 
     1918        emn = 0; 
     1919
     1920 
     1921Hashhit::~Hashhit() { 
     1922        if (cont) delete cont; 
     1923        if (emn) delete emn; 
     1924
     1925 
     1926bool hashhit_cmp(const Hashhit* a, const Hashhit* b) { 
     1927        return a->gameid < b->gameid; 
     1928
     1929 
     1930typedef vector<Hashhit* >* vpsip; 
     1931 
     1932int insert_result(void *results, int argc, char **argv, char **azColName) throw (DBError) { 
     1933        vpsip res = ((pair<vpsip, int>*)results)->first; 
     1934        int orientation = ((pair<vpsip, int>*)results)->second; 
     1935        if (argc==2 && argv[0]) { 
     1936                res->push_back(new Hashhit(atoi(argv[0]), orientation, argv[1])); 
     1937        } else { 
     1938                printf("ouch\n"); 
     1939                throw DBError(); 
     1940        } 
    19001941        return 0; 
    19011942} 
    19021943 
    1903 void Algo_hash::search(PatternList& patternList, GameList& gl, SearchOptions& options) { 
    1904         // for all patterns in patternList 
    1905         //   compute hashKey of pattern 
    1906         //   retrieve all games with this hashKey 
    1907         // go through current game list to find "intersection" 
    1908         // 
    1909         // CAUTION: patterns w/ wildcards are NOT_HASHABLE 
    1910         //          for patterns with contlist, we look for finalPos, but have to 
    1911         //          make add'l check 
    1912 
    1913  
     1944int Algo_hash::search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db) { 
     1945        // FIXME: this works only if gamelist is ordered by id!!! 
     1946        if (gl.start_sorted() == 0) { 
     1947                int plS = patternList.size(); 
     1948                vpsip results = new vector<Hashhit* >; 
     1949                // printf("enter algo_hash.search %d\n", plS); 
     1950                for(int N=0; N<plS; N++) { 
     1951                        long long hashCode = compute_hashkey(patternList.data[N]); 
     1952                        if (hashCode == NOT_HASHABLE) return 1; 
     1953 
     1954                        char sql[100]; 
     1955                        sprintf(sql, "select gameid,hit from algo_hash where hash = %lld", hashCode); 
     1956                        // printf("hc %lld, %s\n", hashCode, sql); 
     1957                        pair<vpsip, int> rN(results, N); 
     1958                        sqlite3_exec(db, sql, insert_result, &rN, 0); 
     1959                } 
     1960                // printf("res-size %d\n", results->size()); 
     1961                sort(results->begin(), results->end(), hashhit_cmp); 
     1962                int counter = 0; // to keep track of progress bar 
     1963                vector<Hashhit* >::iterator resultIT = results->begin(); 
     1964                while (resultIT != results->end()) { 
     1965                        int index = (*resultIT)->gameid; 
     1966 
     1967                        if (0) { // FIXME if trust hash and no contlist: produce list of Hit's 
     1968                                vector<Hit* >* hits = new vector<Hit* >; 
     1969                                while ((*resultIT)->gameid == index) { 
     1970                                        char *label; 
     1971                                        if ((*resultIT)->cont->x != NO_CONT) { // continuation 
     1972                                                label = patternList.updateContinuations((*resultIT)->orientation,  
     1973                                                                                                    (*resultIT)->cont->x, (*resultIT)->cont->y, (*resultIT)->cont->color, 
     1974                                                                                                false, gl.getCurrentWinner()); 
     1975                                                if (label) { 
     1976                                                        hits->push_back(new Hit((*resultIT)->emn, label)); 
     1977                                                        (*resultIT)->emn = 0; 
     1978                                                } 
     1979                                        } else { 
     1980                                                label = new char[3]; 
     1981                                                label[0] = NO_CONT; 
     1982                                                label[1] = 0; 
     1983                                                label[2] = patternList.data[(*resultIT)->orientation].colorSwitch; 
     1984                                                hits->push_back(new Hit((*resultIT)->emn, label)); 
     1985                                                (*resultIT)->emn = 0; 
     1986                                        } 
     1987                                        resultIT++; 
     1988                                        if (resultIT == results->end()) break; 
     1989                                } 
     1990                                if (hits->size()) gl.makeIndexHit(index, hits); 
     1991                                else delete hits; 
     1992                        } else { // produce Candidate list, check using another algorithm 
     1993                                vector<Candidate* >* candidates = new vector<Candidate* >; 
     1994                                while ((*resultIT)->gameid == index) { 
     1995                                        candidates->push_back(new Candidate(0,0,(*resultIT)->orientation)); 
     1996                                        resultIT++; 
     1997                                        if (resultIT == results->end()) break; 
     1998                                } 
     1999                                gl.makeIndexCandidate(index, candidates); 
     2000                        } 
     2001                } 
     2002                for(vector<Hashhit* >::iterator it = results->begin(); it != results->end(); it++) delete *it; 
     2003                delete results; 
     2004                gl.end_sorted(); 
     2005        } 
     2006        return 0; 
     2007
    19142008 
    19152009 
     
    24152509        if (p != string::npos) gameInfoStr.replace(p, 3, 1, winner); 
    24162510 
     2511        // printf("id %s\n", argv[0]); 
    24172512        ((GameList*)gl)->all->push_back(new GameListEntry(atoi(argv[0]), winner, gameInfoStr)); 
    24182513        return 0; 
     
    24622557        if (algos & ALGO_HASH_FULL)  
    24632558                algo_ps[algo_hash_full] = new Algo_hash(boardsize); 
    2464  
     2559        all = 0; 
     2560        currentList = oldList = 0; 
     2561        readDB(); 
     2562
     2563 
     2564void GameList::readDB() { 
     2565        if (oldList) delete oldList; 
     2566        if (currentList) delete currentList; 
     2567        if (all) { 
     2568                for(vector<GameListEntry* >::iterator it = all->begin(); it != all->end(); it++) 
     2569                        delete *it; 
     2570                delete all; 
     2571        } 
    24652572        current = -1; 
    24662573        all = new vector<GameListEntry* >; 
     
    24762583                throw DBError(); 
    24772584  } 
     2585        rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
     2586        if (rc) throw DBError(); 
     2587        rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 
     2588        if (rc) throw DBError(); 
    24782589 
    24792590        string sql = "select "; // those two we need in any case 
     
    24812592        sql += " from games order by "; 
    24822593        sql += orderby; 
     2594        // printf("sql: %s\n", sql.c_str()); 
    24832595        rc = sqlite3_exec(db, sql.c_str(), insertEntry, this, 0); 
    24842596        if (rc == SQLITE_OK) { 
     
    25162628        if (oldList) delete oldList; 
    25172629        oldList = currentList; 
    2518         currentList = new vector<GameListEntry* >; 
    2519         if (oldList && oldList->size()) return (*oldList)[0]->id
     2630        currentList = new vector<pair<int,int> >; 
     2631        if (oldList && oldList->size()) return (*oldList)[0].first
    25202632        else { 
    2521                 delete oldList; 
     2633                if (oldList) delete oldList; 
    25222634                oldList = 0; 
    25232635                return -1; 
     
    25272639int GameList::next() { 
    25282640        current++; 
    2529         if (oldList->size() > current) return (*oldList)[current]->id
     2641        if (current < oldList->size()) return (*oldList)[current].first
    25302642        else { 
    2531                 delete oldList; 
     2643                if (oldList) delete oldList; 
    25322644                oldList = 0; 
    25332645                return -1; 
     
    25352647} 
    25362648 
     2649bool sndcomp(const pair<int,int>& a, const pair<int,int>& b) { 
     2650        return a.second < b.second; 
     2651} 
     2652 
     2653int GameList::start_sorted() { 
     2654        current = 0; 
     2655        if (oldList) delete oldList; 
     2656        oldList = currentList; 
     2657        currentList = new vector<pair<int,int> >; 
     2658        if (!oldList || !oldList->size()) { 
     2659                if (oldList) delete oldList; 
     2660                oldList = 0; 
     2661                return -1; 
     2662        } 
     2663        sort(oldList->begin(), oldList->end()); 
     2664        return 0; 
     2665} 
     2666 
     2667int GameList::end_sorted() { 
     2668        // printf("end sorted\n"); 
     2669        sort(currentList->begin(), currentList->end(), sndcomp); 
     2670        delete oldList; 
     2671        oldList = 0; 
     2672        return 0; 
     2673} 
     2674 
    25372675char GameList::getCurrentWinner() { 
    2538         return (*oldList)[current]->winner; 
     2676        return (*all)[(*oldList)[current].second]->winner; 
    25392677} 
    25402678 
    25412679vector<Candidate* > * GameList::getCurrentCandidateList() { 
    2542         return (*oldList)[current]->candidates; 
     2680        return (*all)[(*oldList)[current].second]->candidates; 
    25432681} 
    25442682 
    25452683void GameList::makeCurrentCandidate(vector<Candidate* > * candidates) { 
    2546         GameListEntry* gle = (*oldList)[current]; 
     2684        GameListEntry* gle = (*all)[(*oldList)[current].second]; 
    25472685        if (gle->candidates) delete gle->candidates; 
    25482686        gle->candidates = candidates; 
    2549         currentList->push_back(gle); 
     2687        currentList->push_back((*oldList)[current]); 
    25502688} 
    25512689 
    25522690void GameList::makeCurrentHit(vector<Hit* > * hits) { 
    2553         GameListEntry* gle = (*oldList)[current]; 
     2691        GameListEntry* gle = (*all)[(*oldList)[current].second]; 
    25542692        if (gle->hits) delete gle->hits; 
    25552693        gle->hits = hits; 
    25562694        sort(gle->hits->begin(), gle->hits->end(), Hit::cmp_pts); 
    2557         currentList->push_back(gle); 
    2558 
     2695        currentList->push_back((*oldList)[current]); 
     2696
     2697 
     2698void GameList::makeIndexHit(int index, vector<Hit* > * hits) { 
     2699        int start = current; 
     2700        int end = oldList->size(); 
     2701        int m = start; 
     2702        while (start < end) { 
     2703                m = (end+start)/2; 
     2704                if (index == (*oldList)[m].first) { 
     2705                        currentList->push_back((*oldList)[m]); 
     2706                        if (hits) { 
     2707                                if ((*all)[(*oldList)[m].second]->hits) delete (*all)[(*oldList)[m].second]->hits; 
     2708                                (*all)[(*oldList)[m].second]->hits = hits; 
     2709                        } 
     2710                        break; 
     2711                } else { 
     2712                        if (index < (*oldList)[m].first) end = m; 
     2713                        else start = m+1; 
     2714                } 
     2715        } 
     2716        current = m; 
     2717
     2718 
     2719void GameList::makeIndexCandidate(int index, vector<Candidate* > * candidates) { 
     2720        int start = current; 
     2721        int end = oldList->size(); 
     2722        int m = start; 
     2723        while (start < end) { 
     2724                m = (end+start)/2; 
     2725                if (index == (*oldList)[m].first) { 
     2726                        currentList->push_back((*oldList)[m]); 
     2727                        if (candidates) { 
     2728                                if ((*all)[(*oldList)[m].second]->candidates) delete (*all)[(*oldList)[m].second]->candidates; 
     2729                                (*all)[(*oldList)[m].second]->candidates = candidates; 
     2730                        } 
     2731                        break; 
     2732                } else { 
     2733                        if (index < (*oldList)[m].first) end = m; 
     2734                        else start = m+1; 
     2735                } 
     2736        } 
     2737        current = m; 
     2738
     2739 
    25592740 
    25602741void GameList::reset() { 
     
    25622743        oldList = 0; 
    25632744        if (currentList) delete currentList; 
    2564         currentList = new vector<GameListEntry* >; 
    2565         for(vector<GameListEntry* >::iterator it = all->begin(); it != all->end(); it++) 
    2566                 currentList->push_back(*it); 
     2745        currentList = new vector<pair<int,int> >; 
     2746        int counter = 0; 
     2747        for(vector<GameListEntry* >::iterator it = all->begin(); it != all->end(); it++) { 
     2748                if ((*it)->hits) { 
     2749                        delete (*it)->hits; 
     2750                        (*it)->hits = 0; 
     2751                } 
     2752                if ((*it)->candidates) { 
     2753                        delete (*it)->candidates; 
     2754                        (*it)->candidates = 0; 
     2755                } 
     2756                currentList->push_back(make_pair((*it)->id, counter++)); 
     2757        } 
    25672758} 
    25682759 
     
    25722763 
    25732764int gis_callback(void *gl, int argc, char **argv, char **azColName) { 
    2574         if (!argc) { 
    2575                 fprintf(stderr, "oops\n"); 
    2576                 return 0; 
    2577         } 
    2578         int index = atoi(argv[0]); 
    2579         GameList* GL = (GameList*)gl; 
    2580         while (GL->current < GL->oldSortedList->size() && (*GL->oldSortedList)[GL->current].first < index) GL->current++; 
    2581         if (GL->current < GL->oldSortedList->size() && (*GL->oldSortedList)[GL->current].first == index) { 
    2582                 GL->sortedList->push_back((*GL->oldSortedList)[GL->current]); 
    2583         } 
     2765        if (!argc) throw DBError(); 
     2766        ((GameList*)gl)->makeIndexHit(atoi(argv[0]), 0); 
    25842767        return 0; 
    2585 } 
    2586  
    2587 bool sndcomp(const pair<int,int>& a, const pair<int,int>& b) { 
    2588         return a.second < b.second; 
    25892768} 
    25902769 
     
    25962775                throw DBError(); 
    25972776  } 
    2598         current = 0; 
    2599         if (oldList) delete oldList; 
    2600         oldList = currentList; 
    2601         currentList = new vector<GameListEntry* >; 
    2602         if (oldList && !oldList->size()) { 
    2603         delete oldList; 
    2604                 oldList = 0; 
    2605                 return; 
    2606         } 
    2607         sortedList = new vector<pair<int,int> >; 
    2608         oldSortedList = new vector<pair<int,int> >; 
    2609         int counter = -1; 
    2610         for(vector<GameListEntry* >::iterator it = oldList->begin(); it != oldList->end(); it++) 
    2611                 oldSortedList->push_back(make_pair((*it)->id, ++counter)); 
    2612         if (orderby != "id") sort(oldSortedList->begin(), oldSortedList->end()); // sort with respect to first component 
    2613  
    2614         string query = "select id from games where "; 
    2615         query += sql; 
    2616         query += " order by id"; 
    2617         rc = sqlite3_exec(db, query.c_str(), gis_callback, this, 0); 
    2618         if( rc!=SQLITE_OK ) throw DBError(); 
    2619  
    2620         rc = sqlite3_close(db); 
    2621         if( rc!=SQLITE_OK ) throw DBError(); 
    2622         db = 0; 
    2623  
    2624         // put results from sortedList into currentList 
    2625         sort(sortedList->begin(), sortedList->end(), sndcomp); // sort w.r.t second component 
    2626         vector<pair<int,int> >::iterator sl_it = sortedList->begin(); 
    2627         for(int counter = 0; counter < oldList->size(); counter++) { 
    2628                 while (sl_it != sortedList->end() && sl_it->second < counter) sl_it++; 
    2629                 if (sl_it != sortedList->end() && sl_it->second == counter) currentList->push_back((*oldList)[counter]); 
    2630         } 
    2631         delete sortedList; 
    2632         delete oldSortedList; 
     2777        if (start_sorted() == 0) {  
     2778                rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
     2779                if (rc) throw DBError(); 
     2780                rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 
     2781                if (rc) throw DBError(); 
     2782 
     2783                string query = "select id from games where "; 
     2784                query += sql; 
     2785                query += " order by id"; 
     2786                rc = sqlite3_exec(db, query.c_str(), gis_callback, this, 0); 
     2787                if( rc!=SQLITE_OK ) throw DBError(); 
     2788 
     2789                rc = sqlite3_close(db); 
     2790                if( rc!=SQLITE_OK ) throw DBError(); 
     2791                db = 0; 
     2792                end_sorted(); 
     2793        } 
    26332794} 
    26342795 
    26352796int GameList::numHits() { 
    26362797        int numHits = 0; 
    2637         for(vector<GameListEntry* >::iterator it = currentList->begin(); it != currentList->end(); it++) { 
    2638                 if ((*it)->hits) numHits += (*it)->hits->size(); 
     2798        for(vector<pair<int,int> >::iterator it = currentList->begin(); it != currentList->end(); it++) { 
     2799                if ((*all)[it->second]->hits) numHits += (*all)[it->second]->hits->size(); 
    26392800        } 
    26402801        return numHits; 
     
    26742835        if (start>end || end > currentList->size()) return result; 
    26752836        for(int i=start; i<end; i++) { 
    2676                 result.push_back((*currentList)[i]->gameInfoStr + ", " + resultsStr((*currentList)[i])); 
     2837                result.push_back((*all)[(*currentList)[i].second]->gameInfoStr + ", " + resultsStr((*all)[(*currentList)[i].second])); 
    26772838        } 
    26782839} 
     
    26882849    throw DBError(); 
    26892850  } 
     2851        rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
     2852        if (rc) throw DBError(); 
     2853        rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 
     2854        if (rc) throw DBError(); 
     2855 
    26902856        if (!readDBs) { 
    26912857                for(int a=0; a < 20; a++) if (algo_ps[a]) algo_ps[a]->readDB(db); 
     
    26932859        } 
    26942860 
    2695         algo_ps[algo_finalpos]->search(pl, *this, so); // FIXME: once more algorithms are available, this 
    2696                                                        // has to become more elaborate 
    2697         algo_ps[algo_movelist]->search(pl, *this, so); 
     2861        if (pl.pattern.sizeX == 19 && pl.pattern.sizeY == 19 && \ 
     2862                        algo_ps[algo_hash_full] && \ 
     2863                        ((Algo_hash*)algo_ps[algo_finalpos])->search(pl, *this, so, db) == 0) { // hash algo successful 
     2864                 
     2865                algo_ps[algo_movelist]->search(pl, *this, so); 
     2866        } else { 
     2867                algo_ps[algo_finalpos]->search(pl, *this, so); // FIXME: once more algorithms are available, this 
     2868                                                         // has to become more elaborate 
     2869                algo_ps[algo_movelist]->search(pl, *this, so); 
     2870        } 
    26982871        sqlite3_close(db); 
    26992872        db = 0; 
     
    27222895        rc = sqlite3_exec(db, sql1, 0, 0, 0); 
    27232896        if( rc!=SQLITE_OK ) throw DBError(); 
     2897        rc = sqlite3_exec(db, "pragma synchronous = off;", 0, 0, 0); 
     2898        if (rc) throw DBError(); 
     2899        rc = sqlite3_exec(db, "pragma cache_size = 300000;", 0, 0, 0); 
     2900        if (rc) throw DBError(); 
    27242901 
    27252902        char* sql = "begin transaction;"; 
     
    27492926        sqlite3_close(db); 
    27502927        db = 0; 
    2751         // FIXME!!! rebuild all, currentList 
     2928        readDB(); 
    27522929} 
    27532930 
     
    27632940        while (root) { 
    27642941                current++; 
    2765                 if (!(current%1000)) { 
    2766                       char* sql = "end transaction;"; 
    2767                       int rc = sqlite3_exec(db, sql, 0, 0, 0); 
    2768                       if (rc) { 
    2769                               sqlite3_close(db); 
    2770                               db = 0; 
    2771                               throw DBError(); 
    2772                       } 
    2773                       sql = "begin transaction;"; 
    2774                       rc = sqlite3_exec(db, sql, 0, 0, 0); 
    2775                       if (rc) { 
    2776                               sqlite3_close(db); 
    2777                               db = 0; 
    2778                               throw DBError(); 
    2779                       } 
    2780                
     2942                // if (!(current%1000)) { 
     2943                //    char* sql = "end transaction;"; 
     2944                //    int rc = sqlite3_exec(db, sql, 0, 0, 0); 
     2945                //    if (rc) { 
     2946                //            sqlite3_close(db); 
     2947                //            db = 0; 
     2948                //            throw DBError(); 
     2949                //    } 
     2950                //    sql = "begin transaction;"; 
     2951                //    rc = sqlite3_exec(db, sql, 0, 0, 0); 
     2952                //    if (rc) { 
     2953                //            sqlite3_close(db); 
     2954                //            db = 0; 
     2955                //            throw DBError(); 
     2956                //    } 
     2957                //
    27812958                char_p* rootNodeProperties = parseRootNode(root); 
    27822959                string dt; 
     
    28112988                                else { 
    28122989                                  month_found = ('0' <= dt[p] && dt[p] <= '9' && '0' <= dt[p+1] && dt[p+1] <= '9'); 
    2813                                         if (month_found && dt.find_first_of("0123456789", p+2) != p+2) { // success: found 4 digits in a row 
     2990                                        if (month_found && dt.find_first_of("0123456789", p+2) != p+2) { 
    28142991                                                date += dt.substr(p,2); 
    28152992                                                date += '-'; 
     
    28313008                                        else { 
    28323009                                                day_found = ('0' <= dt[p] && dt[p] <= '9' && '0' <= dt[p+1] && dt[p+1] <= '9'); 
    2833                                                 if (day_found && dt.find_first_of("0123456789", p+2) != p+2) { // success: found 4 digits in a row 
     3010                                                if (day_found && dt.find_first_of("0123456789", p+2) != p+2) { 
    28343011                                                        date += dt.substr(p,2); 
    28353012                                                } else { 
  • 06/libkombilo/search.h

    r180 r182  
    162162        public: 
    163163                ExtendedMoveNumber* pos; 
    164                 char* label; 
     164                char* label; // this does not really contain the label, but rather the position of the continuation move 
    165165                Hit(ExtendedMoveNumber* POS, char* LABEL); 
    166166                ~Hit(); 
     
    189189                virtual void finalize_process(); 
    190190                virtual int readDB(sqlite3* DB); 
    191                 virtual void search(PatternList& patternList, GameList& gl, SearchOptions& options); 
     191                virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options); 
    192192 
    193193                int gid; 
     
    239239                vector<char* > *data; 
    240240                int readDB(sqlite3* DB); 
    241                 void search(PatternList& patternList, GameList& gl, SearchOptions& options); 
     241                int search(PatternList& patternList, GameList& gl, SearchOptions& options); 
    242242}; 
    243243 
     
    269269                void finalize_process(); 
    270270                int readDB(sqlite3* DB); 
    271                 void search(PatternList& patternList, GameList& gl, SearchOptions& options); 
     271                int search(PatternList& patternList, GameList& gl, SearchOptions& options); 
    272272 
    273273                vector<char> movelist; 
     
    279279 
    280280const long long NOT_HASHABLE = 9223372036854775807LL; 
     281 
     282 
     283class Hashhit { 
     284        public: 
     285                int gameid; 
     286                char orientation; 
     287                MoveNC* cont; 
     288                ExtendedMoveNumber* emn; 
     289 
     290                Hashhit(); 
     291                Hashhit(int GAMEID, char ORIENTATION, char* blob); 
     292                ~Hashhit(); 
     293}; 
     294 
     295 
    281296 
    282297class Algo_hash : public Algorithm { 
     
    296311                void endgame_process(); 
    297312                void finalize_process(); 
    298                 int readDB(sqlite3* DB); 
    299                 void search(PatternList& patternList, GameList& gl, SearchOptions& options); 
     313                int search(PatternList& patternList, GameList& gl, SearchOptions& options, sqlite3* db); 
    300314 
    301315                int insert_hash(long long hashCode, ExtendedMoveNumber& mn, Move* continuation); 
    302316                long long compute_hashkey(Pattern& pattern); 
    303317 
    304                 char* position; 
    305                 char* game; 
    306318                long long currentHashCode; 
    307319                ExtendedMoveNumber* moveNumber; 
     
    390402                algo_p* algo_ps; 
    391403                vector<GameListEntry* > * all; 
    392                 vector<GameListEntry* > * currentList; 
    393                 vector<GameListEntry* > * oldList; 
     404                vector<pair<int,int> > * currentList; // pair of game id and position within all 
     405                                                      // (usually sorted w.r.t. second 
     406                                                                                                                                                                        // component) 
     407                vector<pair<int,int> > * oldList; 
    394408                int current; 
    395409                sqlite3* db; 
     
    417431                int start(); 
    418432                int next(); 
     433                int start_sorted(); 
     434                int end_sorted(); 
    419435                char getCurrentWinner(); 
    420436                vector<Candidate* > *getCurrentCandidateList(); 
    421437                void makeCurrentCandidate(vector<Candidate* > *candidates); 
    422438                void makeCurrentHit(vector<Hit* > *hits); 
    423                 vector<pair<int,int> >* sortedList
    424                 vector<pair<int,int> >* oldSortedList
     439                void makeIndexCandidate(int index, vector<Candidate* > *candidates)
     440                void makeIndexHit(int index, vector<Hit* > *hits)
    425441 
    426442        private: 
    427443                string resultsStr(GameListEntry* gle); 
    428444                char lookupLabel(char x, char y); 
     445                void readDB(); 
    429446}; 
    430447 
  • 06/libkombilo/sgfparser.cc

    r180 r182  
    2626SGFError::SGFError() {} 
    2727 
     28ExtendedMoveNumber::ExtendedMoveNumber() { 
     29        length = 0; 
     30        data = 0; 
     31} 
     32 
    2833ExtendedMoveNumber::ExtendedMoveNumber(int LENGTH, int* DATA) { 
    29         length = length
     34        length = LENGTH
    3035        if (length) data = new int[length]; 
    3136        else data = 0; 
  • 06/libkombilo/sgfparser.h

    r180