tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DelayedSubsetBlock.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DELAYED_SUBSET_BLOCK
2#define TATAMI_DELAYED_SUBSET_BLOCK
3
4#include "../base/Matrix.hpp"
6
7#include <vector>
8#include <algorithm>
9#include <memory>
10#include <cassert>
11
20namespace tatami {
21
25namespace DelayedSubsetBlock_internal {
26
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,
32 const bool row,
33 MaybeOracle<oracle_, Index_> oracle,
34 const Options& opt
35) {
36 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt);
37}
38
39template<bool oracle_, typename Value_, typename Index_>
40auto create_along_dense_extractor(
41 const Matrix<Value_, Index_>& matrix,
42 const Index_ subset_start,
43 const Index_ /* for consistency */,
44 const bool row,
45 MaybeOracle<oracle_, Index_> oracle,
46 const Index_ block_start,
47 const Index_ block_length,
48 const Options& opt
49) {
50 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt);
51}
52
53template<typename Index_>
54void bump_indices(VectorPtr<Index_>& indices_ptr, const Index_ subset_start) {
55 if (subset_start) {
56 const auto ptr2 = new std::vector<Index_>(*indices_ptr);
57 indices_ptr.reset(ptr2);
58 for (auto& i : *ptr2) {
59 i += subset_start;
60 }
61 }
62}
63
64template<bool oracle_, typename Value_, typename Index_>
65auto create_along_dense_extractor(
66 const Matrix<Value_, Index_>& matrix,
67 const Index_ subset_start,
68 const Index_ /* for consistency */,
69 const bool row,
70 MaybeOracle<oracle_, Index_> oracle,
71 VectorPtr<Index_> indices_ptr,
72 const Options& opt
73) {
74 bump_indices(indices_ptr, subset_start);
75 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
76}
77
78template<bool oracle_, typename Value_, typename Index_>
79class AdjustSparseIndices final : public SparseExtractor<oracle_, Value_, Index_> {
80public:
81 AdjustSparseIndices(std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > ext, Index_ subset_start) :
82 my_ext(std::move(ext)), my_subset_start(subset_start) {}
83
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);
86 assert(output.index);
87 for (Index_ i = 0; i < output.number; ++i) {
88 index_buffer[i] = output.index[i] - my_subset_start;
89 }
90 output.index = index_buffer;
91 return output;
92 }
93private:
94 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
95 Index_ my_subset_start;
96};
97
98// For the along-sparse extractors, we wrap it in a AdjustSparseIndices if the indices are requested and the subset doesn't start at zero.
99// Otherwise we can directly return an extractor from the underlying matrix.
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,
105 const bool row,
106 MaybeOracle<oracle_, Index_> oracle,
107 const Options& opt
108) {
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);
112 } else {
113 return ext;
114 }
115}
116
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,
121 const Index_ /* for consistency */,
122 const bool row,
123 MaybeOracle<oracle_, Index_> oracle,
124 const Index_ block_start,
125 const Index_ block_length,
126 const Options& opt
127) {
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);
131 } else {
132 return ext;
133 }
134}
135
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,
140 const Index_ /* for consistency */,
141 const bool row,
142 MaybeOracle<oracle_, Index_> oracle,
143 VectorPtr<Index_> indices_ptr,
144 const Options& opt
145) {
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);
150 } else {
151 return ext;
152 }
153}
154
155template<typename Index_>
156class SubsetOracle final : public Oracle<Index_> {
157public:
158 SubsetOracle(
159 std::shared_ptr<const Oracle<Index_> > oracle,
160 const Index_ shift
161 ) :
162 my_oracle(std::move(oracle)),
163 my_shift(shift)
164 {}
165
166 PredictionIndex total() const {
167 return my_oracle->total();
168 }
169
170 Index_ get(const PredictionIndex i) const {
171 return my_oracle->get(i) + my_shift;
172 }
173
174private:
175 std::shared_ptr<const Oracle<Index_> > my_oracle;
176 Index_ my_shift;
177};
178
179template<bool oracle_, typename Value_, typename Index_>
180class AcrossDense final : public DenseExtractor<oracle_, Value_, Index_> {
181public:
182 template<typename ... Args_>
183 AcrossDense(
184 const Matrix<Value_, Index_>& matrix,
185 const Index_ subset_start,
186 const bool row,
187 MaybeOracle<oracle_, Index_> oracle,
188 Args_&& ... args
189 ) :
190 my_shift(subset_start)
191 {
192 if constexpr(oracle_) {
193 oracle.reset(new SubsetOracle(std::move(oracle), my_shift));
194 }
195 my_ext = make_Extractor<false, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
196 }
197
198 const Value_* fetch(const Index_ i, Value_* const buffer) {
199 return my_ext->fetch(i + my_shift, buffer);
200 }
201
202private:
203 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
204 Index_ my_shift;
205};
206
207template<bool oracle_, typename Value_, typename Index_>
208class AcrossSparse final : public SparseExtractor<oracle_, Value_, Index_> {
209public:
210 template<typename ... Args_>
211 AcrossSparse(
212 const Matrix<Value_, Index_>& matrix,
213 const Index_ subset_start,
214 const bool row,
215 MaybeOracle<oracle_, Index_> oracle,
216 Args_&& ... args
217 ) :
218 my_shift(subset_start)
219 {
220 if constexpr(oracle_) {
221 oracle.reset(new SubsetOracle(std::move(oracle), my_shift));
222 }
223 my_ext = make_Extractor<true, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
224 }
225
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);
228 }
229
230private:
231 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
232 Index_ my_shift;
233};
234
235}
250template<typename Value_, typename Index_>
251class DelayedSubsetBlock final : public Matrix<Value_, Index_> {
252public:
261 std::shared_ptr<const Matrix<Value_, Index_> > matrix,
262 const Index_ subset_start,
263 const Index_ subset_length,
264 const bool by_row
265 ) :
266 my_matrix(std::move(matrix)),
267 my_subset_start(subset_start),
268 my_subset_length(subset_length),
269 my_by_row(by_row)
270 {}
271
272private:
273 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
274 Index_ my_subset_start, my_subset_length;
275 bool my_by_row;
276
277public:
278 Index_ nrow() const {
279 if (my_by_row) {
280 return my_subset_length;
281 } else {
282 return my_matrix->nrow();
283 }
284 }
285
286 Index_ ncol() const {
287 if (my_by_row) {
288 return my_matrix->ncol();
289 } else {
290 return my_subset_length;
291 }
292 }
293
294 bool is_sparse() const {
295 return my_matrix->is_sparse();
296 }
297
298 double is_sparse_proportion() const {
299 return my_matrix->is_sparse_proportion();
300 }
301
302 bool prefer_rows() const {
303 return my_matrix->prefer_rows();
304 }
305
306 double prefer_rows_proportion() const {
307 return my_matrix->prefer_rows_proportion();
308 }
309
310 bool uses_oracle(const bool row) const {
311 return my_matrix->uses_oracle(row);
312 }
313
314 using Matrix<Value_, Index_>::dense_column;
315
316 using Matrix<Value_, Index_>::dense_row;
317
318 using Matrix<Value_, Index_>::sparse_column;
319
320 using Matrix<Value_, Index_>::sparse_row;
321
322 /************************
323 ***** Myopic dense *****
324 ************************/
325private:
326 template<bool oracle_, typename ... Args_>
327 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(
328 const bool row,
329 Args_&&... args
330 ) const {
331 if (row != my_by_row) {
332 return DelayedSubsetBlock_internal::create_along_dense_extractor<oracle_>(
333 *my_matrix,
334 my_subset_start,
335 my_subset_length,
336 row,
337 std::forward<Args_>(args)...
338 );
339 } else {
340 return std::make_unique<DelayedSubsetBlock_internal::AcrossDense<oracle_, Value_, Index_> >(
341 *my_matrix,
342 my_subset_start,
343 row,
344 std::forward<Args_>(args)...
345 );
346 }
347 }
348
349public:
350 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
351 const bool row,
352 const Options& opt
353 ) const {
354 return dense_internal<false>(row, false, opt);
355 }
356
357 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
358 const bool row,
359 const Index_ block_start,
360 const Index_ block_length,
361 const Options& opt
362 ) const {
363 return dense_internal<false>(row, false, block_start, block_length, opt);
364 }
365
366 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
367 const bool row,
368 VectorPtr<Index_> indices_ptr,
369 const Options& opt
370 ) const {
371 return dense_internal<false>(row, false, std::move(indices_ptr), opt);
372 }
373
374 /*************************
375 ***** Myopic sparse *****
376 *************************/
377private:
378 template<bool oracle_, typename ... Args_>
379 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(
380 const bool row,
381 Args_&&... args
382 ) const {
383 if (row != my_by_row) {
384 return DelayedSubsetBlock_internal::create_along_sparse_extractor<oracle_>(
385 *my_matrix,
386 my_subset_start,
387 my_subset_length,
388 row,
389 std::forward<Args_>(args)...
390 );
391 } else {
392 return std::make_unique<DelayedSubsetBlock_internal::AcrossSparse<oracle_, Value_, Index_> >(
393 *my_matrix,
394 my_subset_start,
395 row,
396 std::forward<Args_>(args)...
397 );
398 }
399 }
400
401public:
402 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
403 const bool row,
404 const Options& opt
405 ) const {
406 return sparse_internal<false>(row, false, opt);
407 }
408
409 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
410 const bool row,
411 const Index_ block_start,
412 const Index_ block_length,
413 const Options& opt
414 ) const {
415 return sparse_internal<false>(row, false, block_start, block_length, opt);
416 }
417
418 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
419 const bool row,
420 VectorPtr<Index_> indices_ptr,
421 const Options& opt
422 ) const {
423 return sparse_internal<false>(row, false, std::move(indices_ptr), opt);
424 }
425
426 /**************************
427 ***** Oracular dense *****
428 **************************/
429public:
430 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
431 const bool row,
432 std::shared_ptr<const Oracle<Index_> > oracle,
433 const Options& opt
434 ) const {
435 return dense_internal<true>(row, std::move(oracle), opt);
436 }
437
438 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
439 const bool row,
440 std::shared_ptr<const Oracle<Index_> > oracle,
441 const Index_ block_start,
442 const Index_ block_length,
443 const Options& opt
444 ) const {
445 return dense_internal<true>(row, std::move(oracle), block_start, block_length, opt);
446 }
447
448 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
449 const bool row,
450 std::shared_ptr<const Oracle<Index_> > oracle,
451 VectorPtr<Index_> indices_ptr,
452 const Options& opt
453 ) const {
454 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
455 }
456
457 /***************************
458 ***** Oracular sparse *****
459 ***************************/
460public:
461 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
462 const bool row,
463 std::shared_ptr<const Oracle<Index_> > oracle,
464 const Options& opt
465 ) const {
466 return sparse_internal<true>(row, std::move(oracle), opt);
467 }
468
469 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
470 const bool row,
471 std::shared_ptr<const Oracle<Index_> > oracle,
472 const Index_ block_start,
473 const Index_ block_length,
474 const Options& opt
475 ) const {
476 return sparse_internal<true>(row, std::move(oracle), block_start, block_length, opt);
477 }
478
479 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
480 const bool row,
481 std::shared_ptr<const Oracle<Index_> > oracle,
482 VectorPtr<Index_> indices_ptr,
483 const Options& opt
484 ) const {
485 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
486 }
487};
488
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));
495}
496
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));
500}
501
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);
505}
506
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);
510}
515}
516
517#endif
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
Templated construction of a new extractor.
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