1#ifndef TATAMI_DENSE_MATRIX_H
2#define TATAMI_DENSE_MATRIX_H
26namespace DenseMatrix_internals {
28template<
typename Value_,
typename Index_,
class Storage_>
29class PrimaryMyopicFullDense :
public MyopicDenseExtractor<Value_, Index_> {
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_>
49class PrimaryMyopicBlockDense :
public MyopicDenseExtractor<Value_, Index_> {
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_>
71class PrimaryMyopicIndexDense :
public MyopicDenseExtractor<Value_, Index_> {
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) {
77 size_t offset =
static_cast<size_t>(i) * my_secondary;
78 const auto& indices = *my_indices_ptr;
79 for (
size_t x = 0, end = indices.size(); x < end; ++x) {
80 buffer[x] = my_storage[offset +
static_cast<size_t>(indices[x])];
86 const Storage_& my_storage;
88 VectorPtr<Index_> my_indices_ptr;
91template<
typename Value_,
typename Index_,
class Storage_>
92class SecondaryMyopicFullDense :
public MyopicDenseExtractor<Value_, Index_> {
94 SecondaryMyopicFullDense(
const Storage_& storage, Index_ secondary, Index_ primary) :
95 my_storage(storage), my_secondary(secondary), my_primary(primary) {}
97 const Value_* fetch(Index_ i, Value_* buffer) {
98 for (
size_t x = 0; x < my_primary; ++x) {
99 buffer[x] = my_storage[x * my_secondary +
static_cast<size_t>(i)];
105 const Storage_& my_storage;
110template<
typename Value_,
typename Index_,
class Storage_>
111class SecondaryMyopicBlockDense :
public MyopicDenseExtractor<Value_, Index_> {
113 SecondaryMyopicBlockDense(
const Storage_& storage, Index_ secondary, Index_ block_start, Index_ block_length) :
114 my_storage(storage), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
116 const Value_* fetch(Index_ i, Value_* buffer) {
117 size_t offset = my_block_start * my_secondary +
static_cast<size_t>(i);
118 for (
size_t x = 0; x < my_block_length; ++x) {
119 buffer[x] = my_storage[x * my_secondary + offset];
125 const Storage_& my_storage;
127 size_t my_block_start;
128 size_t my_block_length;
131template<
typename Value_,
typename Index_,
class Storage_>
132class SecondaryMyopicIndexDense :
public MyopicDenseExtractor<Value_, Index_> {
134 SecondaryMyopicIndexDense(
const Storage_& storage, Index_ secondary, VectorPtr<Index_> indices_ptr) :
135 my_storage(storage), my_secondary(secondary), my_indices_ptr(std::move(indices_ptr)) {}
137 const Value_* fetch(Index_ i, Value_* buffer) {
139 const auto& indices = *my_indices_ptr;
140 for (
size_t x = 0, end = indices.size(); x < end; ++x) {
141 buffer[x] = my_storage[
static_cast<size_t>(indices[x]) * my_secondary + offset];
147 const Storage_& my_storage;
149 VectorPtr<Index_> my_indices_ptr;
167template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
177 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) {
178 if (
static_cast<size_t>(my_nrow) *
static_cast<size_t>(my_ncol) != my_values.size()) {
179 throw std::runtime_error(
"length of 'values' should be equal to product of 'nrows' and 'ncols'");
184 Index_ my_nrow, my_ncol;
189 Index_
nrow()
const {
return my_nrow; }
191 Index_
ncol()
const {
return my_ncol; }
208 Index_ primary()
const {
216 Index_ secondary()
const {
228 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options&)
const {
229 if (my_row_major == row) {
230 return std::make_unique<DenseMatrix_internals::PrimaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary());
232 return std::make_unique<DenseMatrix_internals::SecondaryMyopicFullDense<Value_, Index_, Storage_> >(my_values, secondary(), primary());
236 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options&)
const {
237 if (my_row_major == row) {
238 return std::make_unique<DenseMatrix_internals::PrimaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
240 return std::make_unique<DenseMatrix_internals::SecondaryMyopicBlockDense<Value_, Index_, Storage_> >(my_values, secondary(), block_start, block_length);
245 if (my_row_major == row) {
246 return std::make_unique<DenseMatrix_internals::PrimaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
248 return std::make_unique<DenseMatrix_internals::SecondaryMyopicIndexDense<Value_, Index_, Storage_> >(my_values, secondary(), std::move(indices_ptr));
256 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
257 return std::make_unique<FullSparsifiedWrapper<false, Value_, Index_> >(
dense(row, opt), (row ? my_ncol : my_nrow), opt);
260 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
261 return std::make_unique<BlockSparsifiedWrapper<false, Value_, Index_> >(
dense(row, block_start, block_length, opt), block_start, block_length, opt);
265 auto ptr =
dense(row, indices_ptr, opt);
266 return std::make_unique<IndexSparsifiedWrapper<false, Value_, Index_> >(std::move(ptr), std::move(indices_ptr), opt);
273 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
274 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, opt));
277 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 {
278 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, block_start, block_end, opt));
282 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle),
dense(row, std::move(indices_ptr), opt));
289 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
290 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, opt));
293 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 {
294 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, block_start, block_end, opt));
298 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle),
sparse(row, std::move(indices_ptr), opt));
307template<
typename Value_,
typename Index_,
class Storage_ = std::vector<Value_> >
323template<
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:308
DenseColumnMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:315
Dense matrix representation.
Definition DenseMatrix.hpp:168
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &) const
Definition DenseMatrix.hpp:228
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:293
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DenseMatrix.hpp:256
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:273
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition DenseMatrix.hpp:244
DenseMatrix(Index_ nrow, Index_ ncol, Storage_ values, bool row_major)
Definition DenseMatrix.hpp:177
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:281
double prefer_rows_proportion() const
Definition DenseMatrix.hpp:201
bool prefer_rows() const
Definition DenseMatrix.hpp:193
bool is_sparse() const
Definition DenseMatrix.hpp:197
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:297
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DenseMatrix.hpp:264
bool uses_oracle(bool) const
Definition DenseMatrix.hpp:195
double is_sparse_proportion() const
Definition DenseMatrix.hpp:199
Index_ nrow() const
Definition DenseMatrix.hpp:189
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &) const
Definition DenseMatrix.hpp:236
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DenseMatrix.hpp:260
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:277
Index_ ncol() const
Definition DenseMatrix.hpp:191
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DenseMatrix.hpp:289
Dense row-major matrix.
Definition DenseMatrix.hpp:324
DenseRowMatrix(Index_ nrow, Index_ ncol, Storage_ values)
Definition DenseMatrix.hpp:331
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:21
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