tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
SparsifiedWrapper.hpp
Go to the documentation of this file.
1#ifndef TATAMI_SPARSIFIED_WRAPPER_HPP
2#define TATAMI_SPARSIFIED_WRAPPER_HPP
3
4#include "../base/SparseRange.hpp"
5#include "../base/Options.hpp"
6#include "../base/Extractor.hpp"
7
8#include <algorithm>
9
16namespace tatami {
17
29template<bool oracle_, typename Value_, typename Index_>
30class FullSparsifiedWrapper : public SparseExtractor<oracle_, Value_, Index_> {
31public:
40 my_dense(std::move(dense)),
41 my_extent(extent),
42 my_needs_value(opt.sparse_extract_value),
43 my_needs_index(opt.sparse_extract_index)
44 {}
45
54 if (my_needs_value) {
55 output.value = my_dense->fetch(i, value_buffer);
56 }
57 if (my_needs_index) {
58 std::iota(index_buffer, index_buffer + my_extent, static_cast<Index_>(0));
59 output.index = index_buffer;
60 }
61 return output;
62 }
63
64private:
65 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_dense;
66 Index_ my_extent;
67 bool my_needs_value;
68 bool my_needs_index;
69};
70
82template<bool oracle_, typename Value_, typename Index_>
83class BlockSparsifiedWrapper : public SparseExtractor<oracle_, Value_, Index_> {
84public:
96 my_dense(std::move(dense)),
97 my_block_start(block_start),
98 my_block_length(block_length),
99 my_needs_value(opt.sparse_extract_value),
100 my_needs_index(opt.sparse_extract_index)
101 {}
102
110 SparseRange<Value_, Index_> output(my_block_length, NULL, NULL);
111 if (my_needs_value) {
112 output.value = my_dense->fetch(i, value_buffer);
113 }
114 if (my_needs_index) {
115 std::iota(index_buffer, index_buffer + my_block_length, my_block_start);
116 output.index = index_buffer;
117 }
118 return output;
119 }
120
121private:
122 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_dense;
123 Index_ my_block_start, my_block_length;
124 bool my_needs_value;
125 bool my_needs_index;
126};
127
139template<bool oracle_, typename Value_, typename Index_>
140class IndexSparsifiedWrapper : public SparseExtractor<oracle_, Value_, Index_> {
141public:
151 my_dense(std::move(dense)),
152 my_indices_ptr(std::move(indices_ptr)),
153 my_needs_value(opt.sparse_extract_value),
154 my_needs_index(opt.sparse_extract_index)
155 {}
156
164 const auto& ix = *my_indices_ptr;
166 if (my_needs_value) {
167 output.value = my_dense->fetch(i, value_buffer);
168 }
169 if (my_needs_index) {
170 std::copy(ix.begin(), ix.end(), index_buffer);
171 output.index = index_buffer;
172 }
173 return output;
174 }
175
176private:
177 std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > my_dense;
178 VectorPtr<Index_> my_indices_ptr;
179 bool my_needs_value;
180 bool my_needs_index;
181};
182
183}
184
185#endif
Wrap a block dense extractor in the sparse interface.
Definition SparsifiedWrapper.hpp:83
SparseRange< Value_, Index_ > fetch(Index_ i, Value_ *value_buffer, Index_ *index_buffer)
Definition SparsifiedWrapper.hpp:109
BlockSparsifiedWrapper(std::unique_ptr< DenseExtractor< oracle_, Value_, Index_ > > dense, Index_ block_start, Index_ block_length, const Options &opt)
Definition SparsifiedWrapper.hpp:95
Wrap a full dense extractor in the sparse interface.
Definition SparsifiedWrapper.hpp:30
FullSparsifiedWrapper(std::unique_ptr< DenseExtractor< oracle_, Value_, Index_ > > dense, Index_ extent, const Options &opt)
Definition SparsifiedWrapper.hpp:39
SparseRange< Value_, Index_ > fetch(Index_ i, Value_ *value_buffer, Index_ *index_buffer)
Definition SparsifiedWrapper.hpp:52
Wrap an indexed dense extractor in the sparse interface.
Definition SparsifiedWrapper.hpp:140
IndexSparsifiedWrapper(std::unique_ptr< DenseExtractor< oracle_, Value_, Index_ > > dense, VectorPtr< Index_ > indices_ptr, const Options &opt)
Definition SparsifiedWrapper.hpp:150
SparseRange< Value_, Index_ > fetch(Index_ i, Value_ *value_buffer, Index_ *index_buffer)
Definition SparsifiedWrapper.hpp:163
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
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
A range of a sparse vector.
Definition SparseRange.hpp:32