Reader.h
1 /* Copyright (C) 2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 
13 #ifndef READER_H
14 #define READER_H
15 
16 #include <vector>
17 #include <string>
18 #include <fstream>
19 #include <exception>
20 
21 #include "TOC.h"
22 
23 template <typename D>
24 class Reader
25 {
26 
27 private:
28  const std::string filepath;
29  std::ifstream readStream;
30  D& scratch;
31  std::shared_ptr<TOC> toc;
32 
33 public:
34  Reader(const std::string& fname, D& init) :
35  filepath(fname),
36  readStream(filepath, std::ios::binary),
37  scratch(init),
38  toc(std::make_shared<TOC>())
39  {
40  if (!readStream.is_open())
41  throw std::runtime_error("Could not open '" + filepath + "'.");
42  toc->read(readStream);
43  }
44 
45  Reader(const Reader& rdr) :
46  filepath(rdr.filepath),
47  readStream(filepath, std::ios::binary),
48  scratch(rdr.scratch),
49  toc(rdr.toc)
50  {
51  if (!readStream.is_open())
52  throw std::runtime_error("Could not open '" + rdr.filepath + "'.");
53  }
54 
55  void readDatum(D& dest, int i, int j)
56  {
57  if (readStream.eof())
58  readStream.clear();
59 
60  readStream.seekg(toc->getIdx(i, j));
61  dest.read(readStream);
62  }
63 
64  std::unique_ptr<D> readDatum(int i, int j)
65  {
66  if (readStream.eof())
67  readStream.clear();
68 
69  std::unique_ptr<D> ptr = std::make_unique<D>(scratch);
70  readStream.seekg(toc->getIdx(i, j));
71  ptr->read(readStream);
72 
73  return std::move(ptr);
74  }
75 
76  std::unique_ptr<std::vector<std::vector<D>>> readAll()
77  {
78  if (readStream.eof())
79  readStream.clear();
80 
81  auto m_ptr = std::make_unique<std::vector<std::vector<D>>>(
82  toc->getRows(),
83  std::vector<D>(toc->getCols(), scratch));
84 
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);
89  }
90  }
91 
92  return std::move(m_ptr);
93  }
94 
95  std::unique_ptr<std::vector<D>> readRow(int i)
96  {
97 
98  if (readStream.eof())
99  readStream.clear();
100 
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);
105  }
106 
107  return std::move(v_ptr);
108  }
109 
110  std::unique_ptr<std::vector<D>> readCol(int j)
111  {
112  if (readStream.eof())
113  readStream.clear();
114 
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);
119  }
120 
121  return std::move(v_ptr);
122  }
123 
124  const TOC& getTOC() const { return *toc; }
125 };
126 
127 template <typename D>
128 static std::vector<Reader<D>> createReaders(long count,
129  const std::string& dataFilePath,
130  D& dummy)
131 {
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);
136  }
137  return readers;
138 }
139 
140 #endif // READER_H
std::unique_ptr< std::vector< D > > readRow(int i)
Definition: Reader.h:95
void readDatum(D &dest, int i, int j)
Definition: Reader.h:55
std::unique_ptr< std::vector< std::vector< D > > > readAll()
Definition: Reader.h:76
Definition: TOC.h:21
Definition: Reader.h:25
std::unique_ptr< D > readDatum(int i, int j)
Definition: Reader.h:64
Reader(const std::string &fname, D &init)
Definition: Reader.h:34
Reader(const Reader &rdr)
Definition: Reader.h:45
std::unique_ptr< std::vector< D > > readCol(int j)
Definition: Reader.h:110
const TOC & getTOC() const
Definition: Reader.h:124