tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
make_Wrapper.hpp
Go to the documentation of this file.
1#ifndef TATAMI_BUFFERED_EXTRACTOR_HPP
2#define TATAMI_BUFFERED_EXTRACTOR_HPP
3
4#include <memory>
5#include <vector>
6
8#include "../base/Matrix.hpp"
9
15namespace tatami {
16
17/********************
18 *** Myopic dense ***
19 ********************/
20
29template<typename Value_, typename Index_>
31public:
36 my_ext(std::move(ext)),
37 my_buffer(tatami::cast_Index_to_container_size<decltype(my_buffer)>(length))
38 {}
47 Index_ length() const {
48 return my_buffer.size();
49 }
50
56 const Value_* fetch(Index_ i) {
57 return my_ext->fetch(i, my_buffer.data());
58 }
59private:
60 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > my_ext;
61 std::vector<Value_> my_buffer;
62};
63
75template<typename Value_, typename Index_>
77 const Matrix<Value_, Index_>& matrix,
78 bool row,
79 const Options& opt
80) {
81 return MyopicDenseWrapper<Value_, Index_>(matrix.dense(row, opt), (row ? matrix.ncol() : matrix.nrow()));
82}
83
97template<typename Value_, typename Index_>
99 const Matrix<Value_, Index_>& matrix,
100 bool row,
101 Index_ block_start,
102 Index_ block_length,
103 const Options& opt
104) {
105 return MyopicDenseWrapper<Value_, Index_>(matrix.dense(row, block_start, block_length, opt), block_length);
106}
107
120template<typename Value_, typename Index_>
122 const Matrix<Value_, Index_>& matrix,
123 bool row,
124 VectorPtr<Index_> indices_ptr,
125 const Options& opt
126) {
127 const auto ilen = indices_ptr->size();
128 return MyopicDenseWrapper<Value_, Index_>(matrix.dense(row, std::move(indices_ptr), opt), ilen);
129}
130
131/**********************
132 *** Oracular dense ***
133 **********************/
134
143template<typename Value_, typename Index_>
145public:
150 my_ext(std::move(ext)),
151 my_buffer(tatami::cast_Index_to_container_size<decltype(my_buffer)>(length))
152 {}
161 Index_ length() const {
162 return my_buffer.size();
163 }
164
169 const Value_* next() {
170 return my_ext->fetch(my_buffer.data());
171 }
172
173private:
174 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > my_ext;
175 std::vector<Value_> my_buffer;
176};
177
192template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
194 const Matrix<Value_, Index_>& matrix,
195 bool row,
196 std::shared_ptr<Oracle_> oracle,
197 const Options& opt
198) {
199 return OracularDenseWrapper<Value_, Index_>(matrix.dense(row, std::move(oracle), opt), (row ? matrix.ncol() : matrix.nrow()));
200}
201
218template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
220 const Matrix<Value_, Index_>& matrix,
221 bool row,
222 std::shared_ptr<Oracle_> oracle,
223 Index_ block_start,
224 Index_ block_length,
225 const Options& opt
226) {
227 return OracularDenseWrapper<Value_, Index_>(matrix.dense(row, std::move(oracle), block_start, block_length, opt), block_length);
228}
229
245template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
247 const Matrix<Value_, Index_>& matrix,
248 bool row,
249 std::shared_ptr<Oracle_> oracle,
250 VectorPtr<Index_> indices_ptr,
251 const Options& opt
252) {
253 const auto ilen = indices_ptr->size();
254 return OracularDenseWrapper<Value_, Index_>(matrix.dense(row, std::move(oracle), std::move(indices_ptr), opt), ilen);
255}
256
257/*********************
258 *** Myopic sparse ***
259 *********************/
260
269template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
271public:
275 MyopicSparseWrapper(std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > ext, Index_ length, bool needs_value, bool needs_index) :
276 my_ext(std::move(ext)),
277 my_length(length)
278 {
279 if (needs_value) {
281 }
282 if (needs_index) {
284 }
285 }
294 Index_ length() const {
295 return my_length;
296 }
297
304 return my_ext->fetch(i, my_vbuffer.data(), my_ibuffer.data());
305 }
306
307private:
308 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > my_ext;
309 Index_ my_length;
310 std::vector<Value_> my_vbuffer;
311 std::vector<Index_> my_ibuffer;
312};
313
325template<typename Value_, typename Index_>
327 const Matrix<Value_, Index_>& matrix,
328 bool row,
329 const Options& opt
330) {
332 matrix.sparse(row, opt),
333 (row ? matrix.ncol() : matrix.nrow()),
336 );
337}
338
352template<typename Value_, typename Index_>
354 const Matrix<Value_, Index_>& matrix,
355 bool row,
356 Index_ block_start,
357 Index_ block_length,
358 const Options& opt
359) {
361 matrix.sparse(row, block_start, block_length, opt),
362 block_length,
365 );
366}
367
380template<typename Value_, typename Index_>
382 const Matrix<Value_, Index_>& matrix,
383 bool row,
384 VectorPtr<Index_> indices_ptr,
385 const Options& opt
386) {
387 const auto ilen = indices_ptr->size();
389 matrix.sparse(row, std::move(indices_ptr), opt),
390 ilen,
393 );
394}
395
396/***********************
397 *** Oracular sparse ***
398 ***********************/
399
408template<typename Value_, typename Index_>
410public:
414 OracularSparseWrapper(std::unique_ptr<OracularSparseExtractor<Value_, Index_> > ext, Index_ length, bool needs_value, bool needs_index) :
415 my_ext(std::move(ext)),
416 my_length(length)
417 {
418 if (needs_value) {
420 }
421 if (needs_index) {
423 }
424 }
433 Index_ length() const {
434 return my_length;
435 }
436
442 return my_ext->fetch(my_vbuffer.data(), my_ibuffer.data());
443 }
444
445private:
446 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > my_ext;
447 Index_ my_length;
448 std::vector<Value_> my_vbuffer;
449 std::vector<Index_> my_ibuffer;
450};
451
466template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
468 const Matrix<Value_, Index_>& matrix,
469 bool row,
470 std::shared_ptr<Oracle_> oracle,
471 const Options& opt
472) {
474 matrix.sparse(row, std::move(oracle), opt),
475 (row ? matrix.ncol() : matrix.nrow()),
478 );
479}
480
497template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
499 const Matrix<Value_, Index_>& matrix,
500 bool row,
501 std::shared_ptr<Oracle_> oracle,
502 Index_ block_start,
503 Index_ block_length,
504 const Options& opt
505) {
507 matrix.sparse(row, std::move(oracle), block_start, block_length, opt),
508 block_length,
511 );
512}
513
529template<typename Value_, typename Index_, class Oracle_ = Oracle<Index_> >
531 const Matrix<Value_, Index_>& matrix,
532 bool row,
533 std::shared_ptr<Oracle_> oracle,
534 VectorPtr<Index_> indices_ptr,
535 const Options& opt
536) {
537 const auto ilen = indices_ptr->size();
539 matrix.sparse(row, std::move(oracle), std::move(indices_ptr), opt),
540 ilen,
543 );
544}
545
546}
547
548#endif
Virtual classes for extracting matrix data.
Virtual class for a matrix of some numeric type.
Virtual class for a matrix.
Definition Matrix.hpp:59
virtual std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const =0
virtual Index_ ncol() const =0
virtual Index_ nrow() const =0
virtual std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const =0
Extract an element of the target dimension in dense form without an oracle.
Definition Extractor.hpp:24
User-friendly wrapper around a MyopicDenseExtractor.
Definition make_Wrapper.hpp:30
const Value_ * fetch(Index_ i)
Definition make_Wrapper.hpp:56
Index_ length() const
Definition make_Wrapper.hpp:47
Extract an element of the target dimension in sparse form without an oracle.
Definition Extractor.hpp:141
User-friendly wrapper around a MyopicSparseExtractor.
Definition make_Wrapper.hpp:270
SparseRange< Value_, Index_ > fetch(Index_ i)
Definition make_Wrapper.hpp:303
Index_ length() const
Definition make_Wrapper.hpp:294
Extract an element of the target dimension in dense form with an oracle.
Definition Extractor.hpp:70
User-friendly wrapper around a OracularDenseExtractor.
Definition make_Wrapper.hpp:144
Index_ length() const
Definition make_Wrapper.hpp:161
const Value_ * next()
Definition make_Wrapper.hpp:169
Extract an element of the target dimension in sparse form with an oracle.
Definition Extractor.hpp:196
User-friendly wrapper around a OracularSparseExtractor.
Definition make_Wrapper.hpp:409
SparseRange< Value_, Index_ > next()
Definition make_Wrapper.hpp:441
Index_ length() const
Definition make_Wrapper.hpp:433
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
auto cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:51
MyopicSparseWrapper< Value_, Index_ > make_SparseWrapper(const Matrix< Value_, Index_ > &matrix, bool row, const Options &opt)
Definition make_Wrapper.hpp:326
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:85
MyopicDenseWrapper< Value_, Index_ > make_DenseWrapper(const Matrix< Value_, Index_ > &matrix, bool row, const Options &opt)
Definition make_Wrapper.hpp:76
Options for accessing data from a Matrix instance.
Definition Options.hpp:30
bool sparse_extract_index
Definition Options.hpp:40
bool sparse_extract_value
Definition Options.hpp:52
A range of a sparse vector.
Definition SparseRange.hpp:32