1#ifndef TATAMI_DELAYED_CAST_HPP
2#define TATAMI_DELAYED_CAST_HPP
24namespace DelayedCast_internal {
26template<
typename IndexIn_,
typename IndexOut_>
27class CastOracle final :
public Oracle<IndexIn_> {
29 CastOracle(std::shared_ptr<
const Oracle<IndexOut_> > oracle) : my_oracle(std::move(oracle)) {}
31 IndexIn_ get(
const PredictionIndex i)
const {
32 return my_oracle->get(i);
36 return my_oracle->total();
40 std::shared_ptr<const Oracle<IndexOut_> > my_oracle;
43template<
bool oracle_,
typename IndexIn_,
typename IndexOut_>
44MaybeOracle<oracle_, IndexIn_> convert(MaybeOracle<oracle_, IndexOut_> oracle) {
45 if constexpr(!oracle_) {
47 }
else if constexpr(std::is_same<IndexIn_, IndexOut_>::value) {
50 return std::make_shared<CastOracle<IndexIn_, IndexOut_> >(std::move(oracle));
54template<
typename IndexIn_,
typename IndexOut_>
55VectorPtr<IndexIn_> convert(VectorPtr<IndexOut_> indices_ptr) {
56 if constexpr(std::is_same<IndexIn_, IndexOut_>::value) {
59 return std::make_shared<std::vector<IndexIn_> >(indices_ptr->begin(), indices_ptr->end());
63template<
bool oracle_,
typename ValueOut_,
typename IndexOut_,
typename ValueIn_,
typename IndexIn_>
64class Dense final :
public DenseExtractor<oracle_, ValueOut_, IndexOut_> {
67 const Matrix<ValueIn_, IndexIn_>& matrix,
68 const bool row, MaybeOracle<oracle_, IndexOut_> oracle,
71 allocate(row ? matrix.ncol() : matrix.nrow());
72 my_ext = make_Extractor<false, oracle_>(
75 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
81 const Matrix<ValueIn_, IndexIn_>& matrix,
83 MaybeOracle<oracle_, IndexOut_> oracle,
84 const IndexOut_ block_start,
85 const IndexOut_ block_length,
88 allocate(block_length);
89 my_ext = make_Extractor<false, oracle_>(
92 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
93 static_cast<IndexIn_
>(block_start),
94 static_cast<IndexIn_
>(block_length),
100 const Matrix<ValueIn_, IndexIn_>& matrix,
102 MaybeOracle<oracle_, IndexOut_> oracle,
103 VectorPtr<IndexOut_> indices_ptr,
106 allocate(indices_ptr->size());
107 my_ext = make_Extractor<false, oracle_>(
110 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
111 convert<IndexIn_>(std::move(indices_ptr)),
117 void allocate(
const IndexIn_ n) {
118 if constexpr(!no_op) {
124 const ValueOut_* fetch(
const IndexOut_ i, ValueOut_*
const buffer) {
125 if constexpr(no_op) {
126 return my_ext->fetch(i, buffer);
128 const auto ptr = my_ext->fetch(i, my_buffer.data());
129 std::copy_n(ptr, my_buffer.size(), buffer);
135 std::unique_ptr<DenseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
136 static constexpr bool no_op = std::is_same<ValueOut_, ValueIn_>::value;
137 typename std::conditional<no_op, bool, std::vector<ValueIn_> >::type my_buffer;
140template<
bool oracle_,
typename ValueOut_,
typename IndexOut_,
typename ValueIn_,
typename IndexIn_>
141class Sparse final :
public SparseExtractor<oracle_, ValueOut_, IndexOut_> {
144 const Matrix<ValueIn_, IndexIn_>& matrix,
146 MaybeOracle<oracle_, IndexOut_> oracle,
149 allocate(row ? matrix.ncol() : matrix.nrow(), opt);
150 my_ext = make_Extractor<true, oracle_>(
153 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
159 const Matrix<ValueIn_, IndexIn_>& matrix,
161 MaybeOracle<oracle_, IndexOut_> oracle,
162 const IndexOut_ block_start,
163 const IndexOut_ block_length,
166 allocate(block_length, opt);
167 my_ext = make_Extractor<true, oracle_>(
170 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
171 static_cast<IndexIn_
>(block_start),
172 static_cast<IndexIn_
>(block_length),
178 const Matrix<ValueIn_, IndexIn_>& matrix,
180 MaybeOracle<oracle_, IndexOut_> oracle,
181 VectorPtr<IndexOut_> indices_ptr,
184 allocate(indices_ptr->size(), opt);
185 my_ext = make_Extractor<true, oracle_>(
188 convert<oracle_, IndexIn_, IndexOut_>(std::move(oracle)),
189 convert<IndexIn_>(std::move(indices_ptr)),
195 void allocate(
const IndexIn_ n,
const Options& opt) {
196 if constexpr(!no_op_value) {
197 if (opt.sparse_extract_value) {
201 if constexpr(!no_op_index) {
202 if (opt.sparse_extract_index) {
209 SparseRange<ValueOut_, IndexOut_> fetch(
const IndexOut_ i, ValueOut_*
const value_buffer, IndexOut_*
const index_buffer) {
210 IndexIn_*
const iptr = [&]{
211 if constexpr(no_op_index) {
214 return my_ibuffer.data();
218 ValueIn_*
const vptr = [&]{
219 if constexpr(no_op_value) {
222 return my_vbuffer.data();
226 const auto range = my_ext->fetch(i, vptr, iptr);
227 SparseRange<ValueOut_, IndexOut_> output(range.number);
229 if constexpr(no_op_index) {
230 output.index = range.index;
231 }
else if (range.index != NULL) {
232 std::copy_n(range.index, range.number, index_buffer);
233 output.index = index_buffer;
236 if constexpr(no_op_value) {
237 output.value = range.value;
238 }
else if (range.value != NULL) {
239 std::copy_n(range.value, range.number, value_buffer);
240 output.value = value_buffer;
247 std::unique_ptr<SparseExtractor<oracle_, ValueIn_, IndexIn_> > my_ext;
248 static constexpr bool no_op_value = std::is_same<ValueOut_, ValueIn_>::value;
249 typename std::conditional<no_op_value, bool, std::vector<ValueIn_> >::type my_vbuffer;
250 static constexpr bool no_op_index = std::is_same<IndexOut_, IndexIn_>::value;
251 typename std::conditional<no_op_index, bool, std::vector<IndexIn_> >::type my_ibuffer;
272template<
typename ValueOut_,
typename IndexOut_,
typename ValueIn_,
typename IndexIn_>
279 my_matrix(std::move(matrix)),
280 my_nrow(sanisizer::cast<IndexOut_>(attest_for_Index(my_matrix->
nrow()))),
281 my_ncol(sanisizer::cast<IndexOut_>(attest_for_Index(my_matrix->
ncol())))
294 return my_matrix->is_sparse();
298 return my_matrix->is_sparse_proportion();
302 return my_matrix->prefer_rows();
306 return my_matrix->prefer_rows_proportion();
310 return my_matrix->uses_oracle(row);
314 std::shared_ptr<const Matrix<ValueIn_, IndexIn_> > my_matrix;
315 IndexOut_ my_nrow, my_ncol;
321 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> >
dense(
325 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, opt);
328 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> >
dense(
330 const IndexOut_ block_start,
331 const IndexOut_ block_length,
334 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, block_start, block_length, opt);
337 std::unique_ptr<MyopicDenseExtractor<ValueOut_, IndexOut_> >
dense(
342 return std::make_unique<DelayedCast_internal::Dense<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, std::move(indices_my_matrix), opt);
349 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> >
sparse(
353 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, opt);
356 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> >
sparse(
358 const IndexOut_ block_start,
359 const IndexOut_ block_length,
362 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, block_start, block_length, opt);
365 std::unique_ptr<MyopicSparseExtractor<ValueOut_, IndexOut_> >
sparse(
370 return std::make_unique<DelayedCast_internal::Sparse<false, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row,
false, std::move(indices_my_matrix), opt);
377 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> >
dense(
382 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
385 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> >
dense(
388 const IndexOut_ block_start,
389 const IndexOut_ block_length,
392 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
395 std::unique_ptr<OracularDenseExtractor<ValueOut_, IndexOut_> >
dense(
401 return std::make_unique<DelayedCast_internal::Dense<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
408 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> >
sparse(
413 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), opt);
416 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> >
sparse(
419 const IndexOut_ block_start,
420 const IndexOut_ block_length,
423 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), block_start, block_length, opt);
426 std::unique_ptr<OracularSparseExtractor<ValueOut_, IndexOut_> >
sparse(
432 return std::make_unique<DelayedCast_internal::Sparse<true, ValueOut_, IndexOut_, ValueIn_, IndexIn_> >(*my_matrix, row, std::move(oracle), std::move(indices_my_matrix), opt);
447template<
typename ValueOut_,
typename IndexOut_,
typename ValueIn_,
typename IndexIn_>
455template<
typename ValueOut_,
typename IndexOut_,
typename ValueIn_,
typename IndexIn_>
456std::shared_ptr<Matrix<ValueOut_, IndexOut_> >
make_DelayedCast(std::shared_ptr<Matrix<ValueIn_, IndexIn_> > p) {
457 return std::shared_ptr<Matrix<ValueOut_, IndexOut_> >(
new DelayedCast<ValueOut_, IndexOut_, ValueIn_, IndexIn_>(std::move(p)));
Convert index type to container size.
Virtual class for a matrix of some numeric type.
Recast a Matrix to a different interface type.
Definition DelayedCast.hpp:273
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:416
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:365
DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > matrix)
Definition DelayedCast.hpp:278
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:328
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:426
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:395
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, const Options &opt) const
Definition DelayedCast.hpp:321
double prefer_rows_proportion() const
Definition DelayedCast.hpp:305
IndexOut_ nrow() const
Definition DelayedCast.hpp:285
std::unique_ptr< OracularSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:408
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const Options &opt) const
Definition DelayedCast.hpp:377
std::unique_ptr< MyopicDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, VectorPtr< IndexOut_ > indices_my_matrix, const Options &opt) const
Definition DelayedCast.hpp:337
bool uses_oracle(const bool row) const
Definition DelayedCast.hpp:309
bool is_sparse() const
Definition DelayedCast.hpp:293
IndexOut_ ncol() const
Definition DelayedCast.hpp:289
std::unique_ptr< OracularDenseExtractor< ValueOut_, IndexOut_ > > dense(const bool row, std::shared_ptr< const Oracle< IndexOut_ > > oracle, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:385
double is_sparse_proportion() const
Definition DelayedCast.hpp:297
bool prefer_rows() const
Definition DelayedCast.hpp:301
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, const Options &opt) const
Definition DelayedCast.hpp:349
std::unique_ptr< MyopicSparseExtractor< ValueOut_, IndexOut_ > > sparse(const bool row, const IndexOut_ block_start, const IndexOut_ block_length, const Options &opt) const
Definition DelayedCast.hpp:356
Virtual class for a matrix.
Definition Matrix.hpp:59
Predict future access requests on the target dimension.
Definition Oracle.hpp:29
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
typename std::conditional< oracle_, OracularDenseExtractor< Value_, Index_ >, MyopicDenseExtractor< Value_, Index_ > >::type DenseExtractor
Definition Extractor.hpp:284
std::shared_ptr< Matrix< ValueOut_, IndexOut_ > > make_DelayedCast(std::shared_ptr< const Matrix< ValueIn_, IndexIn_ > > p)
Definition DelayedCast.hpp:448
std::size_t PredictionIndex
Definition Oracle.hpp:18
Options for accessing data from a Matrix instance.
Definition Options.hpp:30