Using the vector indexing suite from Boost.Python

This removes the need for a special overload for AddCandidatesToDatabase. Also,
the GetFuture function now provides a more sensible API with the list being
returned instead of accepted as an out parameter.
This commit is contained in:
Strahinja Val Markovic 2012-07-08 15:34:44 -07:00
parent 9a82319c77
commit 30c9637992
7 changed files with 19 additions and 40 deletions

View File

@ -88,24 +88,6 @@ void Completer::EnableThreading()
} }
void Completer::AddCandidatesToDatabase( const Pylist &new_candidates,
const std::string &filetype,
const std::string &filepath,
bool clear_database )
{
int num_candidates = len( new_candidates );
std::vector< std::string > candidates;
candidates.reserve( num_candidates );
for (int i = 0; i < num_candidates; ++i)
{
candidates.push_back( extract< std::string >( new_candidates[ i ] ) );
}
AddCandidatesToDatabase( candidates, filetype, filepath, clear_database );
}
void Completer::AddCandidatesToDatabase( void Completer::AddCandidatesToDatabase(
const std::vector< std::string > &new_candidates, const std::vector< std::string > &new_candidates,
const std::string &filetype, const std::string &filetype,

View File

@ -72,11 +72,6 @@ public:
const std::string &filepath, const std::string &filepath,
bool clear_database ); bool clear_database );
void AddCandidatesToDatabase( const Pylist &new_candidates,
const std::string &filetype,
const std::string &filepath,
bool clear_database );
// Only provided for tests! // Only provided for tests!
std::vector< std::string > CandidatesForQuery( std::vector< std::string > CandidatesForQuery(
const std::string &query ) const; const std::string &query ) const;

View File

@ -32,8 +32,9 @@ bool Future::ResultsReady()
return future_.is_ready(); return future_.is_ready();
} }
void Future::GetResults( Pylist &candidates ) Pylist Future::GetResults()
{ {
Pylist candidates;
AsyncResults results; AsyncResults results;
try try
@ -43,13 +44,15 @@ void Future::GetResults( Pylist &candidates )
catch ( boost::future_uninitialized & ) catch ( boost::future_uninitialized & )
{ {
return; return candidates;
} }
foreach ( const Result& result, *results ) foreach ( const Result& result, *results )
{ {
candidates.append( *result.Text() ); candidates.append( *result.Text() );
} }
return candidates;
} }
} // namespace YouCompleteMe } // namespace YouCompleteMe

View File

@ -36,7 +36,7 @@ public:
Future() {} Future() {}
Future( boost::shared_future< AsyncResults > future ); Future( boost::shared_future< AsyncResults > future );
bool ResultsReady(); bool ResultsReady();
void GetResults( Pylist &candidates ); Pylist GetResults();
private: private:
boost::shared_future< AsyncResults > future_; boost::shared_future< AsyncResults > future_;

View File

@ -20,6 +20,7 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
BOOST_PYTHON_MODULE(indexer) BOOST_PYTHON_MODULE(indexer)
{ {
@ -30,15 +31,13 @@ BOOST_PYTHON_MODULE(indexer)
.def( "ResultsReady", &Future::ResultsReady ) .def( "ResultsReady", &Future::ResultsReady )
.def( "GetResults", &Future::GetResults ); .def( "GetResults", &Future::GetResults );
void (Completer::*actd) (const Pylist&,
const std::string&,
const std::string&,
bool) =
&Completer::AddCandidatesToDatabase;
class_< Completer, boost::noncopyable >( "Completer" ) class_< Completer, boost::noncopyable >( "Completer" )
.def( "EnableThreading", &Completer::EnableThreading ) .def( "EnableThreading", &Completer::EnableThreading )
.def( "AddCandidatesToDatabase", actd ) // .def( "AddCandidatesToDatabase", actd )
.def( "AddCandidatesToDatabase", &Completer::AddCandidatesToDatabase )
.def( "CandidatesForQueryAndTypeAsync", .def( "CandidatesForQueryAndTypeAsync",
&Completer::CandidatesForQueryAndTypeAsync ); &Completer::CandidatesForQueryAndTypeAsync );
class_< std::vector< std::string > >( "StringVec" )
.def( vector_indexing_suite< std::vector< std::string > >() );
} }

View File

@ -19,7 +19,6 @@
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include "Completer.h" #include "Completer.h"
#include "Utils.h" #include "Utils.h"
#include <boost/python.hpp>
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::WhenSorted; using ::testing::WhenSorted;

View File

@ -47,20 +47,19 @@ class CompletionSystem( object ):
if not self.future: if not self.future:
return [] return []
results = [] return self.future.GetResults()
self.future.GetResults( results )
return results
def AddIdentifier( self, identifier ): def AddIdentifier( self, identifier ):
# print identifier
filetype = vim.eval( "&filetype" ) filetype = vim.eval( "&filetype" )
filepath = vim.eval( "expand('%:p')" ) filepath = vim.eval( "expand('%:p')" )
if not filetype or not filepath or not identifier: if not filetype or not filepath or not identifier:
return return
self.completer.AddCandidatesToDatabase( [ identifier ], vector = indexer.StringVec()
vector.append( identifier )
self.completer.AddCandidatesToDatabase( vector,
filetype, filetype,
filepath, filepath,
False ) False )
@ -81,7 +80,9 @@ class CompletionSystem( object ):
if not filetype or not filepath: if not filetype or not filepath:
return return
self.completer.AddCandidatesToDatabase( idents, vector = indexer.StringVec()
vector.extend( idents )
self.completer.AddCandidatesToDatabase( vector,
filetype, filetype,
filepath, filepath,
True ) True )