From c811056b2e27d0b8caba89253a45112e8a0ed31c Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Wed, 15 Sep 2021 01:05:39 +0200 Subject: [PATCH 1/2] Add C# implementation for the approximate counting algorithm --- .../approximate_counting.md | 2 + .../code/csharp/ApproximateCounting.cs | 71 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 contents/approximate_counting/code/csharp/ApproximateCounting.cs diff --git a/contents/approximate_counting/approximate_counting.md b/contents/approximate_counting/approximate_counting.md index 917e79922..b99f5455b 100644 --- a/contents/approximate_counting/approximate_counting.md +++ b/contents/approximate_counting/approximate_counting.md @@ -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 diff --git a/contents/approximate_counting/code/csharp/ApproximateCounting.cs b/contents/approximate_counting/code/csharp/ApproximateCounting.cs new file mode 100644 index 000000000..ff6e3ba34 --- /dev/null +++ b/contents/approximate_counting/code/csharp/ApproximateCounting.cs @@ -0,0 +1,71 @@ +using System; + +namespace ApproximateCounting +{ + class ApproximateCounting + { + static readonly Random Rng = new(); + + // This function takes + // - v: value in register + // - a: a scaling value for the logarithm based on Morris's paper + // It returns n(v,a), the approximate count + static double N(int v, double a) + { + return a * Math.Pow(1 + 1 / a, v - 1); + } + + // This function takes + // - v: value in register + // - a: a scaling value for the logarithm based on Morris's paper + // It returns a new value for v + 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; + } + + // This simulates counting and takes + // - noItems: number of items to count and loop over + // - a: a scaling value for the logarithm based on Morris's paper + // It returns n(v,a), the approximate count + 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); + } + + // This function takes + // - noTrials: the number of counting trials + // - noItems: the number of items to count to + // - a: a scaling value for the logarithm based on Morris's paper + // - threshold: the maximum percent error allowed + // It returns a "pass" / "fail" test value + 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 ? "pass" : "fail"; + } + + static void Main() + { + Console.WriteLine("Counting Tests, 100 trials"); + Console.WriteLine($"Testing 1,000, a = 30, 1% error : {TextApproximateCount(100, 1_000, 30, 0.1)}"); + Console.WriteLine($"Testing 12,345, a = 10, 1% error : {TextApproximateCount(100, 12_345, 10, 0.1)}"); + Console.WriteLine($"Testing 222,222, a = 0.5, 10% error : {TextApproximateCount(100, 222_222, 0.5, 0.2)}"); + } + } +} From 6e8c264156d53f4e0ab988633f4cc9145b2cf2aa Mon Sep 17 00:00:00 2001 From: stormofice <58337328+stormofice@users.noreply.github.com> Date: Thu, 4 Nov 2021 22:05:45 +0100 Subject: [PATCH 2/2] Changed comments to XML docstrings and standardize output --- .../code/csharp/ApproximateCounting.cs | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/contents/approximate_counting/code/csharp/ApproximateCounting.cs b/contents/approximate_counting/code/csharp/ApproximateCounting.cs index ff6e3ba34..d8df57c00 100644 --- a/contents/approximate_counting/code/csharp/ApproximateCounting.cs +++ b/contents/approximate_counting/code/csharp/ApproximateCounting.cs @@ -6,19 +6,17 @@ class ApproximateCounting { static readonly Random Rng = new(); - // This function takes - // - v: value in register - // - a: a scaling value for the logarithm based on Morris's paper - // It returns n(v,a), the approximate count + /// value in register + /// scaling value for the logarithm based on Morris's paper + /// N(v,a) the approximate count for the given values static double N(int v, double a) { return a * Math.Pow(1 + 1 / a, v - 1); } - - // This function takes - // - v: value in register - // - a: a scaling value for the logarithm based on Morris's paper - // It returns a new value for v + + /// value in register + /// scaling value for the logarithm based on Morris's paper + /// Returns the new value for v static int Increment(int v, double a) { var delta = 1 / (N(v + 1, a) - N(v, a)); @@ -29,10 +27,12 @@ static int Increment(int v, double a) return v; } - // This simulates counting and takes - // - noItems: number of items to count and loop over - // - a: a scaling value for the logarithm based on Morris's paper - // It returns n(v,a), the approximate count + /// + /// This function simulates approximate counting + /// + /// number of items to count and loop over + /// a scaling value for the logarithm based on Morris's paper + /// It returns n(v,a), the approximate count static double ApproximateCount(int noItems, double a) { var v = 0; @@ -40,32 +40,34 @@ static double ApproximateCount(int noItems, double a) { v = Increment(v, a); } + return N(v, a); } - // This function takes - // - noTrials: the number of counting trials - // - noItems: the number of items to count to - // - a: a scaling value for the logarithm based on Morris's paper - // - threshold: the maximum percent error allowed - // It returns a "pass" / "fail" test value + /// the number of counting trials + /// the number of items to count to + /// a scaling value for the logarithm based on Morris's paper + /// the maximum percent error allowed + /// "passed" or "failed" depending on the test result 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 ? "pass" : "fail"; + + return Math.Abs((avg - noItems) / noItems) < threshold ? "passed" : "failed"; } static void Main() { - Console.WriteLine("Counting Tests, 100 trials"); - Console.WriteLine($"Testing 1,000, a = 30, 1% error : {TextApproximateCount(100, 1_000, 30, 0.1)}"); - Console.WriteLine($"Testing 12,345, a = 10, 1% error : {TextApproximateCount(100, 12_345, 10, 0.1)}"); - Console.WriteLine($"Testing 222,222, a = 0.5, 10% error : {TextApproximateCount(100, 222_222, 0.5, 0.2)}"); + 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)}"); } } } +