1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
4#include "../base/Matrix.hpp"
6#include "../utils/has_data.hpp"
7#include "../utils/PseudoOracularExtractor.hpp"
28template<
typename Value_,
typename Index_,
class Storage_>
31 PrimaryMyopicFullDense(
const Storage_& storage,
size_t secondary) : my_storage(storage), my_secondary(secondary) {}
33 const Value_* fetch(Index_ i, Value_* buffer) {
34 size_t offset =
static_cast<size_t>(i) * my_secondary;
35 if constexpr(has_data<Value_, Storage_>::value) {
36 return my_storage.data() + offset;
38 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
44 const Storage_& my_storage;
48template<
typename Value_,
typename Index_,
class Storage_>
51 PrimaryMyopicBlockDense(
const Storage_& storage,
size_t secondary, Index_ block_start, Index_ block_length) :
52 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
54 const Value_* fetch(Index_ i, Value_* buffer) {
55 size_t offset =
static_cast<size_t>(i) * my_secondary + my_block_start;
56 if constexpr(has_data<Value_, Storage_>::value) {
57 return my_storage.data() + offset;
59 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
65 const Storage_& my_storage;
67 size_t my_block_start, my_block_length;
70template<
typename Value_,
typename Index_,
class Storage_>
73 PrimaryMyopicIndexDense(
const Storage_& storage,
size_t secondary, VectorPtr<Index_> indices_ptr) :
74 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
76 const Value_* fetch(Index_ i, Value_* buffer) {
78 size_t offset =
static_cast<size_t>(i) * my_secondary;
79 for (
auto x : *my_indices_ptr) {
80 *copy = my_storage[offset +
static_cast<size_t>(x)];
87 const Storage_& my_storage;
89 VectorPtr<Index_> my_indices_ptr;
92template<
typename Value_,
typename Index_,
class Storage_>
95 SecondaryMyopicFullDense(
const Storage_& storage, Index_ secondary, Index_ primary) :
96 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
98 const Value_* fetch(Index_ i, Value_* buffer) {
101 for (Index_ x = 0; x < my_primary; ++x, ++copy, offset += my_secondary) {
102 *copy = my_storage[offset];
108 const Storage_& my_storage;
113template<
typename Value_,
typename Index_,
class Storage_>
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 size_t offset = my_block_start * my_secondary +
static_cast<size_t>(i);
122 for (Index_ x = 0; x < my_block_length; ++x, ++copy, offset += my_secondary) {
123 *copy = my_storage[offset];
129 const Storage_& my_storage;
131 size_t my_block_start;
132 Index_ my_block_length;
135template<
typename Value_,
typename Index_,
class Storage_>
138 SecondaryMyopicIndexDense(
const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
139 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
141 const Value_* fetch(Index_ i, Value_* buffer) {
143 size_t offset =
static_cast<size_t>(i);
144 for (
auto x : *my_indices_ptr) {
145 *copy = my_storage[
static_cast<size_t>(x) * my_secondary + offset];
152 const Storage_& my_storage;
154 VectorPtr<Index_> my_indices_ptr;
172template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
183 if (
static_cast<size_t>(my_nrow) *
static_cast<size_t>(my_ncol) != my_values.size()) {
184 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
221 Index_ secondary()
const {
233 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options&)
const {
234 if (my_row_major ==
row) {
235 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
237 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
242 if (my_row_major ==
row) {
243 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(),
block_start,
block_length);
245 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(),
block_start,
block_length);
250 if (my_row_major ==
row) {
251 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(
indices_ptr));
253 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(
indices_ptr));
262 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(
row,
opt), (
row ? my_ncol : my_nrow),
opt);
271 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(
ptr), std::move(
indices_ptr),
opt);
279 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(
oracle),
dense(
row,
opt));
295 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(
oracle),
sparse(
row,
opt));
312template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
328template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
Wrapper classes for sparse extraction from a dense tatami::Matrix.
Dense column-major matrix.
Definition DenseMatrix.hpp:313
DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:320
Dense matrix representation.
Definition DenseMatrix.hpp:173
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition DenseMatrix.hpp:233
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:298
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DenseMatrix.hpp:261
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:278
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:249
DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major)
Definition DenseMatrix.hpp:182
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:286
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:206
bool prefer_rows() const
Definition DenseMatrix.hpp:198
bool is_sparse() const
Definition DenseMatrix.hpp:202
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:302
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:269
bool uses_oracle(bool) const
Definition DenseMatrix.hpp:200
double is_sparse_proportion() const
Definition DenseMatrix.hpp:204
Index_ nrow() const
Definition DenseMatrix.hpp:194
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:241
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:265
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:282
Index_ ncol() const
Definition DenseMatrix.hpp:196
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:294
Dense row-major matrix.
Definition DenseMatrix.hpp:329
DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:336
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
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