Changeset 182
- Timestamp:
- 09/21/06 23:57:16 (2 years ago)
- Files:
-
- 06/libkombilo/cpptest.cc (modified) (1 diff)
- 06/libkombilo/process.py (modified) (1 diff)
- 06/libkombilo/search.cc (modified) (42 diffs)
- 06/libkombilo/search.h (modified) (8 diffs)
- 06/libkombilo/sgfparser.cc (modified) (1 diff)
- 06/libkombilo/sgfparser.h (modified) (1 diff)
- 06/libkombilo/testhash.py (added)
- 06/libkombilo/testsearch.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
06/libkombilo/cpptest.cc
r180 r182 2 2 3 3 int 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............................................"); 7 8 PatternList pl(p, 0, 0, 19); 8 9 SearchOptions so; 06/libkombilo/process.py
r181 r182 19 19 20 20 starttime = time.time() 21 algos = ALGO_FINALPOS | ALGO_MOVELIST #| ALGO_HASH_FULL21 algos = ALGO_FINALPOS | ALGO_MOVELIST | ALGO_HASH_FULL 22 22 try: 23 23 gl = GameList('t1.db', 'id', '', algos, 19) 06/libkombilo/search.cc
r180 r182 769 769 void Algorithm::finalize_process() {} 770 770 int Algorithm::readDB(sqlite3* DB) { return 0; } 771 void Algorithm::search(PatternList& patternList, GameList& gl, SearchOptions& options) {}771 int Algorithm::search(PatternList& patternList, GameList& gl, SearchOptions& options) { return -1; } 772 772 773 773 Algo_signature::Algo_signature(int bsize) : Algorithm(bsize) { … … 945 945 int Algo_finalpos::readDB(sqlite3* DB) { 946 946 db = DB; 947 // FIXME: do we want to delete data before adding values?948 947 if (data) { 949 948 for(vector<char* >::iterator it = data->begin(); it != data->end(); it++) delete [] *it; … … 971 970 } 972 971 973 voidAlgo_finalpos::search(PatternList& patternList, GameList& gl, SearchOptions& options) { // progress bar?!972 int Algo_finalpos::search(PatternList& patternList, GameList& gl, SearchOptions& options) { // progress bar?! 974 973 975 974 // Put the pattern into bitmap format, which is the format the final … … 1098 1097 delete [] allbitlengths; 1099 1098 delete [] allbits; 1099 return 0; 1100 1100 } 1101 1101 … … 1194 1194 1195 1195 void Algo_movelist::pass_process() { 1196 // FIXME !!! need to add pass move here to end up with correct1197 // counter!1196 movelist.push_back(19); 1197 movelist.push_back(19); 1198 1198 } 1199 1199 … … 1265 1265 } 1266 1266 1267 voidAlgo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) {1267 int Algo_movelist::search(PatternList& patternList, GameList& gl, SearchOptions& options) { 1268 1268 // FIXME progbar, variations!!! 1269 1269 … … 1294 1294 // branchpoints = []; 1295 1295 1296 // printf("oh oh\n");1297 1296 char* movel = (*data1)[index-1]; 1298 1297 int movelistIndex = 0; 1299 1298 int endMovelist = (*data1l)[index-1]; 1300 // printf("oh oh\n");1301 1299 // printf("len movelist: %d\n", (*data1l)[index-1]); 1302 1300 // int nodeCtr = 0; … … 1614 1612 delete [] contListIndex; 1615 1613 } 1614 return 0; 1616 1615 } 1617 1616 … … 1748 1747 1749 1748 int 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 (?,?,?);"; 1751 1769 sqlite3_stmt *ppStmt=0; 1752 1770 int rc = sqlite3_prepare(db, sql, -1, &ppStmt, 0); … … 1754 1772 rc = sqlite3_bind_int64(ppStmt, 1, currentHashCode); 1755 1773 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); 1759 1775 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); 1761 1777 if (rc != SQLITE_OK) return rc; 1762 1778 rc = sqlite3_step(ppStmt); … … 1764 1780 rc = sqlite3_finalize(ppStmt); 1765 1781 if (rc != SQLITE_OK) return rc; 1782 delete [] buf; 1766 1783 return 0; // success 1767 1784 } … … 1769 1786 void Algo_hash::initialize_process(sqlite3* DB) throw(DBError) { 1770 1787 db = DB; 1771 char* zErrMsg = 0;1772 i nt 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); 1774 1791 if (rc != SQLITE_OK) throw DBError(); 1775 1792 } … … 1777 1794 void Algo_hash::newgame_process(int game_id) { 1778 1795 gid = game_id; 1779 game = new char[1]; game[0] = 0; // game id FIXME1780 position = new char[boardsize*boardsize];1781 for(int i=0; i<boardsize*boardsize; i++) position[i] = '.';1782 1796 moveNumber = new ExtendedMoveNumber(0); 1783 1797 currentHashCode = 0; // start with empty board … … 1789 1803 for(vector<pair<long long, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 1790 1804 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(); 1793 1806 delete continuation; 1794 1807 delete *it; … … 1796 1809 delete lfc; 1797 1810 lfc = new vector<pair<long long, ExtendedMoveNumber>* >; 1798 position[x+boardsize*y] = 'B';1799 1811 currentHashCode += hashCodes[x + boardsize*y]; 1800 1812 } … … 1803 1815 for(vector<pair<long long, ExtendedMoveNumber>* >::iterator it = lfc->begin(); it != lfc->end(); it++) { 1804 1816 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(); 1807 1818 delete continuation; 1808 1819 delete *it; … … 1810 1821 delete lfc; 1811 1822 lfc = new vector<pair<long long, ExtendedMoveNumber>* >; 1812 position[x+boardsize*y] = 'W';1813 1823 currentHashCode -= hashCodes[x + boardsize*y]; 1814 1824 } … … 1817 1827 if (removed == 'B') currentHashCode -= hashCodes[x + boardsize*y]; 1818 1828 else currentHashCode += hashCodes[x + boardsize*y]; 1819 position[x+boardsize*y] = '.';1820 1829 } 1821 1830 … … 1826 1835 1827 1836 void Algo_hash::pass_process() { 1828 // FIXME: decide whether we count pass as continuation1837 // (we do not count pass as continuation) 1829 1838 } 1830 1839 … … 1839 1848 delete lfc; 1840 1849 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); 1842 1851 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;1845 1852 1846 1853 if (m.captures) { … … 1850 1857 int yy = it->second; 1851 1858 1852 currentHashCode -= epsilon * hashCodes[xx + boardsize*yy]; 1853 position[xx+boardsize*yy] = '.'; 1859 currentHashCode += epsilon * hashCodes[xx + boardsize*yy]; 1854 1860 } 1855 1861 } … … 1872 1878 } 1873 1879 delete lfc; 1874 delete [] game;1875 delete [] position;1876 1880 delete moveNumber; 1877 1881 } … … 1882 1886 1883 1887 long 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) 1885 1889 return NOT_HASHABLE; 1886 1890 long long hashkey = 0; … … 1897 1901 } 1898 1902 1899 int Algo_hash::readDB(sqlite3* DB) { 1903 Hashhit::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 1915 Hashhit::Hashhit() { 1916 printf("oops\n"); 1917 cont = 0; 1918 emn = 0; 1919 } 1920 1921 Hashhit::~Hashhit() { 1922 if (cont) delete cont; 1923 if (emn) delete emn; 1924 } 1925 1926 bool hashhit_cmp(const Hashhit* a, const Hashhit* b) { 1927 return a->gameid < b->gameid; 1928 } 1929 1930 typedef vector<Hashhit* >* vpsip; 1931 1932 int 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 } 1900 1941 return 0; 1901 1942 } 1902 1943 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 1944 int 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 } 1914 2008 1915 2009 … … 2415 2509 if (p != string::npos) gameInfoStr.replace(p, 3, 1, winner); 2416 2510 2511 // printf("id %s\n", argv[0]); 2417 2512 ((GameList*)gl)->all->push_back(new GameListEntry(atoi(argv[0]), winner, gameInfoStr)); 2418 2513 return 0; … … 2462 2557 if (algos & ALGO_HASH_FULL) 2463 2558 algo_ps[algo_hash_full] = new Algo_hash(boardsize); 2464 2559 all = 0; 2560 currentList = oldList = 0; 2561 readDB(); 2562 } 2563 2564 void 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 } 2465 2572 current = -1; 2466 2573 all = new vector<GameListEntry* >; … … 2476 2583 throw DBError(); 2477 2584 } 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(); 2478 2589 2479 2590 string sql = "select "; // those two we need in any case … … 2481 2592 sql += " from games order by "; 2482 2593 sql += orderby; 2594 // printf("sql: %s\n", sql.c_str()); 2483 2595 rc = sqlite3_exec(db, sql.c_str(), insertEntry, this, 0); 2484 2596 if (rc == SQLITE_OK) { … … 2516 2628 if (oldList) delete oldList; 2517 2629 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; 2520 2632 else { 2521 delete oldList;2633 if (oldList) delete oldList; 2522 2634 oldList = 0; 2523 2635 return -1; … … 2527 2639 int GameList::next() { 2528 2640 current++; 2529 if ( oldList->size() > current) return (*oldList)[current]->id;2641 if (current < oldList->size()) return (*oldList)[current].first; 2530 2642 else { 2531 delete oldList;2643 if (oldList) delete oldList; 2532 2644 oldList = 0; 2533 2645 return -1; … … 2535 2647 } 2536 2648 2649 bool sndcomp(const pair<int,int>& a, const pair<int,int>& b) { 2650 return a.second < b.second; 2651 } 2652 2653 int 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 2667 int 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 2537 2675 char GameList::getCurrentWinner() { 2538 return (* oldList)[current]->winner;2676 return (*all)[(*oldList)[current].second]->winner; 2539 2677 } 2540 2678 2541 2679 vector<Candidate* > * GameList::getCurrentCandidateList() { 2542 return (* oldList)[current]->candidates;2680 return (*all)[(*oldList)[current].second]->candidates; 2543 2681 } 2544 2682 2545 2683 void GameList::makeCurrentCandidate(vector<Candidate* > * candidates) { 2546 GameListEntry* gle = (* oldList)[current];2684 GameListEntry* gle = (*all)[(*oldList)[current].second]; 2547 2685 if (gle->candidates) delete gle->candidates; 2548 2686 gle->candidates = candidates; 2549 currentList->push_back( gle);2687 currentList->push_back((*oldList)[current]); 2550 2688 } 2551 2689 2552 2690 void GameList::makeCurrentHit(vector<Hit* > * hits) { 2553 GameListEntry* gle = (* oldList)[current];2691 GameListEntry* gle = (*all)[(*oldList)[current].second]; 2554 2692 if (gle->hits) delete gle->hits; 2555 2693 gle->hits = hits; 2556 2694 sort(gle->hits->begin(), gle->hits->end(), Hit::cmp_pts); 2557 currentList->push_back(gle); 2558 } 2695 currentList->push_back((*oldList)[current]); 2696 } 2697 2698 void 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 2719 void 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 2559 2740 2560 2741 void GameList::reset() { … … 2562 2743 oldList = 0; 2563 2744 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 } 2567 2758 } 2568 2759 … … 2572 2763 2573 2764 int 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); 2584 2767 return 0; 2585 }2586 2587 bool sndcomp(const pair<int,int>& a, const pair<int,int>& b) {2588 return a.second < b.second;2589 2768 } 2590 2769 … … 2596 2775 throw DBError(); 2597 2776 } 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 } 2633 2794 } 2634 2795 2635 2796 int GameList::numHits() { 2636 2797 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(); 2639 2800 } 2640 2801 return numHits; … … 2674 2835 if (start>end || end > currentList->size()) return result; 2675 2836 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])); 2677 2838 } 2678 2839 } … … 2688 2849 throw DBError(); 2689 2850 } 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 2690 2856 if (!readDBs) { 2691 2857 for(int a=0; a < 20; a++) if (algo_ps[a]) algo_ps[a]->readDB(db); … … 2693 2859 } 2694 2860 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 } 2698 2871 sqlite3_close(db); 2699 2872 db = 0; … … 2722 2895 rc = sqlite3_exec(db, sql1, 0, 0, 0); 2723 2896 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(); 2724 2901 2725 2902 char* sql = "begin transaction;"; … … 2749 2926 sqlite3_close(db); 2750 2927 db = 0; 2751 // FIXME!!! rebuild all, currentList2928 readDB(); 2752 2929 } 2753 2930 … … 2763 2940 while (root) { 2764 2941 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 // } 2781 2958 char_p* rootNodeProperties = parseRootNode(root); 2782 2959 string dt; … … 2811 2988 else { 2812 2989 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 row2990 if (month_found && dt.find_first_of("0123456789", p+2) != p+2) { 2814 2991 date += dt.substr(p,2); 2815 2992 date += '-'; … … 2831 3008 else { 2832 3009 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 row3010 if (day_found && dt.find_first_of("0123456789", p+2) != p+2) { 2834 3011 date += dt.substr(p,2); 2835 3012 } else { 06/libkombilo/search.h
r180 r182 162 162 public: 163 163 ExtendedMoveNumber* pos; 164 char* label; 164 char* label; // this does not really contain the label, but rather the position of the continuation move 165 165 Hit(ExtendedMoveNumber* POS, char* LABEL); 166 166 ~Hit(); … … 189 189 virtual void finalize_process(); 190 190 virtual int readDB(sqlite3* DB); 191 virtual voidsearch(PatternList& patternList, GameList& gl, SearchOptions& options);191 virtual int search(PatternList& patternList, GameList& gl, SearchOptions& options); 192 192 193 193 int gid; … … 239 239 vector<char* > *data; 240 240 int readDB(sqlite3* DB); 241 voidsearch(PatternList& patternList, GameList& gl, SearchOptions& options);241 int search(PatternList& patternList, GameList& gl, SearchOptions& options); 242 242 }; 243 243 … … 269 269 void finalize_process(); 270 270 int readDB(sqlite3* DB); 271 voidsearch(PatternList& patternList, GameList& gl, SearchOptions& options);271 int search(PatternList& patternList, GameList& gl, SearchOptions& options); 272 272 273 273 vector<char> movelist; … … 279 279 280 280 const long long NOT_HASHABLE = 9223372036854775807LL; 281 282 283 class 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 281 296 282 297 class Algo_hash : public Algorithm { … … 296 311 void endgame_process(); 297 312 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); 300 314 301 315 int insert_hash(long long hashCode, ExtendedMoveNumber& mn, Move* continuation); 302 316 long long compute_hashkey(Pattern& pattern); 303 317 304 char* position;305 char* game;306 318 long long currentHashCode; 307 319 ExtendedMoveNumber* moveNumber; … … 390 402 algo_p* algo_ps; 391 403 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; 394 408 int current; 395 409 sqlite3* db; … … 417 431 int start(); 418 432 int next(); 433 int start_sorted(); 434 int end_sorted(); 419 435 char getCurrentWinner(); 420 436 vector<Candidate* > *getCurrentCandidateList(); 421 437 void makeCurrentCandidate(vector<Candidate* > *candidates); 422 438 void makeCurrentHit(vector<Hit* > *hits); 423 v ector<pair<int,int> >* sortedList;424 v ector<pair<int,int> >* oldSortedList;439 void makeIndexCandidate(int index, vector<Candidate* > *candidates); 440 void makeIndexHit(int index, vector<Hit* > *hits); 425 441 426 442 private: 427 443 string resultsStr(GameListEntry* gle); 428 444 char lookupLabel(char x, char y); 445 void readDB(); 429 446 }; 430 447 06/libkombilo/sgfparser.cc
r180 r182 26 26 SGFError::SGFError() {} 27 27 28 ExtendedMoveNumber::ExtendedMoveNumber() { 29 length = 0; 30 data = 0; 31 } 32 28 33 ExtendedMoveNumber::ExtendedMoveNumber(int LENGTH, int* DATA) { 29 length = length;34 length = LENGTH; 30 35 if (length) data = new int[length]; 31 36 else data = 0; 06/libkombilo/sgfparser.h
r180
