-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
fast_line_sentence.h
45 lines (37 loc) · 1.17 KB
/
fast_line_sentence.h
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
#pragma once
#include <fstream>
#include <sstream>
#include <vector>
class FastLineSentence {
public:
explicit FastLineSentence() : is_eof_(false) { }
explicit FastLineSentence(const std::string& filename, size_t offset = 0) : filename_(filename),
fs_(filename),
offset_(offset),
is_eof_(false) {
fs_.seekg(offset_);
}
std::vector<std::string> ReadSentence() {
if (is_eof_) {
return {};
}
std::string line, word;
std::getline(fs_, line);
std::vector<std::string> res;
std::istringstream iss(line);
while (iss >> word) {
res.push_back(word);
}
if (fs_.eof()) {
is_eof_ = true;
}
return res;
}
inline bool IsEof() const { return is_eof_; }
inline void Reset() { fs_.clear(); fs_.seekg(offset_); is_eof_ = false; }
private:
std::string filename_;
std::ifstream fs_;
size_t offset_;
bool is_eof_;
};