buffered reader and writer
This commit is contained in:
parent
e50051b8e6
commit
6be1112091
5 changed files with 137 additions and 1 deletions
78
reader.hpp
Normal file
78
reader.hpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#pragma once
|
||||
#include "record.hpp"
|
||||
#include <istream>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
class Reader {
|
||||
private:
|
||||
std::vector<Record> read_buffer;
|
||||
std::istream& input_stream;
|
||||
size_t buffer_size;
|
||||
size_t total_reads_count;
|
||||
int last_element_idx; // last valid element in buffer
|
||||
int current_idx; // current reading position
|
||||
|
||||
// returns true if at least one record was returned
|
||||
bool refill_buffer()
|
||||
{
|
||||
last_element_idx = -1;
|
||||
current_idx = 0;
|
||||
|
||||
for (size_t i = 0; i < buffer_size; i++) {
|
||||
if (!(input_stream >> read_buffer[i])) {
|
||||
break;
|
||||
}
|
||||
last_element_idx = static_cast<int>(i);
|
||||
}
|
||||
|
||||
return last_element_idx >= 0;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Reader(std::istream& input_stream, size_t buffer_size = 8)
|
||||
: read_buffer(buffer_size)
|
||||
, input_stream(input_stream)
|
||||
, buffer_size(buffer_size)
|
||||
, total_reads_count(0)
|
||||
, last_element_idx(-1)
|
||||
, current_idx(0)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<Record> read()
|
||||
{
|
||||
if (current_idx > last_element_idx) {
|
||||
bool read_new_items = refill_buffer();
|
||||
if (!read_new_items) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
Record record = read_buffer[current_idx];
|
||||
current_idx++;
|
||||
total_reads_count++;
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
size_t total_reads() const
|
||||
{
|
||||
return total_reads_count;
|
||||
}
|
||||
|
||||
// #todo remove if not used in the end
|
||||
// bool has_more() const
|
||||
// {
|
||||
// return current_idx <= last_element_idx || !input_stream.eof();
|
||||
// }
|
||||
|
||||
// void reset()
|
||||
// {
|
||||
// input_stream.clear();
|
||||
// input_stream.seekg(0);
|
||||
// last_element_idx = -1;
|
||||
// current_idx = 0;
|
||||
// total_reads_count = 0;
|
||||
// }
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue