Object LookupFirstKey(IDictionary dict, Object value) {
´ foreach ( IDictionaryEntry e in table )
if ( object.Equals(e.Value, value) )
return e.Value
throw new IndexOutOfBoundsException();
}
If that isn't fast enough, you can maintain two hash-tables, one mapping
each way, and expose that in your own class, kind of like:
public class TwowayHashtable: IDictionary {
IDictionary forward;
IDictionary reverse;
void Add(Object left, Object right) {
forward.Add(left, right);
try {
reverse.Add(left, right);
catch ( Exception e ) {
forward.Remove(left);
throw;
}
}
// Allow choosing either forward or reverse "image" of
// the hashtable
IDictionary Forward { get { return new Image(this, forward); } }
IDictionary Reverse { get { return new Image(this, reverse); } }
class Image: IDictionary {
public TwowayHashtable table;
public IDictionary dict;
public Image(TwowayHashtable table, IDictionary dict)
{ this.table = table; this.dict = dict; }
// Mutation goes to table
public void Add(Object key, Object item) { table.Add(key,item); }
...
// Iteration goes to dict
public IEnumerator GetEnumerator() { return dict.Enumerator(); }
...
// Synchronization goes to table
public Object SyncRoot { get { return table.SyncRoot; } }
...
}
// For iteration, expose any of foreard/reverse
public IEnumarator GetEnumerator() { return forward.Enumerator(); }
public ICollection Keys { get { return forward.Keys; } }
...
}
A Hashtable is not intrinsically ordered. Perhaps you should be using
SortedList.
That wouldn't help, he's asking to to reverse lookup (as i understand it).