473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Hash Code Of A String

I am currently working on a dictionary populating program. I currently
have a socket connection my local news server and am trawling through
all of the articles looking for new words. I am currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?

In my Java version of the program I am using the Multiply Add and Divide
(MAD) method for the compression of the hash code, does C# have any
built in functions(methods) that will do this for me, or does anyone
know of a more efficient way?

j1mb0jay
Jan 11 '08 #1
6 10018
All .NET types have the GetHashCode() method, at minimum inherited from
System.Object.

string str = "fubar";

int hc = str.GetHashCode(); // 418978654
"j1mb0jay" wrote:
I am currently working on a dictionary populating program. I currently
have a socket connection my local news server and am trawling through
all of the articles looking for new words. I am currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?

In my Java version of the program I am using the Multiply Add and Divide
(MAD) method for the compression of the hash code, does C# have any
built in functions(methods) that will do this for me, or does anyone
know of a more efficient way?

j1mb0jay
Jan 11 '08 #2
KH wrote:
All .NET types have the GetHashCode() method, at minimum inherited from
System.Object.

string str = "fubar";

int hc = str.GetHashCode(); // 418978654
"j1mb0jay" wrote:
>I am currently working on a dictionary populating program. I currently
have a socket connection my local news server and am trawling through
all of the articles looking for new words. I am currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?

In my Java version of the program I am using the Multiply Add and Divide
(MAD) method for the compression of the hash code, does C# have any
built in functions(methods) that will do this for me, or does anyone
know of a more efficient way?

j1mb0jay
Much the same as Java the GetHashCode() methodology. How would you
convert the 418978654 to a number that I could use as an index into a
fixed size array?

j1mb0jay
Jan 11 '08 #3
That's really a whole different question; a couple options:

1) GetHashCode() returns a 32-bit interger, so if you created an array of
size UInt32.MaxValue you could use the hashcode directly

2) You could % the hashcode by the size of your array

Neither of those are actually viable options of course. What are you
actually trying to accomplish?
"j1mb0jay" wrote:
KH wrote:
All .NET types have the GetHashCode() method, at minimum inherited from
System.Object.

string str = "fubar";

int hc = str.GetHashCode(); // 418978654
"j1mb0jay" wrote:
I am currently working on a dictionary populating program. I currently
have a socket connection my local news server and am trawling through
all of the articles looking for new words. I am currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?

In my Java version of the program I am using the Multiply Add and Divide
(MAD) method for the compression of the hash code, does C# have any
built in functions(methods) that will do this for me, or does anyone
know of a more efficient way?

j1mb0jay

Much the same as Java the GetHashCode() methodology. How would you
convert the 418978654 to a number that I could use as an index into a
fixed size array?

j1mb0jay
Jan 11 '08 #4
On Fri, 11 Jan 2008 15:24:07 -0800, j1mb0jay <no**@none.comwrote:
Much the same as Java the GetHashCode() methodology. How would you
convert the 418978654 to a number that I could use as an index into a
fixed size array?
And what would the point of that be?

..NET already has collection classes that use the hash code directly (e.g.
Dictionary, Hashtable, etc.).

You can always just use the % operator to take an arbitrary number and map
it to some arbitrarily smaller range. But I don't see the point in this
case.

Pete
Jan 12 '08 #5
On Fri, 11 Jan 2008 16:15:26 -0800, j1mb0jay <no**@none.comwrote:
[...]
I just wanted to improve the insert method of the hash table because
after inserting all words I have to check a maximum 4 words rather than
1 to see if the word already exists in the hash table which is
2.5million words long.
There's no way for you to guarantee that you don't have to check multiple
words, even if you have as many entries in the hash table as you have data
elements. Collisions are always a possibility.

Besides that, you seem to be pursuing a pointless optimization. Even if
your hash table required you to check dozens of words, it would still be
ridiculously faster than a linear search of 2.5 million words. That's the
point of a hash table. You may be able to improve performance slightly by
implementing your own hash table and/or hash function, but IMHO once
you've got to the point of use _some_ kind of hash table, you're likely to
find the algorithm isn't going to get a lot faster by messing around with
the hash table. Your efforts will be better spent looking for other
low-hanging fruit elsewhere in whatever the algorithm you're doing is.
And if you can't get acceptable performance that way while still using a
hash table, you may wind up needing an algorithm that uses a data
structure that's even faster than the hash table.

Pete
Jan 12 '08 #6
Peter Duniho wrote:
On Fri, 11 Jan 2008 16:15:26 -0800, j1mb0jay <no**@none.comwrote:
>[...]
I just wanted to improve the insert method of the hash table because
after inserting all words I have to check a maximum 4 words rather
than 1 to see if the word already exists in the hash table which is
2.5million words long.

There's no way for you to guarantee that you don't have to check
multiple words, even if you have as many entries in the hash table as
you have data elements. Collisions are always a possibility.

Besides that, you seem to be pursuing a pointless optimization. Even if
your hash table required you to check dozens of words, it would still be
ridiculously faster than a linear search of 2.5 million words. That's
the point of a hash table. You may be able to improve performance
slightly by implementing your own hash table and/or hash function, but
IMHO once you've got to the point of use _some_ kind of hash table,
you're likely to find the algorithm isn't going to get a lot faster by
messing around with the hash table. Your efforts will be better spent
looking for other low-hanging fruit elsewhere in whatever the algorithm
you're doing is. And if you can't get acceptable performance that way
while still using a hash table, you may wind up needing an algorithm
that uses a data structure that's even faster than the hash table.

Pete
I am trying to do this for a data structure module for my degree,
writing our own data structures is important for best marks. More marks
are to be gained for good performance. Up to now from the data
structures i have studied this seems to be this best for this task. What
you do recommend for a better ?

j1mb0jay
Jan 12 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Burwell | last post by:
I am converting a VB6 WebClass application to VB.Net. Used the VB upgrade tool to do the conversion - and it left me a lot of little code things to do. Did those code things and got my app to...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
5
by: Gary | last post by:
I found with Asp.Net, you can use this function to very easily create an MD5 hash: System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile( created.ToUpper(),"MD5"); Is there...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
1
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
15
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code...
8
by: Jim Cobban | last post by:
I am writing a program in which I need a hash table implementation of a Map. The version of g++ available for Windo$e does not yet include the TR1 support for this. It just has the original SGI...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.