tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
CompressedSparseMatrix.hpp
Go to the documentation of this file.
1#ifndef TATAMI_COMPRESSED_SPARSE_MATRIX_H
2#define TATAMI_COMPRESSED_SPARSE_MATRIX_H
3
4#include <vector>
5#include <algorithm>
6#include <memory>
7#include <utility>
8#include <stdexcept>
9#include <cstddef>
10
11#include "sanisizer/sanisizer.hpp"
12
13#include "../base/Matrix.hpp"
14#include "../utils/copy.hpp"
16
17#include "primary_extraction.hpp"
18#include "secondary_extraction.hpp"
19
26namespace tatami {
27
31namespace CompressedSparseMatrix_internal {
32
33/********************
34 *** Primary full ***
35 ********************/
36
37template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
38class PrimaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
39public:
40 PrimaryMyopicFullDense(const ValueStorage_& values, const IndexStorage_& indices, const PointerStorage_& pointers, const Index_ secondary) :
41 my_values(values),
42 my_indices(indices),
43 my_pointers(pointers),
44 my_secondary(secondary)
45 {}
46
47 const Value_* fetch(const Index_ i, Value_* const buffer) {
48 const auto start_pos = my_pointers[i], end_pos = my_pointers[i+1];
49 std::fill_n(buffer, my_secondary, static_cast<Value_>(0));
50 for (auto x = start_pos; x < end_pos; ++x) {
51 buffer[my_indices[x]] = my_values[x];
52 }
53 return buffer;
54 }
55
56private:
57 const ValueStorage_& my_values;
58 const IndexStorage_& my_indices;
59 const PointerStorage_& my_pointers;
60 Index_ my_secondary;
61};
62
63template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
64class PrimaryMyopicFullSparse final : public MyopicSparseExtractor<Value_, Index_> {
65public:
66 PrimaryMyopicFullSparse(
67 const ValueStorage_& values,
68 const IndexStorage_& indices,
69 const PointerStorage_& pointers,
70 [[maybe_unused]] const Index_ secondary,
71 const Options& opt
72 ) :
73 my_values(values),
74 my_indices(indices),
75 my_pointers(pointers),
76#ifdef TATAMI_DEBUG_FORCE_COPY
77 my_secondary(secondary),
78#endif
79 my_needs_value(opt.sparse_extract_value),
80 my_needs_index(opt.sparse_extract_index)
81 {}
82
83 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
84 const auto offset = my_pointers[i];
85 I<decltype(offset)> delta = my_pointers[i+1] - offset;
86
87 SparseRange<Value_, Index_> output(delta, NULL, NULL);
88 if (my_needs_value) {
89 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
90 }
91 if (my_needs_index) {
92 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
93 }
94
95#ifdef TATAMI_DEBUG_FORCE_COPY
96 sparse_utils::forcibly_populate_buffers(output, my_secondary, value_buffer, index_buffer);
97#endif
98 return output;
99 }
100
101private:
102 const ValueStorage_& my_values;
103 const IndexStorage_& my_indices;
104 const PointerStorage_& my_pointers;
105#ifdef TATAMI_DEBUG_FORCE_COPY
106 Index_ my_secondary;
107#endif
108 bool my_needs_value, my_needs_index;
109};
110
111/*********************
112 *** Primary block ***
113 *********************/
114
115template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
116class PrimaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
117public:
118 PrimaryMyopicBlockDense(
119 const ValueStorage_& values,
120 const IndexStorage_& indices,
121 const PointerStorage_& pointers,
122 const Index_ secondary,
123 const Index_ block_start,
124 const Index_ block_length
125 ) :
126 my_values(values),
127 my_indices(indices),
128 my_pointers(pointers),
129 my_block_start(block_start),
130 my_block_length(block_length),
131 my_block_end(block_start + block_length),
132 my_search_block_end(my_block_end < secondary)
133 {}
134
135 const Value_* fetch(const Index_ i, Value_* const buffer) {
136 auto iStart = my_indices.begin() + my_pointers[i];
137 auto iEnd = my_indices.begin() + my_pointers[i + 1];
138 sparse_utils::refine_primary_limits(iStart, iEnd, my_block_start, my_search_block_end, my_block_end);
139 const auto start_pos = (iStart - my_indices.begin());
140 const auto end_pos = (iEnd - my_indices.begin());
141
142 std::fill_n(buffer, my_block_length, static_cast<Value_>(0));
143 for (auto x = start_pos; x < end_pos; ++x) {
144 buffer[my_indices[x] - my_block_start] = my_values[x];
145 }
146 return buffer;
147 }
148
149private:
150 const ValueStorage_& my_values;
151 const IndexStorage_& my_indices;
152 const PointerStorage_& my_pointers;
153 Index_ my_block_start, my_block_length, my_block_end;
154 bool my_search_block_end;
155};
156
157template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
158class PrimaryMyopicBlockSparse final : public MyopicSparseExtractor<Value_, Index_> {
159public:
160 PrimaryMyopicBlockSparse(
161 const ValueStorage_& values,
162 const IndexStorage_& indices,
163 const PointerStorage_& pointers,
164 const Index_ secondary,
165 const Index_ block_start,
166 const Index_ block_length,
167 const Options& opt
168 ) :
169 my_values(values),
170 my_indices(indices),
171 my_pointers(pointers),
172 my_block_start(block_start),
173 my_block_end(block_start + block_length),
174#ifdef TATAMI_DEBUG_FORCE_COPY
175 my_block_length(block_length),
176#endif
177 my_search_block_end(my_block_end < secondary),
178 my_needs_value(opt.sparse_extract_value),
179 my_needs_index(opt.sparse_extract_index)
180 {}
181
182 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
183 auto iStart = my_indices.begin() + my_pointers[i];
184 auto iEnd = my_indices.begin() + my_pointers[i + 1];
185 sparse_utils::refine_primary_limits(iStart, iEnd, my_block_start, my_search_block_end, my_block_end);
186 const auto offset = iStart - my_indices.begin();
187 const auto delta = iEnd - iStart;
188
189 SparseRange<Value_, Index_> output(delta, NULL, NULL);
190 if (my_needs_value) {
191 output.value = sparse_utils::extract_primary_vector(my_values, offset, delta, value_buffer);
192 }
193 if (my_needs_index) {
194 output.index = sparse_utils::extract_primary_vector(my_indices, offset, delta, index_buffer);
195 }
196
197#ifdef TATAMI_DEBUG_FORCE_COPY
198 sparse_utils::forcibly_populate_buffers(output, my_block_length, value_buffer, index_buffer);
199#endif
200 return output;
201 }
202
203private:
204 const ValueStorage_& my_values;
205 const IndexStorage_& my_indices;
206 const PointerStorage_& my_pointers;
207 Index_ my_block_start, my_block_end;
208#ifdef TATAMI_DEBUG_FORCE_COPY
209 Index_ my_block_length;
210#endif
211 bool my_search_block_end;
212 bool my_needs_value, my_needs_index;
213};
214
215/***********************
216 *** Primary indexed ***
217 ***********************/
218
219template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
220class PrimaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
221public:
222 PrimaryMyopicIndexDense(
223 const ValueStorage_& values,
224 const IndexStorage_& indices,
225 const PointerStorage_& pointers,
226 const Index_ secondary,
227 const VectorPtr<Index_>& indices_ptr
228 ) :
229 my_values(values),
230 my_indices(indices),
231 my_pointers(pointers),
232 my_retriever(*indices_ptr, secondary),
233 my_num_indices(indices_ptr->size())
234 {}
235
236 const Value_* fetch(const Index_ i, Value_* const buffer) {
237 std::fill_n(buffer, my_num_indices, static_cast<Value_>(0));
238 const auto vIt = my_values.begin() + my_pointers[i];
239 my_retriever.populate(
240 my_indices.begin() + my_pointers[i],
241 my_indices.begin() + my_pointers[i+1],
242 [&](const auto s, const auto offset) -> void {
243 buffer[s] = *(vIt + offset);
244 }
245 );
246 return buffer;
247 }
248
249private:
250 const ValueStorage_& my_values;
251 const IndexStorage_& my_indices;
252 const PointerStorage_& my_pointers;
253 sparse_utils::RetrievePrimarySubsetDense<Index_> my_retriever;
254 std::size_t my_num_indices;
255};
256
257template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
258class PrimaryMyopicIndexSparse final : public MyopicSparseExtractor<Value_, Index_> {
259public:
260 PrimaryMyopicIndexSparse(
261 const ValueStorage_& values,
262 const IndexStorage_& indices,
263 const PointerStorage_& pointers,
264 const Index_ secondary,
265 const VectorPtr<Index_>& indices_ptr,
266 const Options& opt
267 ) :
268 my_values(values),
269 my_indices(indices),
270 my_pointers(pointers),
271 my_retriever(*indices_ptr, secondary),
272#ifdef TATAMI_DEBUG_FORCE_COPY
273 my_length(indices_ptr->size()),
274#endif
275 my_needs_value(opt.sparse_extract_value),
276 my_needs_index(opt.sparse_extract_index)
277 {}
278
279 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
280 Index_ count = 0;
281 auto vcopy = value_buffer;
282 auto icopy = index_buffer;
283
284 auto vIt = my_values.begin() + my_pointers[i];
285 my_retriever.populate(
286 my_indices.begin() + my_pointers[i],
287 my_indices.begin() + my_pointers[i+1],
288 [&](const auto offset, const auto ix) -> void {
289 ++count;
290 if (my_needs_value) {
291 *vcopy = *(vIt + offset);
292 ++vcopy;
293 }
294 if (my_needs_index) {
295 *icopy = ix;
296 ++icopy;
297 }
298 }
299 );
300
301 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
302#ifdef TATAMI_DEBUG_FORCE_COPY
303 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
304#endif
305 return output;
306 }
307
308private:
309 const ValueStorage_& my_values;
310 const IndexStorage_& my_indices;
311 const PointerStorage_& my_pointers;
312 sparse_utils::RetrievePrimarySubsetSparse<Index_> my_retriever;
313#ifdef TATAMI_DEBUG_FORCE_COPY
314 Index_ my_length;
315#endif
316 bool my_needs_value, my_needs_index;
317};
318
319/**********************
320 *** Secondary full ***
321 **********************/
322
323template<class PointerStorage_>
324using Element = I<decltype(std::declval<PointerStorage_>()[0])>;
325
326template<typename Index_, class IndexStorage_, class PointerStorage_>
327class ServeIndices {
328public:
329 ServeIndices(const IndexStorage_& i, const PointerStorage_& p) : my_indices(i), my_pointers(p) {}
330
331private:
332 const IndexStorage_& my_indices;
333 const PointerStorage_& my_pointers;
334
335public:
336 typedef Element<PointerStorage_> Pointer;
337
338 Pointer start_offset(const Index_ primary) const {
339 return my_pointers[primary];
340 }
341
342 Pointer end_offset(const Index_ primary) const {
343 return my_pointers[primary + 1];
344 }
345
346 auto raw(const Index_) const {
347 return my_indices.begin();
348 }
349};
350
351template<typename Index_, class IndexStorage_, class PointerStorage_>
352auto make_ServeIndices(const IndexStorage_& i, const PointerStorage_& p) {
353 return ServeIndices<Index_, IndexStorage_, PointerStorage_>(i, p);
354}
355
356template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
357class SecondaryMyopicFullDense final : public MyopicDenseExtractor<Value_, Index_> {
358public:
359 SecondaryMyopicFullDense(
360 const ValueStorage_& values,
361 const IndexStorage_& indices,
362 const PointerStorage_& pointers,
363 const Index_ secondary
364 ) :
365 my_values(values),
366 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, pointers.size() - 1)
367 {}
368
369 const Value_* fetch(const Index_ i, Value_* const buffer) {
370 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
371 my_cache.search(
372 i,
373 [&](const Index_, const Index_ index_primary, const auto ptr) -> void {
374 buffer[index_primary] = my_values[ptr];
375 }
376 );
377 return buffer;
378 }
379
380private:
381 const ValueStorage_& my_values;
382 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
383};
384
385template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
386class SecondaryMyopicFullSparse final : public MyopicSparseExtractor<Value_, Index_> {
387public:
388 SecondaryMyopicFullSparse(
389 const ValueStorage_& values,
390 const IndexStorage_& indices,
391 const PointerStorage_& pointers,
392 const Index_ secondary,
393 const Options& opt
394 ) :
395 my_values(values),
396 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, pointers.size() - 1),
397#ifdef TATAMI_DEBUG_FORCE_COPY
398 my_length(pointers.size() - 1),
399#endif
400 my_needs_value(opt.sparse_extract_value),
401 my_needs_index(opt.sparse_extract_index)
402 {}
403
404 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
405 Index_ count = 0;
406 my_cache.search(i, [&](const Index_ primary, Index_, const Element<PointerStorage_> ptr) -> void {
407 if (my_needs_value) {
408 value_buffer[count] = my_values[ptr];
409 }
410 if (my_needs_index) {
411 index_buffer[count] = primary;
412 }
413 ++count;
414 });
415
416 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
417#ifdef TATAMI_DEBUG_FORCE_COPY
418 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
419#endif
420 return output;
421 }
422
423private:
424 const ValueStorage_& my_values;
425 sparse_utils::FullSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
426#ifdef TATAMI_DEBUG_FORCE_COPY
427 Index_ my_length;
428#endif
429 bool my_needs_value, my_needs_index;
430};
431
432/***********************
433 *** Secondary block ***
434 ***********************/
435
436template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
437class SecondaryMyopicBlockDense final : public MyopicDenseExtractor<Value_, Index_> {
438public:
439 SecondaryMyopicBlockDense(
440 const ValueStorage_& values,
441 const IndexStorage_& indices,
442 const PointerStorage_& pointers,
443 const Index_ secondary,
444 const Index_ block_start,
445 const Index_ block_length
446 ) :
447 my_values(values),
448 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, block_start, block_length)
449 {}
450
451 const Value_* fetch(const Index_ i, Value_* const buffer) {
452 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
453 my_cache.search(
454 i,
455 [&](const Index_, const Index_ index_primary, const auto ptr) -> void {
456 buffer[index_primary] = my_values[ptr];
457 }
458 );
459 return buffer;
460 }
461
462private:
463 const ValueStorage_& my_values;
464 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
465};
466
467template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
468class SecondaryMyopicBlockSparse final : public MyopicSparseExtractor<Value_, Index_> {
469public:
470 SecondaryMyopicBlockSparse(
471 const ValueStorage_& values,
472 const IndexStorage_& indices,
473 const PointerStorage_& pointers,
474 const Index_ secondary,
475 const Index_ block_start,
476 const Index_ block_length,
477 const Options& opt
478 ) :
479 my_values(values),
480 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, block_start, block_length),
481#ifdef TATAMI_DEBUG_FORCE_COPY
482 my_length(block_length),
483#endif
484 my_needs_value(opt.sparse_extract_value),
485 my_needs_index(opt.sparse_extract_index)
486 {}
487
488 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
489 Index_ count = 0;
490 my_cache.search(
491 i,
492 [&](Index_ primary, Index_, auto ptr) -> void {
493 if (my_needs_value) {
494 value_buffer[count] = my_values[ptr];
495 }
496 if (my_needs_index) {
497 index_buffer[count] = primary;
498 }
499 ++count;
500 }
501 );
502
503 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
504#ifdef TATAMI_DEBUG_FORCE_COPY
505 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
506#endif
507 return output;
508 }
509
510private:
511 const ValueStorage_& my_values;
512 sparse_utils::BlockSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
513#ifdef TATAMI_DEBUG_FORCE_COPY
514 Index_ my_length;
515#endif
516 bool my_needs_value, my_needs_index;
517};
518
519/***********************
520 *** Secondary index ***
521 ***********************/
522
523template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
524class SecondaryMyopicIndexDense final : public MyopicDenseExtractor<Value_, Index_> {
525public:
526 SecondaryMyopicIndexDense(
527 const ValueStorage_& values,
528 const IndexStorage_& indices,
529 const PointerStorage_& pointers,
530 const Index_ secondary,
531 VectorPtr<Index_> sub_ptr
532 ) :
533 my_values(values),
534 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, std::move(sub_ptr))
535 {}
536
537 const Value_* fetch(const Index_ i, Value_* const buffer) {
538 std::fill_n(buffer, my_cache.size(), static_cast<Value_>(0));
539 my_cache.search(
540 i,
541 [&](const Index_, const Index_ index_primary, const Element<PointerStorage_> ptr) -> void {
542 buffer[index_primary] = my_values[ptr];
543 }
544 );
545 return buffer;
546 }
547
548private:
549 const ValueStorage_& my_values;
550 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
551};
552
553template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
554class SecondaryMyopicIndexSparse final : public MyopicSparseExtractor<Value_, Index_> {
555public:
556 SecondaryMyopicIndexSparse(
557 const ValueStorage_& values,
558 const IndexStorage_& indices,
559 const PointerStorage_& pointers,
560 const Index_ secondary,
561 VectorPtr<Index_> indices_ptr,
562 const Options& opt
563 ) :
564 my_values(values),
565#ifdef TATAMI_DEBUG_FORCE_COPY
566 my_length(indices_ptr->size()),
567#endif
568 my_cache(make_ServeIndices<Index_>(indices, pointers), secondary, std::move(indices_ptr)),
569 my_needs_value(opt.sparse_extract_value),
570 my_needs_index(opt.sparse_extract_index)
571 {}
572
573 SparseRange<Value_, Index_> fetch(const Index_ i, Value_* const value_buffer, Index_* const index_buffer) {
574 Index_ count = 0;
575 my_cache.search(
576 i,
577 [&](Index_ primary, Index_, Element<PointerStorage_> ptr) -> void {
578 if (my_needs_value) {
579 value_buffer[count] = my_values[ptr];
580 }
581 if (my_needs_index) {
582 index_buffer[count] = primary;
583 }
584 ++count;
585 }
586 );
587
588 SparseRange<Value_, Index_> output(count, my_needs_value ? value_buffer : NULL, my_needs_index ? index_buffer : NULL);
589#ifdef TATAMI_DEBUG_FORCE_COPY
590 sparse_utils::forcibly_populate_buffers(output, my_length, value_buffer, index_buffer);
591#endif
592 return output;
593 }
594
595private:
596 const ValueStorage_& my_values;
597#ifdef TATAMI_DEBUG_FORCE_COPY
598 Index_ my_length;
599#endif
600 sparse_utils::IndexSecondaryExtractionCache<Index_, ServeIndices<Index_, IndexStorage_, PointerStorage_> > my_cache;
601 bool my_needs_value, my_needs_index;
602};
603
604}
625 bool check = true;
626};
627
644template<typename Value_, typename Index_, class ValueStorage_, class IndexStorage_, class PointerStorage_>
645class CompressedSparseMatrix : public Matrix<Value_, Index_> {
646public:
657 const Index_ nrow,
658 const Index_ ncol,
659 ValueStorage_ values,
660 IndexStorage_ indices,
661 PointerStorage_ pointers,
662 const bool csr,
663 const CompressedSparseMatrixOptions& options
664 ) :
665 my_nrow(nrow),
666 my_ncol(ncol),
667 my_values(std::move(values)),
668 my_indices(std::move(indices)),
669 my_pointers(std::move(pointers)),
670 my_csr(csr)
671 {
672 if (options.check) {
673 const auto nnzero = my_values.size();
674 if (!sanisizer::is_equal(nnzero, my_indices.size())) {
675 throw std::runtime_error("'my_values' and 'my_indices' should be of the same length");
676 }
677
678 const auto npointers = my_pointers.size();
679 const auto check_pointers = [&](const auto dim) {
680 // subtracting 1 from npointers (once we know it's >= 1) instead of adding 1 to dim, as the latter might overflow.
681 return npointers >= 1 && sanisizer::is_equal(npointers - 1, dim);
682 };
683 if (my_csr) {
684 if (!check_pointers(my_nrow)) {
685 throw std::runtime_error("length of 'pointers' should be equal to 'nrow + 1'");
686 }
687 } else {
688 if (!check_pointers(my_ncol)){
689 throw std::runtime_error("length of 'pointers' should be equal to 'ncols + 1'");
690 }
691 }
692
693 if (my_pointers[0] != 0) {
694 throw std::runtime_error("first element of 'pointers' should be zero");
695 }
696 const auto last = my_pointers[npointers - 1]; // don't use back() as this is not guaranteed to be available for arbitrary PointerStorage_.
697 if (!sanisizer::is_equal(nnzero, last)) {
698 throw std::runtime_error("last element of 'pointers' should be equal to length of 'indices'");
699 }
700
701 const auto max_index = (my_csr ? my_ncol : my_nrow);
702 for (I<decltype(npointers)> i = 1; i < npointers; ++i) {
703 const auto start = my_pointers[i - 1], end = my_pointers[i];
704 if (start == end) {
705 continue;
706 } else if (end < start) {
707 throw std::runtime_error("'pointers' should be in non-decreasing order");
708 }
709
710 for (auto x = start; x < end; ++x) {
711 if (my_indices[x] < 0 || sanisizer::is_greater_than_or_equal(my_indices[x], max_index)) {
712 throw std::runtime_error("'indices' should contain non-negative integers less than the number of " + (my_csr ? std::string("columns") : std::string("rows")));
713 }
714 }
715
716 for (I<decltype(start)> j = start + 1; j < end; ++j) { // +1 is safe as we know start < end.
717 if (my_indices[j] <= my_indices[j - 1]) {
718 throw std::runtime_error("'indices' should be strictly increasing within each " + (my_csr ? std::string("row") : std::string("column")));
719 }
720 }
721 }
722 }
723 }
724
728 // For back-compatibility only
729 CompressedSparseMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool csr, bool check = true) :
730 CompressedSparseMatrix(
731 nrow,
732 ncol,
733 std::move(values),
734 std::move(indices),
735 std::move(pointers),
736 csr,
737 [&]{
738 CompressedSparseMatrixOptions options;
739 options.check = check;
740 return options;
741 }()
742 )
743 {}
748private:
749 Index_ my_nrow, my_ncol;
750 ValueStorage_ my_values;
751 IndexStorage_ my_indices;
752 PointerStorage_ my_pointers;
753 bool my_csr;
754
755public:
759 Index_ nrow() const { return my_nrow; }
760
764 Index_ ncol() const { return my_ncol; }
765
769 bool is_sparse() const { return true; }
770
774 double is_sparse_proportion() const { return 1; }
775
779 bool prefer_rows() const { return my_csr; }
780
784 double prefer_rows_proportion() const { return static_cast<double>(my_csr); }
785
789 bool uses_oracle(const bool) const { return false; }
790
791 using Matrix<Value_, Index_>::dense_row;
792
793 using Matrix<Value_, Index_>::dense_column;
794
795 using Matrix<Value_, Index_>::sparse_row;
796
797 using Matrix<Value_, Index_>::sparse_column;
798
799private:
800 Index_ secondary() const {
801 if (my_csr) {
802 return my_ncol;
803 } else {
804 return my_nrow;
805 }
806 }
807
808 /*****************************
809 ******* Dense myopic ********
810 *****************************/
811public:
815 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
816 const bool row,
817 const Options&
818 ) const {
819 if (my_csr == row) {
820 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
821 my_values, my_indices, my_pointers, secondary()
822 );
823 } else {
824 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
825 my_values, my_indices, my_pointers, secondary()
826 );
827 }
828 }
829
833 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
834 const bool row,
835 const Index_ block_start,
836 const Index_ block_length,
837 const Options&
838 ) const {
839 if (my_csr == row) {
840 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
841 my_values, my_indices, my_pointers, secondary(), block_start, block_length
842 );
843 } else {
844 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
845 my_values, my_indices, my_pointers, secondary(), block_start, block_length
846 );
847 }
848 }
849
853 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > dense(
854 const bool row,
855 VectorPtr<Index_> indices_ptr,
856 const Options&
857 ) const {
858 if (my_csr == row) {
859 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
860 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
861 );
862 } else {
863 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexDense<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
864 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr)
865 );
866 }
867 }
868
869 /******************************
870 ******* Sparse myopic ********
871 ******************************/
872public:
876 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
877 const bool row,
878 const Options& opt
879 ) const {
880 if (my_csr == row) {
881 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
882 my_values, my_indices, my_pointers, secondary(), opt
883 );
884 } else {
885 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicFullSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
886 my_values, my_indices, my_pointers, secondary(), opt
887 );
888 }
889 }
890
894 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
895 const bool row,
896 const Index_ block_start,
897 const Index_ block_length,
898 const Options& opt
899 ) const {
900 if (my_csr == row) {
901 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
902 my_values, my_indices, my_pointers, secondary(), block_start, block_length, opt
903 );
904 } else {
905 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicBlockSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
906 my_values, my_indices, my_pointers, secondary(), block_start, block_length, opt
907 );
908 }
909 }
910
914 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > sparse(
915 const bool row,
916 VectorPtr<Index_> indices_ptr,
917 const Options& opt
918 ) const {
919 if (my_csr == row) {
920 return std::make_unique<CompressedSparseMatrix_internal::PrimaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
921 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
922 );
923 } else {
924 return std::make_unique<CompressedSparseMatrix_internal::SecondaryMyopicIndexSparse<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> >(
925 my_values, my_indices, my_pointers, secondary(), std::move(indices_ptr), opt
926 );
927 }
928 }
929
930 /*******************************
931 ******* Dense oracular ********
932 *******************************/
933public:
937 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
938 const bool row,
939 std::shared_ptr<const Oracle<Index_> > oracle,
940 const Options& opt
941 ) const {
942 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, opt));
943 }
944
948 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
949 const bool row,
950 std::shared_ptr<const Oracle<Index_> > oracle,
951 const Index_ block_start,
952 const Index_ block_length,
953 const Options& opt
954 ) const {
955 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, block_start, block_length, opt));
956 }
957
961 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > dense(
962 const bool row,
963 std::shared_ptr<const Oracle<Index_> > oracle,
964 VectorPtr<Index_> indices_ptr,
965 const Options& opt
966 ) const {
967 return std::make_unique<PseudoOracularDenseExtractor<Value_, Index_> >(std::move(oracle), dense(row, std::move(indices_ptr), opt));
968 }
969
970 /********************************
971 ******* Sparse oracular ********
972 ********************************/
973public:
977 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
978 const bool row,
979 std::shared_ptr<const Oracle<Index_> > oracle,
980 const Options& opt
981 ) const {
982 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, opt));
983 }
984
988 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
989 const bool row,
990 std::shared_ptr<const Oracle<Index_> > oracle,
991 const Index_ block_start,
992 const Index_ block_length,
993 const Options& opt
994 ) const {
995 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, block_start, block_length, opt));
996 }
997
1001 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > sparse(
1002 const bool row,
1003 std::shared_ptr<const Oracle<Index_> > oracle,
1004 VectorPtr<Index_> indices_ptr,
1005 const Options& opt
1006 ) const {
1007 return std::make_unique<PseudoOracularSparseExtractor<Value_, Index_> >(std::move(oracle), sparse(row, std::move(indices_ptr), opt));
1008 }
1009};
1010
1016template<typename Value_, typename Index_, class ValueStorage_ = std::vector<Value_>, class IndexStorage_ = std::vector<Index_>, class PointerStorage_ = std::vector<std::size_t> >
1017class CompressedSparseColumnMatrix final : public CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> {
1018public:
1027 CompressedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check = true) :
1028 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), false, check) {}
1029};
1030
1036template<typename Value_, typename Index_, class ValueStorage_ = std::vector<Value_>, class IndexStorage_ = std::vector<Index_>, class PointerStorage_ = std::vector<std::size_t> >
1037class CompressedSparseRowMatrix final : public CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_> {
1038public:
1047 CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check = true) :
1048 CompressedSparseMatrix<Value_, Index_, ValueStorage_, IndexStorage_, PointerStorage_>(nrow, ncol, std::move(values), std::move(indices), std::move(pointers), true, check) {}
1049};
1050
1051}
1052
1053#endif
Virtual class for a matrix of some numeric type.
Mimic the oracle-aware extractor interface.
Compressed sparse column matrix.
Definition CompressedSparseMatrix.hpp:1017
CompressedSparseColumnMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:1027
Compressed sparse matrix representation.
Definition CompressedSparseMatrix.hpp:645
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:977
Index_ nrow() const
Definition CompressedSparseMatrix.hpp:759
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition CompressedSparseMatrix.hpp:894
bool prefer_rows() const
Definition CompressedSparseMatrix.hpp:779
double prefer_rows_proportion() const
Definition CompressedSparseMatrix.hpp:784
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 CompressedSparseMatrix.hpp:948
CompressedSparseMatrix(const Index_ nrow, const Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, const bool csr, const CompressedSparseMatrixOptions &options)
Definition CompressedSparseMatrix.hpp:656
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 CompressedSparseMatrix.hpp:961
bool uses_oracle(const bool) const
Definition CompressedSparseMatrix.hpp:789
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition CompressedSparseMatrix.hpp:876
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 CompressedSparseMatrix.hpp:988
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 CompressedSparseMatrix.hpp:1001
double is_sparse_proportion() const
Definition CompressedSparseMatrix.hpp:774
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition CompressedSparseMatrix.hpp:914
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &) const
Definition CompressedSparseMatrix.hpp:853
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition CompressedSparseMatrix.hpp:937
bool is_sparse() const
Definition CompressedSparseMatrix.hpp:769
Index_ ncol() const
Definition CompressedSparseMatrix.hpp:764
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &) const
Definition CompressedSparseMatrix.hpp:815
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &) const
Definition CompressedSparseMatrix.hpp:833
Compressed sparse row matrix.
Definition CompressedSparseMatrix.hpp:1037
CompressedSparseRowMatrix(Index_ nrow, Index_ ncol, ValueStorage_ values, IndexStorage_ indices, PointerStorage_ pointers, bool check=true)
Definition CompressedSparseMatrix.hpp:1047
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 the CompressedSparseMatrix.
Definition CompressedSparseMatrix.hpp:612
bool check
Definition CompressedSparseMatrix.hpp:625
Options for accessing data from a Matrix instance.
Definition Options.hpp:30