1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
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 (!safe_non_negative_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;
208 Index_
nrow()
const {
return my_nrow; }
210 Index_
ncol()
const {
return my_ncol; }
227 Index_ primary()
const {
235 Index_ secondary()
const {
247 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Options&)
const {
248 if (my_row_major == row) {
249 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
251 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
255 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options&)
const {
256 if (my_row_major == row) {
257 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
259 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
264 if (my_row_major == row) {
265 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
267 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
275 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Options& opt)
const {
276 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(row, opt), (row ? my_ncol : my_nrow), opt);
279 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
const bool row,
const Index_ block_start,
const Index_ block_length,
const Options& opt)
const {
280 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(
dense(row, block_start, block_length, opt), block_start, block_length, opt);
284 auto ptr =
dense(row, indices_ptr, opt);
285 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
292 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
293 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, opt));
296 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
299 const Index_ block_start,
300 const Index_ block_end,
303 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_end, opt));
307 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
314 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
const bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
315 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
318 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
321 const Index_ block_start,
322 const Index_ block_end,
325 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_end, opt));
329 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
338template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
354template<
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:339
DenseColumnMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:346
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:328
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:279
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:306
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:220
bool prefer_rows() const
Definition DenseMatrix.hpp:212
bool is_sparse() const
Definition DenseMatrix.hpp:216
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:314
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DenseMatrix.hpp:275
bool uses_oracle(const bool) const
Definition DenseMatrix.hpp:214
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:263
double is_sparse_proportion() const
Definition DenseMatrix.hpp:218
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &) const
Definition DenseMatrix.hpp:247
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:283
Index_ nrow() const
Definition DenseMatrix.hpp:208
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:255
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:318
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:292
Index_ ncol() const
Definition DenseMatrix.hpp:210
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_end, const Options &opt) const
Definition DenseMatrix.hpp:296
Dense row-major matrix.
Definition DenseMatrix.hpp:355
DenseRowMatrix(const Index_ nrow, const Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:362
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.
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