1

I'm trying to implement a Baudot character encoding. Right now, I'm using 2 dictionaries which are simply mirrors of each other:

Dictionary<char, int> Lookup = new Dictionary<char, int> {
    { ' ', 0x100100 },
    { '-', 0x011000 },
    { '/', 0x010111 },
    { '0', 0x001101 },
    { '1', 0x011101 },
    ...
};

Dictionary<int, char> Reverse = new Dictionary<int, char> {
    { 0x100100, ' ' },
    { 0x011000, '-' },
    { 0x010111, '/' },
    { 0x001101, '0' },
    { 0x011101, '1' },
    ...
};

Is there a built in type that handles this already? Something like:

var lookup = new Lookup<int, char>();
lookup.GetByKey(0x100100);
lookup.GetByValue('c');

I couldn't find anything when I searched for 'reverse lookup' or 'lookup table', everything seemed to be related to DNS or LinqToSql.

(I'm using Baudot because it's necessary for some Cospas Sarsat devices)

Zachary Yates
  • 12,966
  • 7
  • 55
  • 87
  • Is there a char value in your Dictionary for every int value between 0 and whatever the max int value you have is (i.e. as regards the int keys, are they sparsely, or completely populated)? – hatchet - done with SOverflow Mar 21 '14 at 18:48
  • You're talking about such a small number of characters, I think a simple single array would suffice. Direct indexing using the int key to get a char would be faster than anything, and a simple linear search to go from char to int (i.e. indexOf) would probably not be much slower than the cumulative Dictionary overhead for the small size of the array. Other solutions seem like overkill. – hatchet - done with SOverflow Mar 21 '14 at 18:59
  • @hatchet Baudot is a six bit encoding, so no there isn't a value for every possible int. I haven't really figured out what to do in that case yet. – Zachary Yates Mar 21 '14 at 19:01
  • just use a sentinel char value that is not in the Baudot character set to indicate unused positions. What is the min and max int values that have a Baudot char mapped to them? Aren't we talking about a very small set here? – hatchet - done with SOverflow Mar 21 '14 at 19:03
  • Heck, you don't even need an array, just use a string constant of the Baudot characters with sentinel characters occupying any holes in the sequence. – hatchet - done with SOverflow Mar 21 '14 at 19:11
  • @hatchet there's only 33 characters represented in Baudot. There's a link to the encoding in the question body. – Zachary Yates Mar 21 '14 at 19:14
  • I'll post an answer shortly. – hatchet - done with SOverflow Mar 21 '14 at 19:16
  • @hatchet I'll post another question specifically about Baudot. Thanks for your help – Zachary Yates Mar 21 '14 at 19:41
  • @hatchet see also: http://stackoverflow.com/questions/22568251/how-to-implement-baudot-encoding – Zachary Yates Mar 21 '14 at 19:51

3 Answers3

1

I think you are in need of a Bi-Directional Dictionary. There are many such implementations available. I like the one in this link though :

http://blogs.microsoft.co.il/blogs/ranw/BDirectional.txt

The only pre-requisite for this being the key and value should not be of the same type, which in your case applies.

Sourav 'Abhi' Mitra
  • 2,390
  • 16
  • 15
0

I think that there is no such thing... How about some extension method? Create the first dictionary and then the second using the following method. By having two dictionaries you are having two hashtables and your access is always O(1).

public static class DictionaryExtensions
{
    public static IDictionary<U, T> Reverse<T, U>(this IDictionary<T, U> dictionary)
    {
        var result = new Dictionary<U, T>();
        foreach(var x in dictionary)
        {
            result[x.Value] = x.Key;
        }

        return result;
    }
}
Pellared
  • 1,242
  • 2
  • 14
  • 29
0

If it were me, I'd just have a list of pairs.

class WhateverPair
{
    int Binary {get;private set;}
    char TheChar {get;private set;}
    public WhateverPair(int b, char c) { Binary = b; TheChar = c; }
}

Then just fill your list like you have filled your first dictionary.

List<WhateverPair> encodingtable;
char result = encodingtable.First(x=>x.binary == 0x010010).thechar;
int reverse = encodingtable.FirsT(x=>x.thechar == '1').binary;
hometoast
  • 11,522
  • 5
  • 41
  • 58
  • Yeah it will work but it will be less efficient. I would prefer having two Dictionaries (two hashtables). – Pellared Mar 21 '14 at 18:52