If query empty, Results sorted lexicographically

This change was also the root cause of the crash bug I spent two days tracking
down. The problem was that the new bool member was not added to the custom copy
ctor... since we don't really need a custom copy ctor for Result, we're going
with the compiler-provided one.
This commit is contained in:
Strahinja Val Markovic 2012-07-15 19:40:24 -07:00
parent ca861d9f0c
commit 52d8a1236b
3 changed files with 50 additions and 57 deletions

View File

@ -72,6 +72,7 @@ int NumWordBoundaryCharMatches( const std::string &query,
Result::Result( bool is_subsequence ) Result::Result( bool is_subsequence )
: :
query_is_empty_( true ),
is_subsequence_( is_subsequence ), is_subsequence_( is_subsequence ),
first_char_same_in_query_and_text_( false ), first_char_same_in_query_and_text_( false ),
ratio_of_word_boundary_chars_in_query_( 0 ), ratio_of_word_boundary_chars_in_query_( 0 ),
@ -90,6 +91,7 @@ Result::Result( bool is_subsequence,
const std::string &word_boundary_chars, const std::string &word_boundary_chars,
const std::string &query ) const std::string &query )
: :
query_is_empty_( true ),
is_subsequence_( is_subsequence ), is_subsequence_( is_subsequence ),
first_char_same_in_query_and_text_( false ), first_char_same_in_query_and_text_( false ),
ratio_of_word_boundary_chars_in_query_( 0 ), ratio_of_word_boundary_chars_in_query_( 0 ),
@ -104,29 +106,14 @@ Result::Result( bool is_subsequence,
} }
// TODO: do we need a custom copy ctor?
Result::Result( const Result& other )
:
is_subsequence_( other.is_subsequence_ ),
first_char_same_in_query_and_text_(
other.first_char_same_in_query_and_text_ ),
ratio_of_word_boundary_chars_in_query_(
other.ratio_of_word_boundary_chars_in_query_ ),
word_boundary_char_utilization_( other.word_boundary_char_utilization_ ),
query_is_candidate_prefix_( other.query_is_candidate_prefix_ ),
text_is_lowercase_( other.text_is_lowercase_ ),
char_match_index_sum_( other.char_match_index_sum_ ),
text_( other.text_ )
{
}
bool Result::operator< ( const Result &other ) const bool Result::operator< ( const Result &other ) const
{ {
// Yes, this is ugly but it also needs to be fast. Since this is called a // Yes, this is ugly but it also needs to be fast. Since this is called a
// bazillion times, we have to make sure only the required comparisons are // bazillion times, we have to make sure only the required comparisons are
// made, and no more. // made, and no more.
if ( !query_is_empty_ )
{
if ( first_char_same_in_query_and_text_ != if ( first_char_same_in_query_and_text_ !=
other.first_char_same_in_query_and_text_ ) other.first_char_same_in_query_and_text_ )
{ {
@ -182,7 +169,9 @@ bool Result::operator< ( const Result &other ) const
if ( text_is_lowercase_ != other.text_is_lowercase_ ) if ( text_is_lowercase_ != other.text_is_lowercase_ )
return text_is_lowercase_; return text_is_lowercase_;
}
// Lexicographic comparison
return *text_ < *other.text_; return *text_ < *other.text_;
} }
@ -191,6 +180,8 @@ void Result::SetResultFeaturesFromQuery(
const std::string &word_boundary_chars, const std::string &word_boundary_chars,
const std::string &query) const std::string &query)
{ {
query_is_empty_ = query.empty();
if ( query.empty() || text_->empty() ) if ( query.empty() || text_->empty() )
return; return;

View File

@ -35,8 +35,6 @@ public:
const std::string &word_boundary_chars, const std::string &word_boundary_chars,
const std::string &query ); const std::string &query );
Result( const Result& other );
bool operator< ( const Result &other ) const; bool operator< ( const Result &other ) const;
inline bool IsSubsequence() const inline bool IsSubsequence() const
@ -54,6 +52,9 @@ private:
const std::string &query, const std::string &query,
const std::string &word_boundary_chars ); const std::string &word_boundary_chars );
// true when the query for which the result was created was an empty string;
// in these cases we just use a lexicographic comparison
bool query_is_empty_;
// true when the characters of the query are a subsequence of the characters // true when the characters of the query are a subsequence of the characters
// in the candidate text, e.g. the characters "abc" are a subsequence for // in the candidate text, e.g. the characters "abc" are a subsequence for

View File

@ -94,6 +94,7 @@ class ClangCompleter( Completer ):
self.completer = indexer.ClangCompleter() self.completer = indexer.ClangCompleter()
def CandidatesForQuery( self, query ): def CandidatesForQuery( self, query ):
# TODO: sanitize query
files = indexer.UnsavedFileVec() files = indexer.UnsavedFileVec()
# CAREFUL HERE! For UnsavedFile filename and contents we are referring # CAREFUL HERE! For UnsavedFile filename and contents we are referring