MUQ  0.4.3
BlockDataset.h
Go to the documentation of this file.
1 #ifndef BLOCKDATASET_H
2 #define BLOCKDATASET_H
3 
6 
7 #include <boost/any.hpp>
8 
9 #include <functional>
10 
11 #include <typeindex>
12 #include <typeinfo>
13 #include <unordered_map>
14 
15 namespace muq
16 {
17 namespace Utilities
18 {
19 
21  {
22 
23  public:
24 
25  BlockDataset(std::string const& path_,
26  std::shared_ptr<HDF5File> file_,
27  const int startRow_,
28  const int startCol_,
29  const int numRows_,
30  const int numCols_) : path(path_),
31  file(file_),
32  startRow(startRow_),
33  startCol(startCol_),
34  numRows(numRows_),
35  numCols(numCols_){};
36 
37 
38  typedef std::function<void(boost::any const&, BlockDataset& )> AnyWriterType;
39  typedef std::unordered_map<std::type_index, AnyWriterType> AnyWriterMapType;
40 
41  static std::shared_ptr<AnyWriterMapType> GetAnyWriterMap();
42 
43  BlockDataset& operator=(boost::any const& val);
44 
46  BlockDataset& operator=(ScalarType val)
47  {
48  Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic> temp = val*Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic>::Ones(numRows, numCols);
49 
50  file->WritePartialMatrix(path, temp, startRow, startCol);
51 
52  return *this;
53  }
54 
56  BlockDataset& operator=(MatrixType const& val)
57  {
58  assert(val.rows()==numRows);
59  assert(val.cols()==numCols);
60 
61  file->WritePartialMatrix(path, val, startRow, startCol);
62  return *this;
63  }
64 
65  template<typename ScalarType, int rows, int cols>
66  operator Eigen::Matrix<ScalarType,rows,cols>()
67  {
68  return eval<ScalarType,rows,cols>();
69  }
70 
71  template<typename ScalarType=double, int rows=Eigen::Dynamic, int cols=Eigen::Dynamic>
72  Eigen::Matrix<ScalarType,rows,cols> eval()
73  {
74  return file->ReadPartialMatrix<ScalarType, rows, cols>(path, startRow, startCol, numRows, numCols);
75  }
76 
77  template<typename ScalarType, int rows, int cols>
78  operator ScalarType()
79  {
80  assert(numRows==1);
81  assert(numCols==1);
82 
83  return file->ReadPartialMatrix<ScalarType, rows, cols>(path, startRow, startCol, numRows, numCols)(0);
84  }
85 
86 
87 
88  private:
89 
90  // Path to the original dataset
91  const std::string path;
92 
93  // The HDF5File
94  std::shared_ptr<HDF5File> file;
95 
96  // Integers defining the slice of the dataset
97  const int startRow;
98  const int startCol;
99  const int numRows;
100  const int numCols;
101 
102  }; // class BlockDataset
103 
104 
105 #ifndef REGISTER_HDF5BLOCK_ANYTYPE
106 #define REGISTER_HDF5BLOCK_ANYTYPE(REGNAME, NAME) static auto regHDF ##REGNAME \
107  = muq::Utilities::BlockDataset::GetAnyWriterMap()->insert(std::make_pair(std::type_index(typeid(NAME)), muq::Utilities::AnyWriter<NAME>() ));
108 
109 #endif
110 
111 } // namespace Utilities
112 } // namespace muq
113 
114 
115 
116 
117 #endif
BlockDataset & operator=(ScalarType val)
Definition: BlockDataset.h:46
std::unordered_map< std::type_index, AnyWriterType > AnyWriterMapType
Definition: BlockDataset.h:39
Eigen::Matrix< ScalarType, rows, cols > eval()
Definition: BlockDataset.h:72
static std::shared_ptr< AnyWriterMapType > GetAnyWriterMap()
std::function< void(boost::any const &, BlockDataset &)> AnyWriterType
Definition: BlockDataset.h:35
const std::string path
Definition: BlockDataset.h:91
BlockDataset & operator=(boost::any const &val)
Definition: BlockDataset.cpp:6
BlockDataset(std::string const &path_, std::shared_ptr< HDF5File > file_, const int startRow_, const int startCol_, const int numRows_, const int numCols_)
Definition: BlockDataset.h:25
std::shared_ptr< HDF5File > file
Definition: BlockDataset.h:94
BlockDataset & operator=(MatrixType const &val)
Definition: BlockDataset.h:56
int int FloatType value
Definition: json.h:15223