6 #ifndef TURI_UNITY_VECTOR_INDEX_MAPPER_H_ 7 #define TURI_UNITY_VECTOR_INDEX_MAPPER_H_ 10 #include <core/util/dense_bitset.hpp> 22 bool _index_mapping_enabled =
false;
23 std::vector<size_t> data_to_internal_index_mapping;
24 std::vector<size_t> internal_to_data_index_mapping;
31 return !_index_mapping_enabled;
40 size_t num_items = is_active_entry.
size();
41 size_t new_size = is_active_entry.popcount();
44 if(new_size == num_items) {
45 _index_mapping_enabled =
false;
46 internal_to_data_index_mapping.clear();
47 data_to_internal_index_mapping.clear();
52 internal_to_data_index_mapping.resize(new_size);
53 data_to_internal_index_mapping.resize(num_items);
56 for(
size_t src_idx = 0; src_idx < num_items; ++src_idx) {
57 bool entry_active = is_active_entry.
get(src_idx);
60 internal_to_data_index_mapping[map_idx] = src_idx;
61 data_to_internal_index_mapping[src_idx] = map_idx;
64 data_to_internal_index_mapping[src_idx] = size_t(-1);
68 _index_mapping_enabled =
true;
84 if(!_index_mapping_enabled) {
88 DASSERT_EQ(data_vect.size(), data_to_internal_index_mapping.size());
92 for(
size_t i = 0; i < internal_to_data_index_mapping.size(); ++i) {
93 size_t src_idx = internal_to_data_index_mapping[i];
96 data_vect[dest_idx] = std::move(data_vect[src_idx]);
99 data_vect.resize(internal_to_data_index_mapping.size());
108 template <
typename T>
112 if(!_index_mapping_enabled) {
116 DASSERT_TRUE(std::is_sorted(data_vect.begin(), data_vect.end(),
117 [](
const std::pair<size_t, T>& p1,
118 const std::pair<size_t, T>& p2) {
119 DASSERT_NE(p1.first, p2.first);
120 return p1.first < p2.first; }));
123 for(
size_t src_idx = 0; src_idx < data_vect.size(); ++src_idx) {
124 auto& p = data_vect[src_idx];
126 if(mapped_index !=
size_t(-1)) {
127 data_vect[dest_idx] = {mapped_index, std::move(p.second)};
131 data_vect.resize(dest_idx);
133 DASSERT_TRUE(std::is_sorted(data_vect.begin(), data_vect.end(),
134 [](
const std::pair<size_t, T>& p1,
135 const std::pair<size_t, T>& p2) {
136 DASSERT_NE(p1.first, p2.first);
137 return p1.first < p2.first; }));
143 return _index_mapping_enabled || (data_to_internal_index_mapping[data_idx] != size_t(-1));
149 DASSERT_LT(data_idx, data_to_internal_index_mapping.size());
150 if(_index_mapping_enabled) {
151 return data_to_internal_index_mapping[data_idx];
160 if(_index_mapping_enabled) {
161 DASSERT_LT(internal_idx, internal_to_data_index_mapping.size());
162 return internal_to_data_index_mapping[internal_idx];
GL_HOT_FLATTEN void remap_sparse_vector(std::vector< std::pair< size_t, T > > &data_vect) const
size_t map_internal_index_to_data_index(size_t internal_idx) const GL_HOT_INLINE_FLATTEN
bool get(size_t b) const
Returns the value of the bit b.
bool is_active(size_t data_idx) const GL_HOT_INLINE_FLATTEN
size_t set_index_mapping_from_mask(const dense_bitset &is_active_entry)
#define GL_HOT_INLINE_FLATTEN
size_t map_data_index_to_internal_index(size_t data_idx) const GL_HOT_INLINE_FLATTEN
size_t size() const
Returns the number of bits in this bitset.
GL_HOT_FLATTEN void remap_vector(std::vector< T > &data_vect) const
#define DASSERT_TRUE(cond)