tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
ConstantMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CONSTANT_MATRIX_HPP
2#define TATAMI_CONSTANT_MATRIX_HPP
3
4#include "../base/Matrix.hpp"
5#include "../dense/SparsifiedWrapper.hpp"
6#include "../utils/new_extractor.hpp"
7
8#include <algorithm>
9
15namespace tatami {
16
21
22template<bool oracle_, typename Value_, typename Index_>
23class DenseFiller : public DenseExtractor<oracle_, Value_, Index_> {
24public:
25 DenseFiller(Index_ length, Value_ value) : my_length(length), my_value(value) {}
26
27 const Value_* fetch(Index_, Value_* buffer) {
28 std::fill_n(buffer, my_length, my_value);
29 return buffer;
30 }
31private:
32 Index_ my_length;
33 Value_ my_value;
34};
35
36template<bool oracle_, typename Value_, typename Index_>
37class SparseFiller : public SparseExtractor<oracle_, Value_, Index_> {
38public:
39 SparseFiller(const Options& opt) : my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
40
41 SparseRange<Value_, Index_> fetch(Index_, Value_* value_buffer, Index_* index_buffer) {
42 return SparseRange<Value_, Index_>(0, (my_needs_value ? value_buffer : NULL), (my_needs_index ? index_buffer : NULL));
43 }
44private:
45 bool my_needs_value;
46 bool my_needs_index;
47};
48
49}
63template<typename Value_, typename Index_>
64class ConstantMatrix : public Matrix<Value_, Index_> {
65public:
71 ConstantMatrix(Index_ nrow, Index_ ncol, Value_ value) : my_nrow(nrow), my_ncol(ncol), my_value(value) {}
72
73private:
74 Index_ my_nrow, my_ncol;
75 Value_ my_value;
76
77public:
78 Index_ nrow() const {
79 return my_nrow;
80 }
81
82 Index_ ncol() const {
83 return my_ncol;
84 }
85
86 bool is_sparse() const {
87 return my_value == 0;
88 }
89
90 double is_sparse_proportion() const {
91 return static_cast<double>(my_value == 0);
92 }
93
94 bool prefer_rows() const {
95 return true; // pretty much arbitrary here.
96 }
97
98 double prefer_rows_proportion() const {
99 return 1; // pretty much arbitrary here.
100 }
101
102 bool uses_oracle(bool) const {
103 return false;
104 }
105
106 /********************
107 *** Myopic dense ***
108 ********************/
109private:
110 template<bool oracle_>
111 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(bool row, MaybeOracle<oracle_, Index_>, const Options&) const {
112 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(row ? my_ncol : my_nrow, my_value);
113 }
114
115 template<bool oracle_>
116 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(bool, MaybeOracle<oracle_, Index_>, [[maybe_unused]] Index_ block_start, Index_ block_length, const Options&) const {
117 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(block_length, my_value);
118 }
119
120 template<bool oracle_>
121 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(bool, MaybeOracle<oracle_, Index_>, VectorPtr<Index_> indices_ptr, const Options&) const {
122 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(indices_ptr->size(), my_value);
123 }
124
125public:
126 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options& opt) const {
127 return dense_internal<false>(row, false, opt);
128 }
129
130 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
132 }
133
134 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
135 return dense_internal<false>(row, false, std::move(indices_ptr), opt);
136 }
137
138 /**********************
139 *** Oracular dense ***
140 **********************/
141public:
142 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
143 return dense_internal<true>(row, std::move(oracle), opt);
144 }
145
146 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length, const Options& opt) const {
148 }
149
150 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
151 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
152 }
153
154 /*********************
155 *** Myopic sparse ***
156 *********************/
157private:
158 template<bool oracle_>
159 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(bool row, MaybeOracle<oracle_, Index_> oracle, const Options& opt) const {
160 if (my_value == 0) {
161 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
162 } else {
163 return std::make_unique<FullSparsifiedWrapper<oracle_, Value_, Index_> >(dense_internal<oracle_>(row, std::move(oracle), opt), (row ? my_ncol : my_nrow), opt);
164 }
165 }
166
167 template<bool oracle_>
168 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length, const Options& opt) const {
169 if (my_value == 0) {
170 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
171 } else {
172 return std::make_unique<BlockSparsifiedWrapper<oracle_, Value_, Index_> >(dense_internal<oracle_>(row, std::move(oracle), block_start, block_length, opt), block_start, block_length, opt);
173 }
174 }
175
176 template<bool oracle_>
177 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(bool, MaybeOracle<oracle_, Index_>, VectorPtr<Index_> indices_ptr, const Options& opt) const {
178 if (my_value == 0) {
179 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
180 } else {
181 auto host = std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(indices_ptr->size(), my_value);
182 return std::make_unique<IndexSparsifiedWrapper<oracle_, Value_, Index_> >(std::move(host), std::move(indices_ptr), opt);
183 }
184 }
185
186public:
187 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
188 return sparse_internal<false>(row, false, opt);
189 }
190
191 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
193 }
194
195 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
196 return sparse_internal<false>(row, false, std::move(indices_ptr), opt);
197 }
198
199 /**********************
200 *** Oracular sparse ***
201 **********************/
202public:
203 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
204 return sparse_internal<true>(row, std::move(oracle), opt);
205 }
206
207 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length, const Options& opt) const {
209 }
210
211 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
212 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
213 }
214};
215
216}
217
218#endif
Matrix containing a constant value.
Definition ConstantMatrix.hpp:64
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:207
bool is_sparse() const
Definition ConstantMatrix.hpp:86
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:130
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition ConstantMatrix.hpp:187
double is_sparse_proportion() const
Definition ConstantMatrix.hpp:90
Index_ nrow() const
Definition ConstantMatrix.hpp:78
double prefer_rows_proportion() const
Definition ConstantMatrix.hpp:98
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:195
bool uses_oracle(bool) const
Definition ConstantMatrix.hpp:102
Index_ ncol() const
Definition ConstantMatrix.hpp:82
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:150
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition ConstantMatrix.hpp:203
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:134
ConstantMatrix(Index_ nrow, Index_ ncol, Value_ value)
Definition ConstantMatrix.hpp:71
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition ConstantMatrix.hpp:126
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:191
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition ConstantMatrix.hpp:146
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition ConstantMatrix.hpp:211
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition ConstantMatrix.hpp:142
bool prefer_rows() const
Definition ConstantMatrix.hpp:94
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:21
Flexible representations for matrix data.
Definition Extractor.hpp:15
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
typename std::conditional< oracle_, OracularSparseExtractor< Value_, Index_ >, MyopicSparseExtractor< Value_, Index_ > >::type SparseExtractor
Definition Extractor.hpp:284
typename std::conditional< oracle_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Definition new_extractor.hpp:20
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Options for accessing data from a Matrix instance.
Definition Options.hpp:30