Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include "search_server.h"
  2. #include "iterator_range.h"
  3.  
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <sstream>
  7. #include <iostream>
  8. #include "parse.h"
  9.  
  10. vector<string> SplitIntoWords( const string& line ) {
  11. vector<string> res;
  12. for ( auto x: SplitBy(line,' ') )
  13. res.push_back(move(string(x)));
  14. return res;
  15. }
  16.  
  17. vector<string_view> SplitIntoViews( const string& line ) {
  18. return SplitBy(line,' ');
  19. }
  20.  
  21.  
  22. SearchServer::SearchServer(istream& document_input) {
  23. UpdateDocumentBase(document_input);
  24. }
  25.  
  26. void SearchServer::UpdateDocumentBase(istream& document_input) {
  27. InvertedIndex new_index;
  28. for ( string current_document; getline(document_input, current_document); new_index.Add(move(current_document)) ) ;
  29. lock_guard<mutex> lock(mtx);
  30. auto access= this->index.GetAccess();
  31. swap(access.ref_to_value,new_index);
  32. }
  33.  
  34. void SearchServer::AddQueriesStream( istream& query_input, ostream& search_results_output ) {
  35. auto kernel= [this]( istream &qi, ostream &os ) {
  36. AddQueriesStreamWorker(qi,os);
  37. };
  38. async(kernel,ref(query_input),ref(search_results_output));
  39. }
  40.  
  41. void SearchServer::AddQueriesStreamWorker( istream& query_input, ostream& search_results_output ) {
  42.  
  43. int *seen= new int[50100], yes= 0;
  44. for ( int i= 0; i < 50100; seen[i++]= 0 ) ;
  45. int *cnt= new int[50100];
  46.  
  47. //shared_lock<shared_mutex> lock(mtx);
  48. for ( string current_query; getline(query_input, current_query); ) {
  49. ++yes;
  50. const auto words= SplitIntoViews(current_query);
  51. {
  52. auto access = index.GetAccess();
  53. for (const auto &word : words) {
  54. list<size_t> vec;
  55. {
  56. vec= access.ref_to_value.Lookup(word);
  57. }
  58. for ( const auto it: vec ) {
  59. if ( seen[it] == yes )
  60. ++cnt[it];
  61. else seen[it]= yes, cnt[it]= 1;
  62. }
  63. }
  64. }
  65. vector<pair<size_t,size_t>> search_results;
  66. size_t sz;
  67. {
  68. sz= this->index.GetAccess().ref_to_value.size();
  69. }
  70. for ( int i= 0; i < sz; ++i )
  71. if ( seen[i] == yes )
  72. search_results.emplace_back(i,cnt[i]);
  73. partial_sort(
  74. begin(search_results),
  75. min(begin(search_results)+5,end(search_results)),
  76. end(search_results),
  77. []( const pair<size_t, size_t> &lhs, const pair<size_t, size_t> &rhs ) {
  78. int64_t lhs_docid = lhs.first;
  79. auto lhs_hit_count = lhs.second;
  80. int64_t rhs_docid = rhs.first;
  81. auto rhs_hit_count = rhs.second;
  82. return make_pair(lhs_hit_count, -lhs_docid) > make_pair(rhs_hit_count, -rhs_docid);
  83. }
  84. );
  85. search_results_output << current_query << ':';
  86. for ( auto [docid, hitcount] : Head(search_results, 5) ) {
  87. search_results_output << " {"
  88. << "docid: " << docid << ", "
  89. << "hitcount: " << hitcount << '}';
  90. }
  91. search_results_output << endl;
  92. }
  93.  
  94. //delete[] seen, delete[] cnt;
  95. }
  96.  
  97. void InvertedIndex::Add( string document ) {
  98. const size_t docid = docs.size();
  99. for ( const auto& word : SplitIntoViews(document) ) {
  100. insert(word);
  101. assert( find(word) < index.size() );
  102. index[find(word)].push_back(docid);
  103. }
  104. docs.push_back(move(document));
  105. }
  106.  
  107. const list<size_t>& InvertedIndex::Lookup(const string& word) const {
  108. auto id= find(word);
  109. if ( id < +oo )
  110. return index[id];
  111. return empty_list;
  112. }
  113.  
  114. const list<size_t>& InvertedIndex::Lookup(string_view sv) const {
  115. auto id= find(sv);
  116. if ( id < +oo )
  117. return index[id];
  118. return empty_list;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement