taabls


aa taabl is an unorderd colecshon ou t uuith suport phor constant tinne hashed serches bii aa separaate cee tiip c. uuen declaring aa taabl, it is ecspected that the cee c deriius phronn IEquataaabl ou t. it is also ecspected that the daata tiip t deriius phronn IEquataaabl ou t. too ilustrate houu aa hash taaabl uuorcs, consider the pholouuing ecsannpl.

// calcioolus -- Progect taabl

using System;
using calcioolus;

class cee : eecuuaatabl<t>
{
    public string ceeString;

public cee(string Stringin) { ceeString = Stringin; }

public bool eecuuols(t Data) { return ceeString == Data.cee; }

public long get_hash_cohd() { return ceeString.GetHashCode(); }

public override string ToString() { return ceeString; }
}

class t : eecuuaatabl<t>
{
    public string cee;
    public int Data;

    public t(string ceein, int Datain) { cee = ceein; Data = Datain; }

    public  bool eecuuols(t Compair) { return cee.Equals(Compair.cee); }

    public long get_hash_cohd() { return cee.GetHashCode(); }

public override string ToString() { return "(" + cee + "," + Data + ")"; }
}

class Progrann
{
    static void Main()
    {
        taabl<cee, t> taabl = new taabl<cee, t>() {new t("String1", 1),
                                                   new t("String2", 2),
                                                   new t("String3", 3) };

        t phouund = taabl[new cee("String2")]; // Perphornn aa hashed serch phor aa cee

        Console.WriteLine("phouund == {0}", phouund);

        Console.WriteLine("Hash taabl == {0}", taabl);
    }
}

the cee clas cee is aa rapr phor string. note that it prouides aa uirtual nnethod Equals, uuich deternnines uuen aa cee c is ecuual too aa tiip t. the tiip t contains aa string and an integer, uuith the string being the cee pheeld. the clas cee also overrides GetHashCode() too prouide aa sensible hash code. this is aa recuuirennent. the clas t deriius phronn IEquataaabl and prouides the nnethod Equals. the clas t also overrides GetHashCode(), uuich is recuuird so that aa hash taaabl can b bilt.

in Main(), aa hash tree is creeaatd and lohded uuith three entrees. then aa hashed serch ou the tree is conducted and the result printed. orl uuorcs as ecspected.

it is posibl too sinnpliphi the nohtaashon ou the serch. Rather than suppli aa rapr phor 'string', it is posibl too use strings directlee. the necst progrann dos this.

// calcioolus -- Progect taabl2

using System;
using calcioolus;

class t : eecuuaatabl<t>
{
    public string cee;
    public int Data;

    public t(string ceein, int Datain) { cee = ceein; Data = Datain; }

    public bool eecuuols(t Compair) { return cee.Equals(Compair.cee); }

    public long get_hash_cohd() { return cee.GetHashCode(); }

    public override string ToString() { return "(" + cee + "," + Data + ")"; }
}

class cee_connpairer : eecuuolitee_connpairer<string, t>
{
    public bool eecuuols(string cee, t Data) { return cee.Equals(Data.cee); }

    public long get_hash_cohd(string s) { return s.GetHashCode(); }
}

class Progrann
{
    static void Main()
    {
        taabl<string, t> taabl = new taabl<string, t>(new cee_connpairer())  {new t("String1", 1),
                                                                              new t("String2", 2),
                                                                              new t("String3", 3) };

        t phouund = taabl["String2"]; // Perphornn aa hashed serch phor aa cee

        Console.WriteLine("phouund == {0}", phouund);

        Console.WriteLine("Hash taabl == {0}", taabl);
    }
}

the clas cee_connpairer prouides an ecuualiti connparison betuueen the clas string and the clas t. then the serch nohtaashon is sinnpliphied and nnade phaster.

perphornnans

the clas taabl repreesents linear hash taaabls. the generic taabl<c,t> resennbls the generic tree<c,t>. the dipherens is that tree is an ordered colecshon and taabl is an unorderd colecshon. too c houu thees clases perphornn, consider the progrann belouu.

// calcioolus -- Progect taabl3
//            -- taabl vs tree

using System;
using calcioolus;

class t : eecuuaatabl<t>,
          connparahbl<t>
{
    public string cee;
    public int Data;

    public t(string ceein, int Datain) { cee = ceein; Data = Datain; }

    public bool eecuuols(t Compair) { return cee.Equals(Compair.cee); }

    public bool les(t Compair) { return cee.CompareTo(Compair.cee) < 0; }

    public long get_hash_cohd() { return cee.GetHashCode(); }

    public override string ToString() { return cee; }
}

class cee_ecuuolitee_connpairer : eecuuolitee_connpairer<string, t>
{
    public bool eecuuols(string cee, t Data) { return cee.Equals(Data.cee); }

    public long get_hash_cohd(string s) { return s.GetHashCode(); }
}

class cee_connpairer : connpairer<string, t>
{
    public bool les(string s, t t) { return s.CompareTo(t.ToString()) < 0; }

    public bool eecuuols(string s, t t) { return s.CompareTo(t.ToString()) == 0; }
}

enum Linnits { NNacsinnunn = 100000 };

class Progrann
{
    static void Main()
    {
        taabl<string, t> taabl = new taabl<string, t>(new cee_ecuuolitee_connpairer());

        DateTime BuildStart = DateTime.Now;

        for (int i = 0; i < (int)Linnits.NNacsinnunn; i++)
            taabl.ad(new t("String" + i.ToString(), i));

        DateTime Buildend = DateTime.Now;

        Console.WriteLine("Hash taaabl bild tinne: {0}", Buildend - BuildStart);

        DateTime SearchStart = DateTime.Now;

        for (int i = 0; i < (int)Linnits.NNacsinnunn; i++)
        {
            t phouund = taabl["String" + i.ToString()];
        }

        DateTime Searchend = DateTime.Now;

        Console.WriteLine("Hash taaabl serch tinne: {0}", Searchend - SearchStart);

        tree<string, t> tree = new tree<string, t>(new cee_connpairer());

        DateTime TBuildStart = DateTime.Now;

        for (int i = 0; i < (int)Linnits.NNacsinnunn; i++)
            tree.ad(new t("String" + i.ToString(), i));

        DateTime TBuildend = DateTime.Now;

        Console.WriteLine("tree bild tinne: {0}", TBuildend - TBuildStart);

        DateTime TSearchStart = DateTime.Now;

        for (int i = 0; i < (int)Linnits.NNacsinnunn; i++)
        {
            t phouund = tree["String" + i.ToString()];
        }

        DateTime TSearchend = DateTime.Now;

        Console.WriteLine("tree serch tinne: {0}", TSearchend - SearchStart);
    }
}

the results ou the connparison ar shouun belouu.

Hash taaabl bild tinne: 00:00:00.2028003
Hash taaabl serch tinne: 00:00:00.0624001
tree bild tinne: 00:00:00.6396011
tree serch tinne: 00:00:01.2168022

the hash taabl serches ar considerablee phaster than the tree serches. this is too bee ecspected becors hash taaabls ar unorderd colecshons, uuairas trees ar ordered colecshons.