Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <future>
- #include <execution>
- #include <iostream>
- #include <map>
- #include <numeric>
- #include <set>
- #include <sstream>
- #include <string>
- using namespace std;
- vector<string_view> Split(string_view str) {
- vector<string_view> result;
- while (true) {
- const auto space = str.find(' ');
- if (space != 0 && !str.empty()) {
- result.push_back(str.substr(0, space));
- }
- if (space == str.npos) {
- break;
- } else {
- str.remove_prefix(space + 1);
- }
- }
- return result;
- }
- struct Stats {
- map<string, int> word_frequences;
- void operator+=(const Stats &other) {
- for (const auto &it: other.word_frequences) {
- word_frequences[it.first] += it.second;
- }
- }
- };
- using KeyWords = set<string, less<>>;
- Stats foo(const KeyWords &key_words, vector<string> dataset) {
- Stats stat;
- std::for_each(execution::par,
- dataset.begin(), dataset.end(),
- [&key_words, &stat](const auto line) {
- for (const auto word: Split(line)) {
- if (key_words.count(word) > 0) {
- ++stat.word_frequences[string(word)];
- }
- }
- });
- return stat;
- }
- Stats ExploreKeyWords(const KeyWords &key_words, istream &input) {
- const size_t MAX_SIZE = 5000;
- vector<string> dataset;
- dataset.reserve(MAX_SIZE);
- vector<future<Stats>> futures;
- for (string line; getline(input, line);) {
- dataset.push_back(std::move(line));
- if (dataset.size() >= MAX_SIZE) {
- futures.push_back(async(foo, cref(key_words), dataset));
- dataset.clear();
- }
- }
- Stats result;
- if (!dataset.empty()) {
- result += foo(cref(key_words), dataset);
- }
- std::for_each(std::execution::par,
- futures.begin(), futures.end(),
- [&result](auto &f) { result += f.get(); });
- return result;
- }
- int main() {
- const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
- stringstream ss;
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't available\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";
- for (const auto &[word, frequency]: ExploreKeyWords(key_words, ss).word_frequences) {
- cout << word << " " << frequency << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment