Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HashMap.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include "open3d/core/Dtype.h"
30 #include "open3d/core/Tensor.h"
32 
33 namespace open3d {
34 namespace core {
35 
36 class DeviceHashBackend;
37 
39 
40 class HashMap {
41 public:
43  HashMap(int64_t init_capacity,
44  const Dtype& key_dtype,
45  const SizeVector& key_element_shape,
46  const Dtype& value_dtype,
47  const SizeVector& value_element_shapes,
48  const Device& device,
49  const HashBackendType& backend = HashBackendType::Default);
50 
54  HashMap(int64_t init_capacity,
55  const Dtype& key_dtype,
56  const SizeVector& key_element_shape,
57  const std::vector<Dtype>& dtypes_value,
58  const std::vector<SizeVector>& element_shapes_value,
59  const Device& device,
60  const HashBackendType& backend = HashBackendType::Default);
61 
63  ~HashMap() = default;
64 
66  void Reserve(int64_t capacity);
67 
76  std::pair<Tensor, Tensor> Insert(const Tensor& input_keys,
77  const Tensor& input_values);
78 
83  std::pair<Tensor, Tensor> Insert(
84  const Tensor& input_keys,
85  const std::vector<Tensor>& input_values_soa);
86 
92  std::pair<Tensor, Tensor> Activate(const Tensor& input_keys);
93 
98  std::pair<Tensor, Tensor> Find(const Tensor& input_keys);
99 
103  Tensor Erase(const Tensor& input_keys);
104 
108  Tensor GetActiveIndices() const;
109 
113  void Insert(const Tensor& input_keys,
114  const Tensor& input_values,
115  Tensor& output_buf_indices,
116  Tensor& output_masks);
117 
121  void Insert(const Tensor& input_keys,
122  const std::vector<Tensor>& input_values_soa,
123  Tensor& output_buf_indices,
124  Tensor& output_masks);
125 
129  void Activate(const Tensor& input_keys,
130  Tensor& output_buf_indices,
131  Tensor& output_masks);
132 
136  void Find(const Tensor& input_keys,
137  Tensor& output_buf_indices,
138  Tensor& output_masks);
139 
142  void Erase(const Tensor& input_keys, Tensor& output_masks);
143 
146  void GetActiveIndices(Tensor& output_buf_indices) const;
147 
149  void Clear();
150 
155  void Save(const std::string& file_name);
156 
159  static HashMap Load(const std::string& file_name);
160 
162  HashMap Clone() const;
163 
165  HashMap To(const Device& device, bool copy = false) const;
166 
168  int64_t Size() const;
169 
171  int64_t GetCapacity() const;
172 
174  int64_t GetBucketCount() const;
175 
177  Device GetDevice() const;
178 
182  Tensor GetKeyTensor() const;
183 
187  std::vector<Tensor> GetValueTensors() const;
188 
192  Tensor GetValueTensor(size_t index = 0) const;
193 
195  std::vector<int64_t> BucketSizes() const;
196 
198  float LoadFactor() const;
199 
201  std::shared_ptr<DeviceHashBackend> GetDeviceHashBackend() const {
202  return device_hashmap_;
203  }
204 
205 protected:
206  void Init(int64_t init_capacity,
207  const Device& device,
208  const HashBackendType& backend);
209 
210  void InsertImpl(const Tensor& input_keys,
211  const std::vector<Tensor>& input_values_soa,
212  Tensor& output_buf_indices,
213  Tensor& output_masks,
214  bool is_activate_op = false);
215 
216  void CheckKeyLength(const Tensor& input_keys) const;
217  void CheckKeyValueLengthCompatibility(
218  const Tensor& input_keys,
219  const std::vector<Tensor>& input_values_soa) const;
220  void CheckKeyCompatibility(const Tensor& input_keys) const;
221  void CheckValueCompatibility(
222  const std::vector<Tensor>& input_values_soa) const;
223 
224  void PrepareIndicesOutput(Tensor& output_buf_indices, int64_t length) const;
225  void PrepareMasksOutput(Tensor& output_masks, int64_t length) const;
226 
227  std::pair<int64_t, std::vector<int64_t>> GetCommonValueSizeDivisor();
228 
229 private:
230  std::shared_ptr<DeviceHashBackend> device_hashmap_;
231 
232  Dtype key_dtype_;
233  SizeVector key_element_shape_;
234 
235  std::vector<Dtype> dtypes_value_;
236  std::vector<SizeVector> element_shapes_value_;
237 };
238 
239 } // namespace core
240 } // namespace open3d
HashBackendType
Definition: HashMap.h:38
Definition: Dtype.h:39
Definition: SizeVector.h:79
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashMap.h:201
Definition: Device.h:39
Definition: HashMap.h:40
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:50
void To(const core::Tensor &src_im, core::Tensor &dst_im, double scale, double offset)
Definition: IPPImage.cpp:46