28 const std::string filepath;
29 std::ifstream readStream;
31 std::shared_ptr<TOC> toc;
34 Reader(
const std::string& fname, D& init) :
36 readStream(filepath, std::ios::binary),
38 toc(std::make_shared<
TOC>())
40 if (!readStream.is_open())
41 throw std::runtime_error(
"Could not open '" + filepath +
"'.");
42 toc->read(readStream);
46 filepath(rdr.filepath),
47 readStream(filepath, std::ios::binary),
51 if (!readStream.is_open())
52 throw std::runtime_error(
"Could not open '" + rdr.filepath +
"'.");
60 readStream.seekg(toc->getIdx(i, j));
61 dest.read(readStream);
69 std::unique_ptr<D> ptr = std::make_unique<D>(scratch);
70 readStream.seekg(toc->getIdx(i, j));
71 ptr->read(readStream);
73 return std::move(ptr);
76 std::unique_ptr<std::vector<std::vector<D>>>
readAll()
81 auto m_ptr = std::make_unique<std::vector<std::vector<D>>>(
83 std::vector<D>(toc->getCols(), scratch));
85 for (
int i = 0; i < toc->getRows(); i++) {
86 for (
int j = 0; j < toc->getCols(); j++) {
87 readStream.seekg(toc->getIdx(i, j));
88 (*m_ptr)[i][j].read(readStream);
92 return std::move(m_ptr);
95 std::unique_ptr<std::vector<D>>
readRow(
int i)
101 auto v_ptr = std::make_unique<std::vector<D>>(toc->getCols(), scratch);
102 for (
int n = 0; n < toc->getCols(); n++) {
103 readStream.seekg(toc->getIdx(i, n));
104 (*v_ptr)[n].read(readStream);
107 return std::move(v_ptr);
110 std::unique_ptr<std::vector<D>>
readCol(
int j)
112 if (readStream.eof())
115 auto v_ptr = std::make_unique<std::vector<D>>(toc->getRows(), scratch);
116 for (
int n = 0; n < toc->getRows(); n++) {
117 readStream.seekg(toc->getIdx(n, j));
118 (*v_ptr)[n].read(readStream);
121 return std::move(v_ptr);
127 template <
typename D>
128 static std::vector<Reader<D>> createReaders(
long count,
129 const std::string& dataFilePath,
132 std::vector<Reader<D>> readers;
133 readers.reserve(count);
134 for (
long i = 0; i < count; ++i) {
135 readers.emplace_back(dataFilePath.c_str(), dummy);