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"
5#include "../utils/new_extractor.hpp"
6
7#include <vector>
8#include <algorithm>
9#include <memory>
10
19namespace tatami {
20
25
26template<typename Index_>
28 if (subset_start) {
29 auto ptr2 = new std::vector<Index_>(*indices_ptr);
30 indices_ptr.reset(ptr2);
31 for (auto& i : *ptr2) {
32 i += subset_start;
33 }
34 }
35}
36
37template<bool oracle_, typename Value_, typename Index_>
38class AlongDense : public DenseExtractor<oracle_, Value_, Index_> {
39public:
40 AlongDense(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ subset_length, bool row, MaybeOracle<oracle_, Index_> oracle, const Options& opt) :
41 my_ext(new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt)) {}
42
43 AlongDense(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ /* for consistency */, bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length, const Options& opt) :
44 my_ext(new_extractor<false, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt)) {}
45
46 AlongDense(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ /* for consistency */, bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr, const Options& opt) {
47 bump_indices(indices_ptr, subset_start);
48 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
49 }
50
51 const Value_* fetch(Index_ i, Value_* buffer) {
52 return my_ext->fetch(i, buffer);
53 }
54
55private:
56 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
57};
58
59template<bool oracle_, typename Value_, typename Index_>
60class AlongSparse : public SparseExtractor<oracle_, Value_, Index_> {
61public:
62 AlongSparse(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ subset_length, bool row, MaybeOracle<oracle_, Index_> oracle, const Options& opt) :
63 my_ext(new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start, subset_length, opt)), my_shift(subset_start) {}
64
65 AlongSparse(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ /* for consistency */, bool row, MaybeOracle<oracle_, Index_> oracle, Index_ block_start, Index_ block_length, const Options& opt) :
66 my_ext(new_extractor<true, oracle_>(matrix, row, std::move(oracle), subset_start + block_start, block_length, opt)), my_shift(subset_start) {}
67
68 AlongSparse(const Matrix<Value_, Index_>* matrix, Index_ subset_start, Index_ /* for consistency */, bool row, MaybeOracle<oracle_, Index_> oracle, VectorPtr<Index_> indices_ptr, const Options& opt) :
69 my_shift(subset_start)
70 {
71 bump_indices(indices_ptr, subset_start);
72 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::move(indices_ptr), opt);
73 }
74
75 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
76 auto output = my_ext->fetch(i, value_buffer, index_buffer);
77 if (output.index && my_shift) {
78 for (Index_ i = 0; i < output.number; ++i) {
79 index_buffer[i] = output.index[i] - my_shift;
80 }
81 output.index = index_buffer;
82 }
83 return output;
84 }
85
86private:
87 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
88 Index_ my_shift;
89};
90
91template<typename Index_>
92class SubsetOracle : public Oracle<Index_> {
93public:
94 SubsetOracle(std::shared_ptr<const Oracle<Index_> > oracle, Index_ shift) : my_oracle(std::move(oracle)), my_shift(shift) {}
95
96 size_t total() const {
97 return my_oracle->total();
98 }
99
100 Index_ get(size_t i) const {
101 return my_oracle->get(i) + my_shift;
102 }
103
104private:
105 std::shared_ptr<const Oracle<Index_> > my_oracle;
106 Index_ my_shift;
107};
108
109template<bool oracle_, typename Value_, typename Index_>
110class AcrossDense : public DenseExtractor<oracle_, Value_, Index_> {
111public:
112 template<typename ... Args_>
113 AcrossDense(const Matrix<Value_, Index_>* matrix, Index_ subset_start, bool row, MaybeOracle<oracle_, Index_> oracle, Args_&& ... args) : my_shift(subset_start) {
114 if constexpr(oracle_) {
115 auto ptr = new SubsetOracle(std::move(oracle), my_shift);
116 oracle.reset(ptr);
117 }
118 my_ext = new_extractor<false, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
119 }
120
121 const Value_* fetch(Index_ i, Value_* buffer) {
122 return my_ext->fetch(i + my_shift, buffer);
123 }
124
125private:
126 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_ext;
127 Index_ my_shift;
128};
129
130template<bool oracle_, typename Value_, typename Index_>
131class AcrossSparse : public SparseExtractor<oracle_, Value_, Index_> {
132public:
133 template<typename ... Args_>
134 AcrossSparse(const Matrix<Value_, Index_>* matrix, Index_ subset_start, bool row, MaybeOracle<oracle_, Index_> oracle, Args_&& ... args) : my_shift(subset_start) {
135 if constexpr(oracle_) {
136 auto ptr = new SubsetOracle(std::move(oracle), my_shift);
137 oracle.reset(ptr);
138 }
139 my_ext = new_extractor<true, oracle_>(matrix, row, std::move(oracle), std::forward<Args_>(args)...);
140 }
141
142 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
143 return my_ext->fetch(i + my_shift, value_buffer, index_buffer);
144 }
145
146private:
147 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
148 Index_ my_shift;
149};
150
151}
166template<typename Value_, typename Index_>
167class DelayedSubsetBlock : public Matrix<Value_, Index_> {
168public:
177 my_matrix(std::move(matrix)), my_subset_start(subset_start), my_subset_length(subset_length), my_by_row(by_row) {}
178
179private:
180 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
181 Index_ my_subset_start, my_subset_length;
182 bool my_by_row;
183
184public:
185 Index_ nrow() const {
186 if (my_by_row) {
187 return my_subset_length;
188 } else {
189 return my_matrix->nrow();
190 }
191 }
192
193 Index_ ncol() const {
194 if (my_by_row) {
195 return my_matrix->ncol();
196 } else {
197 return my_subset_length;
198 }
199 }
200
201 bool is_sparse() const {
202 return my_matrix->is_sparse();
203 }
204
205 double is_sparse_proportion() const {
206 return my_matrix->is_sparse_proportion();
207 }
208
209 bool prefer_rows() const {
210 return my_matrix->prefer_rows();
211 }
212
213 double prefer_rows_proportion() const {
214 return my_matrix->prefer_rows_proportion();
215 }
216
217 bool uses_oracle(bool row) const {
218 return my_matrix->uses_oracle(row);
219 }
220
222
224
226
228
229 /************************
230 ***** Myopic dense *****
231 ************************/
232private:
233 template<bool oracle_, typename ... Args_>
234 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > dense_internal(bool row, Args_&&... args) const {
235 if (row != my_by_row) {
236 return std::make_unique<DelayedSubsetBlock_internal::AlongDense<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, my_subset_length, row, std::forward<Args_>(args)...);
237 } else {
238 return std::make_unique<DelayedSubsetBlock_internal::AcrossDense<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, row, std::forward<Args_>(args)...);
239 }
240 }
241
242public:
243 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options& opt) const {
244 return dense_internal<false>(row, false, opt);
245 }
246
247 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
249 }
250
251 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
252 return dense_internal<false>(row, false, std::move(indices_ptr), opt);
253 }
254
255 /*************************
256 ***** Myopic sparse *****
257 *************************/
258private:
259 template<bool oracle_, typename ... Args_>
260 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > sparse_internal(bool row, Args_&&... args) const {
261 if (row != my_by_row) {
262 return std::make_unique<DelayedSubsetBlock_internal::AlongSparse<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, my_subset_length, row, std::forward<Args_>(args)...);
263 } else {
264 return std::make_unique<DelayedSubsetBlock_internal::AcrossSparse<oracle_, Value_, Index_> >(my_matrix.get(), my_subset_start, row, std::forward<Args_>(args)...);
265 }
266 }
267
268public:
269 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
270 return sparse_internal<false>(row, false, opt);
271 }
272
273 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
275 }
276
277 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
278 return sparse_internal<false>(row, false, std::move(indices_ptr), opt);
279 }
280
281 /**************************
282 ***** Oracular dense *****
283 **************************/
284public:
285 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
286 return dense_internal<true>(row, std::move(oracle), opt);
287 }
288
289 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 }
292
293 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
294 return dense_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
295 }
296
297 /***************************
298 ***** Oracular sparse *****
299 ***************************/
300public:
301 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
302 return sparse_internal<true>(row, std::move(oracle), opt);
303 }
304
305 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 }
308
309 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
310 return sparse_internal<true>(row, std::move(oracle), std::move(indices_ptr), opt);
311 }
312};
313
328template<typename Value_, typename Index_>
329std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<const Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length, bool by_row) {
330 return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
331}
332
336template<typename Value_, typename Index_>
337std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length, bool by_row) {
338 return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), subset_start, subset_length, by_row));
339}
347template<int margin_, typename Value_, typename Index_>
348std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<const Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
350}
351
352template<int margin_, typename Value_, typename Index_>
353std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubsetBlock(std::shared_ptr<Matrix<Value_, Index_> > matrix, Index_ subset_start, Index_ subset_length) {
355}
360}
361
362#endif
Delayed subsetting to a contiguous block.
Definition DelayedSubsetBlock.hpp:167
Index_ nrow() const
Definition DelayedSubsetBlock.hpp:185
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:293
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:247
double prefer_rows_proportion() const
Definition DelayedSubsetBlock.hpp:213
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:301
Index_ ncol() const
Definition DelayedSubsetBlock.hpp:193
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:243
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:277
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:309
double is_sparse_proportion() const
Definition DelayedSubsetBlock.hpp:205
bool is_sparse() const
Definition DelayedSubsetBlock.hpp:201
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:305
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetBlock.hpp:251
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetBlock.hpp:285
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubsetBlock.hpp:269
DelayedSubsetBlock(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, Index_ subset_start, Index_ subset_length, bool by_row)
Definition DelayedSubsetBlock.hpp:176
bool uses_oracle(bool row) const
Definition DelayedSubsetBlock.hpp:217
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:289
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetBlock.hpp:273
bool prefer_rows() const
Definition DelayedSubsetBlock.hpp:209
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:21
Flexible representations for matrix data.
Definition Extractor.hpp:15
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
typename std::conditional< oracle_, OracularSparseExtractor< Value_, Index_ >, MyopicSparseExtractor< Value_, Index_ > >::type SparseExtractor
Definition Extractor.hpp:284
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, Index_ subset_start, Index_ subset_length, bool by_row)
Definition DelayedSubsetBlock.hpp:329
auto new_extractor(const Matrix< Value_, Index_ > *ptr, bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
Definition new_extractor.hpp:42
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35
Options for accessing data from a Matrix instance.
Definition Options.hpp:30