@@ -21,8 +21,6 @@ template <FIterType type>
2121class FileIterator : public IterInterface <std::string, FileIterator<type>>
2222{};
2323
24- // TODO: Perf.
25- // With the buffered iterator a lot of iterator functions could be optimized.
2624template <>
2725class FileIterator <FIterType::Buffered>
2826 : public IterInterface<std::string, FileIterator<FIterType::Buffered>>
@@ -31,38 +29,34 @@ class FileIterator<FIterType::Buffered>
3129 explicit FileIterator (const std::string& filePath)
3230 {
3331 std::ifstream is{filePath};
34-
3532 if (!is.is_open ())
3633 {
3734 throw std::runtime_error{" Could not open the file." };
3835 }
39-
4036 std::string nextLine;
41-
4237 while (std::getline (is, nextLine))
4338 {
4439 fileLines.push_back (std::move (nextLine));
4540 }
4641 is.close ();
47- ptr = fileLines.begin ();
4842 };
4943
5044 auto next () -> std::optional<std::string>
5145 {
52- [[unlikely]] if (ptr == fileLines.end ())
46+ [[unlikely]] if (ptr == fileLines.size ())
5347 {
5448 return std::nullopt ;
5549 }
56- auto line = * ptr;
50+ auto line = fileLines. at ( ptr) ;
5751 ptr += 1 ;
5852 return std::move (line);
5953 }
6054
61- [[nodiscard]] auto sizeHint () const -> std::optional<size_t> { return fileLines.end () - ptr; }
55+ [[nodiscard]] auto sizeHint () const -> std::optional<size_t> { return fileLines.size () - ptr; }
6256
6357 private:
6458 std::vector<std::string> fileLines{};
65- std::vector<std::string>::iterator ptr;
59+ size_t ptr = 0 ;
6660};
6761
6862template <>
@@ -81,7 +75,6 @@ class FileIterator<FIterType::Lazy>
8175 auto next () -> std::optional<std::string>
8276 {
8377 std::string nextLine;
84-
8578 [[unlikely]] if (!std::getline (is, nextLine))
8679 {
8780 return std::nullopt ;
0 commit comments