1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
use crate::array_default::{ArrayDefault, ArrayIter};
use crate::precisions::{Precision, WordType};
use crate::prelude::HyperLogLogTrait;
use crate::primitive::Primitive;
use core::hash::Hash;
#[derive(Clone, Debug, Copy)]
/// A probabilistic algorithm for estimating the number of distinct elements in a set.
///
/// HyperLogLog is a probabilistic algorithm designed to estimate the number
/// of distinct elements in a set. It does so by taking advantage of the fact
/// that the representation of an element can be transformed into a uniform
/// distribution in a space with a fixed range.
///
/// HyperLogLog works by maintaining a fixed-sized register array,
/// where each register holds a counter. The algorithm splits the input set into subsets,
/// applies a hash function to each element in the subset, and then updates
/// the corresponding counter in the register array.
///
/// HyperLogLog uses a trick called "probabilistic counting" to estimate
/// the number of distinct elements in the set. Each register counter is converted
/// to a binary string, and the algorithm counts the number of leading zeros in
/// each binary string. The maximum number of leading zeros over all counters
/// is used to estimate the number of distinct elements in the set.
///
/// HyperLogLog has a tunable parameter called precision that determines
/// the accuracy of the algorithm. Higher precision leads to better accuracy,
/// but requires more memory. The error rate of the algorithm is guaranteed
/// to be within a certain bound, depending on the chosen precision.
///
/// # Examples
///
/// ```
/// use hyperloglog_rs::prelude::*;
///
/// let mut hll = HyperLogLog::<Precision12, 6>::default();
/// hll.insert(&"apple");
/// hll.insert(&"banana");
/// hll.insert(&"cherry");
///
/// let estimated_cardinality = hll.estimate_cardinality();
/// assert!(estimated_cardinality >= 3.0_f32 * 0.9 &&
/// estimated_cardinality <= 3.0_f32 * 1.1);
/// ```
///
/// # Citations
///
/// This implementation is based on the following papers:
///
/// * Flajolet, Philippe, et al. "HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm." DMTCS Proceedings 1 (2007): 127-146.
/// * Heule, Stefan, Marc Nunkesser, and Alexander Hall. "HyperLogLog in practice: algorithmic engineering of a state of the art cardinality estimation algorithm." Proceedings of the 16th International Conference on Extending Database Technology. 2013.
///
pub struct HyperLogLog<PRECISION: Precision + WordType<BITS>, const BITS: usize> {
pub(crate) words: PRECISION::Words,
pub(crate) number_of_zero_registers: PRECISION::NumberOfZeros,
}
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize> HyperLogLog<PRECISION, BITS> {
/// Create a new HyperLogLog counter.
fn new() -> Self {
Self {
words: PRECISION::Words::default_array(),
number_of_zero_registers: PRECISION::NumberOfZeros::reverse(
PRECISION::NUMBER_OF_REGISTERS,
),
}
}
/// Create a new HyperLogLog counter from an array of words.
///
/// # Arguments
/// * `words` - An array of u64 words to use for the HyperLogLog counter.
///
/// # Returns
/// A new HyperLogLog counter initialized with the given words.
///
/// # Examples
///
/// ```rust
/// use hyperloglog_rs::prelude::*;
///
/// let words = [0_u32; 4];
/// let hll = HyperLogLog::<Precision4, 6>::from_words(&words);
/// assert_eq!(hll.len(), 16);
/// ```
pub fn from_words(words: &PRECISION::Words) -> Self {
let number_of_zero_registers =
PRECISION::NumberOfZeros::reverse(words.iter_elements().fold(
0,
|number_of_zero_registers, word| {
(0..Self::NUMBER_OF_REGISTERS_IN_WORD).fold(
number_of_zero_registers,
|number_of_zero_registers, i| {
let register = (word >> (i * BITS)) & Self::LOWER_REGISTER_MASK;
number_of_zero_registers + (register == 0) as usize
},
)
},
)) - PRECISION::NumberOfZeros::reverse(Self::get_number_of_padding_registers());
Self {
words: *words,
number_of_zero_registers,
}
}
/// Create a new HyperLogLog counter from an array of registers.
///
/// # Arguments
///
/// * `registers` - An array of u32 registers to use for the HyperLogLog counter.
///
/// # Returns
///
/// A new HyperLogLog counter initialized with the given registers.
///
/// # Examples
///
/// ```
/// use hyperloglog_rs::prelude::*;
///
/// let registers = [0_u32; 1 << 4];
/// let hll = HyperLogLog::<Precision4, 6>::from_registers(®isters);
/// assert_eq!(hll.len(), 1 << 4);
/// ```
pub fn from_registers(registers: &[u32]) -> Self {
debug_assert!(
registers.len() == PRECISION::NUMBER_OF_REGISTERS,
"We expect {} registers, but got {}",
PRECISION::NUMBER_OF_REGISTERS,
registers.len()
);
let mut words = PRECISION::Words::default_array();
let number_of_zero_registers = PRECISION::NumberOfZeros::reverse(
words
.iter_elements_mut()
.zip(registers.chunks(Self::NUMBER_OF_REGISTERS_IN_WORD))
.fold(0, |number_of_zero_registers, (word, word_registers)| {
word_registers.iter().copied().enumerate().fold(
number_of_zero_registers,
|number_of_zero_registers, (i, register)| {
debug_assert!(
register <= Self::LOWER_REGISTER_MASK,
"Register value {} is too large for the given number of bits {}",
register,
BITS
);
*word |= register << (i * BITS);
number_of_zero_registers + (register == 0) as usize
},
)
}),
);
Self {
words,
number_of_zero_registers,
}
}
#[inline(always)]
/// Adds an element to the HyperLogLog counter.
///
/// # Arguments
/// * `rhs` - The element to add.
///
/// # Examples
///
/// ```
/// use hyperloglog_rs::prelude::*;
///
/// let mut hll = HyperLogLog::<Precision10, 6>::default();
///
/// hll.insert("Hello");
/// hll.insert("World");
///
/// assert!(hll.estimate_cardinality() >= 2.0);
/// ```
///
/// # Performance
///
/// The performance of this function depends on the size of the HyperLogLog counter (`N`), the number
/// of distinct elements in the input, and the hash function used to hash elements. For a given value of `N`,
/// the function has an average time complexity of O(1) and a worst-case time complexity of O(log N).
/// However, the actual time complexity may vary depending on the distribution of the hashed elements.
///
/// # Errors
///
/// This function does not return any errors.
pub fn insert<T: Hash>(&mut self, rhs: T) {
let (mut hash, index) = self.get_hash_and_index::<T>(&rhs);
// We need to add ones to the hash to make sure that the
// the number of zeros we obtain afterwards is never higher
// than the maximal value that may be represented in a register
// with BITS bits.
hash |= if BITS < 6 {
1 << (64 - ((1 << BITS) - 1))
} else {
1 << (PRECISION::EXPONENT - 1)
};
// Count leading zeros.
let number_of_zeros: u32 = 1 + hash.leading_zeros();
debug_assert!(
number_of_zeros < (1 << BITS),
concat!(
"The number of leading zeros {} must be less than the number of bits {}. ",
"You have obtained this values starting from the hash {:064b} and the precision {}."
),
number_of_zeros,
1 << BITS,
hash,
PRECISION::EXPONENT
);
// Calculate the position of the register in the internal buffer array.
let word_position = index / Self::NUMBER_OF_REGISTERS_IN_WORD;
let register_position_in_u32 = index - word_position * Self::NUMBER_OF_REGISTERS_IN_WORD;
debug_assert!(
word_position < self.get_words().len(),
concat!(
"The word_position {} must be less than the number of words {}. ",
"You have obtained this values starting from the index {} and the number of registers in word {}. ",
"We currently have {} registers. Currently using precision {} and number of bits {}."
),
word_position,
self.get_words().len(),
index,
Self::NUMBER_OF_REGISTERS_IN_WORD,
PRECISION::NUMBER_OF_REGISTERS,
PRECISION::EXPONENT,
BITS
);
// Extract the current value of the register at `index`.
let register_value: u32 = (self.get_words()[word_position]
>> (register_position_in_u32 * BITS))
& Self::LOWER_REGISTER_MASK;
// Otherwise, update the register using a bit mask.
if number_of_zeros > register_value {
self.words[word_position] &=
!(Self::LOWER_REGISTER_MASK << (register_position_in_u32 * BITS));
self.words[word_position] |= number_of_zeros << (register_position_in_u32 * BITS);
self.number_of_zero_registers -=
PRECISION::NumberOfZeros::reverse((register_value == 0) as usize);
// We check that the word we have edited maintains that the padding bits are all zeros
// and have not been manipulated in any way. If these bits were manipulated, it would mean
// that we have a bug in the code.
debug_assert!(
self.words[word_position] & Self::PADDING_BITS_MASK == 0,
concat!(
"The padding bits of the word {} must be all zeros. ",
"We have obtained {} instead."
),
self.words[word_position],
self.words[word_position] & Self::PADDING_BITS_MASK
);
}
}
}
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize> Eq for HyperLogLog<PRECISION, BITS> {
fn assert_receiver_is_total_eq(&self) {
// This is a no-op because we know that `Self` is `Eq`.
}
}
/// Implements PartialEq for HyperLogLog.
///
/// # Implementative details
/// Two HyperLogLog counters are considered equal if they have the same words.
///
/// # Examples
///
/// ```
/// # use hyperloglog_rs::prelude::*;
///
/// let mut hll1 = HyperLogLog::<Precision14, 5>::default();
/// hll1.insert(&2);
///
/// let mut hll2 = HyperLogLog::<Precision14, 5>::default();
/// hll2.insert(&2);
/// hll2.insert(&3);
///
/// assert_ne!(hll1, hll2);
///
/// hll1 |= hll2;
///
/// assert_eq!(hll1, hll2);
/// ```
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize> PartialEq
for HyperLogLog<PRECISION, BITS>
{
/// Returns whether the two HyperLogLog counters are equal.
fn eq(&self, other: &Self) -> bool {
self.words == other.words
}
}
/// Implements the Default trait for HyperLogLog.
///
/// HyperLogLog is a probabilistic cardinality estimator that uses a fixed
/// amount of memory to estimate the number of distinct elements in a set.
///
/// # Examples
///
/// ```rust
/// # use hyperloglog_rs::prelude::*;
///
/// let hll: HyperLogLog<Precision10, 6> = Default::default();
/// assert_eq!(hll.len(), 1024);
/// ```
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize> Default
for HyperLogLog<PRECISION, BITS>
{
/// Returns a new HyperLogLog instance with default configuration settings.
fn default() -> Self {
Self::new()
}
}
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize, T: Hash> From<T>
for HyperLogLog<PRECISION, BITS>
{
/// Create a new HyperLogLog counter from a value.
///
/// This method creates a new empty HyperLogLog counter and inserts the hash
/// of the given value into it. The value can be any type that implements
/// the `Hash` trait.
///
/// # Examples
///
/// ```
/// # use hyperloglog_rs::prelude::*;
///
/// let hll = HyperLogLog::<Precision14, 5>::from("test");
///
/// assert!(hll.estimate_cardinality() >= 1.0_f32);
/// assert!(!hll.is_empty());
/// assert!(hll.may_contain(&"test"));
/// ```
fn from(value: T) -> Self {
let mut hll = Self::new();
hll.insert(value);
hll
}
}
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize> HyperLogLogTrait<PRECISION, BITS>
for HyperLogLog<PRECISION, BITS>
{
#[inline(always)]
/// Returns the number of registers with zero values. This value is used for computing a small
/// correction when estimating the cardinality of a small set.
///
/// # Examples
///
/// ```
/// # use hyperloglog_rs::prelude::*;
///
/// // Create a new HyperLogLog counter with precision 14 and 5 bits per register.
/// let mut hll = HyperLogLog::<Precision14, 5>::default();
///
/// // Add some elements to the counter.
/// hll.insert(&1);
/// hll.insert(&2);
/// hll.insert(&3);
///
/// // Get the number of zero registers.
/// let number_of_zero_registers = hll.get_number_of_zero_registers();
///
/// assert_eq!(number_of_zero_registers, 16381);
/// ```
fn get_number_of_zero_registers(&self) -> usize {
self.number_of_zero_registers.convert()
}
#[inline(always)]
/// Returns the array of words of the HyperLogLog counter.
fn get_words(&self) -> &PRECISION::Words {
&self.words
}
}
impl<PRECISION: Precision + WordType<BITS>, const BITS: usize, A: Hash> core::iter::FromIterator<A>
for HyperLogLog<PRECISION, BITS>
{
#[inline(always)]
/// Creates a new HyperLogLog counter and adds all elements from an iterator to it.
///
/// # Examples
///
/// ```
/// use hyperloglog_rs::prelude::*;
///
/// let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
/// let hll: HyperLogLog<Precision12, 5> = data.iter().collect();
/// assert!(
/// hll.estimate_cardinality() > 0.9 * data.len() as f32,
/// concat!(
/// "The estimate is too low, expected ",
/// "at least {}, got {}",
/// ),
/// 0.9 * data.len() as f32,
/// hll.estimate_cardinality()
/// );
/// assert!(
/// hll.estimate_cardinality() < 1.1 * data.len() as f32,
/// concat!(
/// "The estimate is too high, expected ",
/// "at most {}, got {}",
/// ),
/// 1.1 * data.len() as f32,
/// hll.estimate_cardinality()
/// );
/// ```
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
let mut hll = Self::new();
for item in iter {
hll.insert(item);
}
hll
}
}