Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C# implementation for the approximate counting algorithm #865

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contents/approximate_counting/approximate_counting.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ As we do not have any objects to count, we will instead simulate the counting wi
[import, lang:"julia"](code/julia/approximate_counting.jl)
{% sample lang="cpp" %}
[import, lang:"cpp"](code/c++/approximate_counting.cpp)
{% sample lang="cs" %}
[import, lang:"csharp"](code/csharp/ApproximateCounting.cs)
{% endmethod %}

### Bibliography
Expand Down
73 changes: 73 additions & 0 deletions contents/approximate_counting/code/csharp/ApproximateCounting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;

namespace ApproximateCounting
{
class ApproximateCounting
{
static readonly Random Rng = new();

/// <param name="v">value in register</param>
/// <param name="a">scaling value for the logarithm based on Morris's paper</param>
/// <returns>N(v,a) the approximate count for the given values</returns>
static double N(int v, double a)
{
return a * Math.Pow(1 + 1 / a, v - 1);
}

/// <param name="v">value in register</param>
/// <param name="a">scaling value for the logarithm based on Morris's paper</param>
/// <returns>Returns the new value for v</returns>
static int Increment(int v, double a)
{
var delta = 1 / (N(v + 1, a) - N(v, a));

if (Rng.NextDouble() <= delta)
return v + 1;
else
return v;
}

/// <summary>
/// This function simulates approximate counting
/// </summary>
/// <param name="noItems">number of items to count and loop over</param>
/// <param name="a">a scaling value for the logarithm based on Morris's paper</param>
/// <returns>It returns n(v,a), the approximate count</returns>
static double ApproximateCount(int noItems, double a)
{
var v = 0;
for (var i = 0; i < noItems; i++)
{
v = Increment(v, a);
}

return N(v, a);
}

/// <param name="noTrials">the number of counting trials</param>
/// <param name="noItems">the number of items to count to</param>
/// <param name="a">a scaling value for the logarithm based on Morris's paper</param>
/// <param name="threshold">the maximum percent error allowed</param>
/// <returns>"passed" or "failed" depending on the test result</returns>
static string TextApproximateCount(int noTrials, int noItems, double a, double threshold)
{
var sum = 0.0;
for (var i = 0; i < noTrials; i++)
sum += ApproximateCount(noItems, a);

var avg = sum / noTrials;

return Math.Abs((avg - noItems) / noItems) < threshold ? "passed" : "failed";
}

static void Main()
{
Console.WriteLine("[#]\nCounting Tests, 100 trials");
Console.WriteLine($"[#]\nTesting 1,000, a = 30, 1% error : {TextApproximateCount(100, 1_000, 30, 0.1)}");
Console.WriteLine($"[#]\nTesting 12,345, a = 10, 10% error : {TextApproximateCount(100, 12_345, 10, 0.1)}");
Console.WriteLine(
$"[#]\nTesting 222,222, a = 0.5, 20% error : {TextApproximateCount(100, 222_222, 0.5, 0.2)}");
}
}
}