String to Integer bidirectional mapping (was Hashing: "blair" == "brainlessness" !!!!)

F

Francesc Benavent

Thanks for your answers, I know that my wondering was not a problem.
Once I understand that collisions in hashing are unavoidable, lets
explain my problem:

---------------------
WHAT I WANT TO DO?
---------------------
1. I want to do an object called WordList able to store a list of
strings, and mapping them bidirectionally to integers:

<code>
int id;
string word;
// Using the WordList Object
WordList WL = new WordList();
WL.Write("butterfly");
id = WL.Read("Butterfly"); // id = 34348
word = WL.Read(34348); // word = "Butterfly"
</code>

---------------------
HOW I'M TRYING TO DO IT?
---------------------
2. I've done it based in a hashtable keyed by the GetHashCode of the
string, a simplified code description goes next:

<code>
public int Write(string word)
{
hashtable.Add(word.GetHashCode(), word);
}

public int Read(string word)
{
return(word.GetHashCode());
}

public string Read(int id)
{
return(hashtable[id]);
}
</code>

---------------------
WHY DOES NOT WORK?
---------------------
3. It not works perfectly due to collisions. Some words share
hashcode, then I can't recover the right string from the hash. I
increased the code in some ways:

a) I replaced inserting just a string by inserting an array of
strings. In case of collision I increase the array, this way I could
store different strings although they had the same hashcode.

b) When I wanted to recover I didn't know which of the strings were
the right. Then I replaced the array of strings with an array of
structs (string + code). But any code obtained from the
word.GetHashCode were the same for all the strings of a given array.


-----------------
IN WHICH POINT I'M?
-----------------

I would like to find some way to pick up the right word from the
string array in any of the buckets of the hashtable. But as all
strings share the key I think that is impossible ....

I'm worried to need to start from zero and build a new class based in
a totally different base (may be a linked list, a SortList,
binarySearch... etc...)

If somebody is lost due to my long-and-bad-english message: Read only
the point 1, do you know a way to do it?

thanks!

Francesc
 
J

Jon Skeet [C# MVP]

Francesc Benavent said:
Thanks for your answers, I know that my wondering was not a problem.
Once I understand that collisions in hashing are unavoidable, lets
explain my problem:

---------------------
WHAT I WANT TO DO?
---------------------
1. I want to do an object called WordList able to store a list of
strings, and mapping them bidirectionally to integers:

<code>
int id;
string word;
// Using the WordList Object
WordList WL = new WordList();
WL.Write("butterfly");
id = WL.Read("Butterfly"); // id = 34348
word = WL.Read(34348); // word = "Butterfly"
</code>

One easy way of doing this would be to keep two hashtables and a
counter - one hashtable would go from string to id, the other would go
the other way. When you add an entry, just increment the counter and
add the mapping each way. Simple!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top