forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextQuery.cpp
33 lines (31 loc) · 1.04 KB
/
TextQuery.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
#include <sstream>
#include "TextQuery.h"
#include "QueryResult.h"
TextQuery::TextQuery(std::ifstream &in)
: text(std::make_shared<std::vector<std::string>>()), lines() {
line_no_type ln = 0;
for (std::string line; std::getline(in, line); text->push_back(line), ++ln) {
std::istringstream iss(line);
for (std::string word; iss >> word; ) {
//auto ret = lines.insert({word, std::make_shared<std::set<line_no_type>>()});
//if (!ret.second)
// ret.first->second->insert(ln);
if (lines.find(word) == lines.end())
lines.insert({word, std::make_shared<std::set<line_no_type>>()});
lines.at(word)->insert(ln);
}
}
}
QueryResult TextQuery::query(const std::string &word) const {
auto it = lines.find(word);
if (it == lines.end())
return QueryResult(word);
line_no_type total = 0;
for (const auto &ln : *(it->second)) {
std::istringstream iss((*text)[ln]);
for (std::string wd; iss >> wd; )
if (wd == word)
++total;
}
return QueryResult(word, total, it->second, text);
}