Advertisement
oster_man

MatchDocument(par)

Aug 7th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. //search_server.h
  2.  
  3. //...//
  4. struct Query {
  5.     std::set<std::string> plus_words;
  6.     std::set<std::string> minus_words;
  7.     std::vector<std::string_view> str_plus_words;
  8.     std::vector<std::string_view> str_minus_words;
  9. };
  10. //...//
  11.  
  12. //search_server.cpp
  13.  
  14. //...//
  15. tuple<vector<string>, DocumentStatus> SearchServer::MatchDocument(std::execution::parallel_policy exec_t,const string& raw_query,
  16.                                                         int document_id) const {
  17.     const auto query = ParseQuery(raw_query);
  18.  
  19.     vector<string> matched_words;
  20.  
  21.     if(!document_ids_.count(document_id))
  22.         throw out_of_range("Document don't exists");
  23.  
  24.    
  25.  
  26.     if (!std::all_of(exec_t,query.str_minus_words.begin(),query.str_minus_words.end(),[&](const string_view& word){
  27.         if (word_to_document_freqs_.count(string(word)) == 0) {
  28.             return true;
  29.         }
  30.         if (word_to_document_freqs_.at(string(word)).count(document_id)) {
  31.             matched_words.clear();
  32.             return false;
  33.         }
  34.         return true;
  35.     }))
  36.         return {matched_words, documents_.at(document_id).status};
  37.  
  38.     matched_words.reserve(query.plus_words.size());
  39.     vector<string_view> temp;
  40.     temp.reserve(query.plus_words.size());
  41.     copy_if(exec_t,query.str_plus_words.begin(),query.str_plus_words.end(),back_inserter(temp),[&](const string_view& word){
  42.         if (word_to_document_freqs_.count(string(word)) == 0) {
  43.             return false;
  44.         }
  45.         if(word_to_document_freqs_.at(string(word)).count(document_id)) {
  46.                 return true;
  47.         }
  48.         else
  49.             return false;
  50.     });
  51.  
  52.     std::transform(temp.begin(),temp.end(),back_inserter(matched_words),[](const string_view& sv){
  53.         return sv.data();
  54.     });
  55.  
  56.     return {matched_words, documents_.at(document_id).status};
  57. }
  58.  
  59. SearchServer::Query SearchServer::ParseQuery(const string& text) const {
  60.         Query result;
  61.         const auto string_query = SplitIntoWords(text);
  62.         for_each (string_query.begin(),string_query.end(),[&](const string& word){
  63.             const auto query_word = ParseQueryWord(word);
  64.             if (!query_word.is_stop) {
  65.                 if (query_word.is_minus) {
  66.                     if (!result.minus_words.count(query_word.data)){
  67.                         result.minus_words.insert(query_word.data);
  68.                         result.str_minus_words.push_back(result.minus_words.find(query_word.data)->data());
  69.                     }
  70.                 }
  71.                 else {
  72.                     if (!result.plus_words.count(query_word.data)){
  73.                         result.plus_words.insert(query_word.data);
  74.                         result.str_plus_words.push_back(result.plus_words.find(query_word.data)->data());
  75.                     }
  76.                 }
  77.             }
  78.         });
  79.         return result;
  80.     }
  81. //...//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement