Seredenko-V

RemoveDocument

Aug 4th, 2022
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. void SearchServer::RemoveDocument(int document_id) {
  2.     SearchServer::RemoveDocument(execution::seq, document_id);
  3. }
  4.  
  5. void SearchServer::RemoveDocument(execution::sequenced_policy, int document_id) {
  6.     if (!documents_.count(document_id)) {
  7.         throw invalid_argument("Документа с указанным id не существует.");
  8.     }
  9.     // log(количество документов) * количество слов в удаляемом документе,
  10.     // т.к. у каждого документа свой словарь
  11.     for (const auto& [word, freq] : word_frequencies_in_document_.at(document_id)) {
  12.         // log(количество слов во всех документах)
  13.         word_to_document_freqs_[{ word.begin(), word.end() }].erase(document_id);
  14.     }
  15.     word_frequencies_in_document_.erase(document_id);
  16.     documents_.erase(document_id);
  17.     order_addition_document_.erase(document_id);
  18. }
  19.  
  20. void SearchServer::RemoveDocument(execution::parallel_policy, int document_id) {
  21.     if (!documents_.count(document_id)) {
  22.         throw invalid_argument("Документа с указанным id не существует.");
  23.     }
  24.     // для распараллеливания for
  25.     map<string_view, double>& frequency_word_in_each_document = word_frequencies_in_document_.at(document_id); // частоты слов в документе
  26.     vector<string_view> words(frequency_word_in_each_document.size());
  27.     // параллельное перекладывание указателей на слова в вектор
  28.     transform(execution::par, frequency_word_in_each_document.begin(), frequency_word_in_each_document.end(),
  29.         words.begin(),
  30.         [](const pair<const string_view, double>& ptr_to_word) {
  31.             return ptr_to_word.first;
  32.         });
  33.     // удаление указанного id из ассоциаций с каждым словом
  34.     for_each(execution::par, words.begin(), words.end(),
  35.         [this, document_id](const string_view word) {
  36.             word_to_document_freqs_[{ word.begin(), word.end() }].erase(document_id);
  37.         });
  38.     word_frequencies_in_document_.erase(document_id);
  39.     documents_.erase(document_id);
  40.     order_addition_document_.erase(document_id);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment