-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
131 lines (108 loc) · 4.02 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "process_queries.h"
#include "search_server.h"
#include "log_duration.h"
#include <execution>
#include <iostream>
#include <random>
#include <string>
#include <vector>
using namespace std;
string GenerateWord(mt19937& generator, int max_length) {
const int length = uniform_int_distribution(1, max_length)(generator);
string word;
word.reserve(length);
for (int i = 0; i < length; ++i) {
word.push_back(uniform_int_distribution('a', 'z')(generator));
}
return word;
}
vector<string> GenerateDictionary(mt19937& generator, int word_count, int max_length) {
vector<string> words;
words.reserve(word_count);
for (int i = 0; i < word_count; ++i) {
words.push_back(GenerateWord(generator, max_length));
}
words.erase(unique(words.begin(), words.end()), words.end());
return words;
}
string GenerateQuery(mt19937& generator, const vector<string>& dictionary, int word_count, double minus_prob = 0) {
string query;
for (int i = 0; i < word_count; ++i) {
if (!query.empty()) {
query.push_back(' ');
}
if (uniform_real_distribution<>(0, 1)(generator) < minus_prob) {
query.push_back('-');
}
query += dictionary[uniform_int_distribution<int>(0, dictionary.size() - 1)(generator)];
}
return query;
}
vector<string> GenerateQueries(mt19937& generator, const vector<string>& dictionary, int query_count, int max_word_count) {
vector<string> queries;
queries.reserve(query_count);
for (int i = 0; i < query_count; ++i) {
queries.push_back(GenerateQuery(generator, dictionary, max_word_count));
}
return queries;
}
template <typename ExecutionPolicy>
void Test(string_view mark, const SearchServer& search_server, const vector<string>& queries, ExecutionPolicy&& policy) {
LOG_DURATION(mark);
double total_relevance = 0;
for (const string_view query : queries) {
for (const auto& document : search_server.FindTopDocuments(policy, query)) {
total_relevance += document.relevance;
}
}
cout << total_relevance << endl;
}
#define TEST(policy) Test(#policy, search_server, queries, execution::policy)
void PrintDocument(const Document& document) {
cout << "{ "s
<< "document_id = "s << document.id << ", "s
<< "relevance = "s << document.relevance << ", "s
<< "rating = "s << document.rating << " }"s << endl;
}
int main() {
SearchServer search_server("and with"s);
int id = 0;
for (
const string& text : {
"white cat and yellow hat"s,
"curly cat curly tail"s,
"nasty dog with big eyes"s,
"nasty pigeon john"s,
}
) {
search_server.AddDocument(++id, text, DocumentStatus::ACTUAL, { 1, 2 });
}
cout << "ACTUAL by default:"s << endl;
// sequenced version
for (const Document& document : search_server.FindTopDocuments("curly nasty cat"s)) {
PrintDocument(document);
}
cout << "BANNED:"s << endl;
// sequenced version
for (const Document& document : search_server.FindTopDocuments(execution::seq, "curly nasty cat"s, DocumentStatus::BANNED)) {
PrintDocument(document);
}
cout << "Even ids:"s << endl;
// parallel version
for (const Document& document : search_server.FindTopDocuments(execution::par, "curly nasty cat"s, [](int document_id, DocumentStatus status, int rating) { return document_id % 2 == 0; })) {
PrintDocument(document);
}
{ // speed_test
mt19937 generator;
const auto dictionary = GenerateDictionary(generator, 1000, 10);
const auto documents = GenerateQueries(generator, dictionary, 10'000, 70);
SearchServer search_server(dictionary[0]);
for (size_t i = 0; i < documents.size(); ++i) {
search_server.AddDocument(i, documents[i], DocumentStatus::ACTUAL, { 1, 2, 3 });
}
const auto queries = GenerateQueries(generator, dictionary, 100, 70);
TEST(seq);
TEST(par);
}
return 0;
}