UnorderedBag


An UnorderedBag<T> is an unordered collection of T with support for hashed searches. To illustrate how an unordered bag works, consider the following example.

// Calculus -- Select on an UnorderedBag

using Calculus;
using System;

namespace TestSelect
{
    [Serializable]
    public class T
    {
        public int i;
        public double d;
        public string s;

        public T(int ii, double dd, string ss) { i = ii; d = dd; s = ss; }

        public T(int ii) { i = ii; d = 0; s = null; }

        public override string ToString()
        { return "(" + i.ToString() + "," + d.ToString() + "," + s + ")"; }

        public override bool Equals(object t) { return i == ((T)t).i; }

        public override int GetHashCode() { return i; }
    }

    class DoublePredicate : Calculus.Predicate<T>
    {
        public double dTest;

        public DoublePredicate(double dTestSet)
        {
            dTest = dTestSet;
        }

        public bool Evaluate(T t) { return t.d == dTest; }
    }

    class PredicateSelect
    {
        public static void Main()
        {
            try
            {
                UnorderedBag<T> unorderedBag = new UnorderedBag<T>();

                unorderedBag.Add(new T(1, 1.5, "A"));
                unorderedBag.Add(new T(1, 2.5, "B"));
                unorderedBag.Add(new T(1, 3.5, "C"));
                unorderedBag.Add(new T(1, 2.5, "D"));
                unorderedBag.Add(new T(3, 3.5, "E"));
                unorderedBag.Add(new T(4, 2.5, "F"));

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

                Console.WriteLine();
                Console.WriteLine("Looking for 2.5 in Key Range 1 using Predicate Select");
                Console.WriteLine("Found == {0}", unorderedBag.Select(new T(1), new DoublePredicate(2.5)));
            }
            catch (Exception e) { Console.WriteLine("Exception: {0} ", e); }
        }
    }
}

A query is performed on the unordered bag.