473,748 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(metho ds) that will do this for me, or does anyone
know of a more efficient way?

j1mb0jay
Jan 11 '08 #1
6 10043
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(metho ds) 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(metho ds) 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(metho ds) 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.comw rote:
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.comw rote:
[...]
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.comw rote:
>[...]
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
2934
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 compile. Did a few other code things to take advantage of .Net features not available in VB6. When I start the application on my PC I get the error shown below. It seems to me that the app initiation is failing before any app code is executed. My...
3
3338
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 substitute the set<Data*> with a hash_set<Data*>: typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet; // typedef set<Data*> DataPointerHashSet; // (see complete code below) But it doesn't work! Everything is fine...
47
5086
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 are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
2
3790
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
7
17070
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 MD5 is computing the right Byte for the hash when compared to other MD5 hash generators online. However, when I attempt to convert it back to tring using the line String outputData = textConverter.GetString( result ) ; I essentially get...
5
2629
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 a similar one for Windows programs? I like that function because you don't have to convert to bytes. The variable created is simply a string which makes life easy. :) I tried using it in a windows program but it didn't like it :? --
21
3222
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 (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
1
3596
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 tutorial to help me with this issue I figured I'd make a post just in case someone else runs into the same issue. ....
15
37597
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 Different-2 string.
8
2775
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 implementation. However the SGI implementation is so old that it predates the STL, so it does not, among other things, include hash support for std::basic_string. So I tried implementing the hash map from Stroustrup 3rd edition. At the least I...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9552
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.