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"
10#include "../utils/copy.hpp"
11
12#include <algorithm>
13#include <memory>
14
21namespace tatami {
22
39template<typename Value_, typename Index_, class SubsetStorage_>
40std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<const Matrix<Value_, Index_> > matrix, SubsetStorage_ subset, const bool by_row) {
41 const auto nsub = subset.size();
42 typedef I<decltype(nsub)> Subset;
43
44 bool is_unsorted = false;
45 for (Subset i = 1; i < nsub; ++i) {
46 if (subset[i] < subset[i-1]) {
47 is_unsorted = true;
48 break;
49 }
50 }
51
52 if (!is_unsorted) {
53 bool has_duplicates = false;
54 for (Subset i = 1; i < nsub; ++i) {
55 if (subset[i] == subset[i-1]) {
56 has_duplicates = true;
57 break;
58 }
59 }
60
61 if (!has_duplicates) {
62 bool consecutive = true;
63 for (Subset i = 1; i < nsub; ++i) {
64 if (subset[i] > subset[i-1] + 1) {
65 consecutive = false;
66 break;
67 }
68 }
69
70 if (consecutive) {
71 const auto start = (nsub ? subset[0] : 0);
72 return std::shared_ptr<Matrix<Value_, Index_> >(
73 new DelayedSubsetBlock<Value_, Index_>(std::move(matrix), start, subset.size(), by_row)
74 );
75 } else {
76 return std::shared_ptr<Matrix<Value_, Index_> >(
77 new DelayedSubsetSortedUnique<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), by_row, false)
78 );
79 }
80 } else {
81 return std::shared_ptr<Matrix<Value_, Index_> >(
82 new DelayedSubsetSorted<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), by_row, false)
83 );
84 }
85 }
86
87 bool has_duplicates = false;
88 auto accumulated = create_container_of_Index_size<std::vector<unsigned char> >(by_row ? matrix->nrow() : matrix->ncol());
89 for (Subset i = 0; i < nsub; ++i) {
90 auto& found = accumulated[subset[i]];
91 if (found) {
92 has_duplicates = true;
93 break;
94 } else {
95 found = 1;
96 }
97 }
98
99 if (!has_duplicates) {
100 return std::shared_ptr<Matrix<Value_, Index_> >(
101 new DelayedSubsetUnique<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), by_row, false)
102 );
103 } else {
104 return std::shared_ptr<Matrix<Value_, Index_> >(
105 new DelayedSubset<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), by_row)
106 );
107 }
108}
109
113template<typename Value_, typename Index_, class SubsetStorage_>
114std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<Matrix<Value_, Index_> > matrix, SubsetStorage_ subset, const bool by_row) {
115 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::shared_ptr<const Matrix<Value_, Index_> >(std::move(matrix)), std::move(subset), by_row);
116}
124// Back-compatibility only.
125template<int margin_, typename Value_, typename Index_, class SubsetStorage_>
126std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<const Matrix<Value_, Index_> > matrix, SubsetStorage_ subset) {
127 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), margin_ == 0);
128}
129
130template<int margin_, typename Value_, typename Index_, class SubsetStorage_>
131std::shared_ptr<Matrix<Value_, Index_> > make_DelayedSubset(std::shared_ptr<Matrix<Value_, Index_> > matrix, SubsetStorage_ subset) {
132 return make_DelayedSubset<Value_, Index_, SubsetStorage_>(std::move(matrix), std::move(subset), margin_ == 0);
133}
138}
139
140#endif
Defines a tatami-compatible array view.
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.
Delayed subsetting to a contiguous block.
Definition DelayedSubsetBlock.hpp:241
Delayed subsetting of a matrix with sorted, unique indices.
Definition DelayedSubsetSortedUnique.hpp:164
Delayed subsetting of a matrix with sorted indices.
Definition DelayedSubsetSorted.hpp:413
Delayed subsetting of a matrix with unique indices.
Definition DelayedSubsetUnique.hpp:298
Delayed subsetting of a matrix with general indices.
Definition DelayedSubset.hpp:438
Virtual class for a matrix.
Definition Matrix.hpp:59
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:73
std::shared_ptr< Matrix< Value_, Index_ > > make_DelayedSubset(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row)
Definition make_DelayedSubset.hpp:40