-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
wordcount.cpp
51 lines (42 loc) · 1.35 KB
/
wordcount.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
#include "wordcount.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
// Assumptions
// 1. Function should read the input from the file, i.e. caching the input is
// not allowed.
// 2. The input is always encoded in UTF-8.
// 3. Break only on space, tab and newline (do not break on non-breaking space).
// 4. Sort words by frequency AND secondary sort in alphabetical order.
// Implementation rules
// 1. You can add new files but dependencies are generally not allowed unless it
// is a header-only library.
// 2. Your submission must be single-threaded, however feel free to implement
// multi-threaded version (optional).
#ifdef SOLUTION
//
// Your solution here.
//
#else
// Baseline solution.
// Do not change it - you can use for quickly checking speedups
// of your solution agains the baseline, see check_speedup.py
std::vector<WordCount> wordcount(std::string filePath) {
std::unordered_map<std::string, int> m;
m.max_load_factor(0.5);
std::vector<WordCount> mvec;
std::ifstream inFile{filePath};
if (!inFile) {
std::cerr << "Invalid input file: " << filePath << "\n";
return mvec;
}
std::string s;
while (inFile >> s)
m[s]++;
mvec.reserve(m.size());
for (auto &p : m)
mvec.emplace_back(WordCount{p.second, move(p.first)});
std::sort(mvec.begin(), mvec.end(), std::greater<WordCount>());
return mvec;
}
#endif