tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
make_DelayedSubset.hpp
Go to the documentation of this file.
1#ifndef TATAMI_MAKE_DELAYED_SUBSET_HPP
2#define TATAMI_MAKE_DELAYED_SUBSET_HPP
3
7#include "DelayedSubset.hpp"
9#include "../utils/ArrayView.hpp"
10
11#include <algorithm>
12#include <memory>
13
20namespace tatami {
21
38template<typename Value_, typename Index_, class SubsetStorage_>
39std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<const Matrix<Value_, Index_> > matrix, SubsetStorage_ subset, bool by_row) {
40 bool is_unsorted = false;
41 for (Index_ i = 0, end = subset.size(); i < end; ++i) {
42 if (i) {
43 if (subset[i] < subset[i-1]) {
44 is_unsorted = true;
45 break;
46 }
47 }
48 }
49
50 if (!is_unsorted) {
51 bool has_duplicates = false;
52 for (Index_ i = 0, end = subset.size(); i < end; ++i) {
53 if (i) {
54 if (subset[i] == subset[i-1]) {
55 has_duplicates = true;
56 break;
57 }
58 }
59 }
60
61 if (!has_duplicates) {
62 bool consecutive = true;
63 for (Index_ i = 0, end = subset.size(); i < end; ++i) {
64 if (i) {
65 if (subset[i] > subset[i-1] + 1) {
66 consecutive = false;
67 break;
68 }
69 }
70 }
71
72 if (consecutive) {
73 auto start = (subset.size() ? subset[0] : 0);
74 return std::shared_ptr<Matrix<Value_, Index_> >(
76 );
77 } else {
78 return std::shared_ptr<Matrix<Value_, Index_> >(
80 );
81 }
82 } else {
83 return std::shared_ptr<Matrix<Value_, Index_> >(
85 );
86 }
87 }
88
89 bool has_duplicates = false;
90 std::vector<unsigned char> accumulated(by_row ? matrix->nrow() : matrix->ncol());
91 for (Index_ i = 0, end = subset.size(); i < end; ++i) {
92 auto& found = accumulated[subset[i]];
93 if (found) {
94 has_duplicates = true;
95 break;
96 } else {
97 found = 1;
98 }
99 }
100
101 if (!has_duplicates) {
102 return std::shared_ptr<Matrix<Value_, Index_> >(
104 );
105 } else {
106 return std::shared_ptr<Matrix<Value_, Index_> >(
108 );
109 }
110}
111
115template<typename Value_, typename Index_, class SubsetStorage_>
116std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<Matrix<Value_, Index_> > matrix, SubsetStorage_ subset, bool by_row) {
117 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::shared_ptr<const Matrix<Value_, Index_> >(std::move(matrix)), std::move(subset), by_row);
118}
126// Back-compatibility only.
127template<int margin_, typename Value_, typename Index_, class SubsetStorage_>
128std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<const Matrix<Value_, Index_> > matrix, SubsetStorage_ subset) {
129 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), margin_ == 0);
130}
131
132template<int margin_, typename Value_, typename Index_, class SubsetStorage_>
133std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<Matrix<Value_, Index_> > matrix, SubsetStorage_ subset) {
134 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), margin_ == 0);
135}
140}
141
142#endif
Delayed subsetting to a single contiguous block.
Delayed subsetting with sorted and unique row/column indices.
Delayed subsetting with sorted row/column indices.
Delayed subsetting by unique row/column indices.
Delayed subsetting by rows or columns.
Virtual class for a matrix.
Definition Matrix.hpp:59
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::shared_ptr< Matrix< Value_, Index_ > > make_DelayedSubset(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, bool by_row)
Definition make_DelayedSubset.hpp:39
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35