Open3D (C++ API)  0.15.1
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
33namespace open3d {
34namespace core {
35
36class DeviceHashBackend;
37
39
40class HashMap {
41public:
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,
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,
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
205protected:
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;
218 const Tensor& input_keys,
219 const std::vector<Tensor>& input_values_soa) const;
220 void CheckKeyCompatibility(const Tensor& input_keys) const;
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
229private:
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
Definition: Device.h:39
Definition: Dtype.h:39
Definition: HashMap.h:40
HashMap Clone() const
Clone the hash map with buffers.
Definition: HashMap.cpp:245
std::vector< Tensor > GetValueTensors() const
Definition: HashMap.cpp:293
void CheckKeyLength(const Tensor &input_keys) const
Definition: HashMap.cpp:376
void CheckKeyCompatibility(const Tensor &input_keys) const
Definition: HashMap.cpp:402
void CheckValueCompatibility(const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:417
void PrepareIndicesOutput(Tensor &output_buf_indices, int64_t length) const
Definition: HashMap.cpp:448
~HashMap()=default
Default destructor.
int64_t GetCapacity() const
Get the capacity of the hash map.
Definition: HashMap.cpp:276
float LoadFactor() const
Return size / bucket_count.
Definition: HashMap.cpp:335
void Save(const std::string &file_name)
Definition: HashMap.cpp:237
static HashMap Load(const std::string &file_name)
Definition: HashMap.cpp:241
int64_t GetBucketCount() const
Get the number of buckets of the internal hash map.
Definition: HashMap.cpp:278
Tensor GetKeyTensor() const
Definition: HashMap.cpp:284
void PrepareMasksOutput(Tensor &output_masks, int64_t length) const
Definition: HashMap.cpp:457
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:38
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys, const Tensor &input_values)
Definition: HashMap.cpp:98
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
Definition: HashMap.cpp:118
std::pair< int64_t, std::vector< int64_t > > GetCommonValueSizeDivisor()
Device GetDevice() const
Get the device of the hash map.
Definition: HashMap.cpp:282
std::pair< Tensor, Tensor > Activate(const Tensor &input_keys)
Definition: HashMap.cpp:112
Tensor GetValueTensor(size_t index=0) const
Definition: HashMap.cpp:312
void Init(int64_t init_capacity, const Device &device, const HashBackendType &backend)
Definition: HashMap.cpp:337
int64_t Size() const
Get the size (number of active entries) of the hash map.
Definition: HashMap.cpp:274
void CheckKeyValueLengthCompatibility(const Tensor &input_keys, const std::vector< Tensor > &input_values_soa) const
Definition: HashMap.cpp:383
Tensor Erase(const Tensor &input_keys)
Definition: HashMap.cpp:124
HashMap To(const Device &device, bool copy=false) const
Convert the hash map to another device.
Definition: HashMap.cpp:247
void Reserve(int64_t capacity)
Reserve the internal hash map with the given capacity by rehashing.
Definition: HashMap.cpp:66
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:136
std::vector< int64_t > BucketSizes() const
Return number of elements per bucket.
Definition: HashMap.cpp:331
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashMap.h:201
void Clear()
Clear stored map without reallocating the buffers.
Definition: HashMap.cpp:235
Tensor GetActiveIndices() const
Definition: HashMap.cpp:130
Definition: SizeVector.h:79
Definition: SlabNodeManager.h:58
Definition: Tensor.h:51
HashBackendType
Definition: HashMap.h:38
Definition: PinholeCameraIntrinsic.cpp:35