List instead of vector for storing candidates

This should be faster because appending to a list is O(1), and only amort. O(1)
when appending to a vector.
This commit is contained in:
Strahinja Val Markovic 2012-05-10 21:56:19 -07:00
parent 69c8afb251
commit ba76e3f6e6
2 changed files with 8 additions and 12 deletions

View File

@ -110,16 +110,12 @@ void Completer::AddCandidatesToDatabase(
const std::string &filetype, const std::string &filetype,
const std::string &filepath ) const std::string &filepath )
{ {
std::vector< const Candidate *> &candidates = std::list< const Candidate *> &candidates =
GetCandidateVector( filetype, filepath ); GetCandidateList( filetype, filepath );
int num_candidates = new_candidates.size();
candidates.clear(); candidates.clear();
candidates.reserve( num_candidates );
for (int i = 0; i < num_candidates; ++i) foreach ( const std::string &candidate_text, new_candidates )
{ {
const std::string &candidate_text = new_candidates[ i ];
const Candidate *&candidate = GetValueElseInsert( candidate_repository_, const Candidate *&candidate = GetValueElseInsert( candidate_repository_,
candidate_text, NULL ); candidate_text, NULL );
if ( !candidate ) if ( !candidate )
@ -214,7 +210,7 @@ void Completer::ResultsForQueryAndType( const std::string &query,
} }
std::vector< const Candidate* >& Completer::GetCandidateVector( std::list< const Candidate* >& Completer::GetCandidateList(
const std::string &filetype, const std::string &filetype,
const std::string &filepath ) const std::string &filepath )
{ {
@ -224,11 +220,11 @@ std::vector< const Candidate* >& Completer::GetCandidateVector(
if ( !path_to_candidates ) if ( !path_to_candidates )
path_to_candidates.reset( new FilepathToCandidates() ); path_to_candidates.reset( new FilepathToCandidates() );
boost::shared_ptr< std::vector< const Candidate* > > &candidates = boost::shared_ptr< std::list< const Candidate* > > &candidates =
(*path_to_candidates)[ filepath ]; (*path_to_candidates)[ filepath ];
if ( !candidates ) if ( !candidates )
candidates.reset( new std::vector< const Candidate* >() ); candidates.reset( new std::list< const Candidate* >() );
return *candidates; return *candidates;
} }

View File

@ -42,7 +42,7 @@ typedef boost::unordered_map< std::string, const Candidate* >
// filepath -> *( *candidate ) // filepath -> *( *candidate )
typedef boost::unordered_map< std::string, typedef boost::unordered_map< std::string,
boost::shared_ptr< std::vector< const Candidate* > > > boost::shared_ptr< std::list< const Candidate* > > >
FilepathToCandidates; FilepathToCandidates;
// filetype -> *( filepath -> *( *candidate ) ) // filetype -> *( filepath -> *( *candidate ) )
@ -95,7 +95,7 @@ private:
const std::string &filetype, const std::string &filetype,
std::vector< Result > &results ) const; std::vector< Result > &results ) const;
std::vector< const Candidate* >& GetCandidateVector( std::list< const Candidate* >& GetCandidateList(
const std::string &filetype, const std::string &filetype,
const std::string &filepath ); const std::string &filepath );