1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
7#include "../utils/Index_to_container.hpp"
15#include "sanisizer/sanisizer.hpp"
30namespace DenseMatrix_internals {
32template<
typename Value_,
typename Index_,
class Storage_>
33class PrimaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
35 PrimaryMyopicFullDense(
const Storage_& storage, Index_ secondary) : my_storage(storage), my_secondary(secondary) {}
37 const Value_* fetch(Index_ i, Value_* buffer) {
38 auto offset = sanisizer::product_unsafe<
decltype(my_storage.size())>(my_secondary, i);
39 if constexpr(has_data<Value_, Storage_>::value) {
40 return my_storage.data() + offset;
42 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
48 const Storage_& my_storage;
52template<
typename Value_,
typename Index_,
class Storage_>
53class PrimaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
55 PrimaryMyopicBlockDense(
const Storage_& storage, Index_ secondary, Index_ block_start, Index_ block_length) :
56 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
58 const Value_* fetch(Index_ i, Value_* buffer) {
59 auto offset = sanisizer::nd_offset<
decltype(my_storage.size())>(my_block_start, my_secondary, i);
60 if constexpr(has_data<Value_, Storage_>::value) {
61 return my_storage.data() + offset;
63 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
69 const Storage_& my_storage;
71 Index_ my_block_start, my_block_length;
74template<
typename Value_,
typename Index_,
class Storage_>
75class PrimaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
77 PrimaryMyopicIndexDense(
const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
78 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
80 const Value_* fetch(Index_ i, Value_* buffer) {
81 const auto& indices = *my_indices_ptr;
82 for (
decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
83 buffer[x] = my_storage[sanisizer::nd_offset<
decltype(my_storage.size())>(indices[x], my_secondary, i)];
89 const Storage_& my_storage;
91 VectorPtr<Index_> my_indices_ptr;
94template<
typename Value_,
typename Index_,
class Storage_>
95class SecondaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
97 SecondaryMyopicFullDense(
const Storage_& storage, Index_ secondary, Index_ primary) :
98 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
100 const Value_* fetch(Index_ i, Value_* buffer) {
101 for (
decltype(my_primary) x = 0; x < my_primary; ++x) {
102 buffer[x] = my_storage[sanisizer::nd_offset<
decltype(my_storage.size())>(i, my_secondary, x)];
108 const Storage_& my_storage;
113template<
typename Value_,
typename Index_,
class Storage_>
114class SecondaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
116 SecondaryMyopicBlockDense(
const Storage_& storage, Index_ secondary, Index_ block_start, Index_ block_length) :
117 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
119 const Value_* fetch(Index_ i, Value_* buffer) {
120 for (
decltype(my_block_length) x = 0; x < my_block_length; ++x) {
121 buffer[x] = my_storage[sanisizer::nd_offset<
decltype(my_storage.size())>(i, my_secondary, my_block_start + x)];
127 const Storage_& my_storage;
129 Index_ my_block_start;
130 Index_ my_block_length;
133template<
typename Value_,
typename Index_,
class Storage_>
134class SecondaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
136 SecondaryMyopicIndexDense(
const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
137 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
139 const Value_* fetch(Index_ i, Value_* buffer) {
140 const auto& indices = *my_indices_ptr;
141 for (
decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
142 buffer[x] = my_storage[sanisizer::nd_offset<
decltype(my_storage.size())>(i, my_secondary, indices[x])];
148 const Storage_& my_storage;
150 VectorPtr<Index_> my_indices_ptr;
168template<
typename Value_,
typename Index_,
class Storage_>
178 DenseMatrix(Index_
nrow, Index_
ncol, Storage_ values,
bool row_major) : my_nrow(
nrow), my_ncol(
ncol), my_values(std::move(values)), my_row_major(row_major) {
179 auto nvalues = my_values.size();
182 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
184 }
else if (!safe_non_negative_equal(nvalues / my_nrow, my_ncol) || nvalues % my_nrow != 0) {
185 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
190 Index_ my_nrow, my_ncol;
195 Index_
nrow()
const {
return my_nrow; }
197 Index_
ncol()
const {
return my_ncol; }
214 Index_ primary()
const {
222 Index_ secondary()
const {
234 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options&)
const {
235 if (my_row_major == row) {
236 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
238 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
242 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options&)
const {
243 if (my_row_major == row) {
244 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
246 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
251 if (my_row_major == row) {
252 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
254 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
262 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
263 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(row, opt), (row ? my_ncol : my_nrow), opt);
266 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
267 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(
dense(row, block_start, block_length, opt), block_start, block_length, opt);
271 auto ptr =
dense(row, indices_ptr, opt);
272 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
279 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
280 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, opt));
283 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end,
const Options& opt)
const {
284 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_end, opt));
288 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
295 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
296 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
299 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end,
const Options& opt)
const {
300 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_end, opt));
304 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
313template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
329template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
Virtual class for a matrix of some numeric type.
Wrapper classes for sparse extraction from a dense tatami::Matrix.
Dense column-major matrix.
Definition DenseMatrix.hpp:314
DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:321
Dense matrix representation.
Definition DenseMatrix.hpp:169
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition DenseMatrix.hpp:234
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:299
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DenseMatrix.hpp:262
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:279
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:250
DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major)
Definition DenseMatrix.hpp:178
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 DenseMatrix.hpp:287
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:207
bool prefer_rows() const
Definition DenseMatrix.hpp:199
bool is_sparse() const
Definition DenseMatrix.hpp:203
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 DenseMatrix.hpp:303
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:270
bool uses_oracle(bool) const
Definition DenseMatrix.hpp:201
double is_sparse_proportion() const
Definition DenseMatrix.hpp:205
Index_ nrow() const
Definition DenseMatrix.hpp:195
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:242
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:266
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:283
Index_ ncol() const
Definition DenseMatrix.hpp:197
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:295
Dense row-major matrix.
Definition DenseMatrix.hpp:330
DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:337
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Compile-time checks for the data() method.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
Options for accessing data from a Matrix instance.
Definition Options.hpp:30