Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions include/rusty_iterators/file_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ template <FIterType type>
class FileIterator : public IterInterface<std::string, FileIterator<type>>
{};

// TODO: Perf.
// With the buffered iterator a lot of iterator functions could be optimized.
template <>
class FileIterator<FIterType::Buffered>
: public IterInterface<std::string, FileIterator<FIterType::Buffered>>
Expand All @@ -31,38 +29,34 @@ class FileIterator<FIterType::Buffered>
explicit FileIterator(const std::string& filePath)
{
std::ifstream is{filePath};

if (!is.is_open())
{
throw std::runtime_error{"Could not open the file."};
}

std::string nextLine;

while (std::getline(is, nextLine))
{
fileLines.push_back(std::move(nextLine));
}
is.close();
ptr = fileLines.begin();
};

auto next() -> std::optional<std::string>
{
[[unlikely]] if (ptr == fileLines.end())
[[unlikely]] if (ptr == fileLines.size())
{
return std::nullopt;
}
auto line = *ptr;
auto line = fileLines.at(ptr);
ptr += 1;
return std::move(line);
}

[[nodiscard]] auto sizeHint() const -> std::optional<size_t> { return fileLines.end() - ptr; }
[[nodiscard]] auto sizeHint() const -> std::optional<size_t> { return fileLines.size() - ptr; }

private:
std::vector<std::string> fileLines{};
std::vector<std::string>::iterator ptr;
size_t ptr = 0;
};

template <>
Expand All @@ -81,7 +75,6 @@ class FileIterator<FIterType::Lazy>
auto next() -> std::optional<std::string>
{
std::string nextLine;

[[unlikely]] if (!std::getline(is, nextLine))
{
return std::nullopt;
Expand Down
21 changes: 21 additions & 0 deletions tests/file_iterator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,24 @@ TEST(TestBufferedFileIterator, TestSizeHint)

ASSERT_EQ(it.sizeHint(), 4);
}

TEST(TestBufferedFileIterator, TestSizeHintAfterConsumption)
{
auto testFileName = std::string{"./tests/test_data.txt"};
auto it = FileIterator<FIterType::Buffered>{testFileName}.advanceBy(2);

ASSERT_EQ(it.sizeHint(), 2);
}

TEST(TestBufferedFileIterator, TestCopyBufferedIterator)
{
auto testFileName = std::string{"./tests/test_data.txt"};
auto it = FileIterator<FIterType::Buffered>{testFileName};

it.next();
auto cp = it;
it.next();

EXPECT_THAT(it.collect(), ElementsAreArray({"3", "4"}));
EXPECT_THAT(cp.collect(), ElementsAreArray({"2", "3", "4"}));
}
4 changes: 3 additions & 1 deletion tests/iterator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ TEST(TestIterator, TestCopySavesIteratorState)
auto vec = std::vector{1, 2, 3, 4};

auto it = LazyIterator{vec};

it.next();
auto cp = it;
it.next();

auto x = it.collect();
auto y = cp.collect();

EXPECT_THAT(x, ElementsAreArray(std::array{2, 3, 4}));
EXPECT_THAT(x, ElementsAreArray(std::array{3, 4}));
EXPECT_THAT(y, ElementsAreArray(std::array{2, 3, 4}));
}

Expand Down