tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DelayedCast.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DELAYED_CAST_HPP
2#define TATAMI_DELAYED_CAST_HPP
3
4#include "../base/Matrix.hpp"
7
8#include <memory>
9#include <type_traits>
10#include <algorithm>
11#include <cstddef>
12
19namespace tatami {
20
24namespace DelayedCast_internal {
25
26template<typename IndexIn_, typename IndexOut_>
27class CastOracle final : public Oracle<IndexIn_> {
28public:
29 CastOracle(std::shared_ptr<const Oracle<IndexOut_> > oracle) : my_oracle(std::move(oracle)) {}
30
31 IndexIn_ get(const PredictionIndex i) const {
32 return my_oracle->get(i);
33 }
34
35 PredictionIndex total() const {
36 return my_oracle->total();
37 }
38
39private:
40 std::shared_ptr<const Oracle<IndexOut_> > my_oracle;
41};
42
43template<bool oracle_, typename IndexIn_, typename IndexOut_>
44MaybeOracle<oracle_, IndexIn_> convert(MaybeOracle<oracle_, IndexOut_> oracle) {
45 if constexpr(!oracle_) {
46 return false;
47 } else if constexpr(std::is_same<IndexIn_, IndexOut_>::value) {
48 return oracle;
49 } else {
50 return std::make_shared<CastOracle<IndexIn_, IndexOut_> >(std::move(oracle));
51 }
52}
53
54template<typename IndexIn_, typename IndexOut_>
55VectorPtr<IndexIn_> convert(VectorPtr<IndexOut_> indices_ptr) {
56 if constexpr(std::is_same<IndexIn_, IndexOut_>::value) {
57 return indices_ptr;
58 } else {
59 return std::make_shared<std::vector<IndexIn_> >(indices_ptr->begin(), indices_ptr->end());
60 }
61}
62
63template<bool oracle_, typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
64class Dense final : public DenseExtractor<oracle_, ValueOut_, IndexOut_> {
65public:
66 Dense(
67 const Matrix<ValueIn_, IndexIn_>& matrix,
68 const bool row, MaybeOracle<oracle_, IndexOut_> oracle,
69 const Options& opt
70 ) {
71 allocate(row ? matrix.ncol() : matrix.nrow());
72 my_ext = make_Extractor<false, oracle_>(
73 matrix,
74 row,
75 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
76 opt
77 );
78 }
79
80 Dense(
81 const Matrix<ValueIn_, IndexIn_>& matrix,
82 const bool row,
83 MaybeOracle<oracle_, IndexOut_> oracle,
84 const IndexOut_ block_start,
85 const IndexOut_ block_length,
86 const Options& opt
87 ) {
88 allocate(block_length);
89 my_ext = make_Extractor<false, oracle_>(
90 matrix,
91 row,
92 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
93 static_cast<IndexIn_>(block_start),
94 static_cast<IndexIn_>(block_length),
95 opt
96 );
97 }
98
99 Dense(
100 const Matrix<ValueIn_, IndexIn_>& matrix,
101 const bool row,
102 MaybeOracle<oracle_, IndexOut_> oracle,
103 VectorPtr<IndexOut_> indices_ptr,
104 const Options& opt
105 ) {
106 allocate(indices_ptr->size());
107 my_ext = make_Extractor<false, oracle_>(
108 matrix,
109 row,
110 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
111 convert<IndexIn_>(std::move(indices_ptr)),
112 opt
113 );
114 }
115
116private:
117 void allocate(const IndexIn_ n) {
118 if constexpr(!no_op) {
119 resize_container_to_Index_size(my_buffer, n);
120 }
121 }
122
123public:
124 const ValueOut_* fetch(const IndexOut_ i, ValueOut_* const buffer) {
125 if constexpr(no_op) {
126 return my_ext->fetch(i, buffer);
127 } else {
128 const auto ptr = my_ext->fetch(i, my_buffer.data());
129 std::copy_n(ptr, my_buffer.size(), buffer);
130 return buffer;
131 }
132 }
133
134private:
135 std::unique_ptr<DenseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
136 static constexpr bool no_op = std::is_same<ValueOut_, ValueIn_>::value;
137 typename std::conditional<no_op, bool, std::vector<ValueIn_> >::type my_buffer;
138};
139
140template<bool oracle_, typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
141class Sparse final : public SparseExtractor<oracle_, ValueOut_, IndexOut_> {
142public:
143 Sparse(
144 const Matrix<ValueIn_, IndexIn_>& matrix,
145 const bool row,
146 MaybeOracle<oracle_, IndexOut_> oracle,
147 const Options& opt
148 ) {
149 allocate(row ? matrix.ncol() : matrix.nrow(), opt);
150 my_ext = make_Extractor<true, oracle_>(
151 matrix,
152 row,
153 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
154 opt
155 );
156 }
157
158 Sparse(
159 const Matrix<ValueIn_, IndexIn_>& matrix,
160 const bool row,
161 MaybeOracle<oracle_, IndexOut_> oracle,
162 const IndexOut_ block_start,
163 const IndexOut_ block_length,
164 const Options& opt
165 ) {
166 allocate(block_length, opt);
167 my_ext = make_Extractor<true, oracle_>(
168 matrix,
169 row,
170 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
171 static_cast<IndexIn_>(block_start),
172 static_cast<IndexIn_>(block_length),
173 opt
174 );
175 }
176
177 Sparse(
178 const Matrix<ValueIn_, IndexIn_>& matrix,
179 const bool row,
180 MaybeOracle<oracle_, IndexOut_> oracle,
181 VectorPtr<IndexOut_> indices_ptr,
182 const Options& opt
183 ) {
184 allocate(indices_ptr->size(), opt);
185 my_ext = make_Extractor<true, oracle_>(
186 matrix,
187 row,
188 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
189 convert<IndexIn_>(std::move(indices_ptr)),
190 opt
191 );
192 }
193
194private:
195 void allocate(const IndexIn_ n, const Options& opt) {
196 if constexpr(!no_op_value) {
197 if (opt.sparse_extract_value) {
198 resize_container_to_Index_size(my_vbuffer, n);
199 }
200 }
201 if constexpr(!no_op_index) {
202 if (opt.sparse_extract_index) {
203 resize_container_to_Index_size(my_ibuffer, n);
204 }
205 }
206 }
207
208public:
209 SparseRange<ValueOut_, IndexOut_> fetch(const IndexOut_ i, ValueOut_* const value_buffer, IndexOut_* const index_buffer) {
210 IndexIn_* const iptr = [&]{
211 if constexpr(no_op_index) {
212 return index_buffer;
213 } else {
214 return my_ibuffer.data();
215 }
216 }();
217
218 ValueIn_* const vptr = [&]{
219 if constexpr(no_op_value) {
220 return value_buffer;
221 } else {
222 return my_vbuffer.data();
223 }
224 }();
225
226 const auto range = my_ext->fetch(i, vptr, iptr);
227 SparseRange<ValueOut_, IndexOut_> output(range.number);
228
229 if constexpr(no_op_index) {
230 output.index = range.index;
231 } else if (range.index != NULL) {
232 std::copy_n(range.index, range.number, index_buffer);
233 output.index = index_buffer;
234 }
235
236 if constexpr(no_op_value) {
237 output.value = range.value;
238 } else if (range.value != NULL) {
239 std::copy_n(range.value, range.number, value_buffer);
240 output.value = value_buffer;
241 }
242
243 return output;
244 }
245
246private:
247 std::unique_ptr<SparseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
248 static constexpr bool no_op_value = std::is_same<ValueOut_, ValueIn_>::value;
249 typename std::conditional<no_op_value, bool, std::vector<ValueIn_> >::type my_vbuffer;
250 static constexpr bool no_op_index = std::is_same<IndexOut_, IndexIn_>::value;
251 typename std::conditional<no_op_index, bool, std::vector<IndexIn_> >::type my_ibuffer;
252};
253
254}
272template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
273class DelayedCast final : public Matrix<ValueOut_, IndexOut_> {
274public:
278 DelayedCast(std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > matrix) :
279 my_matrix(std::move(matrix)),
280 my_nrow(sanisizer::cast<IndexOut_>(attest_for_Index(my_matrix->nrow()))),
281 my_ncol(sanisizer::cast<IndexOut_>(attest_for_Index(my_matrix->ncol())))
282 {}
283
284public:
285 IndexOut_ nrow() const {
286 return my_nrow;
287 }
288
289 IndexOut_ ncol() const {
290 return my_ncol;
291 }
292
293 bool is_sparse() const {
294 return my_matrix->is_sparse();
295 }
296
297 double is_sparse_proportion() const {
298 return my_matrix->is_sparse_proportion();
299 }
300
301 bool prefer_rows() const {
302 return my_matrix->prefer_rows();
303 }
304
305 double prefer_rows_proportion() const {
306 return my_matrix->prefer_rows_proportion();
307 }
308
309 bool uses_oracle(const bool row) const {
310 return my_matrix->uses_oracle(row);
311 }
312
313private:
314 std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > my_matrix;
315 IndexOut_ my_nrow, my_ncol;
316
317 /********************
318 *** Myopic dense ***
319 ********************/
320public:
321 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
322 const bool row,
323 const Options& opt
324 ) const {
325 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, opt);
326 }
327
328 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
329 const bool row,
330 const IndexOut_ block_start,
331 const IndexOut_ block_length,
332 const Options& opt
333 ) const {
334 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, block_start, block_length, opt);
335 }
336
337 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
338 const bool row,
339 VectorPtr<IndexOut_> indices_my_matrix,
340 const Options& opt
341 ) const {
342 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, std::move(indices_my_matrix), opt);
343 }
344
345 /*********************
346 *** Myopic sparse ***
347 *********************/
348public:
349 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
350 const bool row,
351 const Options& opt
352 ) const {
353 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, opt);
354 }
355
356 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
357 const bool row,
358 const IndexOut_ block_start,
359 const IndexOut_ block_length,
360 const Options& opt
361 ) const {
362 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, block_start, block_length, opt);
363 }
364
365 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
366 const bool row,
367 VectorPtr<IndexOut_> indices_my_matrix,
368 const Options& opt
369 ) const {
370 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, std::move(indices_my_matrix), opt);
371 }
372
373 /**********************
374 *** Oracular dense ***
375 **********************/
376public:
377 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
378 const bool row,
379 std::shared_ptr<const Oracle<IndexOut_> > oracle,
380 const Options& opt
381 ) const {
382 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
383 }
384
385 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
386 const bool row,
387 std::shared_ptr<const Oracle<IndexOut_> > oracle,
388 const IndexOut_ block_start,
389 const IndexOut_ block_length,
390 const Options& opt
391 ) const {
392 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
393 }
394
395 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
396 const bool row,
397 std::shared_ptr<const Oracle<IndexOut_> > oracle,
398 VectorPtr<IndexOut_> indices_my_matrix,
399 const Options& opt
400 ) const {
401 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
402 }
403
404 /***********************
405 *** Oracular sparse ***
406 ***********************/
407public:
408 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
409 const bool row,
410 std::shared_ptr<const Oracle<IndexOut_> > oracle,
411 const Options& opt
412 ) const {
413 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
414 }
415
416 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
417 const bool row,
418 std::shared_ptr<const Oracle<IndexOut_> > oracle,
419 const IndexOut_ block_start,
420 const IndexOut_ block_length,
421 const Options& opt
422 ) const {
423 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
424 }
425
426 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
427 const bool row,
428 std::shared_ptr<const Oracle<IndexOut_> > oracle,
429 VectorPtr<IndexOut_> indices_my_matrix,
430 const Options& opt
431 ) const {
432 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
433 }
434};
435
447template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
448std::shared_ptr<Matrix<ValueOut_, IndexOut_> > make_DelayedCast(std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > p) {
449 return std::shared_ptr<Matrix<ValueOut_, IndexOut_> >(new DelayedCast<ValueOut_, IndexOut_, ValueIn_, IndexIn_>(std::move(p)));
450}
451
455template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
456std::shared_ptr<Matrix<ValueOut_, IndexOut_> > make_DelayedCast(std::shared_ptr<Matrix<ValueIn_, IndexIn_> > p) {
457 return std::shared_ptr<Matrix<ValueOut_, IndexOut_> >(new DelayedCast<ValueOut_, IndexOut_, ValueIn_, IndexIn_>(std::move(p)));
458}
463}
464
465#endif
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Recast a Matrix to a different interface type.
Definition DelayedCast.hpp:273
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:416
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:365
DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > matrix)
Definition DelayedCast.hpp:278
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:328
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:426
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:395
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, const Options &opt) const
Definition DelayedCast.hpp:321
double prefer_rows_proportion() const
Definition DelayedCast.hpp:305
IndexOut_ nrow() const
Definition DelayedCast.hpp:285
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:408
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:377
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:337
bool uses_oracle(const bool row) const
Definition DelayedCast.hpp:309
bool is_sparse() const
Definition DelayedCast.hpp:293
IndexOut_ ncol() const
Definition DelayedCast.hpp:289
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:385
double is_sparse_proportion() const
Definition DelayedCast.hpp:297
bool prefer_rows() const
Definition DelayedCast.hpp:301
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, const Options &opt) const
Definition DelayedCast.hpp:349
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:356
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
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:85
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:284
std::shared_ptr< Matrix< ValueOut_, IndexOut_ > > make_DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > p)
Definition DelayedCast.hpp:448
std::size_t PredictionIndex
Definition Oracle.hpp:18
Options for accessing data from a Matrix instance.
Definition Options.hpp:30