29 #include <unordered_map> 36 template <
typename Hash,
typename KeyEq>
47 void Rehash(
size_t buckets)
override;
49 void Insert(
const void* input_keys,
50 const void* input_values,
53 size_t count)
override;
55 void Activate(
const void* input_keys,
58 size_t count)
override;
60 void Find(
const void* input_keys,
63 size_t count)
override;
65 void Erase(
const void* input_keys,
67 size_t count)
override;
72 const bool* input_masks,
75 size_t count)
override;
78 const bool* input_masks,
79 const void* input_values,
80 size_t count)
override;
85 size_t Size()
const override;
88 std::shared_ptr<std::unordered_map<void*, void*, Hash, KeyEq>> impl_;
91 std::vector<iterator_t> kv_pairs_;
94 template <
typename Hash,
typename KeyEq>
107 impl_ = std::make_shared<std::unordered_map<void*, void*, Hash, KeyEq>>(
108 init_buckets, Hash(dsize_key), KeyEq(dsize_key));
111 template <
typename Hash,
typename KeyEq>
113 for (
auto kv_pair : kv_pairs_) {
120 template <
typename Hash,
typename KeyEq>
122 return impl_->size();
125 template <
typename Hash,
typename KeyEq>
127 const void* input_values,
131 for (
size_t i = 0; i <
count; ++i) {
132 const uint8_t* src_key =
133 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i;
134 const uint8_t* src_value =
static_cast<const uint8_t*
>(input_values) +
138 uint8_t* dst_key =
static_cast<uint8_t*
>(
140 uint8_t* dst_value =
static_cast<uint8_t*
>(
149 auto res = impl_->insert({dst_key, dst_value});
153 output_iterators[i] =
iterator_t(dst_key, dst_value);
154 output_masks[i] =
true;
159 output_masks[i] =
false;
166 template <
typename Hash,
typename KeyEq>
171 for (
size_t i = 0; i <
count; ++i) {
172 const uint8_t* src_key =
173 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i;
176 uint8_t* dst_key =
static_cast<uint8_t*
>(
178 uint8_t* dummy_value =
static_cast<uint8_t*
>(
186 auto res = impl_->insert({dst_key, dummy_value});
190 output_iterators[i] =
iterator_t(dst_key, dummy_value);
191 output_masks[i] =
true;
196 output_masks[i] =
false;
203 template <
typename Hash,
typename KeyEq>
208 for (
size_t i = 0; i <
count; ++i) {
209 uint8_t* key =
const_cast<uint8_t*
>(
210 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i);
212 auto iter = impl_->find(key);
213 if (iter == impl_->end()) {
215 output_masks[i] =
false;
217 output_iterators[i] =
iterator_t(iter->first, iter->second);
218 output_masks[i] =
true;
223 template <
typename Hash,
typename KeyEq>
227 for (
size_t i = 0; i <
count; ++i) {
228 uint8_t* key =
const_cast<uint8_t*
>(
229 static_cast<const uint8_t*
>(input_keys) + this->
dsize_key_ * i);
231 size_t erased = impl_->erase(key);
232 output_masks[i] = erased > 0;
236 template <
typename Hash,
typename KeyEq>
238 size_t count = impl_->size();
241 for (
auto iter = impl_->begin(); iter != impl_->end(); ++iter, ++i) {
242 output_iterators[i] =
iterator_t(iter->first, iter->second);
249 const bool* input_masks,
257 if (input_masks ==
nullptr || input_masks[tid]) {
258 if (output_keys !=
nullptr) {
259 uint8_t* dst_key_ptr =
260 static_cast<uint8_t*
>(output_keys) + dsize_key * tid;
261 uint8_t* src_key_ptr =
262 static_cast<uint8_t*
>(input_iterators[tid].
first);
267 if (output_values !=
nullptr) {
268 uint8_t* dst_value_ptr =
269 static_cast<uint8_t*
>(output_values) + dsize_value * tid;
270 uint8_t* src_value_ptr =
271 static_cast<uint8_t*
>(input_iterators[tid].
second);
278 template <
typename Hash,
typename KeyEq>
280 const bool* input_masks,
283 size_t iterator_count) {
284 for (
size_t i = 0; i < iterator_count; ++i) {
292 const bool* input_masks,
293 const void* input_values,
298 if (input_masks ==
nullptr || input_masks[tid]) {
299 const uint8_t* src_value_ptr =
300 static_cast<const uint8_t*
>(input_values) + dsize_value * tid;
301 uint8_t* dst_value_ptr =
302 static_cast<uint8_t*
>(input_iterators[tid].
second);
308 template <
typename Hash,
typename KeyEq>
310 const bool* input_masks,
311 const void* input_values,
312 size_t iterator_count) {
313 for (
size_t i = 0; i < iterator_count; ++i) {
319 template <
typename Hash,
typename KeyEq>
321 impl_->rehash(buckets);
324 template <
typename Hash,
typename KeyEq>
326 size_t bucket_count = impl_->bucket_count();
327 std::vector<size_t> ret;
328 for (
size_t i = 0; i < bucket_count; ++i) {
329 ret.push_back(impl_->bucket_size(i));
334 template <
typename Hash,
typename KeyEq>
336 return impl_->load_factor();
void * first
Definition: Traits.h:56
~CPUHashmap()
Definition: HashmapCPU.h:112
static void Memcpy(void *dst_ptr, const Device &dst_device, const void *src_ptr, const Device &src_device, size_t num_bytes)
Definition: MemoryManager.cpp:48
size_t GetIterators(iterator_t *output_iterators) override
Parallel collect all iterators in the hash table.
Definition: HashmapCPU.h:237
void Insert(const void *input_keys, const void *input_values, iterator_t *output_iterators, bool *output_masks, size_t count) override
Parallel insert contiguous arrays of keys and values.
Definition: HashmapCPU.h:126
CPUHashmap(size_t init_buckets, size_t init_capacity, size_t dsize_key, size_t dsize_value, const Device &device)
Definition: HashmapCPU.h:95
void UnpackIteratorsStep(const iterator_t *input_iterators, const bool *input_masks, void *output_keys, void *output_values, const Device &device, size_t dsize_key, size_t dsize_value, size_t tid)
Definition: HashmapCPU.h:248
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:44
void AssignIteratorsStep(iterator_t *input_iterators, const bool *input_masks, const void *input_values, const Device &device, size_t dsize_value, size_t tid)
Definition: HashmapCPU.h:291
void Erase(const void *input_keys, bool *output_masks, size_t count) override
Parallel erase a contiguous array of keys.
Definition: HashmapCPU.h:224
size_t bucket_count_
Definition: DeviceHashmap.h:172
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:40
float LoadFactor() const override
Return size / bucket_count.
Definition: HashmapCPU.h:335
Base class: shared interface.
Definition: DeviceHashmap.h:91
void UnpackIterators(const iterator_t *input_iterators, const bool *input_masks, void *output_keys, void *output_values, size_t count) override
Parallel unpack iterators to contiguous arrays of keys and/or values.
Definition: HashmapCPU.h:279
size_t capacity_
Definition: DeviceHashmap.h:173
void AssignIterators(iterator_t *input_iterators, const bool *input_masks, const void *input_values, size_t count) override
Parallel assign iterators in-place with associated values.
Definition: HashmapCPU.h:309
int count
Definition: FilePCD.cpp:61
size_t Size() const override
Definition: HashmapCPU.h:121
Device device_
Definition: DeviceHashmap.h:176
std::vector< size_t > BucketSizes() const override
Definition: HashmapCPU.h:325
Definition: PinholeCameraIntrinsic.cpp:35
Definition: HashmapCPU.h:37
void * second
Definition: Traits.h:57
size_t dsize_key_
Definition: DeviceHashmap.h:174
void Rehash(size_t buckets) override
Definition: HashmapCPU.h:320
void Activate(const void *input_keys, iterator_t *output_iterators, bool *output_masks, size_t count) override
Definition: HashmapCPU.h:167
size_t dsize_value_
Definition: DeviceHashmap.h:175
void Find(const void *input_keys, iterator_t *output_iterators, bool *output_masks, size_t count) override
Parallel find a contiguous array of keys.
Definition: HashmapCPU.h:204