Thread: class Dictionary

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    class Dictionary

    Hi, I'm programming in C #, and I have a question about the class dictionary, I know that this work as a hash table, but I would like to know how that hash function uses (or which is the one that could use), and that size is its table, here is an example of Dictionary, and here is my question.
    Code:
    private static Dictionary<string, double> PrepareFrequency(string[] words)
    {
        Dictionary<string, double> table = new Dictionary<string, double>();
    
        foreach (string word in words)
        {
            if (table.ContainsKey(word))
                table[word]++;
            else
                table.Add(word, 1);
        }
    
        return table;
    }
    Thanks for your attention.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no idea about any C# code, but I'm pretty sure the answer is pretty much the same whatever language is used.

    The hash function is choosen from one of the few standard functions that take a string and make a well-distributed hash-value. This is the first page I found on the subject:
    http://www.cse.yorku.ca/~oz/hash.html

    Google gives quite a few hits, and some are better than others, but it's not hard to find A hash-function that should give decent distribution - but not all hash-functions are equal, as the above link explains. The important factor for the question in this thread is that it's a "decent" hash-function, not a stupid one.

    The size of any class-based dictionary should be "variable", in the sense that it should allow a small or large number of entries, without too much overhead. It will, most likely, not grow one item at a time - it probably doubles each time it runs out of space, or some such. [Or roughly doubles, it my use for example a table of prime numbers that are suitable sizes, such as:
    Code:
    const size_t tableSizes[] = { 31, 127, 255, 511, 1023, ... };
    Yes, I know, 255 is not a prime number, as it's divisible by 3, 5 and 17. But for it's actually not that critical if it's much smaller than the hash-value itself.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM