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 "../utils/copy.hpp"
8
9#include "primary_extraction.hpp"
10#include "secondary_extraction.hpp"
11
12#include <vector>
13#include <algorithm>
14#include <memory>
15#include <utility>
16#include <stdexcept>
17#include <cstddef>
18
19#include "sanisizer/sanisizer.hpp"
20
27namespace tatami {
28
32namespace FragmentedSparseMatrix_internal {
33
34/********************
35 *** Primary full ***
36 ********************/
37
38template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
39class PrimaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
40public:
41 PrimaryMyopicFullDense(const ValueVectorStorage_& values, const IndexVectorStorage_& indices, const Index_ secondary) :
42 my_values(values), my_indices(indices), my_secondary(secondary) {}
43
44 const Value_* fetch(const Index_ i, Value_* const buffer) {
45 const auto& curv = my_values[i];
46 const auto& curi = my_indices[i];
47
48 std::fill_n(buffer, my_secondary, static_cast<Value_>(0));
49 const auto curnnz = curv.size();
50 for (I<decltype(curnnz)> x = 0; x < curnnz; ++x) {
51 buffer[curi[x]] = curv[x];
52 }
53 return buffer;
54 }
55
56private:
57 const ValueVectorStorage_& my_values;
58 const IndexVectorStorage_& my_indices;
59 Index_ my_secondary;
60};
61
62template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
63class PrimaryMyopicFullSparse final : public MyopicSparseExtractor<Value_, Index_> {
64public:
65 PrimaryMyopicFullSparse(
66 const ValueVectorStorage_& values,
67 const IndexVectorStorage_& indices,
68 [[maybe_unused]] const Index_ secondary,
69 const Options& opt
70 ) :
71 my_values(values),
72 my_indices(indices),
73#ifdef TATAMI_DEBUG_FORCE_COPY
74 my_secondary(secondary),
75#endif
76 my_needs_value(opt.sparse_extract_value),
77 my_needs_index(opt.sparse_extract_index)
78 {}
79
80 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
81 const auto& curv = my_values[i];
82 const auto& curi = my_indices[i];
83
84 SparseRange<Value_, Index_> output(curv.size(), NULL, NULL);
85 if (my_needs_value) {
86 const auto curnnz = curv.size();
87 output.value = sparse_utils::extract_primary_vector(curv, static_cast<I<decltype(curnnz)> >(0), curnnz, value_buffer);
88 }
89 if (my_needs_index) {
90 const auto curnnz = curi.size();
91 output.index = sparse_utils::extract_primary_vector(curi, static_cast<I<decltype(curnnz)> >(0), curnnz, index_buffer);
92 }
93
94#ifdef TATAMI_DEBUG_FORCE_COPY
95 sparse_utils::forcibly_populate_buffers(output, my_secondary, value_buffer, index_buffer);
96#endif
97 return output;
98 }
99
100private:
101 const ValueVectorStorage_& my_values;
102 const IndexVectorStorage_& my_indices;
103#ifdef TATAMI_DEBUG_FORCE_COPY
104 Index_ my_secondary;
105#endif
106 bool my_needs_value, my_needs_index;
107};
108
109/*********************
110 *** Primary block ***
111 *********************/
112
113template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
114class PrimaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
115public:
116 PrimaryMyopicBlockDense(
117 const ValueVectorStorage_& values,
118 const IndexVectorStorage_& indices,
119 const Index_ secondary,
120 const Index_ block_start,
121 const Index_ block_length
122 ) :
123 my_values(values),
124 my_indices(indices),
125 my_block_start(block_start),
126 my_block_length(block_length),
127 my_block_end(block_start + block_length),
128 my_search_block_end(my_block_end < secondary)
129 {}
130
131 const Value_* fetch(const Index_ i, Value_* const buffer) {
132 const auto& curi = my_indices[i];
133 const auto& curv = my_values[i];
134
135 auto iStart = curi.begin();
136 auto iEnd = curi.end();
137 sparse_utils::refine_primary_limits(iStart, iEnd, my_block_start, my_search_block_end, my_block_end);
138 const auto start_pos = (iStart - curi.begin());
139 const auto end_pos = (iEnd - curi.begin());
140
141 std::fill_n(buffer, my_block_length, static_cast<Value_>(0));
142 for (auto x = start_pos; x < end_pos; ++x) {
143 buffer[curi[x] - my_block_start] = curv[x];
144 }
145 return buffer;
146 }
147
148private:
149 const ValueVectorStorage_& my_values;
150 const IndexVectorStorage_& my_indices;
151 Index_ my_block_start, my_block_length, my_block_end;
152 bool my_search_block_end;
153};
154
155template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
156class PrimaryMyopicBlockSparse final : public MyopicSparseExtractor<Value_, Index_> {
157public:
158 PrimaryMyopicBlockSparse(
159 const ValueVectorStorage_& values,
160 const IndexVectorStorage_& indices,
161 const Index_ secondary,
162 const Index_ block_start,
163 const Index_ block_length,
164 const Options& opt
165 ) :
166 my_values(values),
167 my_indices(indices),
168 my_block_start(block_start),
169 my_block_end(block_start + block_length),
170#ifdef TATAMI_DEBUG_FORCE_COPY
171 my_block_length(block_length),
172#endif
173 my_search_block_end(my_block_end < secondary),
174 my_needs_value(opt.sparse_extract_value),
175 my_needs_index(opt.sparse_extract_index)
176 {}
177
178 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
179 const auto& curi = my_indices[i];
180 auto iStart = curi.begin();
181 auto iEnd = curi.end();
182 sparse_utils::refine_primary_limits(iStart, iEnd, my_block_start, my_search_block_end, my_block_end);
183 const auto offset = iStart - curi.begin();
184 const auto delta = iEnd - iStart;
185
186 SparseRange<Value_, Index_> output(delta, NULL, NULL);
187 if (my_needs_value) {
188 output.value = sparse_utils::extract_primary_vector(my_values[i], offset, delta, value_buffer);
189 }
190 if (my_needs_index) {
191 output.index = sparse_utils::extract_primary_vector(curi, offset, delta, index_buffer);
192 }
193
194#ifdef TATAMI_DEBUG_FORCE_COPY
195 sparse_utils::forcibly_populate_buffers(output, my_block_length, value_buffer, index_buffer);
196#endif
197 return output;
198 }
199
200private:
201 const ValueVectorStorage_& my_values;
202 const IndexVectorStorage_& my_indices;
203 Index_ my_block_start, my_block_end;
204#ifdef TATAMI_DEBUG_FORCE_COPY
205 Index_ my_block_length;
206#endif
207 bool my_search_block_end;
208 bool my_needs_value, my_needs_index;
209};
210
211/***********************
212 *** Primary indexed ***
213 ***********************/
214
215template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
216class PrimaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
217public:
218 PrimaryMyopicIndexDense(
219 const ValueVectorStorage_& values,
220 const IndexVectorStorage_& indices,
221 const Index_ secondary,
222 VectorPtr<Index_> indices_ptr
223 ) :
224 my_values(values),
225 my_indices(indices),
226 my_retriever(*indices_ptr, secondary),
227 my_num_indices(indices_ptr->size())
228 {}
229
230 const Value_* fetch(const Index_ i, Value_* const buffer) {
231 const auto& curi = my_indices[i];
232 const auto& curv = my_values[i];
233 std::fill_n(buffer, my_num_indices, static_cast<Value_>(0));
234 my_retriever.populate(
235 curi.begin(),
236 curi.end(),
237 [&](const auto s, const auto offset) -> void {
238 buffer[s] = curv[offset];
239 }
240 );
241 return buffer;
242 }
243
244private:
245 const ValueVectorStorage_& my_values;
246 const IndexVectorStorage_& my_indices;
247 sparse_utils::RetrievePrimarySubsetDense<Index_> my_retriever;
248 std::size_t my_num_indices;
249};
250
251template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
252class PrimaryMyopicIndexSparse final : public MyopicSparseExtractor<Value_, Index_> {
253public:
254 PrimaryMyopicIndexSparse(
255 const ValueVectorStorage_& values,
256 const IndexVectorStorage_& indices,
257 const Index_ secondary,
258 VectorPtr<Index_> indices_ptr,
259 const Options& opt
260 ) :
261 my_values(values),
262 my_indices(indices),
263 my_retriever(*indices_ptr, secondary),
264#ifdef TATAMI_DEBUG_FORCE_COPY
265 my_length(indices_ptr->size()),
266#endif
267 my_needs_value(opt.sparse_extract_value),
268 my_needs_index(opt.sparse_extract_index)
269 {}
270
271 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
272 const auto& curi = my_indices[i];
273 const auto& curv = my_values[i];
274 Index_ count = 0;
275 auto vcopy = value_buffer;
276 auto icopy = index_buffer;
277
278 my_retriever.populate(
279 curi.begin(),
280 curi.end(),
281 [&](const auto offset, const auto ix) -> void {
282 ++count;
283 if (my_needs_value) {
284 *vcopy = curv[offset];
285 ++vcopy;
286 }
287 if (my_needs_index) {
288 *icopy = ix;
289 ++icopy;
290 }
291 }
292 );
293
294 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
295#ifdef TATAMI_DEBUG_FORCE_COPY
296 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
297#endif
298 return output;
299 }
300
301private:
302 const ValueVectorStorage_& my_values;
303 const IndexVectorStorage_& my_indices;
304 sparse_utils::RetrievePrimarySubsetSparse<Index_> my_retriever;
305#ifdef TATAMI_DEBUG_FORCE_COPY
306 Index_ my_length;
307#endif
308 bool my_needs_value, my_needs_index;
309};
310
311/**********************
312 *** Secondary full ***
313 **********************/
314
315template<typename Index_, class IndexVectorStorage_>
316class ServeIndices {
317public:
318 ServeIndices(const IndexVectorStorage_& indices) : my_indices(indices) {}
319
320private:
321 const IndexVectorStorage_& my_indices;
322
323public:
324 typedef I<decltype(std::declval<IndexVectorStorage_>()[0].size())> Pointer;
325
326 Pointer start_offset(const Index_) const {
327 return 0;
328 }
329
330 Pointer end_offset(const Index_ primary) const {
331 return my_indices[primary].size();
332 }
333
334 auto raw(const Index_ primary) const {
335 return my_indices[primary].begin();
336 }
337};
338
339template<typename Index_, class IndexVectorStorage_>
340auto make_ServeIndices(const IndexVectorStorage_& i) {
341 return ServeIndices<Index_, IndexVectorStorage_>(i);
342}
343
344template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
345class SecondaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
346public:
347 SecondaryMyopicFullDense(
348 const ValueVectorStorage_& values,
349 const IndexVectorStorage_& indices,
350 Index_ secondary
351 ) :
352 my_values(values),
353 my_cache(make_ServeIndices<Index_>(indices), secondary, indices.size())
354 {}
355
356 const Value_* fetch(const Index_ i, Value_* const buffer) {
357 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
358 my_cache.search(
359 i,
360 [&](Index_ primary, Index_ index_primary, auto ptr) -> void {
361 buffer[index_primary] = my_values[primary][ptr];
362 }
363 );
364 return buffer;
365 }
366
367private:
368 const ValueVectorStorage_& my_values;
369 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
370};
371
372template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
373class SecondaryMyopicFullSparse final : public MyopicSparseExtractor<Value_, Index_> {
374public:
375 SecondaryMyopicFullSparse(
376 const ValueVectorStorage_& values,
377 const IndexVectorStorage_& indices,
378 const Index_ secondary,
379 const Options& opt
380 ) :
381 my_values(values),
382 my_cache(make_ServeIndices<Index_>(indices), secondary, indices.size()),
383 my_needs_value(opt.sparse_extract_value),
384 my_needs_index(opt.sparse_extract_index)
385 {}
386
387 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
388 Index_ count = 0;
389 my_cache.search(
390 i,
391 [&](const Index_ primary, const Index_, const auto ptr) -> void {
392 if (my_needs_value) {
393 value_buffer[count] = my_values[primary][ptr];
394 }
395 if (my_needs_index) {
396 index_buffer[count] = primary;
397 }
398 ++count;
399 }
400 );
401
402 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
403#ifdef TATAMI_DEBUG_FORCE_COPY
404 sparse_utils::forcibly_populate_buffers(output, static_cast<Index_>(my_values.size()), value_buffer, index_buffer);
405#endif
406 return output;
407 }
408
409private:
410 const ValueVectorStorage_& my_values;
411 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
412 bool my_needs_value, my_needs_index;
413};
414
415/***********************
416 *** Secondary block ***
417 ***********************/
418
419template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
420class SecondaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
421public:
422 SecondaryMyopicBlockDense(
423 const ValueVectorStorage_& values,
424 const IndexVectorStorage_& indices,
425 const Index_ secondary,
426 const Index_ block_start,
427 const Index_ block_length
428 ) :
429 my_values(values),
430 my_cache(make_ServeIndices<Index_>(indices), secondary, block_start, block_length)
431 {}
432
433 const Value_* fetch(const Index_ i, Value_* const buffer) {
434 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
435 my_cache.search(
436 i,
437 [&](const Index_ primary, const Index_ index_primary, const auto ptr) -> void {
438 buffer[index_primary] = my_values[primary][ptr];
439 }
440 );
441 return buffer;
442 }
443
444private:
445 const ValueVectorStorage_& my_values;
446 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
447};
448
449template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
450class SecondaryMyopicBlockSparse final : public MyopicSparseExtractor<Value_, Index_> {
451public:
452 SecondaryMyopicBlockSparse(
453 const ValueVectorStorage_& values,
454 const IndexVectorStorage_& indices,
455 const Index_ secondary,
456 const Index_ block_start,
457 const Index_ block_length,
458 const Options& opt
459 ) :
460 my_values(values),
461 my_cache(make_ServeIndices<Index_>(indices), secondary, block_start, block_length),
462#ifdef TATAMI_DEBUG_FORCE_COPY
463 my_length(block_length),
464#endif
465 my_needs_value(opt.sparse_extract_value),
466 my_needs_index(opt.sparse_extract_index)
467 {}
468
469 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
470 Index_ count = 0;
471 my_cache.search(
472 i,
473 [&](const Index_ primary, const Index_, const auto ptr) -> void {
474 if (my_needs_value) {
475 value_buffer[count] = my_values[primary][ptr];
476 }
477 if (my_needs_index) {
478 index_buffer[count] = primary;
479 }
480 ++count;
481 }
482 );
483
484 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
485#ifdef TATAMI_DEBUG_FORCE_COPY
486 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
487#endif
488 return output;
489 }
490
491private:
492 const ValueVectorStorage_& my_values;
493 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
494#ifdef TATAMI_DEBUG_FORCE_COPY
495 Index_ my_length;
496#endif
497 bool my_needs_value, my_needs_index;
498};
499
500/***********************
501 *** Secondary index ***
502 ***********************/
503
504template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
505class SecondaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
506public:
507 SecondaryMyopicIndexDense(
508 const ValueVectorStorage_& values,
509 const IndexVectorStorage_& indices,
510 const Index_ secondary,
511 VectorPtr<Index_> indices_ptr
512 ) :
513 my_values(values),
514 my_cache(make_ServeIndices<Index_>(indices), secondary, std::move(indices_ptr))
515 {}
516
517 const Value_* fetch(const Index_ i, Value_* const buffer) {
518 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
519 my_cache.search(
520 i,
521 [&](Index_ primary, Index_ index_primary, auto ptr) -> void {
522 buffer[index_primary] = my_values[primary][ptr];
523 }
524 );
525 return buffer;
526 }
527
528private:
529 const ValueVectorStorage_& my_values;
530 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
531};
532
533template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
534class SecondaryMyopicIndexSparse final : public MyopicSparseExtractor<Value_, Index_> {
535public:
536 SecondaryMyopicIndexSparse(
537 const ValueVectorStorage_& values,
538 const IndexVectorStorage_& indices,
539 const Index_ secondary,
540 VectorPtr<Index_> indices_ptr,
541 const Options& opt
542 ) :
543 my_values(values),
544#ifdef TATAMI_DEBUG_FORCE_COPY
545 my_length(indices_ptr->size()),
546#endif
547 my_cache(make_ServeIndices<Index_>(indices), secondary, std::move(indices_ptr)),
548 my_needs_value(opt.sparse_extract_value),
549 my_needs_index(opt.sparse_extract_index)
550 {}
551
552 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
553 Index_ count = 0;
554 my_cache.search(
555 i,
556 [&](const Index_ primary, const Index_, const auto ptr) -> void {
557 if (my_needs_value) {
558 value_buffer[count] = my_values[primary][ptr];
559 }
560 if (my_needs_index) {
561 index_buffer[count] = primary;
562 }
563 ++count;
564 }
565 );
566
567 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
568#ifdef TATAMI_DEBUG_FORCE_COPY
569 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
570#endif
571 return output;
572 }
573
574private:
575 const ValueVectorStorage_& my_values;
576#ifdef TATAMI_DEBUG_FORCE_COPY
577 Index_ my_length;
578#endif
579 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexVectorStorage_> > my_cache;
580 bool my_needs_value, my_needs_index;
581};
582
583}
602 bool check = true;
603};
604
626template<typename Value_, typename Index_, class ValueVectorStorage_, class IndexVectorStorage_>
627class FragmentedSparseMatrix : public Matrix<Value_, Index_> {
628public:
639 const Index_ nrow,
640 const Index_ ncol,
641 ValueVectorStorage_ values,
642 IndexVectorStorage_ indices,
643 const bool row_sparse,
644 const FragmentedSparseMatrixOptions& options
645 ) :
646 my_nrow(nrow),
647 my_ncol(ncol),
648 my_values(std::move(values)),
649 my_indices(std::move(indices)),
650 my_row_sparse(row_sparse)
651 {
652 if (options.check) {
653 if (my_values.size() != my_indices.size()) {
654 throw std::runtime_error("'values' and 'indices' should be of the same length");
655 }
656
657 if (my_row_sparse) {
658 if (!sanisizer::is_equal(my_indices.size(), my_nrow)) {
659 throw std::runtime_error("length of 'indices' should be equal to number of rows'");
660 }
661 } else {
662 if (!sanisizer::is_equal(my_indices.size(), my_ncol)) {
663 throw std::runtime_error("length of 'indices' should be equal to number of columns");
664 }
665 }
666
667 const auto max_index = (my_row_sparse ? my_ncol : my_nrow);
668 const auto num_indices = my_indices.size();
669 for (I<decltype(num_indices)> i = 0; i < num_indices; ++i) {
670 const auto& curv = my_values[i];
671 const auto& curi = my_indices[i];
672 if (!sanisizer::is_equal(curv.size(), curi.size())) {
673 throw std::runtime_error("corresponding elements of 'values' and 'indices' should have the same length");
674 }
675
676 for (const auto x : curi) {
677 if (x < 0 || sanisizer::is_greater_than_or_equal(x, max_index)) {
678 throw std::runtime_error("'indices' should contain non-negative integers less than the number of " + (my_row_sparse ? std::string("columns") : std::string("rows")));
679 }
680 }
681
682 const auto curnnz = curi.size();
683 for (I<decltype(curnnz)> j = 1; j < curnnz; ++j) {
684 if (curi[j] <= curi[j - 1]) {
685 throw std::runtime_error("my_indices should be strictly increasing within each element of 'indices'");
686 }
687 }
688 }
689 }
690 }
691
695 // Back-compatibility only.
696 FragmentedSparseMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool row_sparse, bool check = true) :
697 FragmentedSparseMatrix(
698 nrow,
699 ncol,
700 std::move(values),
701 std::move(indices),
702 row_sparse,
703 [&]{
704 FragmentedSparseMatrixOptions fopt;
705 fopt.check = check;
706 return fopt;
707 }()
708 )
709 {}
714private:
715 Index_ my_nrow, my_ncol;
716 ValueVectorStorage_ my_values;
717 IndexVectorStorage_ my_indices;
718 bool my_row_sparse;
719
720public:
724 Index_ nrow() const { return my_nrow; }
725
729 Index_ ncol() const { return my_ncol; }
730
734 bool is_sparse() const { return true; }
735
739 double is_sparse_proportion() const { return 1; }
740
744 bool prefer_rows() const { return my_row_sparse; }
745
749 double prefer_rows_proportion() const { return static_cast<double>(my_row_sparse); }
750
754 bool uses_oracle(const bool) const { return false; }
755
756 using Matrix<Value_, Index_>::dense;
757
758 using Matrix<Value_, Index_>::sparse;
759
760private:
761 Index_ secondary() const {
762 if (my_row_sparse) {
763 return my_ncol;
764 } else {
765 return my_nrow;
766 }
767 }
768
769 /*****************************
770 ******* Dense myopic ********
771 *****************************/
772private:
776 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
777 const bool row,
778 const Options&
779 ) const {
780 if (my_row_sparse == row) {
781 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicFullDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
782 my_values, my_indices, secondary()
783 );
784 } else {
785 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicFullDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
786 my_values, my_indices, secondary()
787 );
788 }
789 }
790
794 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
795 const bool row,
796 const Index_ block_start,
797 const Index_ block_length,
798 const Options&
799 ) const {
800 if (my_row_sparse == row) {
801 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicBlockDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
802 my_values, my_indices, secondary(), block_start, block_length
803 );
804 } else {
805 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicBlockDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
806 my_values, my_indices, secondary(), block_start, block_length
807 );
808 }
809 }
810
814 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
815 const bool row,
816 VectorPtr<Index_> indices_ptr,
817 const Options&
818 ) const {
819 if (my_row_sparse == row) {
820 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicIndexDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
821 my_values, my_indices, secondary(), std::move(indices_ptr)
822 );
823 } else {
824 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicIndexDense<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
825 my_values, my_indices, secondary(), std::move(indices_ptr)
826 );
827 }
828 }
829
830 /******************************
831 ******* Sparse myopic ********
832 ******************************/
833private:
837 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
838 const bool row,
839 const Options& opt
840 ) const {
841 if (my_row_sparse == row) {
842 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicFullSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
843 my_values, my_indices, secondary(), opt
844 );
845 } else {
846 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicFullSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
847 my_values, my_indices, secondary(), opt
848 );
849 }
850 }
851
855 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
856 const bool row,
857 const Index_ block_start,
858 const Index_ block_length,
859 const Options& opt
860 ) const {
861 if (my_row_sparse == row) {
862 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicBlockSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
863 my_values, my_indices, secondary(), block_start, block_length, opt
864 );
865 } else {
866 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicBlockSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
867 my_values, my_indices, secondary(), block_start, block_length, opt
868 );
869 }
870 }
871
875 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
876 const bool row,
877 VectorPtr<Index_> indices_ptr,
878 const Options& opt
879 ) const {
880 if (my_row_sparse == row) {
881 return std::make_unique<FragmentedSparseMatrix_internal::PrimaryMyopicIndexSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
882 my_values, my_indices, secondary(), std::move(indices_ptr), opt
883 );
884 } else {
885 return std::make_unique<FragmentedSparseMatrix_internal::SecondaryMyopicIndexSparse<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> >(
886 my_values, my_indices, secondary(), std::move(indices_ptr), opt
887 );
888 }
889 }
890
891 /*******************************
892 ******* Dense oracular ********
893 *******************************/
894public:
898 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
899 const bool row,
900 std::shared_ptr<const Oracle<Index_> > oracle,
901 const Options& opt
902 ) const {
903 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
904 }
905
909 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
910 const bool row,
911 std::shared_ptr<const Oracle<Index_> > oracle,
912 const Index_ block_start,
913 const Index_ block_length,
914 const Options& opt
915 ) const {
916 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_length, opt));
917 }
918
922 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
923 const bool row,
924 std::shared_ptr<const Oracle<Index_> > oracle,
925 VectorPtr<Index_> indices_ptr,
926 const Options& opt
927 ) const {
928 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(indices_ptr), opt));
929 }
930
931 /********************************
932 ******* Sparse oracular ********
933 ********************************/
934public:
938 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
939 const bool row,
940 std::shared_ptr<const Oracle<Index_> > oracle,
941 const Options& opt
942 ) const {
943 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
944 }
945
949 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
950 const bool row,
951 std::shared_ptr<const Oracle<Index_> > oracle,
952 const Index_ block_start,
953 const Index_ block_length,
954 const Options& opt
955 ) const {
956 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_length, opt));
957 }
958
962 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
963 const bool row,
964 std::shared_ptr<const Oracle<Index_> > oracle,
965 VectorPtr<Index_> indices_ptr,
966 const Options& opt
967 ) const {
968 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(indices_ptr), opt));
969 }
970};
971
977template<typename Value_, typename Index_, class ValueVectorStorage_ = std::vector<std::vector<Value_> >, class IndexVectorStorage_ = std::vector<std::vector<Index_> > >
978class FragmentedSparseColumnMatrix final : public FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> {
979public:
987 FragmentedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check = true) :
988 FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_>(nrow, ncol, std::move(values), std::move(indices), false, check) {}
989};
990
996template<typename Value_, typename Index_, class ValueVectorStorage_ = std::vector<std::vector<Value_> >, class IndexVectorStorage_ = std::vector<std::vector<Index_> > >
997class FragmentedSparseRowMatrix final : public FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_> {
998public:
1006 FragmentedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check = true) :
1007 FragmentedSparseMatrix<Value_, Index_, ValueVectorStorage_, IndexVectorStorage_>(nrow, ncol, std::move(values), std::move(indices), true, check) {}
1008};
1009
1010
1011}
1012
1013#endif
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Mimic the oracle-aware extractor interface.
Fragmented sparse column matrix.
Definition FragmentedSparseMatrix.hpp:978
FragmentedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check=true)
Definition FragmentedSparseMatrix.hpp:987
Fragmented sparse matrix representation.
Definition FragmentedSparseMatrix.hpp:627
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 FragmentedSparseMatrix.hpp:962
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:938
FragmentedSparseMatrix(const Index_ nrow, const Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, const bool row_sparse, const FragmentedSparseMatrixOptions &options)
Definition FragmentedSparseMatrix.hpp:638
Index_ ncol() const
Definition FragmentedSparseMatrix.hpp:729
double prefer_rows_proportion() const
Definition FragmentedSparseMatrix.hpp:749
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 FragmentedSparseMatrix.hpp:922
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 FragmentedSparseMatrix.hpp:949
double is_sparse_proportion() const
Definition FragmentedSparseMatrix.hpp:739
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 FragmentedSparseMatrix.hpp:909
Index_ nrow() const
Definition FragmentedSparseMatrix.hpp:724
bool is_sparse() const
Definition FragmentedSparseMatrix.hpp:734
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition FragmentedSparseMatrix.hpp:898
bool prefer_rows() const
Definition FragmentedSparseMatrix.hpp:744
bool uses_oracle(const bool) const
Definition FragmentedSparseMatrix.hpp:754
Fragmented sparse row matrix.
Definition FragmentedSparseMatrix.hpp:997
FragmentedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueVectorStorage_ values, IndexVectorStorage_ indices, bool check=true)
Definition FragmentedSparseMatrix.hpp:1006
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.
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< const std::vector< Index_ > > VectorPtr
Definition Matrix.hpp:26
Options for FragmentedSparseMatrix().
Definition FragmentedSparseMatrix.hpp:591
bool check
Definition FragmentedSparseMatrix.hpp:602
Options for accessing data from a Matrix instance.
Definition Options.hpp:30