Hashtable that doesn't store the key?

  • Thread starter Thread starter JackRazz
  • Start date Start date
J

JackRazz

Is it possible to create a hashtable that doesn't store the key? I have a very large
hashtable and I just want it to store the HashCode and the Value (two Int16s).


Thanks - JackRazz
 
JackRazz said:
Is it possible to create a hashtable that doesn't store the key? I have a
very large
hashtable and I just want it to store the HashCode and the Value (two
Int16s).

How will it distinguish 2 keys that have the same HashCode if you don't
store the keys? Equality on HashCode is just a hint that the keys may be
equal, but you have to compare the keys to be sure that they match, unless
you have a perfect hashing function (no collision).

Bruno.
 
Hi JackRazz,

Could You be more descriptive with your problem?

There's an inconsistent in your text:
"I have a very large hashtable" & "I just want it to store (two Int16s)"

If you want to store only two *shorts* then why do you want to use
the hashtable?
Is it possible to create a hashtable that doesn't store the key?

I'm afraid that it is impossible.
*Key* is a main feature of hashtable collection, so only values can
contains *nulls*.

Maybe i missed somethig, so give me your feedback.

Regards
Marcin
 
Bruno,
Thanks for the reply. I googled 'perfect hashing' and now have a much clearer
understanding of what can be done with hashtables. Perfect hashing won't work for me
because I don't know the keys ahead of time.

|
| "JackRazz" <[email protected]> a écrit dans le message de | %23ClmIaI%[email protected]...
| > Is it possible to create a hashtable that doesn't store the key? I have a
| > very large
| > hashtable and I just want it to store the HashCode and the Value (two
| > Int16s).
|
| How will it distinguish 2 keys that have the same HashCode if you don't
| store the keys? Equality on HashCode is just a hint that the keys may be
| equal, but you have to compare the keys to be sure that they match, unless
| you have a perfect hashing function (no collision).
|
| Bruno.
|
| >
| >
| > Thanks - JackRazz
| >
| >
|
|
 
Marcin,
I wanted to store two int16's as the values where the key is a string. I'm new to
hashtables and was hoping for the almost perfect hashtable that didn't require me to
store the key, but I now see the impossiblity of what I wanted to do.

Thanks for the reply - JackRazz


| Hi JackRazz,
|
| Could You be more descriptive with your problem?
|
| There's an inconsistent in your text:
| "I have a very large hashtable" & "I just want it to store (two Int16s)"
|
| If you want to store only two *shorts* then why do you want to use
| the hashtable?
|
| > Is it possible to create a hashtable that doesn't store the key?
|
| I'm afraid that it is impossible.
| *Key* is a main feature of hashtable collection, so only values can
| contains *nulls*.
|
| Maybe i missed somethig, so give me your feedback.
|
| Regards
| Marcin
 
Hi JackRazz,
Marcin,
I wanted to store two int16's as the values where the key is a string.

Hmmm... maybe i missed something.

// but try this:
string key="myKey";
short[] twoShorts=new short[2];
twoShorts[0]=1;
twoShorts[1]=11;

hashTable.Add(key, twoShorts);

// so you can get those shorts by:
short[] tempShorts=hashTable["myKey"];
I'm new to
hashtables and was hoping for the almost perfect hashtable that didn't require me to
store the key, but I now see the impossiblity of what I wanted to do.

Hmmm... i don't what do you expect from hashtable, but i think
that you should look up the other .NET collection.

HTH
Marcin
 
Marcin,
Thats exactly what I'm trying to do, but with a difference. The keys I'm passing in
are all at least 5 chars long and sometimes they are phrases of 25 characters. If I
had say 400,000 items in a hashtable, the key would impact memory heavily. I was
simply trying to use a hashtable, with the hash code only being stored, not the key
due to its memory impact. I'm a bit new to hashtables, so I was thinking that this
might be possible (perfect hashing), but it doesn't like it is, unless I knew the
keys at design time (like say keywords in a compiler).

Anyhow, I'm moving on for now using the .net hashtable, with the key stored. In 15
years, there will probably be the perfect hash that doesn't care how many cycles are
used, etc (:

Thanks for the help - JackRazz




| Hi JackRazz,
|
| > Marcin,
| > I wanted to store two int16's as the values where the key is a string.
|
| Hmmm... maybe i missed something.
|
| // but try this:
| string key="myKey";
| short[] twoShorts=new short[2];
| twoShorts[0]=1;
| twoShorts[1]=11;
|
| hashTable.Add(key, twoShorts);
|
| // so you can get those shorts by:
| short[] tempShorts=hashTable["myKey"];
|
| > I'm new to
| > hashtables and was hoping for the almost perfect hashtable that didn't require me
to
| > store the key, but I now see the impossiblity of what I wanted to do.
|
| Hmmm... i don't what do you expect from hashtable, but i think
| that you should look up the other .NET collection.
|
| HTH
| Marcin
 
JackRazz said:
| > I'm new to
| > hashtables and was hoping for the almost perfect hashtable that didn't require me
to
| > store the key, but I now see the impossiblity of what I wanted to do.

Well... what you describe is known as a cryptograhic hash: a
hash-function where it is virtually impossible to make two inputs which
maps to the same hashed value.

For example, when you digitally sign something, what you sign is not
really the entire document, but typically a cryptographic hash of it
(usually MD5 or SHA-1), but signing the hash is almost as good, since
noone will (hopefully) be able to come up with another text with the
same hashed value.

The key-space of IDictionary is only 2^32, which is (way) too small,
SHA-1 is 2^160, for relying on cryptographic hashing, but there may be
something else you can do.

If you can reject duplicate inserts, you can reject duplicate
hash-values instead. This effectively creates a partitioning of the
key-space, creating sets of equivalent keys.

While this would mean that you would be preventing certain sets of keys
from being in the database at the same time, it might be what you seek.

For example, if the keys are usernames or something similar, you could
say username already-taken... try another one.

BTW: If the keys are actually passwords, do not use the above solution,
2^32 is a small number in todays world :)
 
Back
Top