Jump to content
  • Advertisement
Sign in to follow this  
supercoder74

[.net] inverting a hashtable

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have run into a bit of a problem with my code. If I have a hashtable like this:
System.Collections.Hashtable h=new System.Collections.Hashtable();
h.Add("key1","value1");
h.Add("key2","value2");
h.Add("key3","value3");

Can I make a new hashtable with the keys and the values inverted without hard-coding the values?

Share this post


Link to post
Share on other sites
Advertisement
Just iterate over the table, and add the elements in reverse order to the other table.

Something like:

using System.Collections;
public static Hashtable MakeInverse( Hashtable h ) {
Hashtable inverse = new Hashtable();
foreach( DictionaryEntry de in h ) {
inverse.Add( h.value, h.key );
}
return inverse;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
Just iterate over the table, and add the elements in reverse order to the other table.

Something like:

using System.Collections;
public static Hashtable MakeInverse( Hashtable h ) {
Hashtable inverse = new Hashtable();
foreach( DictionaryEntry de in h ) {
inverse.Add( h.value, h.key ); // if values are non-unique -- kablammo!
}
return inverse;
}

That's not a terrific idea. The uniqueness of keys is guaranteed, but the uniqueness of the stored values is not. Depending on the application, in fact, it may well be the case that different keys have the same value. An inverted hashtable will therefore lose some information. If you care about that, use a different container type for the inverse hashtable to preserve any lost data.

Share this post


Link to post
Share on other sites
Quote:
Original post by kSquared
Quote:
Original post by hplus0603
Just iterate over the table, and add the elements in reverse order to the other table.

Something like:

using System.Collections;
public static Hashtable MakeInverse( Hashtable h ) {
Hashtable inverse = new Hashtable();
foreach( DictionaryEntry de in h ) {
inverse.Add( h.value, h.key ); // if values are non-unique -- kablammo!
}
return inverse;
}

That's not a terrific idea. The uniqueness of keys is guaranteed, but the uniqueness of the stored values is not. Depending on the application, in fact, it may well be the case that different keys have the same value. An inverted hashtable will therefore lose some information. If you care about that, use a different container type for the inverse hashtable to preserve any lost data.

no, it doesnt matter, because with the program I am making I can guarentee that it is unique. Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!