Skip to content

Commit e705bed

Browse files
committed
Merging and Count
Added Merging functionality Added Count property returning rounded number.
1 parent 5c12574 commit e705bed

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using HLL.NET.Hashing;
2+
using HLL.NET.Models;
3+
4+
namespace HLL.NET.Tests
5+
{
6+
public class HyperLogLogMergeTests
7+
{
8+
private readonly IHasher<string> _hasher = new StringHasher();
9+
10+
[Fact]
11+
public void Merge_WithDifferentPrecisions_ShouldThrow()
12+
{
13+
var hll1 = new HyperLogLog<string>(12, _hasher);
14+
var hll2 = new HyperLogLog<string>(14, _hasher);
15+
16+
Assert.Throws<InvalidOperationException>(() => hll1.Merge(hll2));
17+
}
18+
19+
[Fact]
20+
public void Merge_WithSamePrecision_ShouldCombineRegisters()
21+
{
22+
var hll1 = new HyperLogLog<string>(12, _hasher);
23+
var hll2 = new HyperLogLog<string>(12, _hasher);
24+
25+
for (int i = 0; i < 500; i++)
26+
hll1.Add($"userA_{i}");
27+
28+
for (int i = 0; i < 700; i++)
29+
hll2.Add($"userB_{i}");
30+
31+
double estimate1 = hll1.Estimate();
32+
double estimate2 = hll2.Estimate();
33+
34+
hll1.Merge(hll2);
35+
36+
double mergedEstimate = hll1.Estimate();
37+
38+
// Merged estimate should be at least as large as either individual estimate
39+
Assert.True(mergedEstimate >= estimate1, $"Merged estimate {mergedEstimate} is less than estimate1 {estimate1}");
40+
Assert.True(mergedEstimate >= estimate2, $"Merged estimate {mergedEstimate} is less than estimate2 {estimate2}");
41+
}
42+
}
43+
}

HLL.NET/HLL.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackageId>HLL.NET</PackageId>
6-
<Version>1.0.4</Version>
6+
<Version>1.0.5</Version>
77
<Authors>MCUnderground</Authors>
88
<Description>A simple and efficient C# implementation of HyperLogLog for approximate cardinality estimation.</Description>
99
<PackageLicenseExpression>MIT</PackageLicenseExpression>

HLL.NET/HyperLogLog.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ public void Add(T item)
4545
_registers[index].Update((byte)leadingZeros);
4646
}
4747

48+
public void Merge(HyperLogLog<T> other)
49+
{
50+
if (other == null)
51+
throw new ArgumentNullException(nameof(other));
52+
53+
if (Precision != other.Precision)
54+
throw new InvalidOperationException("Cannot merge HyperLogLogs with different precisions.");
55+
56+
for (int i = 0; i < _numRegisters; i++)
57+
{
58+
_registers[i].Update(other._registers[i].Value);
59+
}
60+
61+
}
62+
63+
public ulong Count => (ulong)Math.Round(Estimate());
64+
4865
public double Estimate()
4966
{
5067
double alphaMM = GetAlphaMM();

0 commit comments

Comments
 (0)