-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextQuery.h
More file actions
41 lines (37 loc) · 1 KB
/
TextQuery.h
File metadata and controls
41 lines (37 loc) · 1 KB
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
#ifndef TEXTQUERY_H
#define TEXTQUERY_H
#include <map>
#include <set>
#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <tuple>
class TextQuery {
public:
typedef std::vector<std::string>::size_type line_no;
typedef std::tuple<std::string,
std::shared_ptr<std::set<line_no>>,
std::shared_ptr<std::vector<std::string>>>
Result;
TextQuery(std::ifstream &in) : file(std::make_shared<std::vector<std::string>>()) {
line_no line_num = 0;
for (std::string line; std::getline(in, line); file->push_back(line), ++line_num) {
std::string word;
std::istringstream stream(line);
while (stream >> word) {
std::shared_ptr<std::set<line_no>> &lines = wm[word];
if (!lines)
lines.reset(new std::set<line_no>);
lines->insert(line_num);
}
}
}
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;
};
#endif