Open3D (C++ API)  0.18.0
HashMap.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "open3d/core/Device.h"
11 #include "open3d/core/Dtype.h"
12 #include "open3d/core/Tensor.h"
14 
15 namespace open3d {
16 namespace core {
17 
18 class DeviceHashBackend;
19 
21 
22 class HashMap : public IsDevice {
23 public:
25  HashMap(int64_t init_capacity,
26  const Dtype& key_dtype,
27  const SizeVector& key_element_shape,
28  const Dtype& value_dtype,
29  const SizeVector& value_element_shapes,
30  const Device& device,
31  const HashBackendType& backend = HashBackendType::Default);
32 
36  HashMap(int64_t init_capacity,
37  const Dtype& key_dtype,
38  const SizeVector& key_element_shape,
39  const std::vector<Dtype>& dtypes_value,
40  const std::vector<SizeVector>& element_shapes_value,
41  const Device& device,
42  const HashBackendType& backend = HashBackendType::Default);
43 
45  ~HashMap() = default;
46 
48  void Reserve(int64_t capacity);
49 
58  std::pair<Tensor, Tensor> Insert(const Tensor& input_keys,
59  const Tensor& input_values);
60 
65  std::pair<Tensor, Tensor> Insert(
66  const Tensor& input_keys,
67  const std::vector<Tensor>& input_values_soa);
68 
74  std::pair<Tensor, Tensor> Activate(const Tensor& input_keys);
75 
80  std::pair<Tensor, Tensor> Find(const Tensor& input_keys);
81 
85  Tensor Erase(const Tensor& input_keys);
86 
90  Tensor GetActiveIndices() const;
91 
95  void Insert(const Tensor& input_keys,
96  const Tensor& input_values,
97  Tensor& output_buf_indices,
98  Tensor& output_masks);
99 
103  void Insert(const Tensor& input_keys,
104  const std::vector<Tensor>& input_values_soa,
105  Tensor& output_buf_indices,
106  Tensor& output_masks);
107 
111  void Activate(const Tensor& input_keys,
112  Tensor& output_buf_indices,
113  Tensor& output_masks);
114 
118  void Find(const Tensor& input_keys,
119  Tensor& output_buf_indices,
120  Tensor& output_masks);
121 
124  void Erase(const Tensor& input_keys, Tensor& output_masks);
125 
128  void GetActiveIndices(Tensor& output_buf_indices) const;
129 
131  void Clear();
132 
137  void Save(const std::string& file_name);
138 
141  static HashMap Load(const std::string& file_name);
142 
144  HashMap Clone() const;
145 
147  HashMap To(const Device& device, bool copy = false) const;
148 
150  int64_t Size() const;
151 
153  int64_t GetCapacity() const;
154 
156  int64_t GetBucketCount() const;
157 
159  Device GetDevice() const override;
160 
164  Tensor GetKeyTensor() const;
165 
169  std::vector<Tensor> GetValueTensors() const;
170 
174  Tensor GetValueTensor(size_t index = 0) const;
175 
177  std::vector<int64_t> BucketSizes() const;
178 
180  float LoadFactor() const;
181 
183  std::shared_ptr<DeviceHashBackend> GetDeviceHashBackend() const {
184  return device_hashmap_;
185  }
186 
187 protected:
188  void Init(int64_t init_capacity,
189  const Device& device,
190  const HashBackendType& backend);
191 
192  void InsertImpl(const Tensor& input_keys,
193  const std::vector<Tensor>& input_values_soa,
194  Tensor& output_buf_indices,
195  Tensor& output_masks,
196  bool is_activate_op = false);
197 
198  void CheckKeyLength(const Tensor& input_keys) const;
200  const Tensor& input_keys,
201  const std::vector<Tensor>& input_values_soa) const;
202  void CheckKeyCompatibility(const Tensor& input_keys) const;
204  const std::vector<Tensor>& input_values_soa) const;
205 
206  void PrepareIndicesOutput(Tensor& output_buf_indices, int64_t length) const;
207  void PrepareMasksOutput(Tensor& output_masks, int64_t length) const;
208 
209  std::pair<int64_t, std::vector<int64_t>> GetCommonValueSizeDivisor();
210 
211 private:
212  std::shared_ptr<DeviceHashBackend> device_hashmap_;
213 
214  Dtype key_dtype_;
215  SizeVector key_element_shape_;
216 
217  std::vector<Dtype> dtypes_value_;
218  std::vector<SizeVector> element_shapes_value_;
219 };
220 
221 } // namespace core
222 } // namespace open3d
bool copy
Definition: VtkUtils.cpp:73
Definition: Device.h:18
Definition: Dtype.h:20
Definition: HashMap.h:22
HashMap Clone() const
Clone the hash map with buffers.
Definition: HashMap.cpp:226
std::vector< Tensor > GetValueTensors() const
Definition: HashMap.cpp:274
void CheckKeyLength(const Tensor &input_keys) const
Definition: HashMap.cpp:356
void CheckKeyCompatibility(const Tensor &input_keys) const
Definition: HashMap.cpp:382
void CheckValueCompatibility(const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:397
void PrepareIndicesOutput(Tensor &output_buf_indices, int64_t length) const
Definition: HashMap.cpp:428
~HashMap()=default
Default destructor.
int64_t GetCapacity() const
Get the capacity of the hash map.
Definition: HashMap.cpp:257
float LoadFactor() const
Return size / bucket_count.
Definition: HashMap.cpp:316
void Save(const std::string &file_name)
Definition: HashMap.cpp:218
static HashMap Load(const std::string &file_name)
Definition: HashMap.cpp:222
int64_t GetBucketCount() const
Get the number of buckets of the internal hash map.
Definition: HashMap.cpp:259
Tensor GetKeyTensor() const
Definition: HashMap.cpp:265
void PrepareMasksOutput(Tensor &output_masks, int64_t length) const
Definition: HashMap.cpp:437
HashMap(int64_t init_capacity, const Dtype &key_dtype, const SizeVector &key_element_shape, const Dtype &value_dtype, const SizeVector &value_element_shapes, const Device &device, const HashBackendType &backend=HashBackendType::Default)
Initialize a hash map given a key and a value dtype and element shape.
Definition: HashMap.cpp:19
std::pair< int64_t, std::vector< int64_t > > GetCommonValueSizeDivisor()
Device GetDevice() const override
Get the device of the hash map.
Definition: HashMap.cpp:263
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashMap.h:183
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys, const Tensor &input_values)
Definition: HashMap.cpp:79
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
Definition: HashMap.cpp:99
std::pair< Tensor, Tensor > Activate(const Tensor &input_keys)
Definition: HashMap.cpp:93
Tensor GetValueTensor(size_t index=0) const
Definition: HashMap.cpp:293
void Init(int64_t init_capacity, const Device &device, const HashBackendType &backend)
Definition: HashMap.cpp:318
int64_t Size() const
Get the size (number of active entries) of the hash map.
Definition: HashMap.cpp:255
void CheckKeyValueLengthCompatibility(const Tensor &input_keys, const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:363
Tensor Erase(const Tensor &input_keys)
Definition: HashMap.cpp:105
HashMap To(const Device &device, bool copy=false) const
Convert the hash map to another device.
Definition: HashMap.cpp:228
void Reserve(int64_t capacity)
Reserve the internal hash map with the given capacity by rehashing.
Definition: HashMap.cpp:47
void InsertImpl(const Tensor &input_keys, const std::vector< Tensor > &input_values_soa, Tensor &output_buf_indices, Tensor &output_masks, bool is_activate_op=false)
Definition: HashMap.cpp:117
std::vector< int64_t > BucketSizes() const
Return number of elements per bucket.
Definition: HashMap.cpp:312
void Clear()
Clear stored map without reallocating the buffers.
Definition: HashMap.cpp:216
Tensor GetActiveIndices() const
Definition: HashMap.cpp:111
Definition: Device.h:88
Definition: SizeVector.h:69
Definition: Tensor.h:32
HashBackendType
Definition: HashMap.h:20
Definition: PinholeCameraIntrinsic.cpp:16