1#ifndef TATAMI_DELAYED_SUBSET_BLOCK
2#define TATAMI_DELAYED_SUBSET_BLOCK
25namespace DelayedSubsetBlock_internal {
27template<
typename Index_>
28void bump_indices(VectorPtr<Index_>& indices_ptr, Index_ subset_start) {
30 auto ptr2 =
new std::vector<Index_>(*indices_ptr);
31 indices_ptr.reset(ptr2);
32 for (
auto& i : *ptr2) {
38template<
bool oracle_,
typename Value_,
typename Index_>
39class AlongDense final :
public DenseExtractor<oracle_, Value_, Index_> {
41 AlongDense(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ subset_length,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) :
42 my_ext(
new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt)) {}
44 AlongDense(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ ,
bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length,
const Options& opt) :
45 my_ext(
new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt)) {}
47 AlongDense(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ ,
bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr,
const Options& opt) {
48 bump_indices(indices_ptr, subset_start);
49 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
52 const Value_* fetch(Index_ i, Value_* buffer) {
53 return my_ext->fetch(i, buffer);
57 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
60template<
bool oracle_,
typename Value_,
typename Index_>
61class AlongSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
63 AlongSparse(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ subset_length,
bool row, MaybeOracle<oracle_, Index_> oracle,
const Options& opt) :
64 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt)), my_shift(subset_start) {}
66 AlongSparse(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ ,
bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length,
const Options& opt) :
67 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt)), my_shift(subset_start) {}
69 AlongSparse(
const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ ,
bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr,
const Options& opt) :
70 my_shift(subset_start)
72 bump_indices(indices_ptr, subset_start);
73 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
76 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
77 auto output = my_ext->fetch(i, value_buffer, index_buffer);
78 if (output.index && my_shift) {
79 for (Index_ i = 0; i < output.number; ++i) {
80 index_buffer[i] = output.index[i] - my_shift;
82 output.index = index_buffer;
88 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
92template<
typename Index_>
93class SubsetOracle final :
public Oracle<Index_> {
95 SubsetOracle(std::shared_ptr<
const Oracle<Index_> > oracle, Index_ shift) : my_oracle(std::move(oracle)), my_shift(shift) {}
97 std::size_t total()
const {
98 return my_oracle->total();
101 Index_ get(std::size_t i)
const {
102 return my_oracle->get(i) + my_shift;
106 std::shared_ptr<const Oracle<Index_> > my_oracle;
110template<
bool oracle_,
typename Value_,
typename Index_>
111class AcrossDense final :
public DenseExtractor<oracle_, Value_, Index_> {
113 template<
typename ... Args_>
114 AcrossDense(
const Matrix<Value_, Index_>* matrix, Index_ subset_start,
bool row, MaybeOracle<oracle_, Index_> oracle, Args_&& ... args) : my_shift(subset_start) {
115 if constexpr(oracle_) {
116 auto ptr =
new SubsetOracle(std::move(oracle), my_shift);
119 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
122 const Value_* fetch(Index_ i, Value_* buffer) {
123 return my_ext->fetch(i + my_shift, buffer);
127 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
131template<
bool oracle_,
typename Value_,
typename Index_>
132class AcrossSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
134 template<
typename ... Args_>
135 AcrossSparse(
const Matrix<Value_, Index_>* matrix, Index_ subset_start,
bool row, MaybeOracle<oracle_, Index_> oracle, Args_&& ... args) : my_shift(subset_start) {
136 if constexpr(oracle_) {
137 auto ptr =
new SubsetOracle(std::move(oracle), my_shift);
140 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
143 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
144 return my_ext->fetch(i + my_shift, value_buffer, index_buffer);
148 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
167template<
typename Value_,
typename Index_>
178 my_matrix(std::move(matrix)), my_subset_start(subset_start), my_subset_length(subset_length), my_by_row(by_row) {}
181 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
182 Index_ my_subset_start, my_subset_length;
188 return my_subset_length;
190 return my_matrix->nrow();
196 return my_matrix->ncol();
198 return my_subset_length;
203 return my_matrix->is_sparse();
207 return my_matrix->is_sparse_proportion();
211 return my_matrix->prefer_rows();
215 return my_matrix->prefer_rows_proportion();
219 return my_matrix->uses_oracle(row);
234 template<
bool oracle_,
typename ... Args_>
235 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
bool row, Args_&&... args)
const {
236 if (row != my_by_row) {
237 return std::make_unique<DelayedSubsetBlock_internal::AlongDense<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, my_subset_length, row, std::forward<Args_>(args)...);
239 return std::make_unique<DelayedSubsetBlock_internal::AcrossDense<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, row, std::forward<Args_>(args)...);
244 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row,
const Options& opt)
const {
245 return dense_internal<false>(row,
false, opt);
248 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
249 return dense_internal<false>(row,
false, block_start, block_length, opt);
253 return dense_internal<false>(row,
false, std::move(indices_ptr), opt);
260 template<
bool oracle_,
typename ... Args_>
261 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
bool row, Args_&&... args)
const {
262 if (row != my_by_row) {
263 return std::make_unique<DelayedSubsetBlock_internal::AlongSparse<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, my_subset_length, row, std::forward<Args_>(args)...);
265 return std::make_unique<DelayedSubsetBlock_internal::AcrossSparse<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, row, std::forward<Args_>(args)...);
270 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row,
const Options& opt)
const {
271 return sparse_internal<false>(row,
false, opt);
274 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
bool row, Index_ block_start, Index_ block_length,
const Options& opt)
const {
275 return sparse_internal<false>(row,
false, block_start, block_length, opt);
279 return sparse_internal<false>(row,
false, std::move(indices_ptr), opt);
286 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
287 return dense_internal<true>(row, std::move(oracle), opt);
290 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length,
const Options& opt)
const {
291 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
295 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
302 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle,
const Options& opt)
const {
303 return sparse_internal<true>(row, std::move(oracle), opt);
306 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
bool row, std::shared_ptr<
const Oracle<Index_> > oracle, Index_ block_start, Index_ block_length,
const Options& opt)
const {
307 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
311 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
329template<
typename Value_,
typename Index_>
337template<
typename Value_,
typename Index_>
338std::shared_ptr<Matrix<Value_, Index_> >
make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length,
bool by_row) {
339 return std::shared_ptr<Matrix<Value_, Index_> >(
new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
348template<
int margin_,
typename Value_,
typename Index_>
349std::shared_ptr<Matrix<Value_, Index_> >
make_DelayedSubsetBlock(std::shared_ptr<
const Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
353template<
int margin_,
typename Value_,
typename Index_>
354std::shared_ptr<Matrix<Value_, Index_> >
make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
Virtual class for a matrix of some numeric type.
Delayed subsetting to a contiguous block.
Definition DelayedSubsetBlock.hpp:168
Index_ nrow() const
Definition DelayedSubsetBlock.hpp:186
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 DelayedSubsetBlock.hpp:294
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:248
double prefer_rows_proportion() const
Definition DelayedSubsetBlock.hpp:214
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:302
Index_ ncol() const
Definition DelayedSubsetBlock.hpp:194
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:244
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:278
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 DelayedSubsetBlock.hpp:310
double is_sparse_proportion() const
Definition DelayedSubsetBlock.hpp:206
bool is_sparse() const
Definition DelayedSubsetBlock.hpp:202
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:306
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:252
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:286
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:270
DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, Index_ subset_start, Index_ subset_length, bool by_row)
Definition DelayedSubsetBlock.hpp:177
bool uses_oracle(bool row) const
Definition DelayedSubsetBlock.hpp:218
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:290
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:274
bool prefer_rows() const
Definition DelayedSubsetBlock.hpp:210
Virtual class for a matrix.
Definition Matrix.hpp:59
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_row() const
Definition Matrix.hpp:287
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_column() const
Definition Matrix.hpp:328
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_column() const
Definition Matrix.hpp:537
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_row() const
Definition Matrix.hpp:496
Predict future access requests on the target dimension.
Definition Oracle.hpp:23
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
auto new_extractor(const Matrix< Value_, Index_ > &matrix, bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
Definition new_extractor.hpp:42
std::shared_ptr< Matrix< Value_, Index_ > > make_DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, Index_ subset_start, Index_ subset_length, bool by_row)
Definition DelayedSubsetBlock.hpp:330
Options for accessing data from a Matrix instance.
Definition Options.hpp:30