MoCSI API Reference
Loading...
Searching...
No Matches
MatrixFactory.h
Go to the documentation of this file.
1#ifndef MATRIX_FACTORY_H
2#define MATRIX_FACTORY_H
3
4#include <memory>
5#include <stdexcept>
6#include <string>
7#include <valarray>
8#include <vector>
9
10#include "CoreEnums.h"
11#include "CsrMatrix.h"
12#include "DenseMatrix.h"
13#include "GenericMatrix.h"
14#include "IniParser.h"
15
21template <typename T>
23{
24 private:
25 MatrixType m_default_matrix_type;
26
27 std::unique_ptr<GenericMatrix<T>> createSelectedTypeUnique(MatrixType type);
28 std::shared_ptr<GenericMatrix<T>> createSelectedTypeShared(MatrixType type);
29
30 public:
32 MatrixFactory(const std::string& type);
33
34 std::unique_ptr<GenericMatrix<T>> createUnique();
35 std::unique_ptr<GenericMatrix<T>> createUnique(MatrixType type);
36
37 std::shared_ptr<GenericMatrix<T>> createShared();
38 std::shared_ptr<GenericMatrix<T>> createShared(MatrixType type);
39};
40
47template <typename T>
49 : m_default_matrix_type{setMatrixType(type)}
50{
51}
52
60template <typename T>
62{
63}
64
72template <typename T>
73std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeUnique(MatrixType type)
74{
75 switch (type)
76 {
78 return std::make_unique<DenseMatrix<T>>();
79 case MatrixType::Csr:
80 return std::make_unique<CsrSparseMatrix<T>>();
81 default:
82 std::cerr << "[MatrixFactory]: Unknown MatrixType in _create_selected_type_unique.\n";
83 throw std::invalid_argument(static_cast<std::string>(
84 "[MatrixFactory]: Unknown MatrixType in _create_selected_type_unique."));
85 }
86}
87
95template <typename T>
96std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createSelectedTypeShared(MatrixType type)
97{
98 switch (type)
99 {
101 return std::make_shared<DenseMatrix<T>>();
102 case MatrixType::Csr:
103 return std::make_shared<CsrSparseMatrix<T>>();
104 default:
105 std::cerr << "[MatrixFactory]: Unknown MatrixType in _create_selected_type_shared.\n";
106 throw std::invalid_argument(
107 "[MatrixFactory]: Unknown MatrixType in _create_selected_type_shared.");
108 }
109}
110
116template <typename T>
117std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique()
118{
119 return createSelectedTypeUnique(m_default_matrix_type);
120}
121
129template <typename T>
130std::unique_ptr<GenericMatrix<T>> MatrixFactory<T>::createUnique(MatrixType type)
131{
132 return createSelectedTypeUnique(type);
133}
134
142template <typename T>
143std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared()
144{
145 return createSelectedTypeShared(m_default_matrix_type);
146}
147
155template <typename T>
156std::shared_ptr<GenericMatrix<T>> MatrixFactory<T>::createShared(MatrixType type)
157{
158 return createSelectedTypeShared(type);
159}
160
161#endif
constexpr MatrixType setMatrixType(const std::string &type)
Definition CoreEnums.h:41
MatrixType
Definition CoreEnums.h:36
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
Class that creates empty matrix objects of the specified type.
Definition MatrixFactory.h:23
std::shared_ptr< GenericMatrix< T > > createShared()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:143
MatrixFactory(MatrixType type)
Constructor from enum (internal use) for the MatrixFactory class.
Definition MatrixFactory.h:61
std::unique_ptr< GenericMatrix< T > > createUnique()
Creation function that creates a Matrix based on the default type.
Definition MatrixFactory.h:117