473,666 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which hashing function to use?

Hello,

I need to use a hashing function for relatively short strings (roughly 16
characters or less). The data that needs to be accessed via hash is just a
simple int. I just need to look up values in two dimensional matrices each
time that I encounter a pair of these strings.

I would like to keep the code as simple and standard as
possible, but at the same time to have a reasonable performance.

While looking through the docs on my system I found a few different
implementations :
- hsearch from <search.h>
- tsearch from <search.h>
- bsearch from <stdlib.h>

Which one would you recommend? Or maybe something completly different?
Right now I just use Perl to do the hashing :-) this is nice, but I would
love to have a small, efficient, ANSI C program.

Regards,

January
Nov 16 '07 #1
11 2147
On Nov 16, 3:42 pm, January Weiner <january.wei... @gmail.comwrote :
Hello,

I need to use a hashing function for relatively short strings (roughly 16
characters or less). The data that needs to be accessed via hash is just a
simple int. I just need to look up values in two dimensional matrices each
time that I encounter a pair of these strings.

I would like to keep the code as simple and standard as
possible, but at the same time to have a reasonable performance.

While looking through the docs on my system I found a few different
implementations :
- hsearch from <search.h>
- tsearch from <search.h>
- bsearch from <stdlib.h>

Which one would you recommend? Or maybe something completly different?
Right now I just use Perl to do the hashing :-) this is nice, but I would
love to have a small, efficient, ANSI C program.
Your post is more topical in news:comp.progr amming {follow-ups set}.

I do not think that your problem has an answer yet. It will depend on
lots of things.
For instance, are the strings a static set that never changes? If so,
then a perfect hash is your choice.
There are string searching algorithms faster than a hash table (See
Sedgewick).

Do you need to preserve the data (e.g in a key/value pair set on
disk)?

"I need a hash, which should I use?" is such an open question that any
simple answer will probably be lacking.
Nov 17 '07 #2
user923005 <dc*****@connx. comwrote:
Your post is more topical in news:comp.progr amming {follow-ups set}.
Well, it is not. Not 100%. The reason being that I am looking for
something that is part of the C standard and that is simple to use in C.
What do you think happens when I ask a question like that in
comp.programmin g? Some wise guy will set the Followup-To to comp.lang.c
:-P, that's what would happen. Finally, the reason is that I am not a
computer scientist and need, hm, very practical advice. Practical advice
is something that I always got plenty on comp.lang.c :-) Note that you will
find a discussion on different hashing algorithms in the FAQ of this group
-- however, I do not fully understand it (otherwise I would not be asking
the questions).
I do not think that your problem has an answer yet. It will depend on
lots of things.
For instance, are the strings a static set that never changes? If so,
then a perfect hash is your choice.
There are string searching algorithms faster than a hash table (See
Sedgewick).
OK, as I am not a computer scientist, I will try to look it up somewhere.
Meanwhile, which of that is a part of standard C libraries?
Do you need to preserve the data (e.g in a key/value pair set on
disk)?
Nope, I would go for DBM or SQL if I needed that.
"I need a hash, which should I use?" is such an open question that any
simple answer will probably be lacking.
"I need a hash, which of the ones in the standard C libraries should I use"
is a more specific question, is it not?

Kind regards,

January
Nov 17 '07 #3
January Weiner wrote:
user923005 <dc*****@connx. comwrote:
>Your post is more topical in news:comp.progr amming {follow-ups set}.
[...]
>"I need a hash, which should I use?" is such an open question that any
simple answer will probably be lacking.

"I need a hash, which of the ones in the standard C libraries should I use"
is a more specific question, is it not?
Yes, it is. And the answer is, there are no hash functions in the
standard C library.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 17 '07 #4
Keith Thompson said:
January Weiner wrote:
>user923005 <dc*****@connx. comwrote:
>>Your post is more topical in news:comp.progr amming {follow-ups set}.
[...]
>>"I need a hash, which should I use?" is such an open question that any
simple answer will probably be lacking.

"I need a hash, which of the ones in the standard C libraries should I
use" is a more specific question, is it not?

Yes, it is. And the answer is, there are no hash functions in the
standard C library.
Cases could be made for some of the math functions, and perhaps rand. Not
particularly good cases, admittedly, but theoretically.. .

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 17 '07 #5
"January Weiner" <ja************ @gmail.comwrote in message
Hello,

I need to use a hashing function for relatively short strings (roughly 16
characters or less). The data that needs to be accessed via hash is just
a
simple int. I just need to look up values in two dimensional matrices each
time that I encounter a pair of these strings.

I would like to keep the code as simple and standard as
possible, but at the same time to have a reasonable performance.

While looking through the docs on my system I found a few different
implementations :
- hsearch from <search.h>
- tsearch from <search.h>
- bsearch from <stdlib.h>

Which one would you recommend? Or maybe something completly different?
Right now I just use Perl to do the hashing :-) this is nice, but I would
love to have a small, efficient, ANSI C program.
There's a general-purpose hash function on my website - under Basic
Algorithms, sample chapter on hash tables.
Whilst it is not specially designed for strings, it's pretty good. Use it
for now, and replace it only if performance is unacceptable.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 17 '07 #6
January Weiner wrote:
>
I need to use a hashing function for relatively short strings
(roughly 16 characters or less). The data that needs to be
accessed via hash is just a simple int. I just need to look up
values in two dimensional matrices each time that I encounter a
pair of these strings.

I would like to keep the code as simple and standard as
possible, but at the same time to have a reasonable performance.

While looking through the docs on my system I found a few
different implementations :
- hsearch from <search.h>
- tsearch from <search.h>
- bsearch from <stdlib.h>

Which one would you recommend? Or maybe something completly
different? Right now I just use Perl to do the hashing :-) this
is nice, but I would love to have a small, efficient, ANSI C
program.
There are a couple of very simple, but efficient, string hashing
routines available (in source form) in hashlib.zip. Although
hashlib is copyright and released under GPL, those functions are
pretty common knowledge and there will be no objections to
including them in anything else. You can find hashlib.zip at:

<http://cbfalconer.home .att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 17 '07 #7
CBFalconer <cb********@yah oo.comwrote:
There are a couple of very simple, but efficient, string hashing
routines available (in source form) in hashlib.zip. Although
hashlib is copyright and released under GPL, those functions are
pretty common knowledge and there will be no objections to
including them in anything else. You can find hashlib.zip at:
Thank you very much, that is precisely the answer that I needed.

January

Nov 18 '07 #8
Malcolm McLean <re*******@btin ternet.comwrote :
There's a general-purpose hash function on my website - under Basic
Algorithms, sample chapter on hash tables.
Whilst it is not specially designed for strings, it's pretty good. Use it
for now, and replace it only if performance is unacceptable.
Thanks!

January

Nov 18 '07 #9
Keith Thompson <ks...@mib.orgw rote:
January Weiner wrote:
"I need a hash, which of the ones in the standard C
libraries should I use" is a more specific question,
is it not?

Yes, it is. And the answer is, there are no hash functions
in the standard C library.
There are at least two that fit the raw definition: strlen()
and strtoul(..., ..., 36). Not necessarily useful, but forms
of hashing nonetheless.

--
Peter
Nov 18 '07 #10

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

Similar topics

2
2011
by: Pat | last post by:
I want to look for some one-to-one hashing function. In C++, any one-to-one hashing function?
2
2080
by: Matt Bull | last post by:
Hi, I apologise in advance if this is off topic, but would appreciate any pointers you chaps might be able to provide. I'm relatively novice in the art of C so am after any suggestions about implementing a very simple hash (or quasi hash) I need to be able to store a counter and a timestamp for requests from particular device MAC addresses (6 bytes, presented as xx:xx:xx:xx:xx:xx
11
3424
by: Wm. Scott Miller | last post by:
Hello all! We are building applications here and have hashing algorithms to secure secrets (e.g passwords) by producing one way hashes. Now, I've read alot and I've followed most of the advice that made sense. One comment I've seen alot about is "securing the hashing routine" but no-one explains how to accomplish this. So how do I secure my hashing routine? Do I use code access security, role based security, ACLs, etc or combination?...
10
2849
by: Dino M. Buljubasic | last post by:
Hi, I am using MD5 to hash my passwords and add them to database as hashed. I have noticed though that some passwords don't get recognized and I suppose that it happen because hashing might introduce some characters in my password that are not handled properly by SQL server then. For example, password 'startreck' works just fine password 'test' does not
4
3395
by: wkatz | last post by:
Hi, Gurus. What hashing algorithm outputs hash value as numbers only? For example, if you pass a “John Q. Public” it will output 23324. If there is no such hashing, how hard is it to hire somebody to write a fairly quick one? It could be some fast hashing and then another function that creates numbers. Much obliged. wkatz.
1
1430
by: snipes2002 | last post by:
hi all i want to make a java code to build a 16 element array and do the following things ( insert , search , delete ) using a hashing function (j mod (arraysize)) and if the index is occupied so it will be (j+1 mod n) in the insertion and searching process and in the deletion the array will rehash the rest of the elements !! actually am not so good in java and i dont even have the hashing algorithem soo if any one can help ill be more than...
0
1356
by: s.phonologies | last post by:
Hi friends, I am Sumit problem in C Language I have created a program to store int data by the HASH() my hash function for generate hashkey: int HASH(data) { return data%10;
15
2999
by: Vinodh | last post by:
I am reading about hashing techniques. The map data structure available in C++ STL uses hashing techniques?
1
9560
by: May Amor | last post by:
Helu gurus!!! I have a code below about hashing method with collision resolution...My problem is how to use the collsion resolution again if the hash index though has already a value. Please kinda help me with this..I want to determine also if the table now is full so as to stop user from entering a key. size of the table=19, Thanx a lot..GodBless ________________________________________________ #include<iostream.h> #include<conio.h> ...
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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
8781
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...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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...
0
4198
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...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.