Click here to Skip to main content
Click here to Skip to main content

BiDirectionalHashtable

By , 1 Nov 2005
 

Introduction

Although you can't deny the advantages of Hashtable, sometime it is not enough. Sometime you need to have a better duality - accessing the object from it's key, and the key from the object. This becomes doubly useful and important since the basic Hashtable allows you to use almost any object as the key.

A good example of this need is when displaying complex objects in a TreeView. The ability to retrieve the complex object from its matching TreeNode and vice-versa is very handy. For once, you can easily getthe selected item, or jump to a certain node.

Implementation

The implementation of such a BiDirectionalHashtable is quite simple. All one has to do is keep two simple Hashtables - one where the keys & objects play their normal part. However, in the other Hashtable, the roles are swapped - the objects are used as the keys, and the keys are kept in the Hashtable as the values.

public void AddItem(object key, object item)
{
    if (keysToItems.ContainsKey(key) || itemsToKeys.ContainsKey(item))
    {
        keysToItems.Remove(key);
        itemsToKeys.Remove(item);
    }
    keysToItems[key] = item;
    itemsToKeys[item] = key;
}

private Hashtable keysToItems = new Hashtable();
private Hashtable itemsToKeys = new Hashtable();

Once this is acomplished, all you are left to do is provide methods to easily handle such a class. Methods such as GetKey() and GetItem() are of course called for, as the respective Remove methods.

public object GetKey(object item)
{
    if (item == null)
        throw new ArgumentNullException("keitemy");

    return itemsToKeys[item];
}

public object GetItem(object key)
{
    if (key == null)
        throw new ArgumentNullException("key");

    return keysToItems[key];
}

public void RemoveByKey(object key)
{
    if (key == null)
        throw new ArgumentNullException("key");

    object item = keysToItems[key];
    keysToItems.Remove(key);
    itemsToKeys.Remove(item);
}

public void RemoveByItem(object item)
{
    if (item == null)
        throw new ArgumentNullException("item");

    object key = itemsToKeys[item];
    keysToItems.Remove(key);
    itemsToKeys.Remove(item);
}

You can also supply an indexer, but I woul recommend one only if you are implementing a strong-typed BiDirectionalHashtable, because having a weak-typed index will leave the decision whether the index is a key or an object to the class. This can work only if you can assur that the same object cannot be used as both a key and as a value.

Summary

Although this is a very basic issue, its uses are many, and it certainly ease the life of a programmer, and make a clear and easier to maintain code. OF course, the use of Generics in VS2005 make everything even simplier and cleaner.

History

4/11/2005 - Following tonyt and leppie remarks, addition of items now checks existance of previous items and remove them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Itay Sagui
Team Leader
Israel Israel
Member
I currently work as the development manager in a company called Tzunami Inc. that develops a content migration solution for Microsoft SharePoint . Our product, called Tzunami Deployer is developed using C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionnon-unique values ?membertonyt1 Nov '05 - 15:41 
AnswerRe: non-unique values ?memberleppie1 Nov '05 - 20:04 
GeneralRe: non-unique values ?memberItay Sagui4 Nov '05 - 13:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 1 Nov 2005
Article Copyright 2005 by Itay Sagui
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid