Thread: Dictionary in C# to hash table in C?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Dictionary in C# to hash table in C?

    I've got some code in C# that uses the dictionary data structure to store a lot of information. All this data was put into a file for easy transfer. However, now I need to use that same information in a C program.

    The dictionary has a key of long int with data of type double.

    From all I've been able to tell, this is best implemented in C with a hash table (since a dictionary is just a specialized hash table anyway).

    So here are my problems. First, I have no idea how many entries are in this table. I do know that, once it's read into the C hash table, it won't have to be altered or added to (for now, at least). So I definitely need something that will dynamically change sizes as I read it in from the file.
    My second problem is that I haven't coded in C since 1998 (and most of my work for the past 3 years has been all in C#). I tried searching for some examples, but many of them were either hash tables for very specific purposes different from mine or I was unable to tell how exactly to alter things to do what I need.

    With that second problem especially in mind, how would I go about setting up a hash table? Or is there a better way to implement this table?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It might be better to implement this as a hash-tree. You can do a simple implementation which just creates a binary tree which is sorted by the hash value. Then each tree node contains a linked list which holds all the key/value pairs for that hash value (needed to take care of collisions). If you want to make the implementation better, then make the tree balanced. This is probably best achieved by making the binary tree a Red-Black tree.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    For the purposes I'm using this table, there should never be collisions, since any new value specifically must replace the old value; each key has a unique value associated with it.
    I'll definitely look up hash-trees, though. Someone else had suggested I make it a table of pointers (keys) and this sounds similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  3. Hash table creation and insertion
    By tgshah in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 07:54 PM
  4. Not sure on hash table memory allocations
    By Thumper333 in forum C Programming
    Replies: 3
    Last Post: 09-27-2004, 09:00 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM

Tags for this Thread