Простая генерация гистограммы целочисленных данных в C#

Основываясь на предложении c#-language BastardSaint, я придумал csharp аккуратную и довольно общую c-sharp оболочку:

public class Histogram : SortedDictionary
{
    public void IncrementCount(TVal binToIncrement)
    {
        if (ContainsKey(binToIncrement))
        {
            this[binToIncrement]++;
        }
        else
        {
            Add(binToIncrement, 1);
        }
    }
}

Теперь я могу:

const uint numOfInputDataPoints = 5;
Histogram hist = new Histogram();

// Fill the histogram with data
for (uint i = 0; i < numOfInputDataPoints; i++)
{
    // Grab a result from my algorithm
    uint numOfIterationsForSolution = MyAlorithm.Run();

    // Add the number to the histogram
    hist.IncrementCount( numOfIterationsForSolution );
}

// Report the results
foreach (KeyValuePair histEntry in hist.AsEnumerable())
{
    Console.WriteLine("{0} occurred {1} times", histEntry.Key, histEntry.Value);
}

Мне csharp потребовалось время, чтобы c#.net придумать, как сделать его c-sharp универсальным (для начала c# я просто переопределил конструктор csharp SortedDictionary, что означало, что вы могли c# использовать его только для c#.net ключей uint).

c#

histogram

2022-11-04T11:23:40+00:00