1#ifndef TATAMI_DELAYED_SUBSET_BLOCK
2#define TATAMI_DELAYED_SUBSET_BLOCK
24namespace DelayedSubsetBlock_internal {
26template<
typename Index_>
27void bump_indices(VectorPtr<Index_>& indices_ptr,
const Index_ subset_start) {
29 const auto ptr2 =
new std::vector<Index_>(*indices_ptr);
30 indices_ptr.reset(ptr2);
31 for (
auto& i : *ptr2) {
37template<
bool oracle_,
typename Value_,
typename Index_>
38class AlongDense final :
public DenseExtractor<oracle_, Value_, Index_> {
41 const Matrix<Value_, Index_>& matrix,
42 const Index_ subset_start,
43 const Index_ subset_length,
45 MaybeOracle<oracle_, Index_> oracle,
48 my_ext(
new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt))
52 const Matrix<Value_, Index_>& matrix,
53 const Index_ subset_start,
56 MaybeOracle<oracle_, Index_> oracle,
57 const Index_ block_start,
58 const Index_ block_length,
61 my_ext(
new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt))
65 const Matrix<Value_, Index_>& matrix,
66 const Index_ subset_start,
69 MaybeOracle<oracle_, Index_> oracle,
70 VectorPtr<Index_> indices_ptr,
73 bump_indices(indices_ptr, subset_start);
74 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
77 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
78 return my_ext->fetch(i, buffer);
82 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
85template<
bool oracle_,
typename Value_,
typename Index_>
86class AlongSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
89 const Matrix<Value_, Index_>& matrix,
90 const Index_ subset_start,
91 const Index_ subset_length,
93 MaybeOracle<oracle_, Index_> oracle,
96 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt)),
97 my_shift(subset_start)
101 const Matrix<Value_, Index_>& matrix,
102 const Index_ subset_start,
105 MaybeOracle<oracle_, Index_> oracle,
106 const Index_ block_start,
107 const Index_ block_length,
110 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt)),
111 my_shift(subset_start)
115 const Matrix<Value_, Index_>& matrix,
116 const Index_ subset_start,
119 MaybeOracle<oracle_, Index_> oracle,
120 VectorPtr<Index_> indices_ptr,
123 my_shift(subset_start)
125 bump_indices(indices_ptr, subset_start);
126 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
129 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
130 auto output = my_ext->fetch(i, value_buffer, index_buffer);
131 if (output.index && my_shift) {
132 for (Index_ i = 0; i < output.number; ++i) {
133 index_buffer[i] = output.index[i] - my_shift;
135 output.index = index_buffer;
141 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
145template<
typename Index_>
146class SubsetOracle final :
public Oracle<Index_> {
149 std::shared_ptr<
const Oracle<Index_> > oracle,
152 my_oracle(std::move(oracle)),
157 return my_oracle->total();
160 Index_ get(
const PredictionIndex i)
const {
161 return my_oracle->get(i) + my_shift;
165 std::shared_ptr<const Oracle<Index_> > my_oracle;
169template<
bool oracle_,
typename Value_,
typename Index_>
170class AcrossDense final :
public DenseExtractor<oracle_, Value_, Index_> {
172 template<
typename ... Args_>
174 const Matrix<Value_, Index_>& matrix,
175 const Index_ subset_start,
177 MaybeOracle<oracle_, Index_> oracle,
180 my_shift(subset_start)
182 if constexpr(oracle_) {
183 oracle.reset(
new SubsetOracle(std::move(oracle), my_shift));
185 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
188 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
189 return my_ext->fetch(i + my_shift, buffer);
193 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
197template<
bool oracle_,
typename Value_,
typename Index_>
198class AcrossSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
200 template<
typename ... Args_>
202 const Matrix<Value_, Index_>& matrix,
203 const Index_ subset_start,
205 MaybeOracle<oracle_, Index_> oracle,
208 my_shift(subset_start)
210 if constexpr(oracle_) {
211 oracle.reset(
new SubsetOracle(std::move(oracle), my_shift));
213 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
216 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
217 return my_ext->fetch(i + my_shift, value_buffer, index_buffer);
221 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
240template<
typename Value_,
typename Index_>
252 const Index_ subset_start,
253 const Index_ subset_length,
256 my_matrix(std::move(matrix)),
257 my_subset_start(subset_start),
258 my_subset_length(subset_length),
263 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
264 Index_ my_subset_start, my_subset_length;
270 return my_subset_length;
272 return my_matrix->nrow();
278 return my_matrix->ncol();
280 return my_subset_length;
285 return my_matrix->is_sparse();
289 return my_matrix->is_sparse_proportion();
293 return my_matrix->prefer_rows();
297 return my_matrix->prefer_rows_proportion();
301 return my_matrix->uses_oracle(row);
316 template<
bool oracle_,
typename ... Args_>
317 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
321 if (row != my_by_row) {
322 return std::make_unique<DelayedSubsetBlock_internal::AlongDense<oracle_, Value_, Index_> >(
327 std::forward<Args_>(args)...
330 return std::make_unique<DelayedSubsetBlock_internal::AcrossDense<oracle_, Value_, Index_> >(
334 std::forward<Args_>(args)...
340 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
344 return dense_internal<false>(row,
false, opt);
347 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
349 const Index_ block_start,
350 const Index_ block_length,
353 return dense_internal<false>(row,
false, block_start, block_length, opt);
356 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
361 return dense_internal<false>(row,
false, std::move(indices_ptr), opt);
368 template<
bool oracle_,
typename ... Args_>
369 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
373 if (row != my_by_row) {
374 return std::make_unique<DelayedSubsetBlock_internal::AlongSparse<oracle_, Value_, Index_> >(
379 std::forward<Args_>(args)...
382 return std::make_unique<DelayedSubsetBlock_internal::AcrossSparse<oracle_, Value_, Index_> >(
386 std::forward<Args_>(args)...
392 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
396 return sparse_internal<false>(row,
false, opt);
399 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
401 const Index_ block_start,
402 const Index_ block_length,
405 return sparse_internal<false>(row,
false, block_start, block_length, opt);
408 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
413 return sparse_internal<false>(row,
false, std::move(indices_ptr), opt);
420 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
425 return dense_internal<true>(row, std::move(oracle), opt);
428 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
431 const Index_ block_start,
432 const Index_ block_length,
435 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
438 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
444 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
451 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
456 return sparse_internal<true>(row, std::move(oracle), opt);
459 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
462 const Index_ block_start,
463 const Index_ block_length,
466 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
469 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
475 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
493template<
typename Value_,
typename Index_>
496 const Index_ subset_start,
497 const Index_ subset_length,
506template<
typename Value_,
typename Index_>
507std::shared_ptr<Matrix<Value_, Index_> >
make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length,
bool by_row) {
508 return std::shared_ptr<Matrix<Value_, Index_> >(
new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
517template<
int margin_,
typename Value_,
typename Index_>
518std::shared_ptr<Matrix<Value_, Index_> >
make_DelayedSubsetBlock(std::shared_ptr<
const Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
522template<
int margin_,
typename Value_,
typename Index_>
523std::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:241
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 DelayedSubsetBlock.hpp:428
Index_ nrow() const
Definition DelayedSubsetBlock.hpp:268
bool uses_oracle(const bool row) const
Definition DelayedSubsetBlock.hpp:300
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:340
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:356
double prefer_rows_proportion() const
Definition DelayedSubsetBlock.hpp:296
Index_ ncol() const
Definition DelayedSubsetBlock.hpp:276
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:392
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:347
double is_sparse_proportion() const
Definition DelayedSubsetBlock.hpp:288
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 DelayedSubsetBlock.hpp:459
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 DelayedSubsetBlock.hpp:469
DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, const Index_ subset_start, const Index_ subset_length, const bool by_row)
Definition DelayedSubsetBlock.hpp:250
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 DelayedSubsetBlock.hpp:438
bool is_sparse() const
Definition DelayedSubsetBlock.hpp:284
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:399
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:408
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:451
bool prefer_rows() const
Definition DelayedSubsetBlock.hpp:292
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:420
Virtual class for a matrix.
Definition Matrix.hpp:59
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_row() const
Definition Matrix.hpp:295
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense_column() const
Definition Matrix.hpp:336
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_column() const
Definition Matrix.hpp:545
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse_row() const
Definition Matrix.hpp:504
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Flexible representations for matrix data.
Definition Extractor.hpp:15
auto new_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
Definition new_extractor.hpp:42
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
std::shared_ptr< Matrix< Value_, Index_ > > make_DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, const Index_ subset_start, const Index_ subset_length, bool by_row)
Definition DelayedSubsetBlock.hpp:494
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
std::size_t PredictionIndex
Definition Oracle.hpp:18
Options for accessing data from a Matrix instance.
Definition Options.hpp:30