Click or drag to resize

RankedDictionaryTKey, TValueElementsBetween 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<KeyValuePair<TKey, TValue>> ElementsBetween(
	TKey lower,
	TKey upper
)

Parameters

lower
Type: TKey
Minimum key of the range.
upper
Type: TKey
Maximum key of the range.

Return Value

Type: IEnumerableKeyValuePairTKey, TValue
An enumerator for the specified range.
Exceptions
ExceptionCondition
ArgumentNullExceptionWhen key is null.
InvalidOperationExceptionWhen the dictionary was modified after the enumerator was created.
Remarks

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

Retrieving the first element is a O(log n) operation. Retrieving subsequent elements is a O(1) operation per element.

Examples
C#
// 
// Program: RdExample03.cs
// Purpose: Demonstrate LINQ usage and range query.
// 

using System;
using System.Linq;
using System.Collections.Generic;
using Kaos.Collections;

namespace ExampleApp
{
    class RdExample03
    {
        static void Main()
        {
            var towns = new RankedDictionary<string,int>();

            // Load sample data.
            towns.Add ("Albany", 43600);
            towns.Add ("Bandon", 2960);
            towns.Add ("Corvallis", 54462);
            towns.Add ("Damascus", 10539);
            towns.Add ("Elkton", 195);
            towns.Add ("Florence", 8466);
            towns.Add ("Glide", 1795);
            towns.Add ("Jacksonville", 2235);
            towns.Add ("Lebanon", 13140);
            towns.Add ("Lookingglass", 855);
            towns.Add ("Medford", 75180);
            towns.Add ("Powers", 689);
            towns.Add ("Riddle", 1020);
            towns.Add ("Roseburg", 20480);
            towns.Add ("Scio", 710);
            towns.Add ("Talent", 6066);
            towns.Add ("Umatilla", 6906);
            towns.Add ("Winston", 5379);
            towns.Add ("Yamhill", 820);

            // Here's a typical LINQ-To-Objects operation.
            double avg = towns.Average (x => x.Value);
            Console.WriteLine ($"Average population of all towns = {avg:f0}");

            // Lambda expression
            IEnumerable<KeyValuePair<string,int>> r1 = towns.Where (t => t.Key.CompareTo ("E") < 0);

            Console.WriteLine ("\nTowns A-D:");
            foreach (KeyValuePair<string,int> e in r1)
                Console.WriteLine (e.Key);

            // LINQ range: O(n)
            IEnumerable<KeyValuePair<string,int>> r2 = towns.SkipWhile (t => t.Key.CompareTo ("E") < 0).TakeWhile (t => t.Key.CompareTo ("J") < 0);

            Console.WriteLine ("\nTowns E-G:");
            foreach (KeyValuePair<string,int> e in r2)
                Console.WriteLine (e.Key);

            // 
            // Use the ElementsBetween iterator to query range.
            // Unlike LINQ SkipWhile and TakeWhile, this will perform an optimized (partial scan) lookup.
            // 

            // BtreeDictionary range operator: O(log n)
            IEnumerable<KeyValuePair<string,int>> r3 = towns.ElementsBetween ("K", "M");

            Console.WriteLine ("\nTowns K-L:");
            foreach (KeyValuePair<string,int> town in r3)
                Console.WriteLine (town.Key);

            // Range operator without upper limit: O(log n)
            IEnumerable<KeyValuePair<string,int>> r4 = towns.ElementsFrom ("M");

            Console.WriteLine ("\nTowns M-R:");
            foreach (KeyValuePair<string,int> town in r4)
                // This avoids the issue in the last example where a town named "M" would be included.
                if (town.Key.CompareTo ("S") >= 0)
                    break;
                else
                    Console.WriteLine (town.Key);

            // Range operator without upper limit: O(log n)
            IEnumerable<KeyValuePair<string,int>> r5 = towns.ElementsFrom ("T");

            Console.WriteLine ("\nTowns T-Z:");
            foreach (KeyValuePair<string,int> town in r5)
                Console.WriteLine (town.Key);
        }

        /* Output:

        Average population of all towns = 13447

        Towns A-D:
        Albany
        Bandon
        Corvallis
        Damascus

        Towns E-G:
        Elkton
        Florence
        Glide

        Towns K-L:
        Lebanon
        Lookingglass

        Towns M-R:
        Medford
        Powers
        Riddle
        Roseburg

        Towns T-Z:
        Talent
        Umatilla
        Winston
        Yamhill

         */
    }
}
See Also