Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- #include <future>
- #include <utility>
- #include <algorithm>
- #include <string_view>
- #include <execution>
- #include <numeric>
- #include "log_duration.h"
- using KeyWords = std::set<std::string, std::less<>>;
- struct Stats {
- std::map<std::string, int> word_frequences;
- void operator+=(const Stats& other) {
- for (const auto& [word, frequence] : other.word_frequences) {
- word_frequences[word] += frequence;
- }
- }
- };
- Stats CalcFreq(const KeyWords& key_words, const std::vector<std::string>& lines) {
- Stats stats;
- for (std::string_view line : lines) {
- const int64_t pos_end = line.npos;
- while (true) {
- int64_t space = line.find(' ');
- std::string_view word = line.substr(0, space);
- for_each(key_words.begin(), key_words.end(),
- [&stats, word](auto& key_word) {
- if (key_word == word) {
- ++stats.word_frequences[key_word];
- }
- });
- if (space == pos_end) {
- break;
- }
- else {
- line.remove_prefix(++space);
- }
- }
- }
- return stats;
- }
- Stats ExploreKeyWords(const KeyWords& key_words, std::istream& input) {
- Stats res;
- std::vector<std::future<Stats>> WordsStats;
- std::vector<std::string> lines;
- std::string line;
- while (getline(input, line)) {
- lines.emplace_back(line);
- if (lines.size() >= 5000) {
- WordsStats.emplace_back(move(async(CalcFreq, cref(key_words), move(lines))));
- }
- }
- if (!lines.empty()) {
- WordsStats.emplace_back(move(async(CalcFreq, cref(key_words), move(lines))));
- }
- for (std::future<Stats>& WordsStat : WordsStats) {
- res += WordsStat.get();
- }
- /*for_each(WordsStats.begin(), WordsStats.end(),
- [&res](auto& WordsStat) {
- res += WordsStat.get();
- });*/
- return res;
- }
- int main() {
- const KeyWords key_words = { "yangle", "rocks", "sucks", "all" };
- std::stringstream ss;
- for (int i = 0; i < 50000; ++i) {
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't availablen\n";
- ss << "10 reasons why yangle is the best IT company\n";
- ss << "yangle rocks others suck\n";
- ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
- }
- {
- LOG_DURATION("ExploreKeyWords");
- for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
- std::cout << word << " " << frequency << std::endl;
- }
- }
- /*
- rocks 2
- sucks 1
- yangle 6
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement