ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HashSet.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Helper.h>
11 #include <Logging.h>
12 
16 
17 namespace cloudViewer {
18 namespace core {
19 
20 HashSet::HashSet(int64_t init_capacity,
21  const Dtype& key_dtype,
22  const SizeVector& key_element_shape,
23  const Device& device,
24  const HashBackendType& backend) {
25  internal_ = std::make_shared<HashMap>(
26  init_capacity, key_dtype, key_element_shape, std::vector<Dtype>{},
27  std::vector<SizeVector>{}, device, backend);
28 }
29 
30 void HashSet::Reserve(int64_t capacity) { return internal_->Reserve(capacity); }
31 
32 std::pair<Tensor, Tensor> HashSet::Insert(const Tensor& input_keys) {
33  Tensor output_buf_indices, output_masks;
34  Insert(input_keys, output_buf_indices, output_masks);
35  return std::make_pair(output_buf_indices, output_masks);
36 }
37 
38 std::pair<Tensor, Tensor> HashSet::Find(const Tensor& input_keys) {
39  Tensor output_buf_indices, output_masks;
40  Find(input_keys, output_buf_indices, output_masks);
41  return std::make_pair(output_buf_indices, output_masks);
42 }
43 
44 Tensor HashSet::Erase(const Tensor& input_keys) {
45  Tensor output_masks;
46  Erase(input_keys, output_masks);
47  return output_masks;
48 }
49 
51  Tensor output_buf_indices;
52  GetActiveIndices(output_buf_indices);
53  return output_buf_indices;
54 }
55 
56 void HashSet::Insert(const Tensor& input_keys,
57  Tensor& output_buf_indices,
58  Tensor& output_masks) {
59  internal_->Insert(input_keys, std::vector<Tensor>{}, output_buf_indices,
60  output_masks);
61 }
62 
63 void HashSet::Find(const Tensor& input_keys,
64  Tensor& output_buf_indices,
65  Tensor& output_masks) {
66  internal_->Find(input_keys, output_buf_indices, output_masks);
67 }
68 
69 void HashSet::Erase(const Tensor& input_keys, Tensor& output_masks) {
70  internal_->Erase(input_keys, output_masks);
71 }
72 
73 void HashSet::GetActiveIndices(Tensor& output_buf_indices) const {
74  internal_->GetActiveIndices(output_buf_indices);
75 }
76 
77 void HashSet::Clear() { internal_->Clear(); }
78 
79 void HashSet::Save(const std::string& file_name) {
80  t::io::WriteHashMap(file_name, *internal_);
81 }
82 
83 HashSet HashSet::Load(const std::string& file_name) {
84  HashMap internal = t::io::ReadHashMap(file_name);
85  return HashSet(internal);
86 }
87 
89  HashMap internal_cloned = internal_->To(GetDevice(), /*copy=*/true);
90  return HashSet(internal_cloned);
91 }
92 
93 HashSet HashSet::To(const Device& device, bool copy) const {
94  HashMap internal_converted = internal_->To(device, copy);
95  return HashSet(internal_converted);
96 }
97 
98 int64_t HashSet::Size() const { return internal_->Size(); }
99 
100 int64_t HashSet::GetCapacity() const { return internal_->GetCapacity(); }
101 
102 int64_t HashSet::GetBucketCount() const { return internal_->GetBucketCount(); }
103 
104 Device HashSet::GetDevice() const { return internal_->GetDevice(); }
105 
106 Tensor HashSet::GetKeyTensor() const { return internal_->GetKeyTensor(); }
107 
108 std::vector<int64_t> HashSet::BucketSizes() const {
109  return internal_->BucketSizes();
110 };
111 
112 float HashSet::LoadFactor() const { return internal_->LoadFactor(); }
113 
114 std::shared_ptr<DeviceHashBackend> HashSet::GetDeviceHashBackend() const {
115  return internal_->GetDeviceHashBackend();
116 }
117 
118 HashSet::HashSet(const HashMap& internal_hashmap) {
119  internal_ = std::make_shared<HashMap>(internal_hashmap);
120 }
121 
122 } // namespace core
123 } // namespace cloudViewer
bool copy
Definition: VtkUtils.cpp:74
void Save(const std::string &file_name)
Definition: HashSet.cpp:79
std::pair< Tensor, Tensor > Find(const Tensor &input_keys)
Definition: HashSet.cpp:38
int64_t Size() const
Get the size (number of active entries) of the hash set.
Definition: HashSet.cpp:98
void Clear()
Clear stored map without reallocating the buffers.
Definition: HashSet.cpp:77
Tensor GetKeyTensor() const
Definition: HashSet.cpp:106
HashSet(int64_t init_capacity, const Dtype &key_dtype, const SizeVector &key_element_shape, const Device &device, const HashBackendType &backend=HashBackendType::Default)
Initialize a hash set given a key dtype and element shape.
Definition: HashSet.cpp:20
static HashSet Load(const std::string &file_name)
Load active keys and values from a npz file that contains 'key'.
Definition: HashSet.cpp:83
void Reserve(int64_t capacity)
Reserve the internal hash map with the capcity by rehashing.
Definition: HashSet.cpp:30
int64_t GetBucketCount() const
Get the number of buckets of the internal hash set.
Definition: HashSet.cpp:102
std::shared_ptr< DeviceHashBackend > GetDeviceHashBackend() const
Return the implementation of the device hash backend.
Definition: HashSet.cpp:114
int64_t GetCapacity() const
Get the capacity of the hash set.
Definition: HashSet.cpp:100
Tensor GetActiveIndices() const
Definition: HashSet.cpp:50
float LoadFactor() const
Return size / bucket_count.
Definition: HashSet.cpp:112
std::pair< Tensor, Tensor > Insert(const Tensor &input_keys)
Definition: HashSet.cpp:32
HashSet To(const Device &device, bool copy=false) const
Convert the hash set to another device.
Definition: HashSet.cpp:93
HashSet Clone() const
Clone the hash set with buffers.
Definition: HashSet.cpp:88
std::vector< int64_t > BucketSizes() const
Return number of elements per bucket.
Definition: HashSet.cpp:108
Tensor Erase(const Tensor &input_keys)
Definition: HashSet.cpp:44
Device GetDevice() const override
Get the device of the hash set.
Definition: HashSet.cpp:104
Helper functions for the ml ops.
CLOUDVIEWER_HOST_DEVICE Pair< First, Second > make_pair(const First &_first, const Second &_second)
Definition: SlabTraits.h:49
core::HashMap ReadHashMap(const std::string &file_name)
Definition: HashMapIO.cpp:48
void WriteHashMap(const std::string &file_name, const core::HashMap &hashmap)
Definition: HashMapIO.cpp:17
Generic file read and write utility for python interface.