Seredenko-V

Untitled

Aug 4th, 2022 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. tuple<vector<string_view>, DocumentStatus> SearchServer::MatchDocument(const string_view raw_query, int document_id) const {
  2.     return MatchDocument(execution::seq, raw_query, document_id);
  3. }
  4.  
  5. tuple<vector<string_view>, DocumentStatus> SearchServer::MatchDocument(execution::sequenced_policy policy,
  6.     const string_view raw_query, int document_id) const {
  7.     //LOG_DURATION_STREAM("MatchDocument"s, cout);
  8.     if (document_id < 0 || word_frequencies_in_document_.count(document_id) == 0) {
  9.         throw out_of_range("Документа с указанным id не существует.");
  10.     }
  11.     const Query query = ParseQuery(raw_query);
  12.     vector<string_view> matched_words;
  13.     for (const string_view word : query.plus_words) {
  14.         if (word_to_document_freqs_.count(word) == 0) {
  15.             continue;
  16.         }
  17.         if (word_to_document_freqs_.at(string(word)).count(document_id)) {
  18.             matched_words.push_back(word);
  19.         }
  20.     }
  21.     for (const string_view word : query.minus_words) {
  22.         if (word_to_document_freqs_.count(string(word)) == 0) {
  23.             continue;
  24.         }
  25.         if (word_to_document_freqs_.at(string(word)).count(document_id)) {
  26.             matched_words.clear();
  27.             break;
  28.         }
  29.     }
  30.  
  31.     return { matched_words, documents_.at(document_id).status };
  32. }
  33.  
  34. tuple<vector<string_view>, DocumentStatus> SearchServer::MatchDocument(execution::parallel_policy policy,
  35.     const string_view raw_query, int document_id) const {
  36.     if (document_id < 0 || word_frequencies_in_document_.count(document_id) == 0) {
  37.         throw out_of_range("Документа с указанным id не существует.");
  38.     }
  39.     Query query = ParseQuery(raw_query, false);
  40.  
  41.     if (any_of(policy, query.minus_words.begin(), query.minus_words.end(),
  42.         [this, &document_id](const string_view minus_word) {
  43.             return word_frequencies_in_document_.at(document_id).count(minus_word);
  44.         })) {
  45.         return { {}, documents_.at(document_id).status };
  46.     }
  47.     vector<string_view> matched_words(query.plus_words.size());
  48.  
  49.     vector<string_view>::iterator end_new_size = copy_if(policy, query.plus_words.begin(), query.plus_words.end(),
  50.         matched_words.begin(),
  51.         [this, &document_id](const string_view plus_word) {
  52.             return word_frequencies_in_document_.at(document_id).count(plus_word);
  53.         });
  54.  
  55.     matched_words.resize(distance(matched_words.begin(), end_new_size));
  56.     sort(policy, matched_words.begin(), matched_words.end());
  57.     matched_words.erase(unique(policy, matched_words.begin(), matched_words.end()), matched_words.end());
  58.  
  59.     return { matched_words, documents_.at(document_id).status };
  60. }
Advertisement
Add Comment
Please, Sign In to add comment