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 <algorithm>
5#include <memory>
6
7#include "../base/Matrix.hpp"
8#include "../utils/copy.hpp"
10#include "utils.hpp"
11
18namespace tatami {
19
23namespace DelayedSubsetSortedUnique_internal {
24
25template<typename Index_, class SubsetStorage_>
26VectorPtr<Index_> create(const SubsetStorage_& subset) {
27 return std::make_shared<std::vector<Index_> >(subset.begin(), subset.end());
28}
29
30template<typename Index_, class SubsetStorage_>
31VectorPtr<Index_> create(const SubsetStorage_& subset, const Index_ block_start, const Index_ block_length) {
32 auto pistart = subset.begin() + block_start;
33 return std::make_shared<std::vector<Index_> >(pistart, pistart + block_length);
34}
35
36template<typename Index_, class SubsetStorage_>
37VectorPtr<Index_> create(const SubsetStorage_& subset, const VectorPtr<Index_>& indices_ptr) {
38 auto rawptr = new std::vector<Index_>;
39 VectorPtr<Index_> outptr(rawptr);
40 auto& output = *rawptr;
41
42 const auto& input = *indices_ptr;
43 output.reserve(input.size());
44 for (auto i : input) {
45 output.push_back(subset[i]);
46 }
47
48 return outptr;
49}
50
51template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
52std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
53 const Matrix<Value_, Index_>& matrix,
54 const SubsetStorage_& subset,
55 const bool row,
56 MaybeOracle<oracle_, Index_> oracle,
57 const Options& opt
58) {
59 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt);
60}
61
62template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
63std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
64 const Matrix<Value_, Index_>& matrix,
65 const SubsetStorage_& subset,
66 const bool row,
67 MaybeOracle<oracle_, Index_> oracle,
68 const Index_ block_start,
69 const Index_ block_length,
70 const Options& opt)
71{
72 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt);
73}
74
75template<bool oracle_, typename Value_, typename Index_, class SubsetStorage_>
76std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
77 const Matrix<Value_, Index_>& matrix,
78 const SubsetStorage_& subset,
79 const bool row,
80 MaybeOracle<oracle_, Index_> oracle,
81 VectorPtr<Index_> indices_ptr,
82 const Options& opt)
83{
84 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt);
85}
86
87template<bool oracle_, typename Value_, typename Index_>
88class ParallelSparse final : public SparseExtractor<oracle_, Value_, Index_> {
89public:
90 template<class SubsetStorage_>
91 ParallelSparse(
92 const Matrix<Value_, Index_>& matrix,
93 const SubsetStorage_& subset,
94 const std::vector<Index_>& remap,
95 const bool row,
96 MaybeOracle<oracle_, Index_> oracle,
97 const Options& opt
98 ) :
99 my_ext(make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt)),
100 my_remapping(remap)
101 {}
102
103 template<class SubsetStorage_>
104 ParallelSparse(
105 const Matrix<Value_, Index_>& matrix,
106 const SubsetStorage_& subset,
107 const std::vector<Index_>& remap,
108 const bool row,
109 MaybeOracle<oracle_, Index_> oracle,
110 const Index_ block_start,
111 const Index_ block_length,
112 const Options& opt
113 ) :
114 my_ext(make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt)),
115 my_remapping(remap)
116 {}
117
118 template<class SubsetStorage_>
119 ParallelSparse(
120 const Matrix<Value_, Index_>& matrix,
121 const SubsetStorage_& subset,
122 const std::vector<Index_>& remap,
123 const bool row,
124 MaybeOracle<oracle_, Index_> oracle,
125 VectorPtr<Index_> indices_ptr,
126 const Options& opt
127 ) :
128 my_ext(make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt)),
129 my_remapping(remap)
130 {}
131
132public:
133 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
134 auto out = my_ext->fetch(i, value_buffer, index_buffer);
135 if (out.index) {
136 for (Index_ i = 0; i < out.number; ++i) {
137 index_buffer[i] = my_remapping[out.index[i]];
138 }
139 out.index = index_buffer;
140 }
141 return out;
142 }
143
144private:
145 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
146 const std::vector<Index_>& my_remapping;
147};
148
149}
164template<typename Value_, typename Index_, class SubsetStorage_>
165class DelayedSubsetSortedUnique final : public Matrix<Value_, Index_> {
166public:
176 std::shared_ptr<const Matrix<Value_, Index_> > matrix,
177 SubsetStorage_ subset,
178 const bool by_row,
179 const bool check = true
180 ) :
181 my_matrix(std::move(matrix)),
182 my_subset(std::move(subset)),
183 my_by_row(by_row)
184 {
185 const auto nsub = my_subset.size();
186 if (check) {
187 for (I<decltype(nsub)> i = 1; i < nsub; ++i) {
188 if (my_subset[i] <= my_subset[i-1]) {
189 throw std::runtime_error("subset should be unique and sorted");
190 }
191 }
192 }
193
194 const Index_ mapping_dim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
195 resize_container_to_Index_size(my_mapping_single, mapping_dim);
196 for (I<decltype(nsub)> i = 0; i < nsub; ++i) {
197 my_mapping_single[my_subset[i]] = i;
198 }
199 }
200
201private:
202 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
203 SubsetStorage_ my_subset;
204 bool my_by_row;
205 std::vector<Index_> my_mapping_single;
206
207public:
208 Index_ nrow() const {
209 if (my_by_row) {
210 return my_subset.size();
211 } else {
212 return my_matrix->nrow();
213 }
214 }
215
216 Index_ ncol() const {
217 if (my_by_row) {
218 return my_matrix->ncol();
219 } else {
220 return my_subset.size();
221 }
222 }
223
224 bool is_sparse() const {
225 return my_matrix->is_sparse();
226 }
227
228 double is_sparse_proportion() const {
229 return my_matrix->is_sparse_proportion();
230 }
231
232 bool prefer_rows() const {
233 return my_matrix->prefer_rows();
234 }
235
236 double prefer_rows_proportion() const {
237 return my_matrix->prefer_rows_proportion();
238 }
239
240 bool uses_oracle(const bool row) const {
241 return my_matrix->uses_oracle(row);
242 }
243
244 using Matrix<Value_, Index_>::dense;
245
246 using Matrix<Value_, Index_>::sparse;
247
248 /********************
249 *** Myopic dense ***
250 ********************/
251private:
252 template<typename ... Args_>
253 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
254 const bool row,
255 Args_&& ... args
256 ) const {
257 if (row == my_by_row) {
258 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
259 *my_matrix,
260 my_subset,
261 row,
262 std::forward<Args_>(args)...
263 );
264 } else {
265 return DelayedSubsetSortedUnique_internal::create_parallel_dense<false>(
266 *my_matrix,
267 my_subset,
268 row,
269 false,
270 std::forward<Args_>(args)...
271 );
272 }
273 }
274
275public:
276 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
277 const bool row,
278 const Options& opt
279 ) const {
280 return populate_myopic_dense(row, opt);
281 }
282
283 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
284 const bool row,
285 const Index_ block_start,
286 const Index_ block_length,
287 const Options& opt
288 ) const {
289 return populate_myopic_dense(row, block_start, block_length, opt);
290 }
291
292 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
293 const bool row,
294 VectorPtr<Index_> indices_ptr,
295 const Options& opt
296 ) const {
297 return populate_myopic_dense(row, std::move(indices_ptr), opt);
298 }
299
300 /*********************
301 *** Myopic sparse ***
302 *********************/
303private:
304 template<typename ... Args_>
305 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
306 const bool row,
307 Args_&& ... args
308 ) const {
309 if (row == my_by_row) {
310 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
311 *my_matrix,
312 my_subset,
313 row,
314 std::forward<Args_>(args)...
315 );
316 } else {
317 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<false, Value_, Index_> >(
318 *my_matrix,
319 my_subset,
320 my_mapping_single,
321 row,
322 false,
323 std::forward<Args_>(args)...
324 );
325 }
326 }
327
328public:
329 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
330 const bool row,
331 const Options& opt
332 ) const {
333 return populate_myopic_sparse(row, opt);
334 }
335
336 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
337 const bool row,
338 const Index_ block_start,
339 const Index_ block_length,
340 const Options& opt
341 ) const {
342 return populate_myopic_sparse(row, block_start, block_length, opt);
343 }
344
345 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
346 const bool row,
347 VectorPtr<Index_> indices_ptr,
348 const Options& opt
349 ) const {
350 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
351 }
352
353 /**********************
354 *** Oracular dense ***
355 **********************/
356private:
357 template<typename ... Args_>
358 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
359 const bool row,
360 std::shared_ptr<const Oracle<Index_> > oracle,
361 Args_&& ... args
362 ) const {
363 if (row == my_by_row) {
364 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
365 *my_matrix,
366 my_subset,
367 row,
368 std::move(oracle),
369 std::forward<Args_>(args)...
370 );
371 } else {
372 return DelayedSubsetSortedUnique_internal::create_parallel_dense<true>(
373 *my_matrix,
374 my_subset,
375 row,
376 std::move(oracle),
377 std::forward<Args_>(args)...
378 );
379 }
380 }
381
382public:
383 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
384 const bool row,
385 std::shared_ptr<const Oracle<Index_> > oracle,
386 const Options& opt
387 ) const {
388 return populate_oracular_dense(row, std::move(oracle), opt);
389 }
390
391 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
392 const bool row,
393 std::shared_ptr<const Oracle<Index_> > oracle,
394 const Index_ block_start,
395 const Index_ block_length,
396 const Options& opt
397 ) const {
398 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
399 }
400
401 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
402 const bool row,
403 std::shared_ptr<const Oracle<Index_> > oracle,
404 VectorPtr<Index_> indices_ptr,
405 const Options& opt
406 ) const {
407 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
408 }
409
410 /***********************
411 *** Oracular sparse ***
412 ***********************/
413private:
414 template<typename ... Args_>
415 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
416 const bool row,
417 std::shared_ptr<const Oracle<Index_> > oracle,
418 Args_&& ... args
419 ) const {
420 if (row == my_by_row) {
421 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
422 *my_matrix,
423 my_subset,
424 row,
425 std::move(oracle),
426 std::forward<Args_>(args)...
427 );
428 } else {
429 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<true, Value_, Index_> >(
430 *my_matrix,
431 my_subset,
432 my_mapping_single,
433 row,
434 std::move(oracle),
435 std::forward<Args_>(args)...
436 );
437 }
438 }
439
440public:
441 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
442 const bool row,
443 std::shared_ptr<const Oracle<Index_> > oracle,
444 const Options& opt
445 ) const {
446 return populate_oracular_sparse(row, std::move(oracle), opt);
447 }
448
449 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
450 const bool row,
451 std::shared_ptr<const Oracle<Index_> > oracle,
452 const Index_ block_start,
453 const Index_ block_length,
454 const Options& opt
455 ) const {
456 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
457 }
458
459 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
460 const bool row,
461 std::shared_ptr<const Oracle<Index_> > oracle,
462 VectorPtr<Index_> indices_ptr,
463 const Options& opt
464 ) const {
465 return populate_oracular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
466 }
467};
468
469}
470
471#endif
Virtual class for a matrix of some numeric type.
Delayed subsetting of a matrix with sorted, unique indices.
Definition DelayedSubsetSortedUnique.hpp:165
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:391
Index_ ncol() const
Definition DelayedSubsetSortedUnique.hpp:216
double prefer_rows_proportion() const
Definition DelayedSubsetSortedUnique.hpp:236
bool uses_oracle(const bool row) const
Definition DelayedSubsetSortedUnique.hpp:240
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:336
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:283
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:441
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:329
double is_sparse_proportion() const
Definition DelayedSubsetSortedUnique.hpp:228
Index_ nrow() const
Definition DelayedSubsetSortedUnique.hpp:208
DelayedSubsetSortedUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row, const bool check=true)
Definition DelayedSubsetSortedUnique.hpp:175
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:292
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:345
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:401
bool is_sparse() const
Definition DelayedSubsetSortedUnique.hpp:224
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:449
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:276
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:459
bool prefer_rows() const
Definition DelayedSubsetSortedUnique.hpp:232
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:383
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
Copy data from one buffer to another.
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
std::unique_ptr< Extractor< sparse_, oracle_, Value_, Index_ > > make_Extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, const tatami::Options &opt)
Definition make_Extractor.hpp:69
Options for accessing data from a Matrix instance.
Definition Options.hpp:30