TheDeveloperBlog.com


C# Char Lookup Table

Char lookup table. A lookup table optimizes certain computations. A char lookup table can process specific characters in C# strings. It is a simple character-based associative array. It eliminates costly branches in your character manipulation code.

Lookup table for ROT13 benchmark

  ROT13 lookup table: 1779 ms [faster]
ROT13 transformation: 3447 ms


Example. Using a lookup table—an array that contains transformed values for each index value—is faster in many cases for integer and char transformations and shifts. But it requires some setup time.

Tip: As an aside, the approach to building a lookup table here can be adapted to many ciphers.

And: This includes the Atbash cipher and Caesar ciphers. Here, we see the ROT13 lookup table.

C# program that transforms using ROT13

/// <summary>
/// Contains the ROT13 text cipher code.
/// </summary>
class Rot13Table
{
    /// <summary>
    /// Lookup table to shift characters.
    /// </summary>
    char[] _shift = new char[char.MaxValue];

    /// <summary>
    /// Generates the lookup table.
    /// </summary>
    public Rot13Table()
    {
	// Set these as the same.
	for (int i = 0; i < char.MaxValue; i++)
	{
	    _shift[i] = (char)i;
	}
	// Specify capital letters are shifted.
	for (char c = 'A'; c <= 'Z'; c++)
	{
	    char x;
	    if (c <= 'M')
	    {
		x = (char)(c + 13);
	    }
	    else
	    {
		x = (char)(c - 13);
	    }
	    _shift[(int)c] = x;
	}
	// Specify lowercase letters are shifted.
	for (char c = 'a'; c <= 'z'; c++)
	{
	    char x;
	    if (c <= 'm')
	    {
		x = (char)(c + 13);
	    }
	    else
	    {
		x = (char)(c - 13);
	    }
	    _shift[(int)c] = x;
	}
    }

    /// <summary>
    /// Rotate the specified text with ROT13.
    /// </summary>
    public string Transform(string value)
    {
	try
	{
	    // Convert to char array.
	    char[] a = value.ToCharArray();
	    // Shift each letter we need to shift.
	    for (int i = 0; i < a.Length; i++)
	    {
		int t = (int)a[i];
		a[i] = _shift[t];
	    }
	    // Return new string.
	    return new string(a);
	}
	catch
	{
	    // Just return original value on failure.
	    return value;
	}
    }
}


Example 2. The constructor generates the lookup table. You can see the lookup table is initialized in the same way that a ROT13 loop rotates its input. It has a Transform method. Here we see the calling method.

C# program that uses ROT13 translation

using System;

class Program
{
    static void Main()
    {
	string a = "There was a cute dog in 2008.";
	Console.WriteLine(a);

	Rot13Table t = new Rot13Table();

	a = t.Transform(a);
	Console.WriteLine(a);

	a = t.Transform(a);
	Console.WriteLine(a);
    }
}

Output

There was a cute dog in 2008.
Gurer jnf n phgr qbt va 2008.
There was a cute dog in 2008.


Discussion. My hypothesis from working with C was that the lookup table would be faster versus the basic ROT13 loop. I benchmarked the above two versions with the same string used in the examples. Please see the figures above.

You can use lookup tables for fast substitution of any valid character for another. Alternatively, you can use char lookup tables to access an int or other type based on the char being processed.

Int Type

Summary. We used a class instance in the C# language to encapsulate a lookup table. This provides superior performance to if-statements in a loop. Also, please look at the conventional ROT13 algorithm on this site.

ROT13 Method