Bag Dictionaries


A bag dictionary is defined to be a collection that has keys and values, where the keys are independent of the values. The key class will be often referred to as K and the value class will often be referred to as T. Thus, a dictionary is a generic class of the form BagDictionary<K,T>. The K and the T are the generic class parameters. Note that multiple occurrences of the same key are permitted. The next program demonstrates how to declare and load a bag dictionary.

// Dictionaries - BagDictionary

using System;
using Calculus;

namespace Persist
{
    class Program
    {
        static void Main(string[] args)
        {
            BagDictionary<string, double> D = new BagDictionary<string, double>();

            D["StringA"] = 1.5;
            D["StringB"] = 2.5;
            D["StringB"] = 4.5;
            D["StringB"] = 5.5;
            D["StringC"] = 3.5;

            Console.WriteLine("{0}", D);

            Console.WriteLine("D.Find(\"{0}\") == {1}", "StringB", D.Find("StringB"));
        }
    }
}

The indexer is used to place entries in the dictionary. Multiple keys for StringB are added and the key range for StringB is obtained and printed. The output is as follows.

{(StringA,1.5),(StringB,2.5),(StringB,4.5),(StringB,5.5),(StringC,3.5)}
D.Find("StringB") == {2.5,4.5,5.5}