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"
6#include "../utils/Index_to_container.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 = new_extractor<false, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), opt);
73 }
74
75 Dense(
76 const Matrix<ValueIn_, IndexIn_>& matrix,
77 const bool row,
78 MaybeOracle<oracle_, IndexOut_> oracle,
79 const IndexOut_ block_start,
80 const IndexOut_ block_length,
81 const Options& opt
82 ) {
83 allocate(block_length);
84 my_ext = new_extractor<false, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), block_start, block_length, opt);
85 }
86
87 Dense(
88 const Matrix<ValueIn_, IndexIn_>& matrix,
89 const bool row,
90 MaybeOracle<oracle_, IndexOut_> oracle,
91 VectorPtr<IndexOut_> indices_ptr,
92 const Options& opt
93 ) {
94 allocate(indices_ptr->size());
95 my_ext = new_extractor<false, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), convert<IndexIn_>(std::move(indices_ptr)), opt);
96 }
97
98private:
99 void allocate(const IndexIn_ n) {
100 if constexpr(!no_op) {
101 resize_container_to_Index_size(my_buffer, n);
102 }
103 }
104
105public:
106 const ValueOut_* fetch(const IndexOut_ i, ValueOut_* const buffer) {
107 if constexpr(no_op) {
108 return my_ext->fetch(i, buffer);
109 } else {
110 const auto ptr = my_ext->fetch(i, my_buffer.data());
111 std::copy_n(ptr, my_buffer.size(), buffer);
112 return buffer;
113 }
114 }
115
116private:
117 std::unique_ptr<DenseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
118 static constexpr bool no_op = std::is_same<ValueOut_, ValueIn_>::value;
119 typename std::conditional<no_op, bool, std::vector<ValueIn_> >::type my_buffer;
120};
121
122template<bool oracle_, typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
123class Sparse final : public SparseExtractor<oracle_, ValueOut_, IndexOut_> {
124public:
125 Sparse(
126 const Matrix<ValueIn_, IndexIn_>& matrix,
127 const bool row,
128 MaybeOracle<oracle_, IndexOut_> oracle,
129 const Options& opt
130 ) {
131 allocate(row ? matrix.ncol() : matrix.nrow(), opt);
132 my_ext = new_extractor<true, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), opt);
133 }
134
135 Sparse(
136 const Matrix<ValueIn_, IndexIn_>& matrix,
137 const bool row,
138 MaybeOracle<oracle_, IndexOut_> oracle,
139 const IndexOut_ block_start,
140 const IndexOut_ block_length,
141 const Options& opt
142 ) {
143 allocate(block_length, opt);
144 my_ext = new_extractor<true, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), block_start, block_length, opt);
145 }
146
147 Sparse(
148 const Matrix<ValueIn_, IndexIn_>& matrix,
149 const bool row,
150 MaybeOracle<oracle_, IndexOut_> oracle,
151 VectorPtr<IndexOut_> indices_ptr,
152 const Options& opt
153 ) {
154 allocate(indices_ptr->size(), opt);
155 my_ext = new_extractor<true, oracle_>(matrix, row, convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)), convert<IndexIn_>(std::move(indices_ptr)), opt);
156 }
157
158private:
159 void allocate(const IndexIn_ n, const Options& opt) {
160 if constexpr(!no_op_value) {
161 if (opt.sparse_extract_value) {
162 resize_container_to_Index_size(my_vbuffer, n);
163 }
164 }
165 if constexpr(!no_op_index) {
166 if (opt.sparse_extract_index) {
167 resize_container_to_Index_size(my_ibuffer, n);
168 }
169 }
170 }
171
172public:
173 SparseRange<ValueOut_, IndexOut_> fetch(const IndexOut_ i, ValueOut_* const value_buffer, IndexOut_* const index_buffer) {
174 IndexIn_* const iptr = [&]{
175 if constexpr(no_op_index) {
176 return index_buffer;
177 } else {
178 return my_ibuffer.data();
179 }
180 }();
181
182 ValueIn_* const vptr = [&]{
183 if constexpr(no_op_value) {
184 return value_buffer;
185 } else {
186 return my_vbuffer.data();
187 }
188 }();
189
190 const auto range = my_ext->fetch(i, vptr, iptr);
191 SparseRange<ValueOut_, IndexOut_> output(range.number);
192
193 if constexpr(no_op_index) {
194 output.index = range.index;
195 } else if (range.index != NULL) {
196 std::copy_n(range.index, range.number, index_buffer);
197 output.index = index_buffer;
198 }
199
200 if constexpr(no_op_value) {
201 output.value = range.value;
202 } else if (range.value != NULL) {
203 std::copy_n(range.value, range.number, value_buffer);
204 output.value = value_buffer;
205 }
206
207 return output;
208 }
209
210private:
211 std::unique_ptr<SparseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
212 static constexpr bool no_op_value = std::is_same<ValueOut_, ValueIn_>::value;
213 typename std::conditional<no_op_value, bool, std::vector<ValueIn_> >::type my_vbuffer;
214 static constexpr bool no_op_index = std::is_same<IndexOut_, IndexIn_>::value;
215 typename std::conditional<no_op_index, bool, std::vector<IndexIn_> >::type my_ibuffer;
216};
217
218}
236template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
237class DelayedCast final : public Matrix<ValueOut_, IndexOut_> {
238public:
242 DelayedCast(std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > matrix) : my_matrix(std::move(matrix)) {}
243
244public:
245 IndexOut_ nrow() const {
246 return my_matrix->nrow();
247 }
248
249 IndexOut_ ncol() const {
250 return my_matrix->ncol();
251 }
252
253 bool is_sparse() const {
254 return my_matrix->is_sparse();
255 }
256
257 double is_sparse_proportion() const {
258 return my_matrix->is_sparse_proportion();
259 }
260
261 bool prefer_rows() const {
262 return my_matrix->prefer_rows();
263 }
264
265 double prefer_rows_proportion() const {
266 return my_matrix->prefer_rows_proportion();
267 }
268
269 bool uses_oracle(const bool row) const {
270 return my_matrix->uses_oracle(row);
271 }
272
273private:
274 std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > my_matrix;
275
276 /********************
277 *** Myopic dense ***
278 ********************/
279public:
280 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
281 const bool row,
282 const Options& opt
283 ) const {
284 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, opt);
285 }
286
287 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
288 const bool row,
289 const IndexOut_ block_start,
290 const IndexOut_ block_length,
291 const Options& opt
292 ) const {
293 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, block_start, block_length, opt);
294 }
295
296 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> > dense(
297 const bool row,
298 VectorPtr<IndexOut_> indices_my_matrix,
299 const Options& opt
300 ) const {
301 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, std::move(indices_my_matrix), opt);
302 }
303
304 /*********************
305 *** Myopic sparse ***
306 *********************/
307public:
308 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
309 const bool row,
310 const Options& opt
311 ) const {
312 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, opt);
313 }
314
315 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
316 const bool row,
317 const IndexOut_ block_start,
318 const IndexOut_ block_length,
319 const Options& opt
320 ) const {
321 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, block_start, block_length, opt);
322 }
323
324 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> > sparse(
325 const bool row,
326 VectorPtr<IndexOut_> indices_my_matrix,
327 const Options& opt
328 ) const {
329 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, false, std::move(indices_my_matrix), opt);
330 }
331
332 /**********************
333 *** Oracular dense ***
334 **********************/
335public:
336 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
337 const bool row,
338 std::shared_ptr<const Oracle<IndexOut_> > oracle,
339 const Options& opt
340 ) const {
341 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
342 }
343
344 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
345 const bool row,
346 std::shared_ptr<const Oracle<IndexOut_> > oracle,
347 const IndexOut_ block_start,
348 const IndexOut_ block_length,
349 const Options& opt
350 ) const {
351 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
352 }
353
354 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> > dense(
355 const bool row,
356 std::shared_ptr<const Oracle<IndexOut_> > oracle,
357 VectorPtr<IndexOut_> indices_my_matrix,
358 const Options& opt
359 ) const {
360 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
361 }
362
363 /***********************
364 *** Oracular sparse ***
365 ***********************/
366public:
367 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
368 const bool row,
369 std::shared_ptr<const Oracle<IndexOut_> > oracle,
370 const Options& opt
371 ) const {
372 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
373 }
374
375 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
376 const bool row,
377 std::shared_ptr<const Oracle<IndexOut_> > oracle,
378 const IndexOut_ block_start,
379 const IndexOut_ block_length,
380 const Options& opt
381 ) const {
382 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
383 }
384
385 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> > sparse(
386 const bool row,
387 std::shared_ptr<const Oracle<IndexOut_> > oracle,
388 VectorPtr<IndexOut_> indices_my_matrix,
389 const Options& opt
390 ) const {
391 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
392 }
393};
394
406template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
407std::shared_ptr<Matrix<ValueOut_, IndexOut_> > make_DelayedCast(std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > p) {
408 return std::shared_ptr<Matrix<ValueOut_, IndexOut_> >(new DelayedCast<ValueOut_, IndexOut_, ValueIn_, IndexIn_>(std::move(p)));
409}
410
414template<typename ValueOut_, typename IndexOut_, typename ValueIn_, typename IndexIn_>
415std::shared_ptr<Matrix<ValueOut_, IndexOut_> > make_DelayedCast(std::shared_ptr<Matrix<ValueIn_, IndexIn_> > p) {
416 return std::shared_ptr<Matrix<ValueOut_, IndexOut_> >(new DelayedCast<ValueOut_, IndexOut_, ValueIn_, IndexIn_>(std::move(p)));
417}
422}
423
424#endif
Virtual class for a matrix of some numeric type.
Recast a Matrix to a different interface type.
Definition DelayedCast.hpp:237
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:375
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:324
DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > matrix)
Definition DelayedCast.hpp:242
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:287
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:385
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:354
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, const Options &opt) const
Definition DelayedCast.hpp:280
double prefer_rows_proportion() const
Definition DelayedCast.hpp:265
IndexOut_ nrow() const
Definition DelayedCast.hpp:245
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:367
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:336
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:296
bool uses_oracle(const bool row) const
Definition DelayedCast.hpp:269
bool is_sparse() const
Definition DelayedCast.hpp:253
IndexOut_ ncol() const
Definition DelayedCast.hpp:249
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:344
double is_sparse_proportion() const
Definition DelayedCast.hpp:257
bool prefer_rows() const
Definition DelayedCast.hpp:261
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, const Options &opt) const
Definition DelayedCast.hpp:308
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:315
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
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:92
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:273
std::shared_ptr< Matrix< ValueOut_, IndexOut_ > > make_DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > p)
Definition DelayedCast.hpp:407
std::size_t PredictionIndex
Definition Oracle.hpp:18
Templated construction of a new extractor.
Options for accessing data from a Matrix instance.
Definition Options.hpp:30