BagTable


A BagTable<K,T> is an unordered collection of T with support for hashed searches by a separate key class K. Duplicate keys are permitted. To illustrate how a bag table works, consider the following example.

// Calculus -- Select on a BagTable

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 KeyCompare : Calculus.IEqualityComparer<int, T>
    {
        public virtual bool Equals(int i, T t) { return i == t.i; }

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

    class PredicateSelect
    {
        public static void Main()
        {
            BagTable<int, T> table = new BagTable<int, T>(new KeyCompare());

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

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

            Console.WriteLine();
            Console.WriteLine("Looking for 2.5 in Key Range 1 using Predicate Select");
            Console.WriteLine("Found == {0}", table.Select(1, new DoublePredicate(2.5)));
        }
    }
}

A query is performed on the bag table.