Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.16.0
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/Device.h"
30 #include "open3d/core/Dtype.h"
31 #include "open3d/core/Tensor.h"
33 
34 namespace open3d {
35 namespace core {
36 
37 class DeviceHashBackend;
38 
40 
41 class HashMap : public IsDevice {
42 public:
44  HashMap(int64_t init_capacity,
45  const Dtype& key_dtype,
46  const SizeVector& key_element_shape,
47  const Dtype& value_dtype,
48  const SizeVector& value_element_shapes,
49  const Device& device,
50  const HashBackendType& backend = HashBackendType::Default);
51 
55  HashMap(int64_t init_capacity,
56  const Dtype& key_dtype,
57  const SizeVector& key_element_shape,
58  const std::vector<Dtype>& dtypes_value,
59  const std::vector<SizeVector>& element_shapes_value,
60  const Device& device,
61  const HashBackendType& backend = HashBackendType::Default);
62 
64  ~HashMap() = default;
65 
67  void Reserve(int64_t capacity);
68 
77  std::pair<Tensor, Tensor> Insert(const Tensor& input_keys,
78  const Tensor& input_values);
79 
84  std::pair<Tensor, Tensor> Insert(
85  const Tensor& input_keys,
86  const std::vector<Tensor>& input_values_soa);
87 
93  std::pair<Tensor, Tensor> Activate(const Tensor& input_keys);
94 
99  std::pair<Tensor, Tensor> Find(const Tensor& input_keys);
100 
104  Tensor Erase(const Tensor& input_keys);
105 
109  Tensor GetActiveIndices() const;
110 
114  void Insert(const Tensor& input_keys,
115  const Tensor& input_values,
116  Tensor& output_buf_indices,
117  Tensor& output_masks);
118 
122  void Insert(const Tensor& input_keys,
123  const std::vector<Tensor>& input_values_soa,
124  Tensor& output_buf_indices,
125  Tensor& output_masks);
126 
130  void Activate(const Tensor& input_keys,
131  Tensor& output_buf_indices,
132  Tensor& output_masks);
133 
137  void Find(const Tensor& input_keys,
138  Tensor& output_buf_indices,
139  Tensor& output_masks);
140 
143  void Erase(const Tensor& input_keys, Tensor& output_masks);
144 
147  void GetActiveIndices(Tensor& output_buf_indices) const;
148 
150  void Clear();
151 
156  void Save(const std::string& file_name);
157 
160  static HashMap Load(const std::string& file_name);
161 
163  HashMap Clone() const;
164 
166  HashMap To(const Device& device, bool copy = false) const;
167 
169  int64_t Size() const;
170 
172  int64_t GetCapacity() const;
173 
175  int64_t GetBucketCount() const;
176 
178  Device GetDevice() const override;
179 
183  Tensor GetKeyTensor() const;
184 
188  std::vector<Tensor> GetValueTensors() const;
189 
193  Tensor GetValueTensor(size_t index = 0) const;
194 
196  std::vector<int64_t> BucketSizes() const;
197 
199  float LoadFactor() const;
200 
202  std::shared_ptr<DeviceHashBackend> GetDeviceHashBackend() const {
203  return device_hashmap_;
204  }
205 
206 protected:
207  void Init(int64_t init_capacity,
208  const Device& device,
209  const HashBackendType& backend);
210 
211  void InsertImpl(const Tensor& input_keys,
212  const std::vector<Tensor>& input_values_soa,
213  Tensor& output_buf_indices,
214  Tensor& output_masks,
215  bool is_activate_op = false);
216 
217  void CheckKeyLength(const Tensor& input_keys) const;
218  void CheckKeyValueLengthCompatibility(
219  const Tensor& input_keys,
220  const std::vector<Tensor>& input_values_soa) const;
221  void CheckKeyCompatibility(const Tensor& input_keys) const;
222  void CheckValueCompatibility(
223  const std::vector<Tensor>& input_values_soa) const;
224 
225  void PrepareIndicesOutput(Tensor& output_buf_indices, int64_t length) const;
226  void PrepareMasksOutput(Tensor& output_masks, int64_t length) const;
227 
228  std::pair<int64_t, std::vector<int64_t>> GetCommonValueSizeDivisor();
229 
230 private:
231  std::shared_ptr<DeviceHashBackend> device_hashmap_;
232 
233  Dtype key_dtype_;
234  SizeVector key_element_shape_;
235 
236  std::vector<Dtype> dtypes_value_;
237  std::vector<SizeVector> element_shapes_value_;
238 };
239 
240 } // namespace core
241 } // namespace open3d
HashBackendType
Definition: HashMap.h:39
Definition: Dtype.h:39
Definition: SizeVector.h:88
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashMap.h:202
Definition: Device.h:107
void To(const core::Tensor &src, core::Tensor &dst, double scale, double offset)
Definition: Image.cpp:36
Definition: Device.h:37
Definition: HashMap.h:41
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:51
bool copy
Definition: VtkUtils.cpp:89