-
Notifications
You must be signed in to change notification settings - Fork 33
/
17.03.cpp
105 lines (96 loc) · 2.63 KB
/
17.03.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Exercise 17.3: Rewrite the TextQuery programs from § 12.3 (p. 484) to use a
* tuple instead of the QueryResult class. Explain which design you think is
* better and why.
*
* By Faisal Saadatmand
*/
/*
* The design that uses the QueryResult class is better. It is easier to read,
* expand and maintain.
*/
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
class TextQuery {
public:
using line_no = std::vector<std::string>::size_type;
using query_result = std::tuple<std::string,
std::shared_ptr<std::set<TextQuery::line_no>>,
std::shared_ptr<std::vector<std::string>>>;
TextQuery(std::ifstream &);
query_result query(const std::string &) const;
private:
std::shared_ptr<std::vector<std::string>> file;
std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;
};
TextQuery::TextQuery(std::ifstream &is) : file(new std::vector<std::string>)
{
std::string text;
while (getline(is, text)) {
file->push_back(text);
int n = file->size() - 1;
std::istringstream line(text);
std::string word;
while (line >> word) {
auto &lines = wm[word]; // lines is a shared pointer
if (!lines)
lines.reset(new std::set<line_no>);
lines->insert(n);
}
}
}
TextQuery::query_result
TextQuery::query(const std::string &sought) const
{
static std::shared_ptr<std::set<line_no>> nodata(new std::set<line_no>);
auto loc = wm.find(sought);
if (loc == wm.end())
return std::make_tuple(sought, nodata, file); // not found
return std::make_tuple(sought, loc->second, file);
}
std::string make_plural(std::size_t ctr, const std::string &word,
const std::string &ending)
{
return (ctr > 1) ? word + ending : word;
}
std::ostream &print(std::ostream &os, const TextQuery::query_result &qr)
{
os << std::get<0>(qr) << " occurs " << std::get<1>(qr)->size() << " "
<< make_plural(std::get<1>(qr)->size(), "times", "s") << '\n';
for (auto num : *std::get<1>(qr))
os << "\t(line " << num + 1 << ") "
<< *(std::get<2>(qr)->begin() + num) << std::endl;
return os;
}
void runQueries(std::ifstream &infile)
{
TextQuery tq(infile);
while (true) {
std::cout << "Enter word to look for, or q to quit: ";
std::string s;
if (!(std::cin >> s) || s == "q")
break;
print(std::cout, tq.query(s)) << std::endl;
}
}
int main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << "Usage: " << *argv << " infile\n";
return -1;
}
std::ifstream inFile(*++argv);
if (!inFile) {
std::cerr << "could not open " << *argv << '\n';
return -1;
}
runQueries(inFile);
return 0;
}