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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! HyperLogLog sketches for estimating the number of distinct values.
//!
//! Use [`HllSketch`] to collect values and [`HllUnion`] to combine sketches.
//! [`HllType`] controls register storage; `lg_k` controls the register count and accuracy.
//!
//! The binary serialization format supports cross-language interchange.
//! Cross-language updates must also use compatible hashing; see
//! [`hash::value`](crate::hash::value).
//!
//! # Usage
//!
//! ```
//! use datasketches::common::NumStdDev;
//! use datasketches::hll::HllSketch;
//! use datasketches::hll::HllType;
//!
//! let mut sketch = HllSketch::new(12, HllType::Hll8).unwrap();
//! sketch.update("apple");
//! let upper = sketch.upper_bound(NumStdDev::Two);
//! assert!(upper >= sketch.estimate());
//! ```
//!
//! # Union
//!
//! ```
//! use datasketches::hll::HllSketch;
//! use datasketches::hll::HllType;
//! use datasketches::hll::HllUnion;
//!
//! let mut left = HllSketch::new(10, HllType::Hll8).unwrap();
//! let mut right = HllSketch::new(10, HllType::Hll8).unwrap();
//! left.update("apple");
//! right.update("banana");
//!
//! let mut union = HllUnion::new(10).unwrap();
//! union.update(&left);
//! union.update(&right);
//!
//! let result = union.to_sketch(HllType::Hll8);
//! assert!(result.estimate() >= 2.0);
//! ```
use Hash;
use crateMurmurHash3X64128;
pub use HllSketch;
pub use HllUnion;
/// Storage representation for HLL registers.
///
/// For the same `lg_k` and input, all three types produce the same estimates.
/// They trade memory usage against update speed.
const KEY_BITS_26: u32 = 26;
const KEY_MASK_26: u32 = - 1;
const COUPON_RSE_FACTOR: f64 = 0.409; // At transition point not the asymptote
const COUPON_RSE: f64 = COUPON_RSE_FACTOR / as f64;
const RESIZE_NUMERATOR: u32 = 3; // Resize at 3/4 = 75% load factor
const RESIZE_DENOMINATOR: u32 = 4;
/// A reusable hash of an input value for HLL sketches.
///
/// Compute a coupon once with [`Coupon::from_value`] and pass it to
/// [`HllSketch::update_with_coupon`] on sketches with any `lg_k` or [`HllType`].
///
/// # Examples
///
/// ```
/// use datasketches::hll::Coupon;
/// use datasketches::hll::HllSketch;
/// use datasketches::hll::HllType;
///
/// let c = Coupon::from_value("hello");
///
/// let mut sketch1 = HllSketch::new(10, HllType::Hll8).unwrap();
/// let mut sketch2 = HllSketch::new(12, HllType::Hll8).unwrap();
/// sketch1.update_with_coupon(c);
/// sketch2.update_with_coupon(c);
///
/// assert!(sketch1.estimate() >= 1.0);
/// assert!(sketch2.estimate() >= 1.0);
/// ```
; // Upper six bits: register value; lower 26 bits: slot.