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 <vector>
5#include <cstddef>
6
7#include "SlabCacheStats.hpp"
8
14namespace tatami_chunked {
15
30template<typename Value_, typename Index_, typename Count_ = Index_>
32private:
33 typedef std::vector<Value_> ValuePool;
34 typedef std::vector<Index_> IndexPool;
35 typedef std::vector<Count_> NumberPool;
36
37 Index_ my_target_dim, my_non_target_dim;
38 typename ValuePool::size_type my_slab_size;
39 bool my_needs_value, my_needs_index;
40
41 typename NumberPool::size_type my_offset_number = 0;
42 typename ValuePool::size_type my_offset_slab = 0;
43 ValuePool my_value_pool;
44 IndexPool my_index_pool;
45 NumberPool my_number_pool;
46
47public:
60 template<typename MaxSlabs_>
61 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, MaxSlabs_ max_slabs, bool needs_value, bool needs_index) :
62 my_target_dim(target_dim),
63 my_non_target_dim(non_target_dim),
64 my_slab_size(slab_size),
65 my_needs_value(needs_value),
66 my_needs_index(needs_index),
67 my_number_pool(sanisizer::product<decltype(my_number_pool.size())>(max_slabs, target_dim))
68 {
69 if (needs_value) {
70 my_value_pool.resize(sanisizer::product<decltype(my_value_pool.size())>(max_slabs, slab_size));
71 }
72 if (needs_index) {
73 my_index_pool.resize(sanisizer::product<decltype(my_index_pool.size())>(max_slabs, slab_size));
74 }
75 }
76
88 template<typename MaxSlabs_>
89 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, MaxSlabs_ max_slabs, bool needs_value, bool needs_index) :
90 SparseSlabFactory(target_dim, non_target_dim, sanisizer::product<std::size_t>(target_dim, non_target_dim), max_slabs, needs_value, needs_index) {}
91
103 template<typename MaxSlabs_>
104 SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats<MaxSlabs_>& stats, bool needs_value, bool needs_index) :
105 SparseSlabFactory(target_dim, non_target_dim, stats.slab_size_in_elements, stats.max_slabs_in_cache, needs_value, needs_index) {}
106
110 // Delete the copy constructors as we're passing out pointers.
111 SparseSlabFactory(const SparseSlabFactory&) = delete;
112 SparseSlabFactory& operator=(const SparseSlabFactory&) = delete;
113
114 // Move constructors are okay though.
116 SparseSlabFactory& operator=(SparseSlabFactory&&) = default;
121public:
125 struct Slab {
134 std::vector<Value_*> values;
135
144 std::vector<Index_*> indices;
145
152 Count_* number = NULL;
153 };
154
162 Slab output;
163 output.number = my_number_pool.data() + my_offset_number;
164 my_offset_number += my_target_dim;
165
166 if (my_needs_value) {
167 output.values.reserve(my_target_dim);
168 auto vptr = my_value_pool.data() + my_offset_slab;
169 for (decltype(my_target_dim) p = 0; p < my_target_dim; ++p, vptr += my_non_target_dim) {
170 output.values.push_back(vptr);
171 }
172 }
173
174 if (my_needs_index) {
175 output.indices.reserve(my_target_dim);
176 auto iptr = my_index_pool.data() + my_offset_slab;
177 for (decltype(my_target_dim) p = 0; p < my_target_dim; ++p, iptr += my_non_target_dim) {
178 output.indices.push_back(iptr);
179 }
180 }
181
182 my_offset_slab += my_slab_size;
183 return output;
184 }
185};
186
187}
188
189#endif
Slab cache statistics.
Factory for sparse slabs.
Definition SparseSlabFactory.hpp:31
Slab create()
Definition SparseSlabFactory.hpp:161
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, const SlabCacheStats< MaxSlabs_ > &stats, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:104
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, MaxSlabs_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:89
SparseSlabFactory(Index_ target_dim, Index_ non_target_dim, std::size_t slab_size, MaxSlabs_ max_slabs, bool needs_value, bool needs_index)
Definition SparseSlabFactory.hpp:61
Methods to handle chunked tatami matrices.
Definition ChunkDimensionStats.hpp:4
Statistics for slab caching.
Definition SlabCacheStats.hpp:26
Sparse slab.
Definition SparseSlabFactory.hpp:125
std::vector< Index_ * > indices
Definition SparseSlabFactory.hpp:144
std::vector< Value_ * > values
Definition SparseSlabFactory.hpp:134
Count_ * number
Definition SparseSlabFactory.hpp:152