1#ifndef TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
2#define TATAMI_DELAYED_SUBSET_SORTED_UNIQUE_HPP
22namespace DelayedSubsetSortedUnique_internal {
24template<
typename Index_,
class SubsetStorage_>
25VectorPtr<Index_> create(
const SubsetStorage_& subset) {
26 return std::make_shared<std::vector<Index_> >(subset.begin(), subset.end());
29template<
typename Index_,
class SubsetStorage_>
30VectorPtr<Index_> create(
const SubsetStorage_& subset,
const Index_ block_start,
const Index_ block_length) {
31 auto pistart = subset.begin() + block_start;
32 return std::make_shared<std::vector<Index_> >(pistart, pistart + block_length);
35template<
typename Index_,
class SubsetStorage_>
36VectorPtr<Index_> create(
const SubsetStorage_& subset,
const VectorPtr<Index_>& indices_ptr) {
37 auto rawptr =
new std::vector<Index_>;
38 VectorPtr<Index_> outptr(rawptr);
39 auto& output = *rawptr;
41 const auto& input = *indices_ptr;
42 output.reserve(input.size());
43 for (
auto i : input) {
44 output.push_back(subset[i]);
50template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
51std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
52 const Matrix<Value_, Index_>& matrix,
53 const SubsetStorage_& subset,
55 MaybeOracle<oracle_, Index_> oracle,
58 return new_extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt);
61template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
62std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
63 const Matrix<Value_, Index_>& matrix,
64 const SubsetStorage_& subset,
66 MaybeOracle<oracle_, Index_> oracle,
67 const Index_ block_start,
68 const Index_ block_length,
71 return new_extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt);
74template<
bool oracle_,
typename Value_,
typename Index_,
class SubsetStorage_>
75std::unique_ptr<DenseExtractor<oracle_, Value_, Index_> > create_parallel_dense(
76 const Matrix<Value_, Index_>& matrix,
77 const SubsetStorage_& subset,
79 MaybeOracle<oracle_, Index_> oracle,
80 VectorPtr<Index_> indices_ptr,
83 return new_extractor<false, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt);
86template<
bool oracle_,
typename Value_,
typename Index_>
87class ParallelSparse final :
public SparseExtractor<oracle_, Value_, Index_> {
89 template<
class SubsetStorage_>
91 const Matrix<Value_, Index_>& matrix,
92 const SubsetStorage_& subset,
93 const std::vector<Index_>& remap,
95 MaybeOracle<oracle_, Index_> oracle,
98 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset), opt)),
102 template<
class SubsetStorage_>
104 const Matrix<Value_, Index_>& matrix,
105 const SubsetStorage_& subset,
106 const std::vector<Index_>& remap,
108 MaybeOracle<oracle_, Index_> oracle,
109 const Index_ block_start,
110 const Index_ block_length,
113 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, block_start, block_length), opt)),
117 template<
class SubsetStorage_>
119 const Matrix<Value_, Index_>& matrix,
120 const SubsetStorage_& subset,
121 const std::vector<Index_>& remap,
123 MaybeOracle<oracle_, Index_> oracle,
124 VectorPtr<Index_> indices_ptr,
127 my_ext(
new_extractor<true, oracle_>(matrix, row, std::move(oracle), create<Index_>(subset, indices_ptr), opt)),
132 SparseRange<Value_, Index_> fetch(
const Index_ i, Value_*
const value_buffer, Index_*
const index_buffer) {
133 auto out = my_ext->fetch(i, value_buffer, index_buffer);
135 for (Index_ i = 0; i < out.number; ++i) {
136 index_buffer[i] = my_remapping[out.index[i]];
138 out.index = index_buffer;
144 std::unique_ptr<SparseExtractor<oracle_, Value_, Index_> > my_ext;
145 const std::vector<Index_>& my_remapping;
163template<
typename Value_,
typename Index_,
class SubsetStorage_>
176 SubsetStorage_ subset,
178 const bool check =
true
180 my_matrix(std::move(matrix)),
181 my_subset(std::move(subset)),
184 const auto nsub = my_subset.size();
186 for (I<
decltype(nsub)> i = 1; i < nsub; ++i) {
187 if (my_subset[i] <= my_subset[i-1]) {
188 throw std::runtime_error(
"subset should be unique and sorted");
193 const Index_ mapping_dim = my_by_row ? my_matrix->nrow() : my_matrix->ncol();
195 for (I<
decltype(nsub)> i = 0; i < nsub; ++i) {
196 my_mapping_single[my_subset[i]] = i;
201 std::shared_ptr<const Matrix<Value_, Index_> > my_matrix;
202 SubsetStorage_ my_subset;
204 std::vector<Index_> my_mapping_single;
209 return my_subset.size();
211 return my_matrix->nrow();
217 return my_matrix->ncol();
219 return my_subset.size();
224 return my_matrix->is_sparse();
228 return my_matrix->is_sparse_proportion();
232 return my_matrix->prefer_rows();
236 return my_matrix->prefer_rows_proportion();
240 return my_matrix->uses_oracle(row);
251 template<
typename ... Args_>
252 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> > populate_myopic_dense(
256 if (row == my_by_row) {
257 return std::make_unique<subset_utils::MyopicPerpendicularDense<Value_, Index_, SubsetStorage_> >(
261 std::forward<Args_>(args)...
264 return DelayedSubsetSortedUnique_internal::create_parallel_dense<false>(
269 std::forward<Args_>(args)...
275 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
279 return populate_myopic_dense(row, opt);
282 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
284 const Index_ block_start,
285 const Index_ block_length,
288 return populate_myopic_dense(row, block_start, block_length, opt);
291 std::unique_ptr<MyopicDenseExtractor<Value_, Index_> >
dense(
296 return populate_myopic_dense(row, std::move(indices_ptr), opt);
303 template<
typename ... Args_>
304 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> > populate_myopic_sparse(
308 if (row == my_by_row) {
309 return std::make_unique<subset_utils::MyopicPerpendicularSparse<Value_, Index_, SubsetStorage_> >(
313 std::forward<Args_>(args)...
316 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<false, Value_, Index_> >(
322 std::forward<Args_>(args)...
328 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
332 return populate_myopic_sparse(row, opt);
335 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
337 const Index_ block_start,
338 const Index_ block_length,
341 return populate_myopic_sparse(row, block_start, block_length, opt);
344 std::unique_ptr<MyopicSparseExtractor<Value_, Index_> >
sparse(
349 return populate_myopic_sparse(row, std::move(indices_ptr), opt);
356 template<
typename ... Args_>
357 std::unique_ptr<OracularDenseExtractor<Value_, Index_> > populate_oracular_dense(
362 if (row == my_by_row) {
363 return std::make_unique<subset_utils::OracularPerpendicularDense<Value_, Index_> >(
368 std::forward<Args_>(args)...
371 return DelayedSubsetSortedUnique_internal::create_parallel_dense<true>(
376 std::forward<Args_>(args)...
382 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
387 return populate_oracular_dense(row, std::move(oracle), opt);
390 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
393 const Index_ block_start,
394 const Index_ block_length,
397 return populate_oracular_dense(row, std::move(oracle), block_start, block_length, opt);
400 std::unique_ptr<OracularDenseExtractor<Value_, Index_> >
dense(
406 return populate_oracular_dense(row, std::move(oracle), std::move(indices_ptr), opt);
413 template<
typename ... Args_>
414 std::unique_ptr<OracularSparseExtractor<Value_, Index_> > populate_oracular_sparse(
419 if (row == my_by_row) {
420 return std::make_unique<subset_utils::OracularPerpendicularSparse<Value_, Index_> >(
425 std::forward<Args_>(args)...
428 return std::make_unique<DelayedSubsetSortedUnique_internal::ParallelSparse<true, Value_, Index_> >(
434 std::forward<Args_>(args)...
440 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
445 return populate_oracular_sparse(row, std::move(oracle), opt);
448 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
451 const Index_ block_start,
452 const Index_ block_length,
455 return populate_oracular_sparse(row, std::move(oracle), block_start, block_length, opt);
458 std::unique_ptr<OracularSparseExtractor<Value_, Index_> >
sparse(
464 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:164
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:390
Index_ ncol() const
Definition DelayedSubsetSortedUnique.hpp:215
double prefer_rows_proportion() const
Definition DelayedSubsetSortedUnique.hpp:235
bool uses_oracle(const bool row) const
Definition DelayedSubsetSortedUnique.hpp:239
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:335
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:282
std::unique_ptr< OracularSparseExtractor< Value_, Index_ > > sparse(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:440
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:328
double is_sparse_proportion() const
Definition DelayedSubsetSortedUnique.hpp:227
Index_ nrow() const
Definition DelayedSubsetSortedUnique.hpp:207
DelayedSubsetSortedUnique(std::shared_ptr< const Matrix< Value_, Index_ > > matrix, SubsetStorage_ subset, const bool by_row, const bool check=true)
Definition DelayedSubsetSortedUnique.hpp:174
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:291
std::unique_ptr< MyopicSparseExtractor< Value_, Index_ > > sparse(const bool row, VectorPtr< Index_ > indices_ptr, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:344
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:400
bool is_sparse() const
Definition DelayedSubsetSortedUnique.hpp:223
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:448
std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(const bool row, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:275
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:458
bool prefer_rows() const
Definition DelayedSubsetSortedUnique.hpp:231
std::unique_ptr< OracularDenseExtractor< Value_, Index_ > > dense(const bool row, std::shared_ptr< const Oracle< Index_ > > oracle, const Options &opt) const
Definition DelayedSubsetSortedUnique.hpp:382
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
auto new_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
Definition new_extractor.hpp:42
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:92
Options for accessing data from a Matrix instance.
Definition Options.hpp:30