1#ifndef TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
23namespace DelayedSubsetSortedUnique_internal {
25template<
typename Index_,
class SubsetStorage_>
26VectorPtr<Index_> create(
const SubsetStorage_& subset) {
27 return std::make_shared<std::vector<Index_> >(subset.begin(), subset.end());
30template<
typename Index_,
class SubsetStorage_>
31VectorPtr<Index_> create(
const SubsetStorage_& subset,
const Index_ block_start,
const Index_ block_length) {
32 auto pistart = subset.begin() + block_start;
33 return std::make_shared<std::vector<Index_> >(pistart, pistart + block_length);
36template<
typename Index_,
class SubsetStorage_>
37VectorPtr<Index_> create(
const SubsetStorage_& subset,
const VectorPtr<Index_>& indices_ptr) {
38 auto rawptr =
new std::vector<Index_>;
39 VectorPtr<Index_> outptr(rawptr);
40 auto& output = *rawptr;
42 const auto& input = *indices_ptr;
43 output.reserve(input.size());
44 for (
auto i : input) {
45 output.push_back(subset[i]);
51template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
52std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
53 const Matrix<Value_, Index_>& matrix,
54 const SubsetStorage_& subset,
56 MaybeOracle<oracle_, Index_> oracle,
59 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt);
62template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
63std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
64 const Matrix<Value_, Index_>& matrix,
65 const SubsetStorage_& subset,
67 MaybeOracle<oracle_, Index_> oracle,
68 const Index_ block_start,
69 const Index_ block_length,
72 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt);
75template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
76std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
77 const Matrix<Value_, Index_>& matrix,
78 const SubsetStorage_& subset,
80 MaybeOracle<oracle_, Index_> oracle,
81 VectorPtr<Index_> indices_ptr,
84 return make_Extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt);
87template<
bool oracle_,
typename Value_,
typename Index_>
88class ParallelSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
90 template<
class SubsetStorage_>
92 const Matrix<Value_, Index_>& matrix,
93 const SubsetStorage_& subset,
94 const std::vector<Index_>& remap,
96 MaybeOracle<oracle_, Index_> oracle,
99 my_ext(
make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt)),
103 template<
class SubsetStorage_>
105 const Matrix<Value_, Index_>& matrix,
106 const SubsetStorage_& subset,
107 const std::vector<Index_>& remap,
109 MaybeOracle<oracle_, Index_> oracle,
110 const Index_ block_start,
111 const Index_ block_length,
114 my_ext(
make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt)),
118 template<
class SubsetStorage_>
120 const Matrix<Value_, Index_>& matrix,
121 const SubsetStorage_& subset,
122 const std::vector<Index_>& remap,
124 MaybeOracle<oracle_, Index_> oracle,
125 VectorPtr<Index_> indices_ptr,
128 my_ext(
make_Extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt)),
133 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
134 auto out = my_ext->fetch(i, value_buffer, index_buffer);
136 for (Index_ i = 0; i < out.number; ++i) {
137 index_buffer[i] = my_remapping[out.index[i]];
139 out.index = index_buffer;
145 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
146 const std::vector<Index_>& my_remapping;
164template<
typename Value_,
typename Index_,
class SubsetStorage_>
177 SubsetStorage_ subset,
179 const bool check =
true
181 my_matrix(std::move(matrix)),
182 my_subset(std::move(subset)),
185 const auto nsub = my_subset.size();
187 for (I<
decltype(nsub)> i = 1; i < nsub; ++i) {
188 if (my_subset[i] <= my_subset[i-1]) {
189 throw std::runtime_error(
"subset should be unique and sorted");
194 const Index_ mapping_dim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
196 for (I<
decltype(nsub)> i = 0; i < nsub; ++i) {
197 my_mapping_single[my_subset[i]] = i;
202 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
203 SubsetStorage_ my_subset;
205 std::vector<Index_> my_mapping_single;
210 return my_subset.size();
212 return my_matrix->nrow();
218 return my_matrix->ncol();
220 return my_subset.size();
225 return my_matrix->is_sparse();
229 return my_matrix->is_sparse_proportion();
233 return my_matrix->prefer_rows();
237 return my_matrix->prefer_rows_proportion();
241 return my_matrix->uses_oracle(row);
252 template<
typename ... Args_>
253 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
257 if (row == my_by_row) {
258 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
262 std::forward<Args_>(args)...
265 return DelayedSubsetSortedUnique_internal::create_parallel_dense<false>(
270 std::forward<Args_>(args)...
276 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
280 return populate_myopic_dense(row, opt);
283 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
285 const Index_ block_start,
286 const Index_ block_length,
289 return populate_myopic_dense(row, block_start, block_length, opt);
292 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
297 return populate_myopic_dense(row, std::move(indices_ptr), opt);
304 template<
typename ... Args_>
305 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
309 if (row == my_by_row) {
310 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
314 std::forward<Args_>(args)...
317 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<false, Value_, Index_> >(
323 std::forward<Args_>(args)...
329 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
333 return populate_myopic_sparse(row, opt);
336 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
338 const Index_ block_start,
339 const Index_ block_length,
342 return populate_myopic_sparse(row, block_start, block_length, opt);
345 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
350 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
357 template<
typename ... Args_>
358 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
363 if (row == my_by_row) {
364 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
369 std::forward<Args_>(args)...
372 return DelayedSubsetSortedUnique_internal::create_parallel_dense<true>(
377 std::forward<Args_>(args)...
383 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
388 return populate_oracular_dense(row, std::move(oracle), opt);
391 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
394 const Index_ block_start,
395 const Index_ block_length,
398 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
401 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
407 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
414 template<
typename ... Args_>
415 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
420 if (row == my_by_row) {
421 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
426 std::forward<Args_>(args)...
429 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<true, Value_, Index_> >(
435 std::forward<Args_>(args)...
441 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
446 return populate_oracular_sparse(row, std::move(oracle), opt);
449 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
452 const Index_ block_start,
453 const Index_ block_length,
456 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
459 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
465 return populate_oracular_sparse(row, std::move(oracle), std::move(indices_ptr), opt);
Virtual class for a matrix of some numeric type.
Delayed subsetting of a matrix with sorted, unique indices.
Definition DelayedSubsetSortedUnique.hpp:165
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 DelayedSubsetSortedUnique.hpp:391
Index_ ncol() const
Definition DelayedSubsetSortedUnique.hpp:216
double prefer_rows_proportion() const
Definition DelayedSubsetSortedUnique.hpp:236
bool uses_oracle(const bool row) const
Definition DelayedSubsetSortedUnique.hpp:240
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:336
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Index_ block_start, const Index_ block_length, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:283
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:441
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:329
double is_sparse_proportion() const
Definition DelayedSubsetSortedUnique.hpp:228
Index_ nrow() const
Definition DelayedSubsetSortedUnique.hpp:208
DelayedSubsetSortedUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row, const bool check=true)
Definition DelayedSubsetSortedUnique.hpp:175
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:292
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:345
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 DelayedSubsetSortedUnique.hpp:401
bool is_sparse() const
Definition DelayedSubsetSortedUnique.hpp:224
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 DelayedSubsetSortedUnique.hpp:449
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:276
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 DelayedSubsetSortedUnique.hpp:459
bool prefer_rows() const
Definition DelayedSubsetSortedUnique.hpp:232
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:383
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
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:85
std::unique_ptr< Extractor< sparse_, oracle_, Value_, Index_ > > make_Extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, const tatami::Options &opt)
Definition make_Extractor.hpp:69
Options for accessing data from a Matrix instance.
Definition Options.hpp:30