1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
6#include "../utils/has_data.hpp"
16#include "sanisizer/sanisizer.hpp"
31namespace DenseMatrix_internals {
33template<
typename Value_,
typename Index_,
class Storage_>
34class PrimaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
36 PrimaryMyopicFullDense(
const Storage_& storage,
const Index_ secondary) : my_storage(storage), my_secondary(secondary) {}
38 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
39 const auto offset = sanisizer::product_unsafe<I<
decltype(my_storage.size())> >(my_secondary, i);
40#ifndef TATAMI_DEBUG_FORCE_COPY
41 if constexpr(has_data<Value_, Storage_>::value) {
42 return my_storage.data() + offset;
45 std::copy_n(my_storage.begin() + offset, my_secondary, buffer);
47#ifndef TATAMI_DEBUG_FORCE_COPY
53 const Storage_& my_storage;
57template<
typename Value_,
typename Index_,
class Storage_>
58class PrimaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
60 PrimaryMyopicBlockDense(
const Storage_& storage,
const Index_ secondary,
const Index_ block_start,
const Index_ block_length) :
61 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
63 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
64 const auto offset = sanisizer::nd_offset<I<
decltype(my_storage.size())> >(my_block_start, my_secondary, i);
65#ifndef TATAMI_DEBUG_FORCE_COPY
66 if constexpr(has_data<Value_, Storage_>::value) {
67 return my_storage.data() + offset;
70 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
72#ifndef TATAMI_DEBUG_FORCE_COPY
78 const Storage_& my_storage;
80 Index_ my_block_start, my_block_length;
83template<
typename Value_,
typename Index_,
class Storage_>
84class PrimaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
86 PrimaryMyopicIndexDense(
const Storage_& storage,
const Index_ secondary, VectorPtr<Index_> indices_ptr) :
87 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
89 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
90 const auto& indices = *my_indices_ptr;
91 const auto nindices = indices.size();
92 for (I<
decltype(nindices)> x = 0; x < nindices; ++x) {
93 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(indices[x], my_secondary, i)];
99 const Storage_& my_storage;
101 VectorPtr<Index_> my_indices_ptr;
104template<
typename Value_,
typename Index_,
class Storage_>
105class SecondaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
107 SecondaryMyopicFullDense(
const Storage_& storage,
const Index_ secondary,
const Index_ primary) :
108 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
110 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
111 for (I<
decltype(my_primary)> x = 0; x < my_primary; ++x) {
112 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, x)];
118 const Storage_& my_storage;
123template<
typename Value_,
typename Index_,
class Storage_>
124class SecondaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
126 SecondaryMyopicBlockDense(
const Storage_& storage,
const Index_ secondary,
const Index_ block_start,
const Index_ block_length) :
127 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
129 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
130 for (I<
decltype(my_block_length)> x = 0; x < my_block_length; ++x) {
131 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, my_block_start + x)];
137 const Storage_& my_storage;
139 Index_ my_block_start;
140 Index_ my_block_length;
143template<
typename Value_,
typename Index_,
class Storage_>
144class SecondaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
146 SecondaryMyopicIndexDense(
const Storage_& storage,
const Index_ secondary, VectorPtr<Index_> indices_ptr) :
147 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
149 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
150 const auto& indices = *my_indices_ptr;
151 const auto nindices = indices.size();
152 for (I<
decltype(nindices)> x = 0; x < nindices; ++x) {
153 buffer[x] = my_storage[sanisizer::nd_offset<I<
decltype(my_storage.size())> >(i, my_secondary, indices[x])];
159 const Storage_& my_storage;
161 VectorPtr<Index_> my_indices_ptr;
179template<
typename Value_,
typename Index_,
class Storage_>
190 my_nrow(
nrow), my_ncol(
ncol), my_values(std::move(values)), my_row_major(row_major)
192 const auto nvalues = my_values.size();
195 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
197 }
else if (!sanisizer::is_equal(nvalues / my_nrow, my_ncol) || nvalues % my_nrow != 0) {
198 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
203 Index_ my_nrow, my_ncol;
211 Index_
nrow()
const {
return my_nrow; }
216 Index_
ncol()
const {
return my_ncol; }
248 Index_ primary()
const {
256 Index_ secondary()
const {
271 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Options&)
const {
272 if (my_row_major == row) {
273 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
275 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
282 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options&)
const {
283 if (my_row_major == row) {
284 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
286 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
294 if (my_row_major == row) {
295 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
297 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
308 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Options& opt)
const {
309 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(row, opt), (row ? my_ncol : my_nrow), opt);
315 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options& opt)
const {
316 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(
dense(row, block_start, block_length, opt), block_start, block_length, opt);
323 auto ptr =
dense(row, indices_ptr, opt);
324 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
334 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
335 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, opt));
341 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
344 const Index_ block_start,
345 const Index_ block_length,
348 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_length, opt));
355 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
365 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
366 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
372 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
375 const Index_ block_start,
376 const Index_ block_length,
379 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_length, opt));
386 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
395template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
411template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
Convert index type to container size.
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:396
DenseColumnMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:403
Dense matrix representation.
Definition DenseMatrix.hpp:180
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:385
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:315
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:354
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:241
bool prefer_rows() const
Definition DenseMatrix.hpp:221
bool is_sparse() const
Definition DenseMatrix.hpp:231
DenseMatrix(const Index_ nrow, const Index_ ncol, Storage_ values, const bool row_major)
Definition DenseMatrix.hpp:189
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:365
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DenseMatrix.hpp:308
bool uses_oracle(const bool) const
Definition DenseMatrix.hpp:226
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:293
double is_sparse_proportion() const
Definition DenseMatrix.hpp:236
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &) const
Definition DenseMatrix.hpp:271
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:341
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:322
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:372
Index_ nrow() const
Definition DenseMatrix.hpp:211
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:282
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:334
Index_ ncol() const
Definition DenseMatrix.hpp:216
Dense row-major matrix.
Definition DenseMatrix.hpp:412
DenseRowMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:419
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Copy data from one buffer to another.
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