tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
DelayedSubsetSortedUnique.hpp
Go to the documentation of this file.
1#ifndef TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
3
4#include "../base/Matrix.hpp"
5#include "utils.hpp"
6
7#include <algorithm>
8#include <memory>
9
16namespace tatami {
17
22
23template<typename Index_, class SubsetStorage_>
25 return std::make_shared<std::vector<Index_> >(subset.begin(), subset.end());
26}
27
28template<typename Index_, class SubsetStorage_>
30 auto pistart = subset.begin() + block_start;
31 return std::make_shared<std::vector<Index_> >(pistart, pistart + block_length);
32}
33
34template<typename Index_, class SubsetStorage_>
36 auto rawptr = std::make_shared<std::vector<Index_> >();
38 auto& output = *rawptr;
39
40 const auto& input = *indices_ptr;
41 output.reserve(input.size());
42 for (auto i : input) {
43 output.push_back(subset[i]);
44 }
45
46 return outptr;
47}
48
49template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
50std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
51 const Matrix<Value_, Index_>* matrix,
52 const SubsetStorage_& subset,
53 bool row,
55 const Options& opt)
56{
58}
59
60template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
61std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
62 const Matrix<Value_, Index_>* matrix,
64 bool row,
68 const Options& opt)
69{
71}
72
73template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
74std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
75 const Matrix<Value_, Index_>* matrix,
77 bool row,
80 const Options& opt)
81{
83}
84
85template<bool oracle_, typename Value_, typename Index_>
86class ParallelSparse : public SparseExtractor<oracle_, Value_, Index_> {
87public:
88 template<class SubsetStorage_>
89 ParallelSparse(
90 const Matrix<Value_, Index_>* matrix,
91 const SubsetStorage_& subset,
92 const std::vector<Index_>& remap,
93 bool row,
94 MaybeOracle<oracle_, Index_> oracle,
95 const Options& opt
96 ) :
97 my_ext(new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt)),
98 my_remapping(remap)
99 {}
100
101 template<class SubsetStorage_>
102 ParallelSparse(
103 const Matrix<Value_, Index_>* matrix,
104 const SubsetStorage_& subset,
105 const std::vector<Index_>& remap,
106 bool row, MaybeOracle<oracle_, Index_> oracle,
107 Index_ block_start,
108 Index_ block_length,
109 const Options& opt
110 ) :
111 my_ext(new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt)),
112 my_remapping(remap)
113 {}
114
115 template<class SubsetStorage_>
116 ParallelSparse(
117 const Matrix<Value_, Index_>* matrix,
118 const SubsetStorage_& subset,
119 const std::vector<Index_>& remap,
120 bool row,
121 MaybeOracle<oracle_, Index_> oracle,
122 VectorPtr<Index_> indices_ptr,
123 const Options& opt
124 ) :
125 my_ext(new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt)),
126 my_remapping(remap)
127 {}
128
129public:
130 SparseRange<Value_, Index_> fetch(Index_ i, Value_* value_buffer, Index_* index_buffer) {
131 auto out = my_ext->fetch(i, value_buffer, index_buffer);
132 if (out.index) {
133 for (Index_ i = 0; i < out.number; ++i) {
134 index_buffer[i] = my_remapping[out.index[i]];
135 }
136 out.index = index_buffer;
137 }
138 return out;
139 }
140
141private:
142 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
143 const std::vector<Index_>& my_remapping;
144};
145
146}
161template<typename Value_, typename Index_, class SubsetStorage_>
162class DelayedSubsetSortedUnique : public Matrix<Value_, Index_> {
163public:
173 my_matrix(std::move(matrix)), my_subset(std::move(subset)), my_by_row(by_row)
174 {
175 if (check) {
176 for (Index_ i = 1, end = my_subset.size(); i < end; ++i) {
177 if (my_subset[i] <= my_subset[i-1]) {
178 throw std::runtime_error("subset should be unique and sorted");
179 }
180 }
181 }
182
183 Index_ mapping_dim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
184 my_mapping_single.resize(mapping_dim);
185 for (Index_ i = 0, end = my_subset.size(); i < end; ++i) {
186 my_mapping_single[my_subset[i]] = i;
187 }
188 }
189
190private:
191 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
192 SubsetStorage_ my_subset;
193 bool my_by_row;
194 std::vector<Index_> my_mapping_single;
195
196public:
197 Index_ nrow() const {
198 if (my_by_row) {
199 return my_subset.size();
200 } else {
201 return my_matrix->nrow();
202 }
203 }
204
205 Index_ ncol() const {
206 if (my_by_row) {
207 return my_matrix->ncol();
208 } else {
209 return my_subset.size();
210 }
211 }
212
213 bool is_sparse() const {
214 return my_matrix->is_sparse();
215 }
216
217 double is_sparse_proportion() const {
218 return my_matrix->is_sparse_proportion();
219 }
220
221 bool prefer_rows() const {
222 return my_matrix->prefer_rows();
223 }
224
225 double prefer_rows_proportion() const {
226 return my_matrix->prefer_rows_proportion();
227 }
228
229 bool uses_oracle(bool row) const {
230 return my_matrix->uses_oracle(row);
231 }
232
234
236
237 /********************
238 *** Myopic dense ***
239 ********************/
240private:
241 template<typename ... Args_>
242 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(bool row, Args_&& ... args) const {
243 if (row == my_by_row) {
244 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
245 } else {
246 return DelayedSubsetSortedUnique_internal::create_parallel_dense<false>(my_matrix.get(), my_subset, row, false, std::forward<Args_>(args)...);
247 }
248 }
249
250public:
251 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options& opt) const {
252 return populate_myopic_dense(row, opt);
253 }
254
255 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
256 return populate_myopic_dense(row, block_start, block_length, opt);
257 }
258
259 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
260 return populate_myopic_dense(row, std::move(indices_ptr), opt);
261 }
262
263 /*********************
264 *** Myopic sparse ***
265 *********************/
266private:
267 template<typename ... Args_>
268 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(bool row, Args_&& ... args) const {
269 if (row == my_by_row) {
270 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(my_matrix.get(), my_subset, row, std::forward<Args_>(args)...);
271 } else {
272 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<false, Value_, Index_> >(my_matrix.get(), my_subset, my_mapping_single, row, false, std::forward<Args_>(args)...);
273 }
274 }
275
276public:
277 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
278 return populate_myopic_sparse(row, opt);
279 }
280
281 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_length, const Options& opt) const {
282 return populate_myopic_sparse(row, block_start, block_length, opt);
283 }
284
285 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> indices_ptr, const Options& opt) const {
286 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
287 }
288
289 /**********************
290 *** Oracular dense ***
291 **********************/
292private:
293 template<typename ... Args_>
294 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Args_&& ... args) const {
295 if (row == my_by_row) {
296 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
297 } else {
298 return DelayedSubsetSortedUnique_internal::create_parallel_dense<true>(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
299 }
300 }
301
302public:
303 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
304 return populate_oracular_dense(row, std::move(oracle), opt);
305 }
306
307 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 {
308 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
309 }
310
311 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
312 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
313 }
314
315 /***********************
316 *** Oracular sparse ***
317 ***********************/
318private:
319 template<typename ... Args_>
320 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Args_&& ... args) const {
321 if (row == my_by_row) {
322 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(my_matrix.get(), my_subset, row, std::move(oracle), std::forward<Args_>(args)...);
323 } else {
324 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<true, Value_, Index_> >(my_matrix.get(), my_subset, my_mapping_single, row, std::move(oracle), std::forward<Args_>(args)...);
325 }
326 }
327
328public:
329 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
330 return populate_oracular_sparse(row, std::move(oracle), opt);
331 }
332
333 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 {
334 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
335 }
336
337 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> indices_ptr, const Options& opt) const {
338 return populate_oracular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
339 }
340};
341
342}
343
344#endif
Delayed subsetting of a matrix with sorted, unique indices.
Definition DelayedSubsetSortedUnique.hpp:162
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 DelayedSubsetSortedUnique.hpp:337
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 DelayedSubsetSortedUnique.hpp:307
Index_ ncol() const
Definition DelayedSubsetSortedUnique.hpp:205
double prefer_rows_proportion() const
Definition DelayedSubsetSortedUnique.hpp:225
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:303
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:285
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:329
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:277
double is_sparse_proportion() const
Definition DelayedSubsetSortedUnique.hpp:217
DelayedSubsetSortedUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, bool by_row, bool check=true)
Definition DelayedSubsetSortedUnique.hpp:172
Index_ nrow() const
Definition DelayedSubsetSortedUnique.hpp:197
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:281
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 DelayedSubsetSortedUnique.hpp:333
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 DelayedSubsetSortedUnique.hpp:311
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:259
bool uses_oracle(bool row) const
Definition DelayedSubsetSortedUnique.hpp:229
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:251
bool is_sparse() const
Definition DelayedSubsetSortedUnique.hpp:213
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, Index_ block_start, Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:255
bool prefer_rows() const
Definition DelayedSubsetSortedUnique.hpp:221
Virtual class for a matrix.
Definition Matrix.hpp:59
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_, 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 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