Home All Groups Group Topic Archive Search About
Author
18 Apr 2006 3:01 PM
Chris
I need to do a mapping between two strings.  I want to be able to look
up either string from the other.  So if I know String A, I want to get
String B.  If I know String B I want to get String A.  I usually use
something like a hashtable, but that is one way.  What should I use to
get a two way collection?

Thanks
Chris

Author
18 Apr 2006 3:21 PM
Larry Lard
Chris wrote:
> I need to do a mapping between two strings.  I want to be able to look
> up either string from the other.  So if I know String A, I want to get
> String B.  If I know String B I want to get String A.  I usually use
> something like a hashtable, but that is one way.  What should I use to
> get a two way collection?

Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.

--
Larry Lard
Replies to group please
Author
18 Apr 2006 4:48 PM
Chris
Larry Lard wrote:
Show quoteHide quote
> Chris wrote:
>
>>I need to do a mapping between two strings.  I want to be able to look
>>up either string from the other.  So if I know String A, I want to get
>>String B.  If I know String B I want to get String A.  I usually use
>>something like a hashtable, but that is one way.  What should I use to
>>get a two way collection?
>
>
> Nothing built-in I can think of, but building your own wouldn't be too
> hard - since both 'keys' and 'values' are Strings, just inherit from
> Hashtable and override Add to also insert (b, a) whenever you insert
> (a, b). Unless you need to know 'which way round' A and B were
> originally supplied, in which case you'd need to do something like
> inherit from DictionaryBase, and maintain two Hashtables internally,
> one for 'original way round' mapping and one for 'the other way round'
> mapping.
>

I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris
Author
18 Apr 2006 5:00 PM
Jeff Dillon
use an exhaustive search the other way around

Show quoteHide quote
"Chris" <no@spam.com> wrote in message
news:O5TiofwYGHA.1352@TK2MSFTNGP05.phx.gbl...
> Larry Lard wrote:
>> Chris wrote:
>>
>>>I need to do a mapping between two strings.  I want to be able to look
>>>up either string from the other.  So if I know String A, I want to get
>>>String B.  If I know String B I want to get String A.  I usually use
>>>something like a hashtable, but that is one way.  What should I use to
>>>get a two way collection?
>>
>>
>> Nothing built-in I can think of, but building your own wouldn't be too
>> hard - since both 'keys' and 'values' are Strings, just inherit from
>> Hashtable and override Add to also insert (b, a) whenever you insert
>> (a, b). Unless you need to know 'which way round' A and B were
>> originally supplied, in which case you'd need to do something like
>> inherit from DictionaryBase, and maintain two Hashtables internally,
>> one for 'original way round' mapping and one for 'the other way round'
>> mapping.
>>
>
> I just hoped there was a way to do it w/o using two hashtables.
>
> Thanks for the input.
> Chris
Author
18 Apr 2006 10:42 PM
Mythran
Show quote Hide quote
"Jeff Dillon" <jeffdil***@hotmail.com> wrote in message
news:%23kGhjmwYGHA.3936@TK2MSFTNGP05.phx.gbl...
> use an exhaustive search the other way around
>
> "Chris" <no@spam.com> wrote in message
> news:O5TiofwYGHA.1352@TK2MSFTNGP05.phx.gbl...
>> Larry Lard wrote:
>>> Chris wrote:
>>>
>>>>I need to do a mapping between two strings.  I want to be able to look
>>>>up either string from the other.  So if I know String A, I want to get
>>>>String B.  If I know String B I want to get String A.  I usually use
>>>>something like a hashtable, but that is one way.  What should I use to
>>>>get a two way collection?
>>>
>>>
>>> Nothing built-in I can think of, but building your own wouldn't be too
>>> hard - since both 'keys' and 'values' are Strings, just inherit from
>>> Hashtable and override Add to also insert (b, a) whenever you insert
>>> (a, b). Unless you need to know 'which way round' A and B were
>>> originally supplied, in which case you'd need to do something like
>>> inherit from DictionaryBase, and maintain two Hashtables internally,
>>> one for 'original way round' mapping and one for 'the other way round'
>>> mapping.
>>>
>>
>> I just hoped there was a way to do it w/o using two hashtables.
>>
>> Thanks for the input.
>> Chris
>
>

If you aren't too worried about speed, you can use a StringCollection as a
base for something like the following collection class:

public class MyCollection : NameValueCollection
{
    public string GetKeyByValue(string Value)
    {
        string[] keys = base.BaseGetAllKeys();
        foreach (string key in keys) {
            if (Array.IndexOf(base.GetValues(key), Value) >= 0) {
                return key;
            }
        }
        return null;
    }
}

hth :)

Mythran
Author
19 Apr 2006 4:26 AM
tommaso.gastaldi
How about putting both:
(key=A, value=B)
and
(key=B, value=A)               [if A<>B]

in the same hashtable? This way you could use the same hashtable to
lookup both ways (from A get B, from B get A)...  mmm right?

-tom