tatami_chunked
Helpers to create custom chunked tatami matrices
Loading...
Searching...
No Matches
SparseSlabFactory.hpp
Go to the documentation of this file.
1#ifndef TATAMI_CHUNKED_SPARSE_SLAB_FACTORY_HPP
2#define TATAMI_CHUNKED_SPARSE_SLAB_FACTORY_HPP
3
4#include "SlabCacheStats.hpp"
5#include "utils.hpp"
6
7#include <vector>
8#include <cstddef>
9
10#include "sanisizer/sanisizer.hpp"
11
17namespace tatami_chunked {
18
33template<typename Value_, typename Index_, typename Count_ = Index_>
35private:
36 Index_ my_target_dim, my_non_target_dim;
37 bool my_needs_value, my_needs_index;
38
39 // Might as well use size_t here, as we'll be doing pointer arithmetic in create().
40 std::size_t my_slab_size;
41 std::size_t my_offset_number = 0;
42 std::size_t my_offset_slab = 0;
43
44 std::vector<Value_> my_value_pool;
45 std::vector<Index_> my_index_pool;
46 std::vector<Count_> my_number_pool;
47
48public:
59 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, Index_ max_slabs, bool needs_value, bool needs_index) :
60 my_target_dim(target_dim),
61 my_non_target_dim(non_target_dim),
62 my_needs_value(needs_value),
63 my_needs_index(needs_index),
64 my_slab_size(slab_size),
65 my_number_pool(sanisizer::product<I<decltype(my_number_pool.size())> >(max_slabs, target_dim))
66 {
67 if (needs_value) {
68 my_value_pool.resize(sanisizer::product<I<decltype(my_value_pool.size())> >(max_slabs, slab_size));
69 }
70 if (needs_index) {
71 my_index_pool.resize(sanisizer::product<I<decltype(my_index_pool.size())> >(max_slabs, slab_size));
72 }
73 }
74
84 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, Index_ max_slabs, bool needs_value, bool needs_index) :
85 SparseSlabFactory(target_dim, non_target_dim, sanisizer::product<std::size_t>(target_dim, non_target_dim), max_slabs, needs_value, needs_index) {}
86
96 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats<Index_>& stats, bool needs_value, bool needs_index) :
97 SparseSlabFactory(target_dim, non_target_dim, stats.slab_size_in_elements, stats.max_slabs_in_cache, needs_value, needs_index) {}
98
102 // Delete the copy constructors as we're passing out pointers.
103 SparseSlabFactory(const SparseSlabFactory&) = delete;
104 SparseSlabFactory& operator=(const SparseSlabFactory&) = delete;
105
106 // Move constructors are okay though.
108 SparseSlabFactory& operator=(SparseSlabFactory&&) = default;
113public:
117 struct Slab {
126 std::vector<Value_*> values;
127
136 std::vector<Index_*> indices;
137
144 Count_* number = NULL;
145 };
146
154 Slab output;
155 output.number = my_number_pool.data() + my_offset_number;
156 my_offset_number += my_target_dim;
157
158 if (my_needs_value) {
159 output.values.reserve(my_target_dim);
160 auto vptr = my_value_pool.data() + my_offset_slab;
161 for (I<decltype(my_target_dim)> p = 0; p < my_target_dim; ++p, vptr += my_non_target_dim) {
162 output.values.push_back(vptr);
163 }
164 }
165
166 if (my_needs_index) {
167 output.indices.reserve(my_target_dim);
168 auto iptr = my_index_pool.data() + my_offset_slab;
169 for (I<decltype(my_target_dim)> p = 0; p < my_target_dim; ++p, iptr += my_non_target_dim) {
170 output.indices.push_back(iptr);
171 }
172 }
173
174 my_offset_slab += my_slab_size;
175 return output;
176 }
177};
178
179}
180
181#endif
Slab cache statistics.
Factory for sparse slabs.
Definition SparseSlabFactory.hpp:34
Slab create()
Definition SparseSlabFactory.hpp:153
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, Index_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:59
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats< Index_ > &stats, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:96
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, Index_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:84
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:11
Statistics for slab caching.
Definition SlabCacheStats.hpp:47
Sparse slab.
Definition SparseSlabFactory.hpp:117
std::vector< Index_ * > indices
Definition SparseSlabFactory.hpp:136
std::vector< Value_ * > values
Definition SparseSlabFactory.hpp:126
Count_ * number
Definition SparseSlabFactory.hpp:144