Click or drag to resize

RankedBagTElementsBetween Method

Returns an enumerator that iterates over a range with the supplied bounds.

Namespace:  Kaos.Collections
Assembly:  KaosCollections (in KaosCollections.dll) Version: 4.2.0.0
Syntax
C#
public IEnumerable<T> ElementsBetween(
	T lower,
	T upper
)

Parameters

lower
Type: T
Minimum item value of the range.
upper
Type: T
Maximum item value of the range.

Return Value

Type: IEnumerableT
An enumerator for the supplied range.
Exceptions
ExceptionCondition
InvalidOperationExceptionWhen the bag was modified after the enumerator was created.
Remarks

If either lower or upper are present in the bag, they will be included in the results.

Retrieving the initial item is a O(log n) operation. Retrieving each subsequent item is a O(1) operation.

Examples
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