Click or drag to resize

RankedBagT Constructor (IEnumerableT)

Initializes a new bag instance that contains items copied from the supplied collection.

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
public RankedBag(
	IEnumerable<T> collection
)

Parameters

collection
Type: System.Collections.GenericIEnumerableT
The enumerable collection to be copied.
Exceptions
ExceptionCondition
InvalidOperationExceptionWhen comparer is null and no other comparer available.
ArgumentNullExceptionWhen collection is null.
Remarks
This constructor is a O(n log n) operation, where n is the size of collection.
Examples
This program shows using this class for some basic statistical calculations.
C#
using System;
using System.Linq;
using Kaos.Collections;

namespace ExampleApp
{
    class RbExample03
    {
        static double Median (RankedBag<int> vals)
        {
            if (vals.Count == 0) return 0D;
            if (vals.Count % 2 == 1) return (double) vals.ElementAt (vals.Count/2);
            return (vals.ElementAt (vals.Count/2-1) + vals.ElementAt (vals.Count/2)) / 2D;
        }

        static double Mean (RankedBag<int> vals) => vals.Sum() / (double) vals.Count;
        static double Variance (RankedBag<int> vals) => vals.Sum (x => Math.Pow (Mean (vals) - x, 2)) / vals.Count;
        static double StandardDeviation (RankedBag<int> vals) => Math.Sqrt (Variance (vals));

        static void Main()
        {
            var scores = new RankedBag<int> { 2, 5, 4, 4, 5, 4, 7, 9 };

            var mean = Mean (scores);
            var stddev = StandardDeviation (scores);

            Console.WriteLine ($"Count = {scores.Count}, median = {Median (scores)}, mean = {Mean (scores)}");
            Console.WriteLine ($"Variance = {Variance (scores)}");
            Console.WriteLine ($"Standard deviation = {StandardDeviation (scores)}");

            Console.WriteLine ($"\nLow score: {scores.Min}");
            Console.WriteLine ($"High score: {scores.Max}");

            Console.Write ("Scores within 1 standard deviation:");
            foreach (var score in scores.ElementsBetween ((int) (mean-stddev+0.5), (int) (mean+stddev+0.5)))
                Console.Write ($" {score}");
            Console.WriteLine();
        }

        /* Output:

        Count = 8, median = 4.5, mean = 5
        Variance = 4
        Standard deviation = 2

        Low score: 2
        High score: 9
        Scores within 1 standard deviation: 4 4 4 5 5 7

        */
    }
}
See Also