Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
minhash.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31# ifndef MINHASH_H
32# define MINHASH_H
33
44# include <algorithm>
45# include <iterator>
46# include <limits>
47# include <cstdint>
48
49# include <ah-errors.H>
50# include <tpl_array.H>
51# include <hash-fct.H>
52
53namespace Aleph
54{
63 template <typename T>
64 class MinHash
65 {
66 size_t k_;
70 public:
75 explicit MinHash(size_t k = 128)
76 : k_(k)
77 {
78 ah_domain_error_if(k == 0) << "MinHash: k must be > 0";
79
81 for (size_t i = 0; i < k; ++i)
82 signature_.append(std::numeric_limits<uint64_t>::max());
84
85 // Use dft_hash_fct to generate independent seeds
86 for (size_t i = 0; i < k_; ++i)
87 seeds_.append(static_cast<uint32_t>(dft_hash_fct(i, 0xdeadbeef)));
88 }
89
94 MinHash(const MinHash & other) = default;
95
108 MinHash(MinHash && other) : MinHash(other.k_)
109 {
110 swap(other);
111 }
112
118 MinHash & operator = (const MinHash & other) = default;
119
126 MinHash & operator = (MinHash && other) noexcept
127 {
128 swap(other);
129 return *this;
130 }
131
135 void swap(MinHash & other) noexcept
136 {
137 std::swap(k_, other.k_);
138 signature_.swap(other.signature_);
139 seeds_.swap(other.seeds_);
140 }
141
147 void update(const T & val)
148 {
149 for (size_t i = 0; i < k_; ++i)
150 if (const auto h = static_cast<uint64_t>(murmur3hash(val, seeds_[i])); h < signature_[i])
151 signature_(i) = h;
152 }
153
164 template <std::input_iterator Itor>
165 void update(Itor beg, const Itor & end)
166 {
167 while (beg != end)
168 {
169 update(*beg);
170 ++beg;
171 }
172 }
173
179 [[nodiscard]] double similarity(const MinHash & other) const
180 {
181 ah_domain_error_if(k_ != other.k_)
182 << "MinHash::similarity: signature size mismatch";
183
184 size_t matches = 0;
185 for (size_t i = 0; i < k_; ++i)
186 if (signature_[i] == other.signature_[i])
187 ++matches;
188
189 return static_cast<double>(matches) / static_cast<double>(k_);
190 }
191
197 [[nodiscard]] const Array<uint64_t> &get_signature() const noexcept
198 {
199 return signature_;
200 }
201
206 void clear()
207 {
208 constexpr uint64_t inf = std::numeric_limits<uint64_t>::max();
209 for (size_t i = 0; i < k_; ++i)
210 signature_(i) = inf;
211 }
212
222 MinHash & merge(const MinHash & other)
223 {
224 ah_domain_error_if(k_ != other.k_)
225 << "MinHash::merge: signature size mismatch ("
226 << k_ << " vs " << other.k_ << ")";
227 for (size_t i = 0; i < k_; ++i)
228 signature_(i) = std::min(signature_(i), other.signature_(i));
229 return *this;
230 }
231
237 [[nodiscard]] size_t size() const noexcept { return k_; }
238 };
239} // namespace Aleph
240
241# endif // MINHASH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
long double h
Definition btreepic.C:154
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
MinHash signature generator.
Definition minhash.H:65
MinHash & merge(const MinHash &other)
Merge another MinHash signature into this one.
Definition minhash.H:222
MinHash & operator=(const MinHash &other)=default
Copy the size and the signature of another MinHash.
size_t size() const noexcept
Number of hash functions used in the signature.
Definition minhash.H:237
void clear()
Reset the signature to the initial all-maximum state.
Definition minhash.H:206
void update(const T &val)
Add an element to the set.
Definition minhash.H:147
double similarity(const MinHash &other) const
Estimate Jaccard similarity with another signature.
Definition minhash.H:179
MinHash(const MinHash &other)=default
Copy the size and the signature of another MinHash.
MinHash(MinHash &&other)
Take the signature of another MinHash, which is left as an empty one of the same size.
Definition minhash.H:108
MinHash(size_t k=128)
Construct with signature size.
Definition minhash.H:75
void swap(MinHash &other) noexcept
Exchange in O(1) the state of this MinHash and another.
Definition minhash.H:135
size_t k_
Signature size.
Definition minhash.H:66
const Array< uint64_t > & get_signature() const noexcept
Returns the current signature.
Definition minhash.H:197
Array< uint64_t > signature_
Minimized hash values.
Definition minhash.H:67
Array< uint32_t > seeds_
Random seeds for hash functions.
Definition minhash.H:68
void update(Itor beg, const Itor &end)
Add all elements in a range to the MinHash signature.
Definition minhash.H:165
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
Definition hash-fct.H:1030
size_t murmur3hash(const Key &key, std::uint32_t seed)
Definition hash-fct.H:361
static int * k
Dynamic array container with automatic resizing.