tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
FragmentedSparseMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_FRAGMENTED_SPARSE_MATRIX_H
2#define TATAMI_FRAGMENTED_SPARSE_MATRIX_H
3
4#include "../base/Matrix.hpp"
5#include "primary_extraction.hpp"
6#include "secondary_extraction.hpp"
7#include "../utils/ElementType.hpp"
8#include "../utils/PseudoOracularExtractor.hpp"
9
10#include <vector>
11#include <algorithm>
12#include <memory>
13#include <utility>
14#include <stdexcept>
15
22namespace tatami {
23
28
29/********************
30 *** Primary full ***
31 ********************/
32
33template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
34class PrimaryMyopicFullDense : public MyopicDenseExtractor<Value_, Index_> {
35public:
36 PrimaryMyopicFullDense(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, Index_ secondary) :
37 my_values(values), my_indices(indices), my_secondary(secondary) {}
38
39 const Value_* fetch(Index_ i, Value_* buffer) {
40 const auto& curv = my_values[i];
41 const auto& curi = my_indices[i];
42
43 std::fill_n(buffer, my_secondary, static_cast<Value_>(0));
44 size_t end = curv.size();
45 for (size_t x = 0; x < end; ++x) {
46 buffer[curi[x]] = curv[x];
47 }
48 return buffer;
49 }
50
51private:
52 const ValueVectorStorage_& my_values;
53 const IndexVectorStorage_& my_indices;
54 Index_ my_secondary;
55};
56
57template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
58class PrimaryMyopicFullSparse : public MyopicSparseExtractor<Value_, Index_> {
59public:
60 PrimaryMyopicFullSparse(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, [[maybe_unused]] Index_ secondary /* for consistency only */, const Options& opt) :
61 my_values(values), my_indices(indices), my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
62
63 SparseRange<Value_, Index_> fetch(Index_ i, Value_* vbuffer, Index_* index_buffer) {
64 const auto& curv = my_values[i];
65 const auto& curi = my_indices[i];
66
67 SparseRange<Value_, Index_> output(curv.size(), NULL, NULL);
68 if (my_needs_value) {
69 output.value = sparse_utils::extract_primary_vector(curv, static_cast<size_t>(0), curv.size(), vbuffer);
70 }
71 if (my_needs_index) {
72 output.index = sparse_utils::extract_primary_vector(curi, static_cast<size_t>(0), curi.size(), index_buffer);
73 }
74 return output;
75 }
76
77private:
78 const ValueVectorStorage_& my_values;
79 const IndexVectorStorage_& my_indices;
80 bool my_needs_value, my_needs_index;
81};
82
83/*********************
84 *** Primary block ***
85 *********************/
86
87template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
88class PrimaryMyopicBlockDense : public MyopicDenseExtractor<Value_, Index_> {
89public:
90 PrimaryMyopicBlockDense(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, Index_ secondary, Index_ block_start, Index_ block_length) :
91 my_values(values), my_indices(indices), my_secondary(secondary), my_block_start(block_start), my_block_length(block_length) {}
92
93 const Value_* fetch(Index_ i, Value_* buffer) {
94 const auto& curi = my_indices[i];
95 const auto& curv = my_values[i];
96
97 auto iStart = curi.begin();
98 auto iEnd = curi.end();
99 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
100 size_t offset = (iStart - curi.begin());
101 size_t number = iEnd - iStart;
102
103 std::fill_n(buffer, my_block_length, static_cast<Value_>(0));
104 for (size_t i = 0; i < number; ++i) {
105 auto cur_offset = offset + i;
106 buffer[curi[cur_offset] - my_block_start] = curv[cur_offset];
107 }
108 return buffer;
109 }
110
111private:
112 const ValueVectorStorage_& my_values;
113 const IndexVectorStorage_& my_indices;
114 Index_ my_secondary;
115 Index_ my_block_start, my_block_length;
116};
117
118template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
119class PrimaryMyopicBlockSparse : public MyopicSparseExtractor<Value_, Index_> {
120public:
121 PrimaryMyopicBlockSparse(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, Index_ secondary, Index_ block_start, Index_ block_length, const Options& opt) :
122 my_values(values),
123 my_indices(indices),
124 my_secondary(secondary),
125 my_block_start(block_start),
126 my_block_length(block_length),
127 my_needs_value(opt.sparse_extract_value),
128 my_needs_index(opt.sparse_extract_index)
129 {}
130
131 SparseRange<Value_, Index_> fetch(Index_ i, Value_* vbuffer, Index_* index_buffer) {
132 const auto& curi = my_indices[i];
133 auto iStart = curi.begin();
134 auto iEnd = curi.end();
135 sparse_utils::refine_primary_block_limits(iStart, iEnd, my_secondary, my_block_start, my_block_length);
136 size_t offset = iStart - curi.begin();
137 size_t delta = iEnd - iStart;
138
139 SparseRange<Value_, Index_> output(delta, NULL, NULL);
140 if (my_needs_value) {
141 output.value = sparse_utils::extract_primary_vector(my_values[i], offset, delta, vbuffer);
142 }
143 if (my_needs_index) {
144 output.index = sparse_utils::extract_primary_vector(curi, offset, delta, index_buffer);
145 }
146 return output;
147 }
148
149private:
150 const ValueVectorStorage_& my_values;
151 const IndexVectorStorage_& my_indices;
152 Index_ my_secondary;
153 Index_ my_block_start, my_block_length;
154 bool my_needs_value, my_needs_index;
155};
156
157/***********************
158 *** Primary indexed ***
159 ***********************/
160
161template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
162class PrimaryMyopicIndexDense : public MyopicDenseExtractor<Value_, Index_> {
163public:
164 PrimaryMyopicIndexDense(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, Index_ secondary, VectorPtr<Index_> indices_ptr) :
165 my_values(values), my_indices(indices), my_retriever(*indices_ptr, secondary), my_num_indices(indices_ptr->size()) {}
166
167 const Value_* fetch(Index_ i, Value_* buffer) {
168 const auto& curi = my_indices[i];
169 const auto& curv = my_values[i];
170 std::fill_n(buffer, my_num_indices, static_cast<Value_>(0));
171 my_retriever.populate(
172 curi.begin(),
173 curi.end(),
174 [&](size_t s, size_t offset) {
175 buffer[s] = curv[offset];
176 }
177 );
178 return buffer;
179 }
180
181private:
182 const ValueVectorStorage_& my_values;
183 const IndexVectorStorage_& my_indices;
184 sparse_utils::RetrievePrimarySubsetDense<Index_> my_retriever;
185 size_t my_num_indices;
186};
187
188template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
189class PrimaryMyopicIndexSparse : public MyopicSparseExtractor<Value_, Index_> {
190public:
191 PrimaryMyopicIndexSparse(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, Index_ secondary, VectorPtr<Index_> indices_ptr, const Options& opt) :
192 my_values(values), my_indices(indices), my_retriever(*indices_ptr, secondary), my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
193
194 SparseRange<Value_, Index_> fetch(Index_ i, Value_* vbuffer, Index_* index_buffer) {
195 const auto& curi = my_indices[i];
196 const auto& curv = my_values[i];
197 Index_ count = 0;
198 auto vcopy = vbuffer;
199 auto icopy = index_buffer;
200
201 my_retriever.populate(
202 curi.begin(),
203 curi.end(),
204 [&](size_t offset, Index_ ix) {
205 ++count;
206 if (my_needs_value) {
207 *vcopy = curv[offset];
208 ++vcopy;
209 }
210 if (my_needs_index) {
211 *icopy = ix;
212 ++icopy;
213 }
214 }
215 );
216
217 return SparseRange<Value_, Index_>(count, my_needs_value ? vbuffer : NULL, my_needs_index ? index_buffer : NULL);
218 }
219
220private:
221 const ValueVectorStorage_& my_values;
222 const IndexVectorStorage_& my_indices;
223 sparse_utils::RetrievePrimarySubsetSparse<Index_> my_retriever;
224 bool my_needs_value, my_needs_index;
225};
226
227/**********************
228 *** Secondary full ***
229 **********************/
230
231template<typename Index_, class IndexVectorStorage_>
232class ServeIndices {
233public:
234 ServeIndices(const IndexVectorStorage_& indices) : my_indices(indices) {}
235
236private:
237 const IndexVectorStorage_& my_indices;
238
239public:
240 typedef size_t pointer_type;
241
243 return 0;
244 }
245
246 pointer_type end_offset(Index_ primary) const {
247 return my_indices[primary].size();
248 }
249
250 auto raw(Index_ primary) const {
251 return my_indices[primary].begin();
252 }
253};
254
255template<typename Index_, class IndexVectorStorage_>
258}
259
260template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
261class SecondaryMyopicFullDense : public MyopicDenseExtractor<Value_, Index_> {
262public:
264 my_values(values), my_cache(make_ServeIndices<Index_>(indices), secondary, indices.size()) {}
265
266 const Value_* fetch(Index_ i, Value_* buffer) {
267 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
268 my_cache.search(i, [&](Index_ primary, Index_ index_primary, size_t ptr) {
269 buffer[index_primary] = my_values[primary][ptr];
270 });
271 return buffer;
272 }
273
274private:
275 const ValueVectorStorage_& my_values;
276 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
277};
278
279template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
280class SecondaryMyopicFullSparse : public MyopicSparseExtractor<Value_, Index_> {
281public:
283 my_values(values), my_cache(make_ServeIndices<Index_>(indices), secondary, indices.size()), my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
284
286 Index_ count = 0;
287 my_cache.search(i, [&](Index_ primary, Index_, size_t ptr) {
288 if (my_needs_value) {
289 value_buffer[count] = my_values[primary][ptr];
290 }
291 if (my_needs_index) {
292 index_buffer[count] = primary;
293 }
294 ++count;
295 });
296 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
297 }
298
299private:
300 const ValueVectorStorage_& my_values;
301 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
302 bool my_needs_value, my_needs_index;
303};
304
305/***********************
306 *** Secondary block ***
307 ***********************/
308
309template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
310class SecondaryMyopicBlockDense : public MyopicDenseExtractor<Value_, Index_> {
311public:
314
315 const Value_* fetch(Index_ i, Value_* buffer) {
316 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
317 my_cache.search(i, [&](Index_ primary, Index_ index_primary, size_t ptr) {
318 buffer[index_primary] = my_values[primary][ptr];
319 });
320 return buffer;
321 }
322
323private:
324 const ValueVectorStorage_& my_values;
325 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
326};
327
328template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
329class SecondaryMyopicBlockSparse : public MyopicSparseExtractor<Value_, Index_> {
330public:
332 my_values(values), my_cache(make_ServeIndices<Index_>(indices), secondary, block_start, block_length), my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
333
335 Index_ count = 0;
336 my_cache.search(i, [&](Index_ primary, Index_, size_t ptr) {
337 if (my_needs_value) {
338 value_buffer[count] = my_values[primary][ptr];
339 }
340 if (my_needs_index) {
341 index_buffer[count] = primary;
342 }
343 ++count;
344 });
345 return SparseRange<Value_, Index_>(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
346 }
347
348private:
349 const ValueVectorStorage_& my_values;
350 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
351 bool my_needs_value, my_needs_index;
352};
353
354/***********************
355 *** Secondary index ***
356 ***********************/
357
358template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
359class SecondaryMyopicIndexDense : public MyopicDenseExtractor<Value_, Index_> {
360public:
362 my_values(values), my_cache(make_ServeIndices<Index_>(indices), secondary, std::move(indices_ptr)) {}
363
364 const Value_* fetch(Index_ i, Value_* buffer) {
365 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
366 my_cache.search(i, [&](Index_ primary, Index_ index_primary, size_t ptr) {
367 buffer[index_primary] = my_values[primary][ptr];
368 });
369 return buffer;
370 }
371
372private:
373 const ValueVectorStorage_& my_values;
374 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
375};
376
377template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
378class SecondaryMyopicIndexSparse : public MyopicSparseExtractor<Value_, Index_> {
379public:
381 my_values(values), my_cache(make_ServeIndices<Index_>(indices), secondary, std::move(indices_ptr)), my_needs_value(opt.sparse_extract_value), my_needs_index(opt.sparse_extract_index) {}
382
384 Index_ count = 0;
385 my_cache.search(i, [&](Index_ primary, Index_, size_t ptr) {
386 if (my_needs_value) {
387 vbuffer[count] = my_values[primary][ptr];
388 }
389 if (my_needs_index) {
390 index_buffer[count] = primary;
391 }
392 ++count;
393 });
394 return SparseRange<Value_, Index_>(count, my_needs_value ? vbuffer : NULL, my_needs_index ? index_buffer : NULL);
395 }
396
397private:
398 const ValueVectorStorage_& my_values;
399 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
400 bool my_needs_value, my_needs_index;
401};
402
403}
429template<
430 typename Value_,
431 typename Index_,
432 class ValueVectorStorage_ = std::vector<std::vector<Value_> >,
433 class IndexVectorStorage_ = std::vector<std::vector<Index_> >
434>
435class FragmentedSparseMatrix : public Matrix<Value_, Index_> {
436public:
450 FragmentedSparseMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool row_sparse, bool check = true) :
451 my_nrow(nrow), my_ncol(ncol), my_values(std::move(values)), my_indices(std::move(indices)), my_row_sparse(row_sparse)
452 {
453 if (check) {
454 if (my_values.size() != my_indices.size()) {
455 throw std::runtime_error("'values' and 'indices' should be of the same length");
456 }
457
458 if (my_row_sparse) {
459 if (my_indices.size() != static_cast<size_t>(my_nrow)) {
460 throw std::runtime_error("length of 'indices' should be equal to number of rows'");
461 }
462 } else {
463 if (my_indices.size() != static_cast<size_t>(my_ncol)) {
464 throw std::runtime_error("length of 'indices' should be equal to number of columns");
465 }
466 }
467
468 ElementType<ElementType<IndexVectorStorage_> > max_index = (my_row_sparse ? my_ncol : my_nrow);
469 for (size_t i = 0, end = my_indices.size(); i < end; ++i) {
470 const auto& curv = my_values[i];
471 const auto& curi = my_indices[i];
472 if (curv.size() != curi.size()) {
473 throw std::runtime_error("corresponding elements of 'values' and 'indices' should have the same length");
474 }
475
476 for (auto x : curi) {
477 if (x < 0 || x >= max_index) {
478 throw std::runtime_error("'indices' should contain non-negative integers less than the number of " + (my_row_sparse ? std::string("columns") : std::string("rows")));
479 }
480 }
481
482 for (size_t j = 1, jend = curi.size(); j < jend; ++j) {
483 if (curi[j] <= curi[j - 1]) {
484 throw std::runtime_error("my_indices should be strictly increasing within each element of 'indices'");
485 }
486 }
487 }
488 }
489 }
490
491private:
492 Index_ my_nrow, my_ncol;
493 ValueVectorStorage_ my_values;
494 IndexVectorStorage_ my_indices;
495 bool my_row_sparse;
496
497public:
498 Index_ nrow() const { return my_nrow; }
499
500 Index_ ncol() const { return my_ncol; }
501
502 bool is_sparse() const { return true; }
503
504 double is_sparse_proportion() const { return 1; }
505
506 bool prefer_rows() const { return my_row_sparse; }
507
508 double prefer_rows_proportion() const { return static_cast<double>(my_row_sparse); }
509
510 bool uses_oracle(bool) const { return false; }
511
512 using Matrix<Value_, Index_>::dense;
513
514 using Matrix<Value_, Index_>::sparse;
515
516private:
517 Index_ secondary() const {
518 if (my_row_sparse) {
519 return my_ncol;
520 } else {
521 return my_nrow;
522 }
523 }
524
525 /*****************************
526 ******* Dense myopic ********
527 *****************************/
528private:
529 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, const Options&) const {
530 if (my_row_sparse == row) {
531 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicFullDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
532 my_values, my_indices, secondary()
533 );
534 } else {
535 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicFullDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
536 my_values, my_indices, secondary()
537 );
538 }
539 }
540
541 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, Index_ block_start, Index_ block_end, const Options&) const {
542 if (my_row_sparse == row) {
543 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicBlockDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
544 my_values, my_indices, secondary(), block_start, block_end
545 );
546 } else {
547 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicBlockDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
548 my_values, my_indices, secondary(), block_start, block_end
549 );
550 }
551 }
552
553 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(bool row, VectorPtr<Index_> subset_ptr, const Options&) const {
554 if (my_row_sparse == row) {
555 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicIndexDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
556 my_values, my_indices, secondary(), std::move(subset_ptr)
557 );
558 } else {
559 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicIndexDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
560 my_values, my_indices, secondary(), std::move(subset_ptr)
561 );
562 }
563 }
564
565 /******************************
566 ******* Sparse myopic ********
567 ******************************/
568private:
569 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, const Options& opt) const {
570 if (my_row_sparse == row) {
571 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicFullSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
572 my_values, my_indices, secondary(), opt
573 );
574 } else {
575 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicFullSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
576 my_values, my_indices, secondary(), opt
577 );
578 }
579 }
580
581 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, Index_ block_start, Index_ block_end, const Options& opt) const {
582 if (my_row_sparse == row) {
583 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicBlockSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
584 my_values, my_indices, secondary(), block_start, block_end, opt
585 );
586 } else {
587 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicBlockSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
588 my_values, my_indices, secondary(), block_start, block_end, opt
589 );
590 }
591 }
592
593 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(bool row, VectorPtr<Index_> subset_ptr, const Options& opt) const {
594 if (my_row_sparse == row) {
595 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicIndexSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
596 my_values, my_indices, secondary(), std::move(subset_ptr), opt
597 );
598 } else {
599 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicIndexSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
600 my_values, my_indices, secondary(), std::move(subset_ptr), opt
601 );
602 }
603 }
604
605 /*******************************
606 ******* Dense oracular ********
607 *******************************/
608public:
609 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
610 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
611 }
612
613 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end, const Options& opt) const {
614 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_end, opt));
615 }
616
617 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> subset_ptr, const Options& opt) const {
618 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(subset_ptr), opt));
619 }
620
621 /********************************
622 ******* Sparse oracular ********
623 ********************************/
624public:
625 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, const Options& opt) const {
626 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
627 }
628
629 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, Index_ block_start, Index_ block_end, const Options& opt) const {
630 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_end, opt));
631 }
632
633 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(bool row, std::shared_ptr<const Oracle<Index_> > oracle, VectorPtr<Index_> subset_ptr, const Options& opt) const {
634 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(subset_ptr), opt));
635 }
636};
637
643template<typename Value_, typename Index_, class ValueVectorStorage_ = std::vector<std::vector<Value_> >, class IndexVectorStorage_ = std::vector<std::vector<Index_> > >
644class FragmentedSparseColumnMatrix : public FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> {
645public:
653 FragmentedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check = true) :
654 FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_>(nrow, ncol, std::move(values), std::move(indices), false, check) {}
655};
656
662template<typename Value_, typename Index_, class ValueVectorStorage_ = std::vector<std::vector<Value_> >, class IndexVectorStorage_ = std::vector<std::vector<Index_> > >
663class FragmentedSparseRowMatrix : public FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> {
664public:
672 FragmentedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check = true) :
673 FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_>(nrow, ncol, std::move(values), std::move(indices), true, check) {}
674};
675
676
677}
678
679#endif
Fragmented sparse column matrix.
Definition FragmentedSparseMatrix.hpp:644
FragmentedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check=true)
Definition FragmentedSparseMatrix.hpp:653
Fragmented sparse matrix representation.
Definition FragmentedSparseMatrix.hpp:435
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:609
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > subset_ptr, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:633
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:625
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:629
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, Index_ block_start, Index_ block_end, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:613
Index_ ncol() const
Definition FragmentedSparseMatrix.hpp:500
double prefer_rows_proportion() const
Definition FragmentedSparseMatrix.hpp:508
bool uses_oracle(bool) const
Definition FragmentedSparseMatrix.hpp:510
double is_sparse_proportion() const
Definition FragmentedSparseMatrix.hpp:504
Index_ nrow() const
Definition FragmentedSparseMatrix.hpp:498
bool is_sparse() const
Definition FragmentedSparseMatrix.hpp:502
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(bool row, std::shared_ptr< const Oracle< Index_ > > oracle, VectorPtr< Index_ > subset_ptr, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:617
FragmentedSparseMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool row_sparse, bool check=true)
Definition FragmentedSparseMatrix.hpp:450
bool prefer_rows() const
Definition FragmentedSparseMatrix.hpp:506
Fragmented sparse row matrix.
Definition FragmentedSparseMatrix.hpp:663
FragmentedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check=true)
Definition FragmentedSparseMatrix.hpp:672
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::remove_cv< typename std::remove_reference< decltype(std::declval< Array_ >()[0])>::type >::type ElementType
Definition ElementType.hpp:17
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