[.net] inverting a hashtable

Started by
2 comments, last by supercoder74 18 years, 8 months ago
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?
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
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;}
enum Bool { True, False, FileNotFound };
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
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!
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.

This topic is closed to new replies.

Advertisement