deadEddie
deadEddie

Reputation: 242

store dictionary to hashtable

I'm working on a scrabble cheater sort of program, where the user enters a group of letters and, after the letters are jumbled around a bit, they are compared to a word list I have stored in a database. Because searching through the database for each letter combination is so slow, I tried storing the words in a List first, so they would be available in memory, which really improved performance. But I would like to make it faster yet by storing the word lists in List in a Hashtable where the key is the first letter of the word, but I'm a little stuck on how to go about it.

What I have (and obviously won't work) is:

public Hashtable populateWordTable()
    {
        Hashtable wordTable = new Hashtable();
        List<string> wordTableWordList = new List<string>();
        connect = new SqlConnection(connectionString);
        SqlCommand find = new SqlCommand("Select * FROM English order by Word", connect);
        // starting with first record, store each word into the List<string> wordlist
        SqlDataReader dr = null;
        int found = 0;
        try
        {
            connect.Open();
            dr = find.ExecuteReader();
            string key = string.Empty;
            string keyStartingPosition = "a";
            while (dr.Read())
            {
                // if word is present
                if (!dr.IsDBNull(0))
                {
                    found = Convert.ToInt32(dr[0]);
                    key = dr[1].ToString().Substring(0, 1);                        
                }
                if (found > 0)
                {
                    // if we have transitioned to the next letter in the alphabet
                    if (key != keyStartingPosition)
                    {
                        wordTable.Add(keyStartingPosition, wordTableWordList);
                        List<string> newList = new List<string>();
                        newList.Add(dr[1].ToString());
                        keyStartingPosition = key;
                    }
                    // still in the same letter in the alphabet
                    else
                    {
                        wordTableWordList.Add(dr[1].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            connect.Close();
        }

        return wordTable;
    }

Upvotes: 0

Views: 474

Answers (2)

KappaG3
KappaG3

Reputation: 671

You should use a Dictionary<char, IEnumerable<string>> (Reference). Its usage is rather straightforward, when you add words you first check if it ContainsKey(word[0]), and if it doesn't you Add it. You could first assign an empty list, and fill it through each iteration, or build the list first and then assign it to the key. It's up to you.

Upvotes: 1

Chris Doggett
Chris Doggett

Reputation: 20757

Having done something like this before, I found the best structure was a DAWG (directed acyclic word graph). I translated Steve Hanov's Python DAWG builder to C# last year, but lost it when a RAID drive went bad. Shouldn't be too hard for you to translate to C#.

Upvotes: 2

Related Questions