1#ifndef TATAMI_DELAYED_SUBSET_BLOCK
2#define TATAMI_DELAYED_SUBSET_BLOCK
25namespace DelayedSubsetBlock_internal {
27template<
bool oracle_,
typename Value_,
typename Index_>
28auto create_along_dense_extractor(
29 const Matrix<Value_, Index_>& matrix,
30 const Index_ subset_start,
31 const Index_ subset_length,
33 MaybeOracle<oracle_, Index_> oracle,
36 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt);
39template<
bool oracle_,
typename Value_,
typename Index_>
40auto create_along_dense_extractor(
41 const Matrix<Value_, Index_>& matrix,
42 const Index_ subset_start,
45 MaybeOracle<oracle_, Index_> oracle,
46 const Index_ block_start,
47 const Index_ block_length,
50 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt);
53template<
typename Index_>
54void bump_indices(VectorPtr<Index_>& indices_ptr,
const Index_ subset_start) {
56 const auto ptr2 =
new std::vector<Index_>(*indices_ptr);
57 indices_ptr.reset(ptr2);
58 for (
auto& i : *ptr2) {
64template<
bool oracle_,
typename Value_,
typename Index_>
65auto create_along_dense_extractor(
66 const Matrix<Value_, Index_>& matrix,
67 const Index_ subset_start,
70 MaybeOracle<oracle_, Index_> oracle,
71 VectorPtr<Index_> indices_ptr,
74 bump_indices(indices_ptr, subset_start);
75 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
78template<
bool oracle_,
typename Value_,
typename Index_>
79class AdjustSparseIndices final :
public SparseExtractor<oracle_, Value_, Index_> {
81 AdjustSparseIndices(std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > ext, Index_ subset_start) :
82 my_ext(std::move(ext)), my_subset_start(subset_start) {}
84 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
85 auto output = my_ext->fetch(i, value_buffer, index_buffer);
87 for (Index_ i = 0; i < output.number; ++i) {
88 index_buffer[i] = output.index[i] - my_subset_start;
90 output.index = index_buffer;
94 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
95 Index_ my_subset_start;
100template<
bool oracle_,
typename Value_,
typename Index_>
101std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > create_along_sparse_extractor(
102 const Matrix<Value_, Index_>& matrix,
103 const Index_ subset_start,
104 const Index_ subset_length,
106 MaybeOracle<oracle_, Index_> oracle,
109 auto ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt);
110 if (subset_start && opt.sparse_extract_index) {
111 return std::make_unique<AdjustSparseIndices<oracle_, Value_, Index_> >(std::move(ext), subset_start);
117template<
bool oracle_,
typename Value_,
typename Index_>
118std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > create_along_sparse_extractor(
119 const Matrix<Value_, Index_>& matrix,
120 const Index_ subset_start,
123 MaybeOracle<oracle_, Index_> oracle,
124 const Index_ block_start,
125 const Index_ block_length,
128 auto ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt);
129 if (subset_start && opt.sparse_extract_index) {
130 return std::make_unique<AdjustSparseIndices<oracle_, Value_, Index_> >(std::move(ext), subset_start);
136template<
bool oracle_,
typename Value_,
typename Index_>
137std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > create_along_sparse_extractor(
138 const Matrix<Value_, Index_>& matrix,
139 const Index_ subset_start,
142 MaybeOracle<oracle_, Index_> oracle,
143 VectorPtr<Index_> indices_ptr,
146 bump_indices(indices_ptr, subset_start);
147 auto ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
148 if (subset_start && opt.sparse_extract_index) {
149 return std::make_unique<AdjustSparseIndices<oracle_, Value_, Index_> >(std::move(ext), subset_start);
155template<
typename Index_>
156class SubsetOracle final :
public Oracle<Index_> {
159 std::shared_ptr<
const Oracle<Index_> > oracle,
162 my_oracle(std::move(oracle)),
167 return my_oracle->total();
170 Index_ get(
const PredictionIndex i)
const {
171 return my_oracle->get(i) + my_shift;
175 std::shared_ptr<const Oracle<Index_> > my_oracle;
179template<
bool oracle_,
typename Value_,
typename Index_>
180class AcrossDense final :
public DenseExtractor<oracle_, Value_, Index_> {
182 template<
typename ... Args_>
184 const Matrix<Value_, Index_>& matrix,
185 const Index_ subset_start,
187 MaybeOracle<oracle_, Index_> oracle,
190 my_shift(subset_start)
192 if constexpr(oracle_) {
193 oracle.reset(
new SubsetOracle(std::move(oracle), my_shift));
195 my_ext = make_Extractor<false, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
198 const Value_* fetch(
const Index_ i, Value_*
const buffer) {
199 return my_ext->fetch(i + my_shift, buffer);
203 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
207template<
bool oracle_,
typename Value_,
typename Index_>
208class AcrossSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
210 template<
typename ... Args_>
212 const Matrix<Value_, Index_>& matrix,
213 const Index_ subset_start,
215 MaybeOracle<oracle_, Index_> oracle,
218 my_shift(subset_start)
220 if constexpr(oracle_) {
221 oracle.reset(
new SubsetOracle(std::move(oracle), my_shift));
223 my_ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
226 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
227 return my_ext->fetch(i + my_shift, value_buffer, index_buffer);
231 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
250template<
typename Value_,
typename Index_>
262 const Index_ subset_start,
263 const Index_ subset_length,
266 my_matrix(std::move(matrix)),
267 my_subset_start(subset_start),
268 my_subset_length(subset_length),
273 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
274 Index_ my_subset_start, my_subset_length;
280 return my_subset_length;
282 return my_matrix->nrow();
288 return my_matrix->ncol();
290 return my_subset_length;
295 return my_matrix->is_sparse();
299 return my_matrix->is_sparse_proportion();
303 return my_matrix->prefer_rows();
307 return my_matrix->prefer_rows_proportion();
311 return my_matrix->uses_oracle(row);
314 using Matrix<Value_, Index_>::dense_column;
316 using Matrix<Value_, Index_>::dense_row;
318 using Matrix<Value_, Index_>::sparse_column;
320 using Matrix<Value_, Index_>::sparse_row;
326 template<
bool oracle_,
typename ... Args_>
327 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
331 if (row != my_by_row) {
332 return DelayedSubsetBlock_internal::create_along_dense_extractor<oracle_>(
337 std::forward<Args_>(args)...
340 return std::make_unique<DelayedSubsetBlock_internal::AcrossDense<oracle_, Value_, Index_> >(
344 std::forward<Args_>(args)...
350 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
354 return dense_internal<false>(row,
false, opt);
357 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
359 const Index_ block_start,
360 const Index_ block_length,
363 return dense_internal<false>(row,
false, block_start, block_length, opt);
366 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
371 return dense_internal<false>(row,
false, std::move(indices_ptr), opt);
378 template<
bool oracle_,
typename ... Args_>
379 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
383 if (row != my_by_row) {
384 return DelayedSubsetBlock_internal::create_along_sparse_extractor<oracle_>(
389 std::forward<Args_>(args)...
392 return std::make_unique<DelayedSubsetBlock_internal::AcrossSparse<oracle_, Value_, Index_> >(
396 std::forward<Args_>(args)...
402 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
406 return sparse_internal<false>(row,
false, opt);
409 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
411 const Index_ block_start,
412 const Index_ block_length,
415 return sparse_internal<false>(row,
false, block_start, block_length, opt);
418 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
423 return sparse_internal<false>(row,
false, std::move(indices_ptr), opt);
430 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
435 return dense_internal<true>(row, std::move(oracle), opt);
438 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
441 const Index_ block_start,
442 const Index_ block_length,
445 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
448 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
454 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
461 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
466 return sparse_internal<true>(row, std::move(oracle), opt);
469 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
472 const Index_ block_start,
473 const Index_ block_length,
476 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
479 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
485 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
492template<
typename Value_,
typename Index_>
493std::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) {
494 return std::shared_ptr<Matrix<Value_, Index_> >(
new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
497template<
typename Value_,
typename Index_>
498std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length,
bool by_row) {
499 return std::shared_ptr<Matrix<Value_, Index_> >(
new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
502template<
int margin_,
typename Value_,
typename Index_>
503std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<
const Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
504 return make_DelayedSubsetBlock(std::move(matrix), subset_start, subset_length, margin_ == 0);
507template<
int margin_,
typename Value_,
typename Index_>
508std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
509 return make_DelayedSubsetBlock(std::move(matrix), subset_start, subset_length, margin_ == 0);
Virtual class for a matrix of some numeric type.
Delayed subsetting to a contiguous block.
Definition DelayedSubsetBlock.hpp:251
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:438
Index_ nrow() const
Definition DelayedSubsetBlock.hpp:278
bool uses_oracle(const bool row) const
Definition DelayedSubsetBlock.hpp:310
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:350
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:366
double prefer_rows_proportion() const
Definition DelayedSubsetBlock.hpp:306
Index_ ncol() const
Definition DelayedSubsetBlock.hpp:286
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:402
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:357
double is_sparse_proportion() const
Definition DelayedSubsetBlock.hpp:298
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:469
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:479
DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, const Index_ subset_start, const Index_ subset_length, const bool by_row)
Definition DelayedSubsetBlock.hpp:260
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:448
bool is_sparse() const
Definition DelayedSubsetBlock.hpp:294
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:409
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:418
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:461
bool prefer_rows() const
Definition DelayedSubsetBlock.hpp:302
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:430
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
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:284
std::size_t PredictionIndex
Definition Oracle.hpp:18
Options for accessing data from a Matrix instance.
Definition Options.hpp:30