-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.3.cc
127 lines (91 loc) · 2.44 KB
/
12.3.cc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <memory>
using namespace std;
class QueryResult{
friend ostream &print(ostream &, QueryResult qr);
public:
QueryResult(const string & s, shared_ptr<vector<string>> file, shared_ptr<set<int>> lines):
word(s), m_file(file), m_lines(lines){}
set<int>::iterator begin(){
return m_lines->begin();
}
set<int>::const_iterator begin() const{
return m_lines->cbegin();
}
set<int>::iterator end(){
return m_lines->end();
}
set<int>::const_iterator end() const{
return m_lines->cend();
}
shared_ptr<vector<string>> getLine() const{
return m_file;
}
private:
string word;
shared_ptr<vector<string>> m_file;
shared_ptr<set<int>> m_lines;
};
class TextQuery{
public:
TextQuery(ifstream & infile);
QueryResult query(const string & s) const;
private:
shared_ptr<vector<string>> file;
map<string, shared_ptr<set<int>> > sToLine;
};
TextQuery::TextQuery(ifstream & infile)
// :lineString(make_shared<vector<string>>()),
:file(new vector<string>())
{
string s;
int line = 1;
while(getline(infile, s)){
file->push_back(s);
istringstream ss(s);
string word;
while(ss >> word){
auto & p = sToLine[word];
if(!p)
p.reset(new set<int>());
p->insert(line);
}
++line;
}
cout << line << endl;
}
QueryResult TextQuery::query(const string & s) const{
shared_ptr<set<int>> nodata(new set<int>());
auto iter = sToLine.find(s);
if(iter == sToLine.end())
return QueryResult(s, file, nodata);
else
return QueryResult(s, file, iter->second);
}
ostream & print(ostream & os, QueryResult result){
os << result.word << " occurs " << result.m_lines->size() << " times" << endl;
for(auto i : *result.m_lines)
os << "\t" << "(line " << i << ") " << (*result.m_file)[i - 1] << endl;
return os;
}
void runQueries(ifstream & infile){
TextQuery tq(infile);
while(true){
cout << "enter word to look for, or q to quit: ";
string s;
while(!(cin >> s) || s == "q") break;
print(cout, tq.query(s)) << endl;
}
}
int main(int argc, char const *argv[])
{
ifstream in("12.3.cc");
runQueries(in);
return 0;
}