1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
28namespace DenseMatrix_internals {
30template<
typename Value_,
typename Index_,
class Storage_>
31class PrimaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
33 typedef decltype(std::declval<Storage_>().size()) Size;
35 PrimaryMyopicFullDense(
const Storage_& storage, Size secondary) : my_storage(storage), my_secondary(secondary) {}
37 const Value_* fetch(Index_ i, Value_* buffer) {
38 Size offset =
static_cast<Size
>(i) * my_secondary;
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 typedef decltype(std::declval<Storage_>().size()) Size;
57 PrimaryMyopicBlockDense(
const Storage_& storage, Size secondary, Index_ block_start, Index_ block_length) :
58 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
60 const Value_* fetch(Index_ i, Value_* buffer) {
61 Size offset =
static_cast<Size
>(i) * my_secondary + my_block_start;
62 if constexpr(has_data<Value_, Storage_>::value) {
63 return my_storage.data() + offset;
65 std::copy_n(my_storage.begin() + offset, my_block_length, buffer);
71 const Storage_& my_storage;
73 Size my_block_start, my_block_length;
76template<
typename Value_,
typename Index_,
class Storage_>
77class PrimaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
79 typedef decltype(std::declval<Storage_>().size()) Size;
81 PrimaryMyopicIndexDense(
const Storage_& storage, Size secondary, VectorPtr<Index_> indices_ptr) :
82 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
84 const Value_* fetch(Index_ i, Value_* buffer) {
85 Size offset =
static_cast<Size
>(i) * my_secondary;
86 const auto& indices = *my_indices_ptr;
87 for (
decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
88 buffer[x] = my_storage[offset +
static_cast<Size
>(indices[x])];
94 const Storage_& my_storage;
96 VectorPtr<Index_> my_indices_ptr;
99template<
typename Value_,
typename Index_,
class Storage_>
100class SecondaryMyopicFullDense final :
public MyopicDenseExtractor<Value_, Index_> {
102 typedef decltype(std::declval<Storage_>().size()) Size;
104 SecondaryMyopicFullDense(
const Storage_& storage, Index_ secondary, Index_ primary) :
105 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
107 const Value_* fetch(Index_ i, Value_* buffer) {
108 for (
decltype(my_primary) x = 0; x < my_primary; ++x) {
109 buffer[x] = my_storage[x * my_secondary +
static_cast<Size
>(i)];
115 const Storage_& my_storage;
120template<
typename Value_,
typename Index_,
class Storage_>
121class SecondaryMyopicBlockDense final :
public MyopicDenseExtractor<Value_, Index_> {
123 typedef decltype(std::declval<Storage_>().size()) Size;
125 SecondaryMyopicBlockDense(
const Storage_& storage, Index_ secondary, Index_ block_start, Index_ block_length) :
126 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
128 const Value_* fetch(Index_ i, Value_* buffer) {
129 Size offset = my_block_start * my_secondary +
static_cast<Size
>(i);
130 for (
decltype(my_block_length) x = 0; x < my_block_length; ++x) {
131 buffer[x] = my_storage[x * my_secondary + offset];
137 const Storage_& my_storage;
140 Size my_block_length;
143template<
typename Value_,
typename Index_,
class Storage_>
144class SecondaryMyopicIndexDense final :
public MyopicDenseExtractor<Value_, Index_> {
146 typedef decltype(std::declval<Storage_>().size()) Size;
148 SecondaryMyopicIndexDense(
const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
149 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
151 const Value_* fetch(Index_ i, Value_* buffer) {
153 const auto& indices = *my_indices_ptr;
154 for (
decltype(indices.size()) x = 0, end = indices.size(); x < end; ++x) {
155 buffer[x] = my_storage[
static_cast<Size
>(indices[x]) * my_secondary + offset];
161 const Storage_& my_storage;
163 VectorPtr<Index_> my_indices_ptr;
181template<
typename Value_,
typename Index_,
class Storage_>
191 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) {
192 auto nvalues = my_values.size();
195 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
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(
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(
bool row, Index_ block_start, 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(
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(
bool row, Index_ block_start, 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(
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(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end,
const Options& opt)
const {
297 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_end, opt));
301 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
308 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
309 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
312 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 {
313 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_end, opt));
317 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
326template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
342template<
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:327
DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:334
Dense matrix representation.
Definition DenseMatrix.hpp:182
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition DenseMatrix.hpp:247
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:312
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DenseMatrix.hpp:275
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:292
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:263
DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major)
Definition DenseMatrix.hpp:191
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:300
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
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:316
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:283
bool uses_oracle(bool) const
Definition DenseMatrix.hpp:214
double is_sparse_proportion() const
Definition DenseMatrix.hpp:218
Index_ nrow() const
Definition DenseMatrix.hpp:208
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:255
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:279
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:296
Index_ ncol() const
Definition DenseMatrix.hpp:210
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:308
Dense row-major matrix.
Definition DenseMatrix.hpp:343
DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:350
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:23
Compile-time checks for the data() method.
Sign-aware integer comparisons.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
bool safe_non_negative_equal(Left_ l, Right_ r)
Definition integer_comparisons.hpp:23
Options for accessing data from a Matrix instance.
Definition Options.hpp:30