473,770 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Effective hashing

Hi

I would like some suggestions on how to, effectively compute a Hash Value
for a "Collection " of simple Field Objects:
internal class Field
{
private string _fieldName;
private object _fieldValue;

public Field(string fieldName, object fieldValue)
{
this._fieldName = fieldName;
this._fieldValu e = fieldValue;
}

public int GetHashcode()
{
Return ????????
}
}

The HashCode for Field must ofcourse be computed based on both Name and
Value
These Field objects are stored in a derived HashTable:
internal class FieldGroup : Hashtable
{

public int GetHashCode()
{
string key;
Field fld;

foreach (int key in this.Keys) {
fld = this(key);
?????????
}
}
}
FieldGroup objects are filled with Field objects in this way:
FieldGroup fg = new FieldGroup();
string aName;
object aValue;
fg(aName) = new Field(myName, myValue);
I would like a suggestion on the most effective way to calculate a unique
HashCOde for such a FieldGroup object based on its contained Field objects.


Thanks in advance

Farouche
Jul 21 '05 #1
3 1640
Farouche wrote:
public int GetHashcode()
{ return (this._fieldNam e.GetHashCode()
^ this._fieldValu e.GetHashCode() ); } internal class FieldGroup : Hashtable
{

public int GetHashCode()
{
string key;
Field fld;

foreach (int key in this.Keys) {
fld = this(key);
?????????
}
}
this(key)?... don't you mean this[key]. and i don't think your foreach
loop works either, but i think i get the point.

If the ordering doesn't matter (it shouldn't on a hashtable, unless it's
linked, which your's isn't), you can use XOR too here on the keys and
the fields:

public int GetHashCode() {
int hashCode = 0;
foreach ( Object key in Keys )
hashCode = hashCode ^ key.GetHashCode () ^ this[key].GetHashCode();
return hashCode;
}

Instead of recomputing the hash for every call to GetHashCode() you can
have the hash as a member:

protected int _hashCode = 0;
public Object this[Object key] {
get { return base[key]; }
set {
// note: cunningly removed 2 times xor with key,.GetHashCod e()
// if key already exists
if ( ContainsKey(key ) )
_hashCode = this[key].GetHashCode();
else
_hashCode = _hashCode ^ key.GetHashCode ()
base[key] = value;
_hashCode = value.GetHashCo de();
}
}
public void Add(Object key, Object value) {
if ( ContainsKey(key ) )
return;
base.Add(key, value);
_hashCode = _hashCode ^ key.GetHashCode () ^ value.GetHashCo de();
}
public void Remove(Object key) {
if ( ContainsKey(key ) ) {
Object value = base[key];
base.Remove(key );
_hashCode = _hashCode ^ key.GetHashCode () ^ value.GetHashCo de();
}

and just have GetHashCode return it:

public int GetHashCode() { return _hashCode; }

This is of course only an efficient implementation if you expect to
actually invoke GetHashCode().. .

And this is just a sketch, you need to work on it to make value == null
work, I suggest assigning a hashcode of 0 to null values, or simply
rejecting to accept inserts of null values.

Note that this approach has bad properties if the keys and values in the
hash has the same hashcode, or if you insert the same value twice. for
example:

FieldGroup fg = new FieldGroup();
int h1 = fg.GetHashCode( );
fg[1] = 1;
int h2 = fg.GetHashCode( );

will make h1 == h2.

Note that to update it on remove you simply xor again... xor has NICE
properties ;)
I would like a suggestion on the most effective way to calculate a unique
HashCOde for such a FieldGroup object based on its contained Field objects.
You probably cannot make it unique, (2^32 is a pretty small number, any
hash that small has a good number of collisions... randomly inserting
you would roughly expect 1 collision in 2^16 inserts, it's the "brithday
paradox" :)
Thanks in advance


Watch out, I may just have wasted your time :)

--
Helge
Jul 21 '05 #2
Helge Jensen wrote:
_hashCode = this[key].GetHashCode();

WHOOPS! thats:

_hashCode = _hashCode ^ this[key].GetHashCode();

Sorry for the blunder!

--
Helge
Jul 21 '05 #3
"Helge Jensen" <he**********@s log.dk> wrote in message
news:41******** ******@slog.dk. ..

.......

Watch out, I may just have wasted your time :)

--
Helge

Thanks for the reply, I will have a look into it and do some serious
testing.
Farouche
Jul 21 '05 #4

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

Similar topics

2
2022
by: Pat | last post by:
I want to look for some one-to-one hashing function. In C++, any one-to-one hashing function?
1
3011
by: snowteo | last post by:
Hi,I have to do this exercises can you help me: 1)Write a program to implement exetendible hashing.If the table is small enough to fin in main memory,how does its performance compare with open and closed hasing? 2)A basic program consists of a series of statements,each of which is numbered in ascending order.Control is passed by use of a goto or gosub and a statement number.Write a program that reads in a legal BASIC program and renumbers...
11
3437
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
2869
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
3
282
by: Farouche | last post by:
Hi I would like some suggestions on how to, effectively compute a Hash Value for a "Collection" of simple Field Objects: internal class Field { private string _fieldName; private object _fieldValue;
19
3844
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library?
8
4577
by: Maya | last post by:
Hello all, I'm using MD5 hashing in my application to give unique values to huge list of items my application receives, originally every item's name was difficult to use as an id for this item although its unique but because it had certain characters and variable lengths I ended up using MD5 hashing of the name.
1
4418
by: Tinku | last post by:
Hi friends I know Static Hashing and i know about Dynamic Hashing, still i have problem to make program with Dynamic Hashing I am new in "C" world, please help me, my problem is: i have to make program in Dynamic hashing i have to store int value in nodes user only enter int value by this value i have to find hash key and make symbol table my struct are
15
3010
by: Vinodh | last post by:
I am reading about hashing techniques. The map data structure available in C++ STL uses hashing techniques?
0
9617
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
10257
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
10099
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
10037
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
9904
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...
1
7456
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
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.