Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void SearchServer::RemoveDocument(int document_id) {
- SearchServer::RemoveDocument(execution::seq, document_id);
- }
- void SearchServer::RemoveDocument(execution::sequenced_policy, int document_id) {
- if (!documents_.count(document_id)) {
- throw invalid_argument("Документа с указанным id не существует.");
- }
- // log(количество документов) * количество слов в удаляемом документе,
- // т.к. у каждого документа свой словарь
- for (const auto& [word, freq] : word_frequencies_in_document_.at(document_id)) {
- // log(количество слов во всех документах)
- word_to_document_freqs_[{ word.begin(), word.end() }].erase(document_id);
- }
- word_frequencies_in_document_.erase(document_id);
- documents_.erase(document_id);
- order_addition_document_.erase(document_id);
- }
- void SearchServer::RemoveDocument(execution::parallel_policy, int document_id) {
- if (!documents_.count(document_id)) {
- throw invalid_argument("Документа с указанным id не существует.");
- }
- // для распараллеливания for
- map<string_view, double>& frequency_word_in_each_document = word_frequencies_in_document_.at(document_id); // частоты слов в документе
- vector<string_view> words(frequency_word_in_each_document.size());
- // параллельное перекладывание указателей на слова в вектор
- transform(execution::par, frequency_word_in_each_document.begin(), frequency_word_in_each_document.end(),
- words.begin(),
- [](const pair<const string_view, double>& ptr_to_word) {
- return ptr_to_word.first;
- });
- // удаление указанного id из ассоциаций с каждым словом
- for_each(execution::par, words.begin(), words.end(),
- [this, document_id](const string_view word) {
- word_to_document_freqs_[{ word.begin(), word.end() }].erase(document_id);
- });
- word_frequencies_in_document_.erase(document_id);
- documents_.erase(document_id);
- order_addition_document_.erase(document_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment