Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
word_count_sequential.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * examples/word_count/word_count_sequential.cpp
3  *
4  * A simple sequential word count implementation using std::unordered_map.
5  *
6  * Part of Project Thrill - http://project-thrill.org
7  *
8  * Copyright (C) 2016 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
11  ******************************************************************************/
12 
15 
16 #include <fstream>
17 #include <iostream>
18 #include <string>
19 #include <unordered_map>
20 
21 using namespace thrill; // NOLINT
22 
23 int main(int argc, char* argv[]) {
24  if (argc == 1) {
25  std::cerr << "Usage: " << argv[0] << " <files>" << std::endl;
26  return 0;
27  }
28 
30 
31  std::unordered_map<std::string, size_t> count_map;
32 
33  for (int argi = 1; argi < argc; ++argi) {
34 
35  std::ifstream in(argv[argi]);
36  if (!in.good()) {
37  std::cerr << "Could not open " << argv[argi] << std::endl;
38  abort();
39  }
40 
41  std::string line;
42  while (std::getline(in, line, '\n'))
43  {
45  line, ' ', [&](const common::StringView& sv) {
46  if (sv.size() == 0) return;
47  ++count_map[sv.ToString()];
48  });
49  }
50  }
51 
52  std::cerr << "word_counting done: " << timer << " s" << std::endl;
53 
54  for (auto& p : count_map) {
55  std::cout << p.first << ": " << p.second << std::endl;
56  }
57 
58  std::cerr << "after output: " << timer << " s" << std::endl;
59 
60  return 0;
61 }
62 
63 /******************************************************************************/
int main(int argc, char *argv[])
static void SplitView(const std::string &str, char sep, F &&callback, std::string::size_type limit=std::string::npos)
Split the given string at each separator character into distinct substrings, and call the given callb...
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
size_t size() const noexcept
Returns the size of this StringView.
Definition: string_view.hpp:92
std::string ToString() const
Returns the data of this StringView as an std::string.
StringView is a reference to a part of a string, consisting of only a char pointer and a length...
Definition: string_view.hpp:31