Skip to content

Commit 683e5c0

Browse files
fix: copy of buffered file iterator (#96)
1 parent 2e45da5 commit 683e5c0

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

include/rusty_iterators/file_iterator.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ template <FIterType type>
2121
class 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.
2624
template <>
2725
class 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

6862
template <>
@@ -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;

tests/file_iterator.test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ TEST(TestBufferedFileIterator, TestSizeHint)
6969

7070
ASSERT_EQ(it.sizeHint(), 4);
7171
}
72+
73+
TEST(TestBufferedFileIterator, TestSizeHintAfterConsumption)
74+
{
75+
auto testFileName = std::string{"./tests/test_data.txt"};
76+
auto it = FileIterator<FIterType::Buffered>{testFileName}.advanceBy(2);
77+
78+
ASSERT_EQ(it.sizeHint(), 2);
79+
}
80+
81+
TEST(TestBufferedFileIterator, TestCopyBufferedIterator)
82+
{
83+
auto testFileName = std::string{"./tests/test_data.txt"};
84+
auto it = FileIterator<FIterType::Buffered>{testFileName};
85+
86+
it.next();
87+
auto cp = it;
88+
it.next();
89+
90+
EXPECT_THAT(it.collect(), ElementsAreArray({"3", "4"}));
91+
EXPECT_THAT(cp.collect(), ElementsAreArray({"2", "3", "4"}));
92+
}

tests/iterator.test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ TEST(TestIterator, TestCopySavesIteratorState)
2222
auto vec = std::vector{1, 2, 3, 4};
2323

2424
auto it = LazyIterator{vec};
25+
2526
it.next();
2627
auto cp = it;
28+
it.next();
2729

2830
auto x = it.collect();
2931
auto y = cp.collect();
3032

31-
EXPECT_THAT(x, ElementsAreArray(std::array{2, 3, 4}));
33+
EXPECT_THAT(x, ElementsAreArray(std::array{3, 4}));
3234
EXPECT_THAT(y, ElementsAreArray(std::array{2, 3, 4}));
3335
}
3436

0 commit comments

Comments
 (0)