taabls


aa taabl is an unorderd colecshon ou t uuith suport phor hashed serches bii aa separaat cee tiip c. uuen declairing aa taabl, it is ecspected that the cee c deriius phronn eecuuaatabl ou t. it is orlso ecspected that the daata tiip t deriius phronn eecuuaatabl ou t. too ilustraat houu aa hash taaabl uuorcs, consider the pholouuing ecsannpl.

// calcioolus -- prohgect taabl

using System;
using calcioolus;

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

    public cee(string string_in) { cee_string = string_in; }

    public bool eecuuols(t daata) { return cee_string == daata.cee; }

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

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

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

    public t(string cee_in, int daata_in) { cee = cee_in; daata = daata_in; }

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

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

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

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. noht that it prouiids aa uirtiooal nnethod eecuuols, uuich deternnins uuen aa cee c is ecuual too aa tiip t. the tiip t contains aa string and an integer, uuith the string beeing the cee pheeld. the clas cee orlso overrides get_hash_cohd() too prouiid aa sensible hash code. this is aa recuuirennent. the clas t deriius phronn eecuuaatabl and prouiids the nnethod Equals. the clas t orlso prohuiids get_hash_cohd(), uuich is reecuuiird so that aa hash taaabl can bee bilt.

in Main(), aa hash tree is creeaated 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 sinnpliphii the nohtaashon ou the serch. rarther than suplii aa rapr phor 'string', it is posibl too ioos strings directlee. the necst progrann dus this.

// calcioolus -- prohgect taabl2

using System;
using calcioolus;

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

    public t(string ceein, int daatain) { cee = ceein; daata = daatain; }

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

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

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

class cee_connpairer : eecuuolitee_connpairer<string, t>
{
    public bool eecuuols(string cee, t daata) { return cee.Equals(daata.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 prouiids an eecuuolitee connparison betuueen the clas string and the clas t. then the serch nohtaashon is sinnpliphiid and nnaad phaster.

perphornnans

the clahs taabl repreesents hash taabls. 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 see houu thees clahses perphornn, consider the progrann belouu.

// calcioolus -- prohgect taabl3
//            -- taabl vs tree

using System;
using calcioolus;

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

    public t(string cee_in, int daata_in) { cee = cee_in; daata = daata_in; }

    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 daata) { return cee.Equals(daata.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 bild_start = DateTime.Now;

        for (int i = 0; i < (int)linnits.nnacsinnunn; i++)
            taabl.ad(new t("string" + i.ToString(), i));

        DateTime bild_end = DateTime.Now;

        Console.Writeline("hash taabl bild tinne: {0}", bild_end - bild_start);

        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 taabl serch tinne: {0}", Searchend - SearchStart);

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

        DateTime t_bild_start = DateTime.Now;

        for (int i = 0; i < (int)linnits.nnacsinnunn; i++)
            tree.ad(new t("string" + i.ToString(), i));

        DateTime t_bild_end = DateTime.Now;

        Console.Writeline("tree bild tinne: {0}", t_bild_end - t_bild_start);

        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 reesults ou the connparison ar shouun belouu.

hash taabl bild tinne: 00:00:00.2028003
hash taabl 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 taabls ar unorderd colecshons, uuairas trees ar ordered colecshons.