1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright (C) 2013 Google Inc.
//
// This file is part of YouCompleteMe.
//
// YouCompleteMe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// YouCompleteMe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
#include "IdentifierDatabase.h"
#include "standard.h"
#include "Candidate.h"
#include "CandidateRepository.h"
#include "IdentifierUtils.h"
#include "Result.h"
#include "Utils.h"
#include <boost/thread/locks.hpp>
#include <boost/unordered_set.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/cxx11/any_of.hpp>
using boost::algorithm::any_of;
using boost::algorithm::is_upper;
namespace YouCompleteMe {
IdentifierDatabase::IdentifierDatabase()
: candidate_repository_( CandidateRepository::Instance() ) {
}
void IdentifierDatabase::AddIdentifiers(
const FiletypeIdentifierMap &filetype_identifier_map ) {
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
foreach ( const FiletypeIdentifierMap::value_type & filetype_and_map,
filetype_identifier_map ) {
foreach( const FilepathToIdentifiers::value_type & filepath_and_identifiers,
filetype_and_map.second ) {
AddIdentifiersNoLock( filepath_and_identifiers.second,
filetype_and_map.first,
filepath_and_identifiers.first );
}
}
}
void IdentifierDatabase::AddIdentifiers(
const std::vector< std::string > &new_candidates,
const std::string &filetype,
const std::string &filepath ) {
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
AddIdentifiersNoLock( new_candidates, filetype, filepath );
}
void IdentifierDatabase::ClearCandidatesStoredForFile(
const std::string &filetype,
const std::string &filepath ) {
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
GetCandidateSet( filetype, filepath ).clear();
}
void IdentifierDatabase::ResultsForQueryAndType(
const std::string &query,
const std::string &filetype,
std::vector< Result > &results ) const {
FiletypeCandidateMap::const_iterator it;
{
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
it = filetype_candidate_map_.find( filetype );
if ( it == filetype_candidate_map_.end() || query.empty() )
return;
}
Bitset query_bitset = LetterBitsetFromString( query );
bool query_has_uppercase_letters = any_of( query, is_upper() );
boost::unordered_set< const Candidate * > seen_candidates;
seen_candidates.reserve( candidate_repository_.NumStoredCandidates() );
{
boost::lock_guard< boost::mutex > locker( filetype_candidate_map_mutex_ );
foreach ( const FilepathToCandidates::value_type & path_and_candidates,
*it->second ) {
foreach ( const Candidate * candidate, *path_and_candidates.second ) {
if ( ContainsKey( seen_candidates, candidate ) )
continue;
else
seen_candidates.insert( candidate );
if ( !candidate->MatchesQueryBitset( query_bitset ) )
continue;
Result result = candidate->QueryMatchResult(
query, query_has_uppercase_letters );
if ( result.IsSubsequence() )
results.push_back( result );
}
}
}
std::sort( results.begin(), results.end() );
}
// WARNING: You need to hold the filetype_candidate_map_mutex_ before calling
// this function and while using the returned set.
std::set< const Candidate * > &IdentifierDatabase::GetCandidateSet(
const std::string &filetype,
const std::string &filepath ) {
boost::shared_ptr< FilepathToCandidates > &path_to_candidates =
filetype_candidate_map_[ filetype ];
if ( !path_to_candidates )
path_to_candidates.reset( new FilepathToCandidates() );
boost::shared_ptr< std::set< const Candidate * > > &candidates =
( *path_to_candidates )[ filepath ];
if ( !candidates )
candidates.reset( new std::set< const Candidate * >() );
return *candidates;
}
// WARNING: You need to hold the filetype_candidate_map_mutex_ before calling
// this function and while using the returned set.
void IdentifierDatabase::AddIdentifiersNoLock(
const std::vector< std::string > &new_candidates,
const std::string &filetype,
const std::string &filepath ) {
std::set< const Candidate *> &candidates =
GetCandidateSet( filetype, filepath );
std::vector< const Candidate * > repository_candidates =
candidate_repository_.GetCandidatesForStrings( new_candidates );
candidates.insert( repository_candidates.begin(),
repository_candidates.end() );
}
} // namespace YouCompleteMe
|