Vince's CSV Parser
csv::CSVReader::iterator Class Reference

An input iterator capable of handling large files. More...

#include <csv_reader.hpp>

Public Member Functions

 iterator (CSVReader *reader)
 
 iterator (CSVReader *, CSVRow &&)
 
CONSTEXPR_14 reference operator* ()
 Access the CSVRow held by the iterator.
 
CONSTEXPR_14 pointer operator-> ()
 Return a pointer to the CSVRow the iterator has stopped at.
 
iteratoroperator++ ()
 Pre-increment iterator. More...
 
iterator operator++ (int)
 Post-increment iterator.
 
CONSTEXPR bool operator== (const iterator &other) const noexcept
 Returns true if iterators were constructed from the same CSVReader and point to the same row.
 
CONSTEXPR bool operator!= (const iterator &other) const noexcept
 

Detailed Description

An input iterator capable of handling large files.

Note
Created by CSVReader::begin() and CSVReader::end().
Iterating over a file
TEST_CASE("Basic CSVReader Iterator Test", "[read_ints_iter]") {
// A file with 100 rows and columns A, B, ... J
// where every value in the ith row is the number i
CSVReader reader("./tests/data/fake_data/ints.csv");
std::vector<std::string> col_names = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
};
int i = 1;
SECTION("Basic Iterator") {
for (auto it = reader.begin(); it != reader.end(); ++it) {
REQUIRE((*it)[0].get<int>() == i);
i++;
}
}
SECTION("Iterator Post-Increment") {
auto it = reader.begin();
REQUIRE((it++)->operator[]("A").get<int>() == 1);
REQUIRE(it->operator[]("A").get<int>() == 2);
}
SECTION("Range-Based For Loop") {
for (auto& row : reader) {
for (auto& j : col_names) REQUIRE(row[j].get<int>() == i);
i++;
}
}
}
CSVReader(csv::string_view filename, CSVFormat format=CSVFormat::guess_csv())
Reads an arbitrarily large CSV file using memory-mapped IO.
Definition: csv_reader.cpp:154
internals::ColNamesPtr col_names
Pointer to a object containing column information.
Definition: csv_reader.hpp:195
Using with <algorithm> library
TEST_CASE("CSVReader Iterator + std::max_elem", "[iter_max_elem]") {
// The first is such that each value in the ith row is the number i
// There are 100 rows
// The second file is a database of California state employee salaries
CSVReader r1("./tests/data/fake_data/ints.csv"),
r2("./tests/data/real_data/2015_StateDepartment.csv");
// Find largest number
auto int_finder = [](CSVRow& left, CSVRow& right) {
return (left["A"].get<int>() < right["A"].get<int>());
};
auto max_int = std::max_element(r1.begin(), r2.end(), int_finder);
// Find highest salary
auto wage_finder = [](CSVRow& left, CSVRow& right) {
return (left["Total Wages"].get<double>() < right["Total Wages"].get<double>());
};
auto max_wage = std::max_element(r2.begin(), r2.end(), wage_finder);
REQUIRE((*max_int)["A"] == 100);
REQUIRE((*max_wage)["Total Wages"] == "812064.87");
}

Definition at line 69 of file csv_reader.hpp.

Member Function Documentation

◆ operator++()

CSVReader::iterator & csv::CSVReader::iterator::operator++ ( )

Pre-increment iterator.

Advance the iterator by one row.

If this CSVReader has an associated file, then the iterator will lazily pull more data from that file until the end of file is reached.

Note
This iterator does not block the thread responsible for parsing CSV.

Definition at line 46 of file csv_reader_iterator.cpp.


The documentation for this class was generated from the following files: