1#ifndef TATAMI_CONSTANT_MATRIX_HPP
2#define TATAMI_CONSTANT_MATRIX_HPP
20namespace ConstantMatrix_internal {
22template<
bool oracle_,
typename Value_,
typename Index_>
23class DenseFiller :
public DenseExtractor<oracle_, Value_, Index_> {
25 DenseFiller(Index_ length, Value_ value) : my_length(length), my_value(value) {}
27 const Value_* fetch(Index_, Value_* buffer) {
28 std::fill_n(buffer, my_length, my_value);
36template<
bool oracle_,
typename Value_,
typename Index_>
37class SparseFiller :
public SparseExtractor<oracle_, Value_, Index_> {
39 SparseFiller(
const Options& opt) : my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
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));
63template<
typename Value_,
typename Index_>
74 Index_ my_nrow, my_ncol;
91 return static_cast<double>(my_value == 0);
110 template<
bool oracle_>
112 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(row ? my_ncol : my_nrow, my_value);
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);
120 template<
bool oracle_>
122 return std::make_unique<ConstantMatrix_internal::DenseFiller<oracle_, Value_, Index_> >(indices_ptr->size(), my_value);
126 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options& opt)
const {
127 return dense_internal<false>(row,
false, opt);
130 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
131 return dense_internal<false>(row,
false, block_start, block_length, opt);
135 return dense_internal<false>(row,
false, std::move(indices_ptr), opt);
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);
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 {
147 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
151 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
158 template<
bool oracle_>
161 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
163 return std::make_unique<FullSparsifiedWrapper<oracle_, Value_, Index_> >(dense_internal<oracle_>(row, std::move(oracle), opt), (row ? my_ncol : my_nrow), opt);
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 {
170 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
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);
176 template<
bool oracle_>
179 return std::make_unique<ConstantMatrix_internal::SparseFiller<oracle_, Value_, Index_> >(opt);
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);
187 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
188 return sparse_internal<false>(row,
false, opt);
191 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
192 return sparse_internal<false>(row,
false, block_start, block_length, opt);
196 return sparse_internal<false>(row,
false, std::move(indices_ptr), opt);
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);
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 {
208 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
212 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
Virtual class for a matrix of some numeric type.
Wrapper classes for sparse extraction from a dense tatami::Matrix.
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
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
typename std::conditional< oracle_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Definition new_extractor.hpp:20
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
Options for accessing data from a Matrix instance.
Definition Options.hpp:30